Next post: AutoShoop

Math and JavaScript

In 2006, I wrote a Venn Diagram drawing webpage that could depict arbitrary expressions.

Screenshot

If you have Internet Explorer, you can see it here.


 

In 2006, I also wrote a what I called "Ben's LogicEvaluator".

Screenshot

Interestingly, it can correctly handle complex expressions with nested parentheses. I wrote this before I knew anything about parsing, and in effect I invented my own approach to evaluate infix notation.

Looking at the source code, it looks like it runs a loop looking for the first "innermost" subexpression, a subexpression like avb that just contained raw variables and contained no groups. It would evaluate this subexpression and put the result in a placeholder value, and then replace the subexpression with a reference to this placeholder variable. This loop would be repeated, each time condensing a subexpression like "avb" to a placeholder "x", until the entire expression was condensed into one placeholder. I did this all with a few dozen lines of JavaScript and regular expressions; it's actually a pretty effective approach.

(I wrote this because I was in a philosophy course that assigned some fairly tedious homework; this tool was fun to write and saved me some time).

If you have Internet Explorer, you can see it here.


 

In 2007, I wanted to make an online calculator that would have built-in functions for everything important for probability, statistics, and geometry.

Screenshot

I used it at the time for my statistics class, and kept the code kicking around, but never got around to finishing everything that I had planned. (I had wanted to add many more statistics features). I basically ran into a brick wall when I wanted to support complex numbers... JavaScript did not support for overloading operators.

The features include:

  • use arrow keys to retrieve history for the feel of a command prompt
  • when you press an operation like *, automatically enters the ans() representing the last result
  • auto-paren completetion. sin(4 doesn't throw an error (apparently needs IE)
  • geometry functions like trisolve('3,4,5'), trisolve('3,3,45o'), trisolve('45o,45o,5'), deg2rad, rad2deg
  • probstat functions like permutation, combination, mean, variance

 

In 2009, I rewrote MathConsole in Python, and called it "Minimath".

Screenshot

As you can see, the UI is pretty sparse now, but my main design goal was to save typing for commonly-occuring tasks.

  • Fills-in incomplete expressions. It is perfectly ok to evaluate "sin(pi" without the closing paren, because it will understand what you meant.
  • Line history, with arrow keys. Control-Up and Control-Down search history based on prefix like Matlab.
  • Supports complex numbers, saving values in variables.
  • You can press Alt-. to create a "→" symbol to store values, like a TI 83.
  • "ans" refers to last result.
  • If the first key you press is an operator, it fills in the "ans" like a TI 83. This one saves a lot of time.
  • Any valid Python expression can also be evaluated.
  • Pressing the "\" key (while not in a string literal) creates a lambda symbol, so that expressions like "f = λx.x+2" can be used.
  • Pretty print, which behind-the-scenes is parsing the expression, creating and rendering a temporary LaTeX file.
  • Uses Numpy library which provides many math functions.
Another feature is variable substitution. Pressing alt-, makes a "←" symbol that can be used to subsitute values into an expression. For example, the result of "2*x^2 + 3*x ← 4" is 44.

Added Syntax

  • The symbol "^" now is exponentiation.
  • The ternary expression ? : can be used as in C.
  • The shortened "for(i,5)" can be used in place of "for i in range(5)".
  • Syntax for making arrays: "arr = a[1 2 3]" and "arr = a[1 2 3;4 5 6]". (Note that arrays are different than Python lists).
  • "i[0,4,10]" is like Matlab's linspace, 10 elements equally spaced from 0 to 4.
  • "i[0..4]" is an inclusive range, the result is [0,1,2,3,4].
  • It allows more items on one line than Python, just use semicolons. In particular, something like "for val in array:t+=val;print val" will work all on one line.