How to use the clifford.numba._multivector.MultiVectorType function in clifford

To help you get started, we’ve selected a few clifford examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pygae / clifford / clifford / numba / _multivector.py View on Github external
@overload_call(MultiVectorType)
def ga_call(self, *args):
    # a numba quirk means that varargs can end up passed in two different ways
    if len(args) == 1 and isinstance(args[0], (types.StarArgTuple, types.StarArgUniTuple)):
        args = args[0].types

    # grade selection
    if len(args) > 0:
        # grade projection
        grades = self.layout_type.obj._basis_blade_order.grades
        if all(isinstance(arg, types.IntegerLiteral) for arg in args):
            # Optimized case where the mask can be computed at compile-time.
            inds = (grades == args[0].literal_value)
            for arg in args[1:]:
                inds |= (grades == arg.literal_value)
            # using `nonzero` makes the resulting array smaller.
            inds = inds.nonzero()
github pygae / clifford / clifford / numba / _multivector.py View on Github external
@numba.extending.overload_method(MultiVectorType, 'conjugate')
def MultiVector_conjugate(self):
    return MultiVector.conjugate
github pygae / clifford / clifford / numba / _multivector.py View on Github external
def typer(layout, value):
        if isinstance(layout, LayoutType) and isinstance(value, types.Array):
            return MultiVectorType(layout, value.dtype)
    return typer
github pygae / clifford / clifford / numba / _multivector.py View on Github external
@numba.extending.unbox(MultiVectorType)
def unbox_MultiVector(typ: MultiVectorType, obj: MultiVector, c) -> NativeValue:
    value = c.pyapi.object_getattr_string(obj, "value")
    layout = c.pyapi.object_getattr_string(obj, "layout")
    mv = cgutils.create_struct_proxy(typ)(c.context, c.builder)
    mv.layout = c.unbox(typ.layout_type, layout).value
    mv.value = c.unbox(typ.value_type, value).value
    c.pyapi.decref(value)
    c.pyapi.decref(layout)
    is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred())
    return NativeValue(mv._getvalue(), is_error=is_error)
github pygae / clifford / clifford / numba / _multivector.py View on Github external
def _numba_type_(self):
    layout_type = self.layout._numba_type_

    cache = layout_type._cache
    dt = self.value.dtype

    # now use the dtype to key that cache.
    try:
        return cache[dt]
    except KeyError:
        # Computing and hashing `dtype_type` is slow, so we do not use it as a
        # hash key. The raw numpy dtype is much faster to use as a key.
        dtype_type = _numpy_support.from_dtype(dt)
        ret = cache[dt] = MultiVectorType(layout_type, dtype_type)
        return ret
github pygae / clifford / clifford / numba / _multivector.py View on Github external
def MultiVector___abs__(self):
    if isinstance(self, MultiVectorType):
        return MultiVector.__abs__
github pygae / clifford / clifford / numba / _multivector.py View on Github external
@numba.extending.box(MultiVectorType)
def box_MultiVector(typ: MultiVectorType, val: llvmlite.ir.Value, c) -> MultiVector:
    mv = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
    mv_obj = c.box(typ.value_type, mv.value)
    layout_obj = c.box(typ.layout_type, mv.layout)

    # All the examples use `c.pyapi.unserialize(c.pyapi.serialize_object(MultiVector))` here.
    # Doing so is much slower, as it incurs pickle. This is probably safe.
    class_obj_ptr = c.context.add_dynamic_addr(c.builder, id(MultiVector), info=MultiVector.__name__)
    class_obj = c.builder.bitcast(class_obj_ptr, c.pyapi.pyobj)
    res = c.pyapi.call_function_objargs(class_obj, (layout_obj, mv_obj))
    c.pyapi.decref(mv_obj)
    c.pyapi.decref(layout_obj)
    return res
github pygae / clifford / clifford / numba / _multivector.py View on Github external
@lower_constant(MultiVectorType)
def lower_constant_MultiVector(context, builder, typ: MultiVectorType, pyval: MultiVector) -> llvmlite.ir.Value:
    mv = cgutils.create_struct_proxy(typ)(context, builder)
    mv.value = context.get_constant_generic(builder, typ.value_type, pyval.value)
    mv.layout = context.get_constant_generic(builder, typ.layout_type, pyval.layout)
    return mv._getvalue()
github pygae / clifford / clifford / numba / _multivector.py View on Github external
def ga_xor(a, b):
    if isinstance(a, MultiVectorType) and isinstance(b, MultiVectorType):
        if a.layout_type != b.layout_type:
            raise numba.TypingError("MultiVector objects belong to different layouts")
        omt_func = a.layout_type.obj.omt_func
        def impl(a, b):
            return a.layout.MultiVector(omt_func(a.value, b.value))
        return impl
    elif isinstance(a, types.abstract.Number) and isinstance(b, MultiVectorType):
        def impl(a, b):
            return b.layout.MultiVector(b.value*a)
        return impl
    elif isinstance(a, MultiVectorType) and isinstance(b, types.abstract.Number):
        def impl(a, b):
            return a.layout.MultiVector(a.value*b)
        return impl
github pygae / clifford / clifford / numba / _multivector.py View on Github external
mv = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
    mv_obj = c.box(typ.value_type, mv.value)
    layout_obj = c.box(typ.layout_type, mv.layout)

    # All the examples use `c.pyapi.unserialize(c.pyapi.serialize_object(MultiVector))` here.
    # Doing so is much slower, as it incurs pickle. This is probably safe.
    class_obj_ptr = c.context.add_dynamic_addr(c.builder, id(MultiVector), info=MultiVector.__name__)
    class_obj = c.builder.bitcast(class_obj_ptr, c.pyapi.pyobj)
    res = c.pyapi.call_function_objargs(class_obj, (layout_obj, mv_obj))
    c.pyapi.decref(mv_obj)
    c.pyapi.decref(layout_obj)
    return res


numba.extending.make_attribute_wrapper(MultiVectorType, 'value', 'value')
numba.extending.make_attribute_wrapper(MultiVectorType, 'layout', 'layout')


@numba.extending.overload(operator.add)
def ga_add(a, b):
    if isinstance(a, MultiVectorType) and isinstance(b, MultiVectorType):
        if a.layout_type != b.layout_type:
            raise numba.TypingError("MultiVector objects belong to different layouts")
        def impl(a, b):
            return a.layout.MultiVector(a.value + b.value)
        return impl
    elif isinstance(a, types.abstract.Number) and isinstance(b, MultiVectorType):
        scalar_index = b.layout_type.obj._basis_blade_order.bitmap_to_index[0]
        ret_type = np.result_type(_numpy_support.as_dtype(a), _numpy_support.as_dtype(b.value_type.dtype))
        def impl(a, b):
            op = b.value.astype(ret_type)
            op[scalar_index] += a