Ejemplo n.º 1
0
    def integral(self) -> Dict[ChannelID, ExpressionScalar]:
        expressions = {}
        shape = (len(self.defined_channels),)

        for i, channel in enumerate(self._channels):
            def value_trafo(v):
                try:
                    return v.underlying_expression[i]
                except TypeError:
                    return IndexedBroadcast(v.underlying_expression, shape, i)
            pre_entry = TableEntry(0, self._entries[0].v, None)
            entries = [pre_entry] + self._entries
            expressions[channel] = TableEntry._sequence_integral(entries, expression_extractor=value_trafo)
        return expressions
    def test_sequence_integral(self):
        def get_sympy(v):
            return v.sympified_expression

        entries = [TableEntry(0, 0), TableEntry(1, 0, 'hold')]
        self.assertEqual(ExpressionScalar(0),
                         TableEntry._sequence_integral(entries, get_sympy))

        entries = [TableEntry(0, 1), TableEntry(1, 1, 'hold')]
        self.assertEqual(ExpressionScalar(1),
                         TableEntry._sequence_integral(entries, get_sympy))

        entries = [TableEntry(0, 0), TableEntry(1, 1, 'linear')]
        self.assertEqual(ExpressionScalar(.5),
                         TableEntry._sequence_integral(entries, get_sympy))

        entries = [
            TableEntry('t0', 'a', 'linear'),
            TableEntry('t1', 'b', 'linear'),
            TableEntry('t2', 'c', 'hold')
        ]
        self.assertEqual(ExpressionScalar('(t1-t0)*(a+b)/2 + (t2-t1)*b'),
                         TableEntry._sequence_integral(entries, get_sympy))