News for hackers. Stuff that matters.
Re:Matrix Calculus by mjfl on Saturday November 18, 2017 @03:58AM (#15728328)
Along the same lines, does there exist an "algebra checker" that could, say, take in two successive lines of latex, with perhaps a hint of how to get from one to the other, and confirm that there are no algebra errors?
Re:Matrix Calculus by CodesInChaos on Saturday November 18, 2017 @04:33AM (#15728394)
The choice of LaTeX comes with two problems:

1) It's designed for describe graphical representation and not semantics.

2) parsing it properly is very difficult, especially considering how macros and other definitions can influence the syntax.

Re:Matrix Calculus by yorwba on Saturday November 18, 2017 @04:49AM (#15728417)
You could:

1) assume a standard interpretation of symbols (e.g. this site uses a-g for scalars, h-z for vectors and A-Z for matrices) and make those assumptions explicit and modifiable (e.g. WolframAlpha displays "assuming X is a Y, use as a Z instead").

2) Support only the subset that MathJax can handle, which seems to be enough for most purposes.

I definitely agree that LaTeX is not the optimal input format for that purpose, though.

Re:Matrix Calculus by qubex on Sunday November 19, 2017 @07:42AM (#15733648)
See my comment above about my experiences with this: I tried, and a corner case killed me. There’s a reason why Mathematica and other CAS packages have invented their own idiosyncratic notations. LATEX and MathML are truly hopeless when it comes to communicating with algebra systems. It looks deceptively ’doable’, but really it’s a recipe for disaster.
Re:Matrix Calculus by ivan_ah on Saturday November 18, 2017 @07:18AM (#15728706)
I'm with cosmic_ape regarding the recommendation for SymPy. Write the two lines as symbolic expressions `expr1` and `expr2` then run `simplify(expr1-expr2)` to check if the answer is zero.

If you want to start with LaTeX then the parser from this library might be useful: https://github.com/Khan/KAS

Re:Matrix Calculus by cosmic_ape on Saturday November 18, 2017 @05:53AM (#15728543)
SymPy does not take Latex as input, but it has symbolic equivalence checkers.

Methods are highly heuristic, and some languages (all expressions with,say, \pi, and exp function, or something similar) are in general undecidable.

Re:Matrix Calculus by rav on Saturday November 18, 2017 @08:48AM (#15728914)
To elaborate: Symbolic equivalence checking for two expressions f1 and f2 can be done by seeing if sympy.simplify(sympy.Eq(f1, f2)) is sympy.true.

  import sympy
  from sympy.abc import x, y, alpha, s
  quad = s ** 2 - alpha * s - 2
  # Let s1 and s2 be the two solutions to the quadratic equation 'quad == 0'
  s1, s2 = sympy.solve(quad, s)
  u = (x - s2) / (x - s1) * (y - s1) / (y - s2)
  f1 = (s2 - s1 * u) / (1 - u)
  f2 = (x * y - alpha * x - 2) / (y - x)
  # Claim: f1 is equal to f2
  print(sympy.simplify(sympy.Eq(f1, f2)))
  # Prints "True"
Re:Matrix Calculus by yorwba on Saturday November 18, 2017 @09:40AM (#15729094)
Is there something for implication or equivalence of equations? I was trying the example from Smaug123's sibling comment (https://news.ycombinator.com/item?id=15728598), but

  >>> import sympy as sp
  >>> x = sp.Symbol('x')
  >>> sp.simplify(sp.Implies(sp.Eq(x**2 + 2*x + 1, 0), sp.Eq(x, -1)))

  Eq(x, -1) | Ne(x**2 + 2*x + 1, 0)

  >>> sp.solve(sp.simplify(sp.Implies(sp.Eq(x**2 + 2*x + 1, 0), sp.Eq(x, -1))))

  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.5/site-packages/sympy/solvers/solvers.py", line 1065, in solve
    solution = _solve(f[0], *symbols, **flags)
  File "/home/user/.local/lib/python3.5/site-packages/sympy/solvers/solvers.py", line 1401, in _solve
    f_num, sol = solve_linear(f, symbols=symbols)
  File "/home/user/.local/lib/python3.5/site-packages/sympy/solvers/solvers.py", line 1971, in solve_linear
    eq = lhs - rhs
  TypeError: unsupported operand type(s) for -: 'Or' and 'int'

  >>> sp.solveset(sp.simplify(sp.Implies(sp.Eq(x**2 + 2*x + 1, 0), sp.Eq(x, -1))))

  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.5/site-packages/sympy/solvers/solveset.py", line 880, in solveset
    raise ValueError("%s is not a valid SymPy expression" % (f))
  ValueError: Eq(x, -1) | Ne(x**2 + 2*x + 1, 0) is not a valid SymPy expression
none of the obvious ways appear to work. Does Sympy not support this kind of equational reasoning?
Re:Matrix Calculus by eitland on Saturday November 18, 2017 @04:54AM (#15728428)
I think there used to be a open source tool called Maxima or something that would simplify equations. Havent used it for the last 10 years and never used it much anyway.
Re:Matrix Calculus by lottin on Saturday November 18, 2017 @05:47AM (#15728530)
I can recommended Maxima too. It's useful for checking for obvious errors and for simplifying expressions. It can be used for more advanced stuff, but I found it has a steep learning curve.
Re:Matrix Calculus by qubex on Sunday November 19, 2017 @07:39AM (#15733638)
On a whim I programmed a symbolic integral-solving library in Python (I am not a professional programmer) and, as an ill-advised ’extension’, bolted on the ability to receive and emit LATEX expressions. Long story short, I was boasting about it on Nichols Nassim Taleb’s twitter feed, submitted an answer to a problem question he had posed that got mis-parsed by the front end, gave the wrong answer (which was entirely consistent, because the check stage compared the answer by deriving the flawed question), and got banned, flamed, and persecuted to Hell and back. True story.

From this I learnt the hard way that tokenisation and representation (in LATEX or MathML) do not belong in the same place as a CAS (Computer-assisted Algebra System).

Re:Matrix Calculus by yorwba on Sunday November 19, 2017 @09:06AM (#15733931)
I believe that LaTeX/MathML absolutely do belong in a CAS, just not as the primary means of interaction. (I.e. you do your work in the CAS's own language and then export to LaTeX.)

But maybe your example can convince me otherwise. Could you show the specific LaTeX code in question and describe how your program mishandled it?

Re:Matrix Calculus by qubex on Sunday November 19, 2017 @12:49PM (#15734944)
I had set myself the target of implementing the Risch Algorithm (https://en.wikipedia.org/wiki/Risch_algorithm) in SymPy (3) but the event was so embarrassing I deleted my code and never published it publicly.

I’m not a programmer. I erred in trying to do a programmer’s job. Also, I discovered why Nicholas Nassim Taleb has the reputation for being rather uncharitable (but deep down I feel like I deserved it, because hey, I stated a mathematical untruth).

Re:Matrix Calculus by SoerenL on Sunday November 19, 2017 @08:28AM (#15733803)
As one of the authors of this tool I understand that you would like to have some way of trusting the output.

Internally we check it numerically, i.e., generate some random data for the given variables and check the derivative by comparing it to an approximation via finite differences. We will ship this code (hopefully soon) with one of the next versions. You can then check it yourself. Otherwise, as far as I know there does not exist any other tool that computes matrix derivatives so I understand it is hard to convince anyone of the correctness of the results. But I hope the numerical tests will be helpful.

Re:Matrix Calculus by chillydawg on Saturday November 18, 2017 @04:20AM (#15728368)
Mathematica can probably do that
Re:Matrix Calculus by Smaug123 on Saturday November 18, 2017 @06:15AM (#15728598)
Correct:

    Implies[x^2 + 2 x + 1 == 0, x == -1] // FullSimplify
Re:Matrix Calculus by glinia on Saturday November 18, 2017 @03:05PM (#15730395)

  [In] FullSimplify[expr1 == expr2]

  [Out] True