# How to use the transonic.Type function in transonic

## To help you get started, we’ve selected a few transonic 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. fluiddyn / transonic / data_tests / type_hint_notemplate.py View on Github ``````import numpy as np

import transonic as ts
from transonic import Type, NDim, Array, Union

T = Type(int, np.complex128)
N = NDim(1, 3)

A = Array[T, N]
A1 = Array[np.float32, N + 1]

A3d = Array[np.float32, "3d"]
N1 = NDim(4, 5)
N1 = NDim(4, 5)

T = Type(int, np.complex128)

@ts.boost
def compute(a: A, b: A, c: T, d: Union[A, A1], e: str):
print(e)
tmp = a + b
if 1 and 2:
tmp *= 2
return tmp

main = partial(lambda x: x, lambda x: x)`````` fluiddyn / transonic / data_tests / type_hint_notemplate.py View on Github ``````from functools import partial

import numpy as np

import transonic as ts
from transonic import Type, NDim, Array, Union

T = Type(int, np.complex128)
N = NDim(1, 3)

A = Array[T, N]
A1 = Array[np.float32, N + 1]

A3d = Array[np.float32, "3d"]
N1 = NDim(4, 5)
N1 = NDim(4, 5)

T = Type(int, np.complex128)

@ts.boost
def compute(a: A, b: A, c: T, d: Union[A, A1], e: str):
print(e)
tmp = a + b`````` fluiddyn / transonic / doc / examples / type_hints_notemplate.py View on Github ``````import numpy as np
from transonic import Type, NDim, Array, boost

T = Type(int, np.complex128)
N = NDim(1, 3)

A = Array[T, N]
A1 = Array[np.float32, N + 1]

@boost
def compute(a: A, b: A, c: T, d: A1, e: str):
print(e)
tmp = a + b
return tmp`````` fluiddyn / transonic / doc / examples / blocks_type_hints.py View on Github ``````import numpy as np

from transonic import Transonic, Type, NDim, Array

T = Type(float, complex)
N = NDim(2, 3)
A = Array[T, N]
A1 = Array[T, N + 1]

ts = Transonic()

class MyClass:
def __init__(self, a, b):
self.a = a
self.b = b

def compute(self, n):

a = self.a
b = self.b`````` fluiddyn / transonic / tmp / analyses / examples / 1_type_hint.py View on Github ``````T = Type(int, np.complex128)

dim = 2
dim += 1

N = NDim(1, dim)

A = Array[T, N]
A1 = Array[np.float32, N + 1]

A3d = Array[np.float32, "3d"]
N1 = NDim(4, 5)
N1 = NDim(4, 5)

T = Type(int, np.complex128)

a_type_var = "hello"
myconst = 0

cdict = skimage.color.color_dict

@ts.boost
def compute(a: A, b: A, c: T, d: Union[A, A1], e: str):
print(e)
tmp = a + b + myconst
return tmp`````` fluiddyn / transonic / doc / examples / not_implemented / type_hint_shape.py View on Github ``````"""
Not yet implemented...

Many things can be expressed in Pythran specifications (see
in particular stride arrays and partial shapes...

We could also express these concepts in strings, mainly following Pythran...

"""

from transonic import boost, Type, NDim, Shape, Array

T = Type(int, float)

# here the shape of the array is only defined with the ShapeVar
A = Array[T, Shape("[3, :]", "[3, :, :]", "[::, ::]", "[::, ::, ::]")]

@boost
def compute(a: A, b: A, c: T):
return a + b

# if there is a NDimVar, we can use the ellipsis
A1 = Array[T, NDim(1, 3), Shape("[3, ...]", "[::, ...]")]

@boost
def compute1(a: A1, b: A1, c: T):`````` fluiddyn / transonic / tmp / var_annot / simple.py View on Github ``````import numpy as np

from transonic import Type, NDim, Array, boost

T = Type(np.float64, np.complex128)
N = NDim(1)
A = Array[T, N]

@boost
def func(a: A):
i: int
n: int = a.shape

for i in range(n):
a[i] = a[i] + 1.`````` fluiddyn / transonic / tmp / analyses / examples / 5_blocks_type_hints.py View on Github ``````import numpy as np

import foo

from transonic import Transonic, Type, NDim, Array

T = Type(float, complex)
N = NDim(1, 2)
A = Array[T, N]
A1 = Array[T, N + 1]

ts = Transonic()

class MyClass:
def __init__(self, a, b):
self.a = a
self.b = b

def compute(self, n):

a = self.a
b = self.b`````` fluiddyn / transonic / doc / examples / bench_proj_perp / bench.py View on Github ``````import numpy as np
from transonic import boost, Array, Type

A = Array[Type(np.float64, np.complex128), "3d"]
Af = "float[:,:,:]"
A = Af  # issue fused type with Cython

def proj(vx: A, vy: A, vz: A, kx: Af, ky: Af, kz: Af, inv_k_square_nozero: Af):
tmp = (kx * vx + ky * vy + kz * vz) * inv_k_square_nozero
vx -= kx * tmp
vy -= ky * tmp
vz -= kz * tmp

def proj_loop(
vx: A, vy: A, vz: A, kx: Af, ky: Af, kz: Af, inv_k_square_nozero: Af
):

# type annotations only useful for Cython``````

## transonic

Make your Python code fly at transonic speeds! GitHub BSD-3-Clause Latest version published 1 month ago

60 / 100