def setUp(self): self.a = sympy.Symbol("a")*sympy.Symbol("a") self.nan = nan self.n = NewArray([1.0, 1, Inf]) self.x = sympy.Symbol("x") self.k = sympy.Symbol("k") self.y = sympy.Symbol("y") self.z = sympy.Symbol("z") self.arr3 = NewArray([self.y, self.z, self.x]) self.arr3_ = NewArray([self.x, self.z, self.k]) self.arr3_NaN = NewArray([self.x, self.z, nan]) self.arr4 = NewArray([self.x, self.y, self.z, self.k])
def test_sym_func(): a = 1 b = sympy.Symbol("x") c = NewArray((sympy.Symbol("x"), sympy.Symbol("y"))) d = NewArray((sympy.Symbol("x"), sympy.Symbol("x"), sympy.Symbol("z"))) e = NewArray([sympy.Symbol("x")] * self_grpup) sym_func = funcs["sym_func"] sym_func(a) sym_func(b) sym_func(c) s = sym_func(d) s = sym_func(e)
def test_sub(self): self.assertEqual(self.arr3 - self.a, NewArray([self.y - self.a, self.z - self.a, self.x - self.a])) arrs = NewArray( [i - j for i, j in zip(self.arr3_ , self.arr3)] ) self.assertEqual(self.arr3_ - self.arr3,arrs) self.assertNotEqual(self.arr3_NaN - self.arr3,arrs) arrs2 = NewArray( [i - j for i, j in zip(self.n,self.arr3)] ) self.assertEqual(self.n - self.arr3, arrs2)
def test_rdiv(self): self.assertEqual(self.a / self.arr3, NewArray([self.a/self.y ,self.a/ self.z , self.a/self.x])) # self.assertEqual(self.a / self.arr3, 1/NewArray([self.y / self.a, self.z / self.a, self.x / self.a])) arrs = NewArray( [i / j for i, j in zip(self.arr3_ , self.arr3)] ) self.assertEqual(self.arr3_ / self.arr3,arrs) self.assertNotEqual(self.arr3_NaN / self.arr3,arrs) arrs2 = NewArray( [i / j for i, j in zip(self.n,self.arr3)] ) self.assertEqual(self.n / self.arr3, arrs2)
def test_add(self): self.assertEqual(self.a + self.arr3, NewArray([self.y + self.a, self.z + self.a, self.x + self.a])) self.assertEqual(self.arr3+self.a , NewArray([self.y + self.a, self.z + self.a, self.x + self.a])) arrs = NewArray( [i + j for i, j in zip(self.arr3_ , self.arr3)] ) self.assertEqual(self.arr3_ + self.arr3,arrs) self.assertNotEqual(self.arr3_NaN + self.arr3,arrs) arrs2 = NewArray( [i + j for i, j in zip(self.n,self.arr3)] ) self.assertEqual(self.arr3 + self.n, arrs2)
def test_mul(self): self.assertEqual(self.a * self.arr3, NewArray([self.y * self.a, self.z * self.a, self.x * self.a])) self.assertEqual(self.arr3*self.a , NewArray([self.y * self.a, self.z * self.a, self.x * self.a])) arrs = NewArray( [i * j for i, j in zip(self.arr3_ , self.arr3)] ) self.assertEqual(self.arr3_ * self.arr3,arrs) self.assertNotEqual(self.arr3_NaN * self.arr3,arrs) arrs2 = NewArray( [i * j for i, j in zip(self.n,self.arr3)] ) self.assertEqual(self.n*self.arr3, arrs2)
def tsum(*ters, name="gx0"): """ Parameters ---------- ters: tuple of SymbolTerminalDetail SymbolTerminalDetail name: str sepcific the name of results. Returns ------- SymbolTerminalDetail """ for i in ters: assert isinstance(i, SymbolTerminalDetail), "only the SymbolTerminals can be added" assert all(ters[0].dim == i.dim for i in ters) values = np.array([i.value for i in ters]) dim = Dim(np.array([i.dim for i in ters])) sym = NewArray([i.sym for i in ters], shape=(len(ters),)) name = str(name) prob = sum([i.prob for i in ters]) / len(ters) res = SymbolTerminalDetail(values, name, dim=dim, prob=prob, init_sym=sym, init_name=str(list(sym))) return res
def gsymf(x): if isinstance(x, (np.ndarray, NewArray)): if x.shape[0] == 2: res = operation(x[0], x[1]) if keep: return NewArray(res) else: return res else: # >=3 if is_jump: return x else: res = operation(*x) if keep: return NewArray(res) else: return res else: # 1 return x
def test_mdiv(self): f = sym_map()["MDiv"] self.assertEqual(f(self.arr3), self.arr3) self.assertEqual(f(NewArray([self.y,self.z])), self.y/self.z)
def test_msub(self): f = sym_map()["MSub"] self.assertEqual(f(self.arr3), self.arr3) self.assertEqual(f(NewArray([self.y,self.z])), self.y-self.z)
def test_conv(self): f = sym_map()["Conv"] self.assertEqual(f(self.arr3), self.arr3) assert True==(f(NewArray([self.y,self.z]))==NewArray([self.z,self.y]))
def test_sin(self): f = sym_map()["exp"] self.assertEqual(f(self.arr3), NewArray([f(self.y),f(self.z), f(self.x)]))