def test_rnd_number_tuples(self): x1, x2, x3 = xx = sp.symbols('x1:4') s = sum(xx) res_a1 = st.rnd_number_subs_tuples(s) self.assertTrue(isinstance(res_a1, list)) self.assertEqual(len(res_a1), len(xx)) c1 = [ len(e) == 2 and e[0].is_Symbol and st.is_number(e[1]) for e in res_a1 ] self.assertTrue(all(c1)) t = sp.Symbol('t') f = sp.Function('f')(t) fdot = f.diff(t) fddot = f.diff(t, 2) ff = sp.Matrix([f, fdot, fddot, x1 * x2]) for i in range(100): res_b1 = st.rnd_number_subs_tuples(ff, seed=i) expct_b1_set = set([f, fdot, fddot, t, x1, x2]) res_b1_atom_set = set(lzip(*res_b1)[0]) self.assertEqual(expct_b1_set, res_b1_atom_set) # highest order has to be returned first self.assertEqual(res_b1[0][0], fddot) self.assertEqual(res_b1[1][0], fdot) self.assertTrue(all([st.is_number(e[1]) for e in res_b1]))
def test_rnd_number_tuples2(self): x1, x2, x3 = xx = st.symb_vector('x1:4') yy = st.symb_vector('y1:4') s = sum(xx) res_a1 = st.rnd_number_subs_tuples(s, seed=1) res_a2 = st.rnd_number_subs_tuples(s, seed=2) self.assertNotEqual(res_a1, res_a2) res_b1 = st.rnd_number_subs_tuples(s, seed=2) self.assertEqual(res_b1, res_a2) xxyy = xx + yy rnst1 = st.rnd_number_subs_tuples(xxyy) rnst2 = st.rnd_number_subs_tuples(xxyy, exclude=x1) rnst3 = st.rnd_number_subs_tuples(xxyy, exclude=[x1, x2]) rnst4 = st.rnd_number_subs_tuples(xxyy, exclude=xx) symbols1 = xxyy.subs(rnst1).atoms(sp.Symbol) symbols2 = xxyy.subs(rnst2).atoms(sp.Symbol) symbols3 = xxyy.subs(rnst3).atoms(sp.Symbol) symbols4 = xxyy.subs(rnst4).atoms(sp.Symbol) self.assertEqual(symbols1, set()) self.assertEqual(symbols2, set([x1])) self.assertEqual(symbols3, set([x1, x2])) self.assertEqual(symbols4, set([x1, x2, x3])) # this was a bug: rnst = st.rnd_number_subs_tuples(xxyy, prime=True, exclude=[x1, x2]) self.assertEqual(xxyy.subs(rnst).atoms(sp.Symbol), set([x1, x2]))
def test_rnd_number_tuples3(self): a, b = sp.symbols('a, b', commutative=False) term1 = a * b - b * a st.warnings.simplefilter("always") with st.warnings.catch_warnings(record=True) as cm: st.rnd_number_subs_tuples(term1) self.assertEqual(len(cm), 1) self.assertTrue('not commutative' in str(cm[0].message)) with st.warnings.catch_warnings(record=True) as cm2: st.subs_random_numbers(term1) self.assertEqual(len(cm2), 1) self.assertTrue('not commutative' in str(cm2[0].message))