Esempio n. 1
0
    def integral(self) -> Dict[ChannelID, ExpressionScalar]:
        expressions = {channel: 0 for channel in self._channels}
        for first_entry, second_entry in zip(self._entries[:-1],
                                             self._entries[1:]):
            substitutions = {
                't0': first_entry.t.sympified_expression,
                't1': second_entry.t.sympified_expression
            }

            v0 = sympy.IndexedBase(
                Broadcast(first_entry.v.underlying_expression,
                          (len(self.defined_channels), )))
            v1 = sympy.IndexedBase(
                Broadcast(second_entry.v.underlying_expression,
                          (len(self.defined_channels), )))

            for i, channel in enumerate(self._channels):
                substitutions['v0'] = v0[i]
                substitutions['v1'] = v1[i]

                expressions[
                    channel] += first_entry.interp.integral.sympified_expression.subs(
                        substitutions)

        expressions = {
            c: ExpressionScalar(expressions[c])
            for c in expressions
        }
        return expressions
Esempio n. 2
0
    def test_integral(self):
        symbolic = Broadcast(a * c, (3, ))
        indexed = symbolic[1]

        integ = sympy.Integral(symbolic, (a, 0, b))
        idx_integ = sympy.Integral(indexed, (a, 0, b))
        self.assertEqual(integ,
                         Broadcast(sympy.Integral(a * c, (a, 0, b)), (3, )))
        self.assertEqual(idx_integ,
                         Broadcast(sympy.Integral(a * c, (a, 0, b)), (3, ))[1])

        diffed = sympy.diff(symbolic, a)
        idx_diffed = sympy.diff(indexed, a)
        self.assertEqual(symbolic.subs(a, 1), diffed)
        self.assertEqual(indexed.subs(a, 1), idx_diffed)
Esempio n. 3
0
    def test_sympification(self):
        symbolic = Broadcast(a, (3, ))
        as_str = str(symbolic)

        re_sympified = qc_sympify(as_str)
        self.assertEqual(re_sympified, symbolic)

        sympification = qc_sympify('Broadcast(a, (3,))')
        self.assertEqual(sympification, symbolic)
Esempio n. 4
0
    def test_scalar_broad_cast(self):
        symbolic = Broadcast(a, (6,))
        self.assertIs(symbolic.func, Broadcast)
        self.assertEqual(symbolic.args, (a, (6,)))

        subs_symbol = symbolic.subs({a: b})
        self.assertIs(subs_symbol.func, Broadcast)
        self.assertEqual(subs_symbol.args, (b, (6,)))

        subs_scalar = symbolic.subs({a: 3.4})
        self.assertEqual(subs_scalar, sympy.Array([3.4, 3.4, 3.4, 3.4, 3.4, 3.4]))

        subs_symbol_vector = symbolic.subs({a: (b, 1, 2, 3, 4, 5)})
        self.assertEqual(subs_symbol_vector, sympy.Array([b, 1, 2, 3, 4, 5]))

        subs_numeric_vector = symbolic.subs({a: (0, 1, 2, 3, 4, 5)})
        self.assertEqual(subs_numeric_vector, sympy.Array([0, 1, 2, 3, 4, 5]))

        with self.assertRaises(ValueError):
            symbolic.subs({a: (b, 4, 5)})

        with self.assertRaises(ValueError):
            symbolic.subs({a: (8, 5, 3, 5, 5, 4, 4, 5)})
Esempio n. 5
0
    def test_numeric_evaluation(self):
        symbolic = Broadcast(a, (b, ))

        arguments = {'a': (1, 2., 3), 'b': 3}
        expected = np.asarray([1, 2., 3])
        result, _ = evaluate_lambdified(symbolic, ['a', 'b'], arguments, None)
        np.testing.assert_array_equal(expected, result)

        with self.assertRaises(ValueError):
            arguments = {'a': (1, 2., 3), 'b': 4}
            evaluate_lambdified(symbolic, ['a', 'b'], arguments, None)

        arguments = {'a': 1, 'b': 3}
        expected = np.asarray([1, 1, 1])
        result, _ = evaluate_lambdified(symbolic, ['a', 'b'], arguments, None)
        np.testing.assert_array_equal(expected, result)
