Exemple #1
0
def differentiate_term(term, vars, use_sympy=False):
    factors = term.factors

    for var in vars:
        affected_factors = set(
            factor for factor in factors
            if var in _factor_symbols(factor, use_sympy=use_sympy))
        if not affected_factors:
            return Term({Factor('0', eval_method='literal')})
        factors = factors.difference(affected_factors).union(
            _differentiate_factors(affected_factors, var, use_sympy=use_sympy))

    return Term(factors or {Factor('1', eval_method='literal')})
Exemple #2
0
def test_differentiate_term():
    t = Term([Factor('a'), Factor('log(b)')])

    assert str(differentiate_term(t, ['a'])) == 'log(b)'
    assert str(differentiate_term(t, ['log(b)'])) == 'a'
    assert str(differentiate_term(t, ['b'])) == '0'

    assert str(differentiate_term(t, ['a'], use_sympy=True)) == 'log(b)'
    assert str(differentiate_term(t, ['log(b)'], use_sympy=True)) == '0'  # 'log(b)' is not in the free symbol list.
    assert str(differentiate_term(t, ['b'], use_sympy=True)) == '(1/b):a'
Exemple #3
0
 def test_to_terms(self, token_a):
     assert token_a.to_terms() == {Term([token_a.to_factor()])}
Exemple #4
0
 def term1(self):
     return Term([Factor('c'), Factor('b')])
Exemple #5
0
 def term2(self):
     return Term([Factor('c'), Factor('d')])