derivatives.py 9.54 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
# This file is part of TRS (http://math.kompiler.org)
#
# TRS is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# TRS is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TRS.  If not, see <http://www.gnu.org/licenses/>.
from .utils import find_variables, first_sorted_variable
from ..node import ExpressionLeaf as L, Scope, OP_DER, OP_MUL, OP_LOG, \
        OP_SIN, OP_COS, OP_TAN, OP_ADD, OP_DIV, E, sin, cos, der, ln
from ..possibilities import Possibility as P, MESSAGES
from ..translate import _


def second_arg(node):
    """
    Get the second child of a node if it exists, None otherwise.
    """
    return node[1] if len(node) > 1 else None


def same_der(root, new):
    """
    Replace root with a new derrivative, using the same operator as root
    (OP_PRIME or DXDER).
    """
    return der(new, second_arg(root))


def get_derivation_variable(node, variables=None):
    """
    Find the variable to derive over.

    >>> print get_derivation_variable(der(L('x')))
    'x'
    """
    if len(node) > 1:
        assert node[1].is_identifier()
        return node[1].value

    if not variables:
        variables = find_variables(node)

    if not len(variables):
        return None

    return first_sorted_variable(variables)


def chain_rule(root, args):
    """
    Apply the chain rule:
    [f(g(x)]'  ->  f'(g(x)) * g'(x)

    f'(g(x)) is not expressable in the current syntax, so calculate it directly
    using the application function in the arguments. g'(x) is simply expressed
    as der(g(x), x).
    """
    g, f_deriv, f_deriv_args = args

    return f_deriv(root, f_deriv_args) * same_der(root, g)


MESSAGES[chain_rule] = _('Apply the chain rule to {0}.')


def match_zero_derivative(node):
    """
    der(x, y)  ->  0
    der(n)     ->  0
    """
    assert node.is_op(OP_DER)

    variables = find_variables(node[0])
    var = get_derivation_variable(node, variables)

    if not var or var not in variables:
        return [P(node, zero_derivative)]

    return []


def zero_derivative(root, args):
    """
    der(x, y)  ->  0
    der(n)     ->  0
    """
    return L(0)


MESSAGES[zero_derivative] = _('Constant {0[0]} has derivative 0.')


def match_one_derivative(node):
    """
    der(x)     ->  1  # Implicit x
    der(x, x)  ->  1  # Explicit x
    """
    assert node.is_op(OP_DER)

    var = get_derivation_variable(node)

    if var and node[0] == L(var):
        return [P(node, one_derivative)]

    return []


def one_derivative(root, args):
    """
    der(x)     ->  1
    der(x, x)  ->  1
    """
    return L(1)


MESSAGES[one_derivative] = _('Variable {0[0]} has derivative 1.')


def match_const_deriv_multiplication(node):
    """
    der(c * f(x), x)  ->  c * der(f(x), x)
    """
    assert node.is_op(OP_DER)

    p = []

    if node[0].is_op(OP_MUL):
        x = L(get_derivation_variable(node))
        scope = Scope(node[0])

        for n in scope:
            if not n.contains(x):
                p.append(P(node, const_deriv_multiplication, (scope, n, x)))

    return p


def const_deriv_multiplication(root, args):
    """
    der(c * f(x), x)  ->  c * der(f(x), x)
    """
    scope, c, x = args

    scope.remove(c)

    return c * same_der(root, scope.as_nary_node())


MESSAGES[const_deriv_multiplication] = \
        _('Bring multiplication with {2} in derivative {0} to the outside.')


def match_variable_power(node):
    """
    der(x ^ n)           ->  n * x ^ (n - 1)
    der(x ^ n, x)        ->  n * x ^ (n - 1)
    der(f(x) ^ n)        ->  n * f(x) ^ (n - 1) * der(f(x))  # Chain rule
    der(f(x) ^ g(x), x)  ->  der(e ^ ln(f(x) ^ g(x)), x)
    """
    assert node.is_op(OP_DER)

    if not node[0].is_power():
        return []

    root, exponent = node[0]

    rvars = find_variables(root)
    evars = find_variables(exponent)
    x = get_derivation_variable(node, rvars | evars)

    if x in rvars:
        if x in evars:
            return [P(node, power_rule)]

        if root.is_variable():
            return [P(node, variable_root)]

        return [P(node, chain_rule, (root, variable_root, ()))]

    if exponent.is_variable():
        return [P(node, variable_exponent)]

    return [P(node, chain_rule, (exponent, variable_exponent, ()))]


def power_rule(root, args):
    """
    [f(x) ^ g(x)]'  ->  [e ^ ln(f(x) ^ g(x))]'
    """
    return same_der(root, L(E) ** ln(root[0]))


MESSAGES[power_rule] = \
        _('Write {0} as a logarithm to be able to separate root and exponent.')


def variable_root(root, args):
    """
    der(x ^ n, x)  ->  n * x ^ (n - 1)
    """
    x, n = root[0]

    return (n).negate(root[0].negated) * x ** (n - 1)


MESSAGES[variable_root] = \
        _('Apply standard derivative `d/dx x ^ n = n * x ^ (n - 1)` on {0}.')


def variable_exponent(root, args):
    """
    der(g ^ x, x)  ->  g ^ x * ln(g)

    Shortcut rule (because of presence on formula list):
    der(e ^ x, x)  ->  e ^ x
    """
    g, x = root[0]

    if g == E:
        return (g ** x).negate(root[0].negated)

    return (g ** x).negate(root[0].negated) * ln(g)


MESSAGES[variable_exponent] = \
        _('Apply standard derivative `d/dx g ^ x = g ^ x * ln g`.')


def match_logarithmic(node):
    """
    der(log(x, g), x)     ->  1 / (x * ln(g))
    der(log(f(x), g), x)  ->  1 / (f(x) * ln(g)) * der(f(x), x)
    """
    assert node.is_op(OP_DER)

    x = get_derivation_variable(node)

    if x and node[0].is_op(OP_LOG):
        f = node[0][0]
        x = L(x)

        if f == x:
            return [P(node, logarithmic, ())]

        if f.contains(x):
            return [P(node, chain_rule, (f, logarithmic, ()))]

    return []


def logarithmic(root, args):
    """
    der(log(x, g), x)  ->  1 / (xln(g))

    Shortcut function (because of presence on formula list):
    der(ln(x), x)      ->  1 / x
    """
    x, g = root[0]

    if g == E:
        return L(1) / x

    return L(1) / (x * ln(g))


MESSAGES[logarithmic] = \
        _('Apply standard derivative `d/dx log(x, g) = 1 / (x * ln(g))`.')


def match_goniometric(node):
    """
    der(sin(x), x)     ->  cos(x)
    der(sin(f(x)), x)  ->  cos(f(x)) * der(f(x), x)
    der(cos(x), x)     ->  -sin(x)
    der(cos(f(x)), x)  ->  -sin(f(x)) * der(f(x), x)
    der(tan(x), x)     ->  der(sin(x) / cos(x), x)
    """
    assert node.is_op(OP_DER)

    x = get_derivation_variable(node)

    if x and not node[0].is_leaf:
        op = node[0].op

        if op in (OP_SIN, OP_COS):
            f = node[0][0]
            x = L(x)
            handler = sinus if op == OP_SIN else cosinus

            if f == x:
                return [P(node, handler)]

            if f.contains(x):
                return [P(node, chain_rule, (f, handler, ()))]

        if op == OP_TAN:
            return [P(node, tangens)]

    return []


def sinus(root, args):
    """
    der(sin(x), x)  ->  cos(x)
    """
    return cos(root[0][0])


MESSAGES[sinus] = _('Apply standard derivative `d/dx sin(x)` is `cos(x)`.')


def cosinus(root, args):
    """
    der(cos(x), x)  ->  -sin(x)
    """
    return -sin(root[0][0])


MESSAGES[cosinus] = _('Apply standard derivative `d/dx cos(x)` is `-sin(x)`.')


def tangens(root, args):
    """
    der(tan(x), x)  ->  der(sin(x) / cos(x), x)
    """
    x = root[0][0]

    return same_der(root, sin(x) / cos(x))


MESSAGES[tangens] = \
        _('Convert the tanges to a division and apply the product rule.')


def match_sum_product_rule(node):
    """
    [f(x) + g(x)]'  ->  f'(x) + g'(x)
    [f(x) * g(x)]'  ->  f'(x) * g(x) + f(x) * g'(x)
    """
    assert node.is_op(OP_DER)

    x = get_derivation_variable(node)

    if not x or node[0].is_leaf or node[0].op not in (OP_ADD, OP_MUL):
        return []

    scope = Scope(node[0])
    x = L(x)
    functions = [n for n in scope if n.contains(x)]

    if node[0].op == OP_MUL:
        if len(functions) < 2:
            return []

        handler = product_rule
    else:
        handler = sum_rule

    return [P(node, handler, (scope, f)) for f in functions]


def sum_rule(root, args):
    """
    [f(x) + g(x)]'  ->  f'(x) + g'(x)
    """
    scope, f = args
    scope.remove(f)

    return same_der(root, f) + same_der(root, scope.as_nary_node())


MESSAGES[sum_rule] = _('Apply the sum rule to {0}.')


def product_rule(root, args):
    """
    [f(x) * g(x)]'  ->  f'(x) * g(x) + f(x) * g'(x)

    Note that implicitely:
    [f(x) * g(x) * h(x)]'  ->  f'(x) * (g(x) * h(x)) + f(x) * [g(x) * h(x)]'
    """
    scope, f = args
    scope.remove(f)
    gh = scope.as_nary_node()

    return same_der(root, f) * gh + f * same_der(root, gh)


MESSAGES[product_rule] = _('Apply the product rule to {0}.')


def match_quotient_rule(node):
    """
    [f(x) / g(x)]'  ->  (f'(x) * g(x) - f(x) * g'(x)) / g(x) ^ 2
    """
    assert node.is_op(OP_DER)

    x = get_derivation_variable(node)

    if not x or not node[0].is_op(OP_DIV):
        return []

    f, g = node[0]
    x = L(x)

    if f.contains(x) and g.contains(x):
        return [P(node, quotient_rule)]

    return []


def quotient_rule(root, args):
    """
    [f(x) / g(x)]'  ->  (f'(x) * g(x) - f(x) * g'(x)) / g(x) ^ 2
    """
    f, g = root[0]

    return (same_der(root, f) * g - f * same_der(root, g)) / g ** 2


MESSAGES[quotient_rule] = _('Apply the quotient rule to {0}.')