Esempio n. 6
0
    def test_symbolic_shape(self):
        symbolic = Broadcast(a, (b, ))
        self.assertIs(symbolic.func, Broadcast)
        self.assertEqual(symbolic.args, (a, (b, )))

        subs_b = symbolic.subs({b: 6})
        self.assertIs(subs_b.func, Broadcast)
        self.assertEqual(subs_b.args, (a, (6, )))

        subs_a = symbolic.subs({a: 3})
        self.assertIs(subs_a.func, Broadcast)
        self.assertEqual(subs_a.args, (3, (b, )))

        subs_both_scalar = symbolic.subs({a: 3, b: 6})
        self.assertEqual(subs_both_scalar, sympy.Array([3, 3, 3, 3, 3, 3]))

        subs_both_array = symbolic.subs({a: (1, 2, 3, 4, 5, 6), b: 6})
        self.assertEqual(subs_both_array, sympy.Array([1, 2, 3, 4, 5, 6]))

        with self.assertRaises(ValueError):
            symbolic.subs({a: (1, 2, 3, 4, 5, 6), b: 7})
Esempio n. 7
0
    def test_numeric_evaluation(self):
        with self.assertWarns(UnsupportedBroadcastArgumentWarning):
            # use c (and not b as above)
            # here because sympy caches function call results and we want the warning everytime
            symbolic = Broadcast(a, (c, ))

        arguments = {'a': (1, 2., 3), 'c': 3}
        expected = np.asarray([1, 2., 3])
        result, _ = evaluate_lambdified(symbolic, ['a', 'c'], arguments, None)
        np.testing.assert_array_equal(expected, result)

        with self.assertRaises(ValueError):
            arguments = {'a': (1, 2., 3), 'c': 4}
            evaluate_lambdified(symbolic, ['a', 'c'], arguments, None)

        arguments = {'a': 1, 'c': 3}
        expected = np.asarray([1, 1, 1])
        result, _ = evaluate_lambdified(symbolic, ['a', 'c'], arguments, None)
        np.testing.assert_array_equal(expected, result)
Esempio n. 8
0
    def test_indexing(self):
        symbolic = Broadcast(a, (3, ))
        indexed = symbolic[1]

        self.assertEqual(7, indexed.subs(a, 7))
        self.assertEqual(7, indexed.subs(a, (6, 7, 8)))
Esempio n. 9
0
    def test_array_broadcast(self):
        expected = sympy.Array([1, 2, a, b])

        self.assertEqual(expected, Broadcast(list(expected), (4, )))
        self.assertEqual(expected, Broadcast(tuple(expected), (4, )))
        self.assertEqual(expected, Broadcast(expected, (4, )))
Esempio n. 10
0
    (a * dummy_a + sympy.exp(dummy_a), {
        dummy_a: b
    }, a * b + sympy.exp(b)),
]

##################################################### SYMPIFY ##########################################################
simple_sympify = [('a*b', a * b), ('a*6', a * 6), ('sin(a)', sin(a))]

complex_sympify = [
    ('Sum(a, (i, 0, n))', Sum(a, (i, 0, n))),
]

len_sympify = [('len(a)', Len(a)), ('Len(a)', Len(a))]

index_sympify = [('a[i]', a_[i]),
                 ('Broadcast(a, (3,))[0]', Broadcast(a, (3, ))[0]),
                 ('IndexedBroadcast(a, (3,), 1)',
                  IndexedBroadcast(a, (3, ), 1))]

#################################################### EVALUATION ########################################################
eval_simple = [(a * b, {
    'a': 2,
    'b': 3
}, 6), (a * b, {
    'a': 2,
    'b': np.float32(3.5)
}, 2 * np.float32(3.5)), (a + b, {
    'a': 3.4,
    'b': 76.7
}, 3.4 + 76.7)]