Skip to content

Commit

Permalink
Added a section on implicit multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jul 1, 2014
1 parent 3cc93fd commit 9d2ac66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 27 additions & 1 deletion docs/expressions.md
Expand Up @@ -303,7 +303,7 @@ Operators | Description
`^`, `.^` | Exponentiation
`+`, `-` | Unary plus, unary minus
`x unit` | Unit
`*`, `/`, `.*`, `./`, `%`, `mod` | Multiply, divide, modulus
`*`, `/`, `.*`, `./`, `%`, `mod` | Multiply, divide, modulus, implicit multiply
`+`, `-` | Add, subtract
`:` | Range
`==`, `!=`, `<`, `>`, `<=`, `>=` | Comparison
Expand Down Expand Up @@ -665,6 +665,32 @@ math.eval('a=3; b=4; a + b \n a * b'); // Array, [7, 12]
```


### Implicit multiplication

The expression parser supports implicit multiplication. Implicit multiplication
has the same precedence as explicit multiplications and divisions, so `3/4 mm`
is evaluated as `(3 / 4) * mm`. Here some examples:

Expression | Evaluated as:
--------------- | ----------------------
(3 + 2) b | (3 + 2) * b
3 / 4 mm | (3 / 4) * mm
(1 + 2) (4 - 2) | (1 + 2) * (4 - 2)
sqrt(2)(4 + 1) | sqrt(2) * (4 + 1)
A[2, 3] | A[2, 3] # get subset
(A)[2, 3] | (A) * [2, 3]
[2, 3][1, 3] | [2, 3] * [1, 3]

Implicit multiplication can be tricky as there is ambiguity on how an expression
is evaluated. Use it carefully.

```js
math.eval('(1 + 2)(4 - 2)'); // Number, 6
math.eval('3/4 mm'); // Unit, 0.75 mm
math.eval('2 + 3i'); // Complex, 2 + 3i
```


### Comments

Comments can be added to explain or describe calculations in text. A comment
Expand Down
2 changes: 1 addition & 1 deletion examples/complex_numbers.js
Expand Up @@ -38,7 +38,7 @@ console.log();
console.log('perform operations');
print(math.add(a, b)); // 8 - 4i
print(math.multiply(a, b)); // 36 - 26i
print(math.sin(a)); // -9.6541 + 2.8417i
print(math.sin(a)); // -9.6541254768548 + 2.8416922956064i

// some operations will return a complex number depending on the arguments
print(math.sqrt(4)); // 2
Expand Down

0 comments on commit 9d2ac66

Please sign in to comment.