Calculus.jl
Calculus.jl copied to clipboard
Add cancellation of common variables in 'simplify'
These simplifications breaks matrix algebra.
Ivar, I tested it only on simple expressions. Can you give some matrix example that breaks after this commit?
If you have a matrix in the denumerator, you can not shorten it by this kind of simple elimination. Sometimes you will end up with an identity matrix eye(A), but in more complicated cases you will get a completely wrong answer. I am not sure if that is a common use case. We might have a setting in simplify to restrict to matrix safe operations.
So this is because * means normal or matrix multiplication depending on argument types. It is not very convenient for symbolic computations, but cannot be changed. Another symbolic math software (Mathematica and Maple) use . (dot) for matrix multiplication. Making multiple dispatch to identify arguments with acceptable type will be not very helpful.
Let's wait for @johnmyleswhite comments on this issue.
Yes, it is because (users of) julia does not follow its basic API design principle for \ and*. One function should only have one meaning and behave roughly the same way for different types. You could argue that it is concistent if users would use .* for all scalar operations, but I don't think that is the behavior we are seeing.
For simplify or simplify! I think that we must respect the design of the language and assume that \ and* refers to matrix operations and must adhere to matrix associativity rules.
Because I'm traveling I won't have time to think a lot about this, but I had never intended for these symbolic operations to work on matrices, just real numbers. If we can make them more general, that would be great.
I think it would be very helpful to discuss this issue on the julia-users mailing list, specifically asking for feedback from Stefan, Viral, Steven Johnson and Alan Edelman.
I think we should restrict to real numbers only. Anyway now all functions in the package don't support matrix/vector arguments. For example with matrices even diff(x_x, x) will be not 2_x, but (Mathematica code):
In[1]:= D[x.x, x]
Out[1]= 1.x + x.1