def truedivide(a, b): print("---------------------------------------------") print("Numpy : %2d true divide %2d = %d" % (a, b, np.true_divide(a, b))) print("Ocean : %2d true divide %2d = %d" % (a, b, ocean.trueDivide(a, b))) for dtype in [ocean.int8, ocean.half, ocean.float, ocean.double]: z = ocean.trueDivide(dtype(a), dtype(b)) print("Ocean : %2d true divide %2d = %s (%s,%s)" % (a, b, z, dtype.name, z.dtype.name)) A = ocean.asTensor([a], dtype) B = ocean.asTensor([b], dtype) print(ocean.trueDivide(A, B))
def divide(a, b): print("") print("Numpy : %2d divide %2d = %d" % (a, b, np.divide(a, b))) print("Python: %2d divide %2d = %d" % (a, b, a / b)) print("Ocean : %2d divide %2d = %d" % (a, b, ocean.divide(a, b))) for dtype in [ocean.int8, ocean.half, ocean.float, ocean.double]: z = ocean.divide(dtype(a), dtype(b)) print("Ocean : %2d divide %2d = %s (%s,%s)" % (a, b, z, dtype.name, z.dtype.name)) A = ocean.asTensor([a], dtype) B = ocean.asTensor([b], dtype) print(A / B)
import pyOcean_cpu as ocean a = ocean.asTensor([1 + 2j, 2 + 3j], 'r') b = ocean.asTensor([1, 2 + 1j]) print(a) print(ocean.conj(a)) print(b) print(ocean.gemm(1, a, b)) print(ocean.gemm(1, a.T, 'C', b)) print(ocean.gemm(1, a.T, 'C', b.T, 'C')) print(ocean.gemm(1, a, b.T, 'C'))
import pyOcean_cpu as ocean import sys def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() A = ocean.asTensor([[1 + 2j, 3 + 1j], [1, 2 + 2j]]) print(A) print(ocean.maximumAbs(A)) failTest("ocean.maximumAbs([[]])")
import pyOcean_cpu as ocean a = ocean.asTensor([ocean.nan, 3, 4, ocean.nan], ocean.float) a.fillNaN(ocean.inf) print(a)
import pyOcean_cpu as ocean print(ocean.sumAbs([1,-2,3])) for dtype in [ocean.int8, ocean.int16, ocean.half, ocean.chalf, ocean.cdouble] : a = ocean.asTensor([1,-2,5],dtype) print(ocean.sumAbs(a))
import pyOcean_cpu as ocean device = ocean.cpu dtypes = [ocean.bool, ocean.int8, ocean.uint8, ocean.int32] v1 = [0,2,3,7,127] v2 = [0,1,2,4,8,15] for dtype in dtypes : print("\n========= Tensor %s (%s) =========" % (dtype.name, device.name)) a1 = ocean.asTensor(v1,'r',dtype,device).broadcastTo([len(v2),len(v1)]).replicate() a2 = ocean.asTensor(v2,dtype,device).reshape([len(v2),1]) print(a1) print(a2) print("\n------ a1 ^= a2 ------") a1 ^= a2 print(a1)
import pyOcean_cpu as ocean for dtype in [ocean.int8, ocean.half, ocean.cfloat]: print("========= (%s) =========" % dtype.name) a = ocean.asTensor([1, 2, 3], dtype) print([ocean.allLE(a, 3), True]) print([ocean.allLT(a, 3), False]) print([ocean.allLE(a, 3.1), True]) print([ocean.allLT(a, 3.01), True]) print([ocean.allLE(a, 100000), True]) print([ocean.allLT(a, ocean.inf), True]) print([ocean.allLE(a, -ocean.inf), False]) print([ocean.allLE(a, 1), False]) print([ocean.allLE(a, -1000), False]) print([ocean.allGE(a, 1), True]) print([ocean.allGT(a, 1), False]) print([ocean.allGE(a, 0.99), True]) print([ocean.allGT(a, 0.99), True]) print([ocean.allGT(a, -10000), True]) print([ocean.allGE(a, -ocean.inf), True]) print([ocean.allInRange(a, 1, 3), True]) print([ocean.allInRange(a, 1.1, 3), False]) print([ocean.allInRange(a, 1, 3.1), True]) print([ocean.allInRange(a, 1.1, 2.9), False]) print([ocean.allInRange(a, 1, False, 3), False]) print([ocean.allInRange(a, 1, 3, False), False])
import pyOcean_cpu as ocean import numpy as np import pyOceanNumpy print(ocean.asTensor([np.int8(1), np.int16(2), np.int32(3), np.int64(4)])) print(ocean.asTensor([np.uint8(5), np.uint16(6), np.uint32(7), np.uint64(8)])) print(ocean.asTensor([np.float16(9.0), np.float32(10.0), np.float64(11.0)])) print(ocean.asTensor([np.complex64(12 + 13j), np.complex128(14 + 15j)]))
import pyOcean_cpu as ocean device = ocean.cpu dtypes = [ocean.bool, ocean.int8, ocean.uint8] for dtype in dtypes: print("\n========= Tensor %s (%s) =========" % (dtype.name, device.name)) a = ocean.asTensor([0, 1, -1], dtype, device) print(a) print(ocean.bitwiseNot(a)) print(~a) print(ocean.logicalNot(a)) r = ocean.tensor(3, dtype, device) ocean.bitwiseNot(a, r) print(r) ocean.logicalNot(a, r) print(r)
import pyOcean_cpu as ocean device = ocean.cpu v1 = [1, 2, 3, 1, ocean.nan, 2 + 3j, 4 - 5j] v2 = [3, 2, 1, 1, 1, 2 + 2j, 4 - 6j] dtypes = [ocean.int8, ocean.uint8, ocean.float, ocean.chalf, ocean.cdouble] for dtype in dtypes: print("\n========= %s (%s) =========" % (dtype.name, device.name)) a1 = ocean.asTensor(v1, dtype, device) a2 = ocean.asTensor(v2, dtype, device) r1 = ocean.tensor([len(v1)], dtype, device) r2 = ocean.tensor([len(v1)], ocean.cfloat, device) print(a1) print(a2) print("\n------ min(a1,a2), min(a2,a1), max(a1,a2) ------") print(ocean.min(a1, a2)) print(ocean.min(a2, a1)) print(ocean.max(a1, a2)) print("\n------ fmin(a1,a2), fmin(a2,a1), fmax(a1,a2) ------") print(ocean.fmin(a1, a2)) print(ocean.fmin(a2, a1)) print(ocean.fmax(a1, a2)) print("\n------ fmin(a1,a2,r1), fmin(a1,a2,r2) ------") ocean.fmin(a1, a2, r1) ocean.fmin(a1, a2, r2) print(r1)
def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() device = ocean.cpu print("\n========= Tensor and tensor =========") a = ocean.asTensor([1, 2, 3, 4], 'r', ocean.float, device) b = ocean.asTensor([0, 0.5, 1, 2], ocean.float, device) print(a**b) print(pow(a, b)) print(ocean.power(a, b)) c = ocean.tensor([4, 4], ocean.float, device) ocean.power(a, b, c) print(c) c = ocean.tensor([4, 4], ocean.cdouble, device) ocean.power(a, b, c) print(c) c = ocean.tensor([4, 4], ocean.int16, device)
import pyOcean_cpu as ocean a = ocean.asTensor([[1, 2], [3, 4]], 'r', ocean.float) b = ocean.arange(6, ocean.float).reshape([2, 3]) print(a) print(b) print(ocean.gemm(ocean.float(3), a, b)) alpha = ocean.asTensor([1, 2, 3], ocean.float).reshape([1, 1, 3]) print(ocean.gemm(alpha, a, b)) print(ocean.gemm(ocean.float(1), a.T, b))
## Square-root operation with overlap import pyOcean_cpu as ocean a = ocean.asTensor([4, 9, 16, 25]) a1 = ocean.tensor(a.storage, 0, [3]) a2 = ocean.tensor(a.storage, 1, [3]) print(a1) print(a2) ocean.sqrt(a1, a2) print(a1) print(a2)
import pyOcean_cpu as ocean import sys def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() a = ocean.asTensor([[1, 2, 0, 0, 3], [0, 4, 5, 0, 0]], 'R') print(a) print(ocean.find(a)) a = ocean.tensor([]) failTest("ocean.find(a)")
import pyOcean_cpu as ocean import numpy as np import pyOceanNumpy a = np.arange(24).reshape([3, 2, 4]) print(a) b = ocean.asTensor(a).reverseAxes2() print(b) b.fill(3) b.sync() print(a)
import pyOcean_cpu as ocean device = ocean.cpu dtypes = [ocean.bool, ocean.int8, ocean.uint8, ocean.int32] v1 = [1,2,3,7,127] v2 = [0,1,2,4,8,15] for dtype in dtypes : print("\n========= Tensor %s (%s) =========" % (dtype.name, device.name)) a1 = ocean.asTensor(v1,'r',dtype,device) a2 = ocean.asTensor(v2,dtype,device).reshape([len(v2),1]) r = ocean.tensor([len(v2),len(v1)],dtype,device) print(a1) print(a2) print("\n------ bitwiseAnd(a1,a2), (a1 & a2), bitwiseAnd(a1,a2,r) ------") print(ocean.bitwiseAnd(a1,a2)) print(a1 & a2) ocean.bitwiseAnd(a1,a2,r) print(r)
import pyOcean_cpu as ocean a = ocean.full([2,3],1.0) b = ocean.full([3,2],2.0) print(ocean.asTensor([a,b,[[3]]], 0, ocean.int16))
import pyOcean_cpu as ocean import numpy as np import pyOceanNumpy t = np.asarray([[1,2],[3,4],[5,6]],np.int32) print(t) print("====== ocean.asTensor(t) ======") a = ocean.asTensor(t); print(a) print("====== ocean.asTensor([t,a]) ======") b = ocean.asTensor([t,a]) print(b)
## Cloning of tensors import pyOcean_cpu as ocean a = ocean.asTensor([[1, 2, 3], [4, 5, 6]], "R", ocean.float) print(a) print(a.clone()) print(a.clone(ocean.cpu))
## Index binding import pyOcean_cpu as ocean a = [True,True,False]; b = ocean.index[a] print([b,b.isBound()]) b.bind([3],True) print([b,b.isBound()]) b.bind([3],[ocean.int64.size],True) print([b,b.isBound()]) a = ocean.asTensor([1,2,3],ocean.int64) print(a[b])
import pyOcean_cpu as ocean a = ocean.asTensor([1, 2, 3]) b = ocean.tensor(a.size, a.dtype) a.byteswap() b.copy(a) print(a) print(b)
import pyOcean_cpu as ocean A = ocean.asTensor([[0,1],[2,3]],'r',ocean.float) b = ocean.asTensor([1,2],ocean.float) B = b.reshape([2,1]); c = ocean.tensor([2],ocean.float); C = ocean.tensor([2,1],ocean.float); print(A) print(b) print(B) print("\n====== A*b ======"); print(ocean.gemm(1,A,b)) print("\n====== A*B ======"); print(ocean.gemm(1,A,B)) print("\n====== c = A*b ======"); ocean.gemm(1,A,b,0,c); print(c) print("\n====== C = A*b ======"); ocean.gemm(1,A,b,0,C); print(C) print("\n====== c = A*B ======"); ocean.gemm(1,A,B,0,c); print(c) print("\n====== C = A*B ======"); ocean.gemm(1,A,B,0,C); print(C)
import pyOcean_cpu as ocean A = ocean.asTensor([[0, 0, 1], [0, 1, 1], [1, 0, 0]], 'r', ocean.bool) print("\n--------- A ---------") print(A) print("\n--------- A' ---------") print(A.T) print("\n--------- A * A ---------") print(ocean.multiply(A, A)) print("\n--------- A * A' ---------") print(ocean.multiply(A, A, 'T')) print("\n--------- A' * A ---------") print(ocean.multiply(A, 'T', A)) print("\n--------- A' * A' ---------") print(ocean.multiply(A, 'T', A, 'T'))
import pyOcean_cpu as ocean a = ocean.asTensor(range(24)) b = a.reshape([2, 3, 4]) print(b) a = ocean.tensor([4, 6], [7, 1]) a.copy(range(a.nelem)) b = a.reshape([2, 3, 4]) print(a) print(b) b = a.reshape([6, 4]) print(b) a.reshape([6, 4], True) print(a)
## Indexing with a zero-dimensional Boolean array import pyOcean_cpu as ocean a = ocean.asTensor(True,ocean.bool); b = ocean.zeros([3,3]) b[a] = 3 print(b)
import pyOcean_cpu as ocean device = ocean.cpu dtypes = [ocean.int8, ocean.int16, ocean.float, ocean.chalf, ocean.cdouble] otypes = [ocean.float, ocean.uint8, ocean.double, ocean.int32, ocean.chalf] print("\n========= fabs =========") for (dtype, otype) in zip(dtypes, otypes): a = ocean.asTensor([-3 - 4j], dtype, device) b = ocean.tensor([1], otype, device) ocean.fabs(a, b) print(b) print("\n========= absolute =========") for (dtype, otype) in zip(dtypes, otypes): a = ocean.asTensor([-3 - 4j], dtype, device) b = ocean.tensor([1], otype, device) ocean.absolute(a, b) print(b)
import pyOcean_cpu as ocean a = ocean.asTensor(range(8),ocean.float).reshape([1,8]) a = a.broadcastTo([6,8]) print(a) v = a.split(0,[0,2,3,1]) for i in range(4) : print("\n------ Slice %d ------" % (i+1)) print(v[i]) b = ocean.merge(v,0) print(b) print("\n------ Allowed case #1 ------") ocean.merge(v,0,a) print(a) print("\n------ Allowed case #2 ------") b = ocean.tensor(a.size, a.strides, a.dtype) b.fill(0) ocean.merge(v,0,b) print(b) print("\n------ Non-allowed case #1 ------") v[-1] = v[-1].clone() try : ocean.merge(v,0,a) except : print("Expected error: merging along zero-strided dimension")
import pyOcean_cpu as ocean import numpy as np import pyOceanNumpy a = ocean.asTensor([]) print(a) a = ocean.asTensor(1) print(a) a = ocean.asTensor(2, ocean.int16) print(a) a = ocean.asTensor(np.float32(3)) print(a) a = ocean.asTensor(np.int8(4)) print(a) a = ocean.asTensor([[1, 2, 3], [4, 5, 6]]) print(a) a = ocean.asTensor([[1, 2, 3], [4, 5, 6.]]) print(a) t = np.asarray([[1, 2], [3, 4]], np.int32) print(t) a = ocean.asTensor(t) print(a) a = ocean.asTensor(t, ocean.float) print(a) # Combination of tensors b = ocean.asTensor( [t, a, [(1, 1.5), [np.float(3), ocean.asTensor(4)]]], ocean.double)
import pyOcean_cpu as ocean # Type casting applied to storage objects s = ocean.asTensor([1, 2, 3]).storage print(s) t = ocean.int8(s) print(t) ocean.float(s, True) print(s)