Commit a81bf04595f by Pierre Augier

Doc and test const function

parent 9a37733faaa
import numpy as np
from transonic import Transonic, boost, Array, Union
from transonic import Transonic, boost, Array, Union, const
# transonic def func(int, float)
......@@ -32,6 +32,14 @@ def func2(a: A, b: float):
return a - func_tmp(b)
A1 = Array[int, "1d", "C", "memview"]
def func3(c: const(A1)):
return c[0] + 1
ts = Transonic()
import numpy as np
from transonic import boost, Array
from transonic import boost, Array, const
T_index = np.int32
# we use a type variable because it can be replaced by a fused type.
......@@ -19,7 +19,7 @@ def row_sum(arr: A2d, columns: A1d_i):
@boost(boundscheck=False, wraparound=False)
def row_sum_loops(arr: V2d, columns: V1d_i):
def row_sum_loops(arr: const(V2d), columns: const(V1d_i)):
# locals type annotations are used only for Cython
i: T_index
j: T_index
......@@ -38,6 +38,8 @@ User API
.. autofunction:: typeof
.. autofunction:: const
Internal API
......@@ -59,6 +61,10 @@ Internal API
.. autofunction:: format_type_as_backend_type
.. autoclass:: ConstType
import re
from enum import Enum, auto
......@@ -631,6 +637,9 @@ class UnionMeta(Meta):
type_, backend_type_formatter, **kwargs
def short_repr(self):
return self.__name__
class Union(metaclass=UnionMeta):
"""Similar to typing.Union
......@@ -988,6 +997,8 @@ def typeof(obj):
class ConstType(Type):
"""Private API class for const"""
def __init__(self, type_):
self.type = type_
......@@ -1001,6 +1012,22 @@ class ConstType(Type):
def __repr__(self):
return f"const({repr(self.type)})"
def is_fused_type(self):
return self.type.is_fused_type()
def get_template_parameters(self):
return self.type.get_template_parameters()
def short_repr(self):
if hasattr(self.type, "short_repr"):
short_repr_type = self.type.short_repr()
short_repr_type = repr(self.type)
return f"constI{short_repr_type}I"
def const(type_):
"""Declare a type as constant (``const`` C/Cython keyword)"""
return ConstType(type_)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment