Skip to content

Commit fa4ad60

Browse files
Improved compiler, added symbolic operators
1 parent 144bc35 commit fa4ad60

File tree

4 files changed

+180
-142
lines changed

4 files changed

+180
-142
lines changed

README.md

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h1 style="text-align:center"><img src="https://i.imgur.com/XBOpBt1.png"></h1>
1+
<h1 style="text-align:center"><img src="https://i.imgur.com/XBOpBt1.png"></h1>
22

33
Complex-js is a lightweight module that enables complex mathematics
44
in JavaScript. It comes with every elementary function and all
@@ -52,23 +52,32 @@ Functions are denoted as `Complex.staticMethod`. For example,
5252
to evaluate the tangent of the imaginary unit, do the following:
5353

5454
```js
55-
console.log(Complex.tan(Complex(0,1)));
55+
console.log(Complex.tan(Complex(0, 1)));
5656
```
5757

5858
All functions are static, meaning that they are called directly by
5959
the Complex namespace. Operators are non-static methods, which means
6060
they must be called by an instance of `Complex`. For example, to raise
61-
1+5i to the power of 3 e^((2/3)pi i), do the following:
61+
1+5i to the power of 3 e^(pi i), do the following:
6262

6363
```js
64-
console.log(Complex(1,5).pow(Complex.Polar(3,2/3*Math.PI)));
64+
console.log(Complex(1, 5).pow(Complex.Polar(3, Math.PI)));
6565
```
6666

6767
Notice how `pow` is a method of a `Complex` instance, and not of the
68-
namespace Complex. That's because it is an operator rather than a
69-
function. Non-static methods are denoted as
70-
`Complex.prototype.nonStaticMethod`.
68+
namespace Complex. That's because it is an operator rather than a function in
69+
math. Non-static methods are denoted as `Complex.prototype.nonStaticMethod`.
70+
Now you can use symbolic operators as well. These include addition ([`+`](#add)),
71+
subtraction ([`-`](#sub)), multiplication ([`*`](#mult)), division ([`/`](#div)),
72+
modulii ([`%`](#mod)), powers ([`^`](#pow)), and equalities ([`=`](#equals)).
73+
Below is a couple examples.
7174

75+
```js
76+
// 1+5i
77+
var onePlusFiveI = Complex(1, 0)['+'](Complex(0, 5));
78+
// e^(pi i)*3
79+
var negThree = Complex.exp(Complex(0, Math.PI))['*'](Complex(3, 0));
80+
```
7281

7382
<a name="coordinate-notation"></a>
7483
## Coordinate Notation
@@ -78,7 +87,7 @@ to declare a Complex number with cartesian coordinates, you can call
7887
the default constructor with the following arguments:
7988

8089
```js
81-
var cartesian_1_plus_5_i = Complex(1,5);
90+
var onePlusFiveI = Complex(1, 5);
8291
```
8392

8493
Declaring it with the `new` keyword is optional, since the
@@ -89,7 +98,7 @@ not recommended. Exponential notation is supported through the
8998
secondary Polar constructor as such:
9099

91100
```js
92-
var exponential_1_e_to_pi_i = Complex.Polar(1,Math.PI);
101+
var negOne = Complex.Polar(1, Math.PI);
93102
```
94103

95104
Note that this constructor does not support the `new` keyword and
@@ -115,44 +124,49 @@ default. A simple use-case example is below.
115124

116125
HTML:
117126
```html
118-
<!-- the value is (5+i)^(.00003+10*sin(5i)) -->
119-
<div>
120-
<span>Evaluate:</span>
121-
<input type="text" id="calc" value="(5+i)^(3e-5+10*sin(5i))"/>
122-
</div>
123-
<div>
124-
<span>Cartesian:</span>
125-
<span id="ans-cart"></span>
126-
</div>
127-
<div>
128-
<span>Exponential:</span>
129-
<span id="ans-expo"></span>
130-
</div>
131-
<script type="text/javascript" src="complex.min.js"></script>
132-
<script type="text/javascript">
133-
...
134-
</script>
127+
<!DOCTYPE html>
128+
<head>
129+
<script type="text/javascript" src="complex.min.js"></script>
130+
</head>
131+
<body>
132+
<div>
133+
<span>Evaluate:</span>
134+
<input type="text" id="calc" value="(5+i)^(3e-5+10*sin(5i))"/>
135+
</div>
136+
<div>
137+
<span>Cartesian:</span>
138+
<span id="ans-cart"></span>
139+
</div>
140+
<div>
141+
<span>Exponential:</span>
142+
<span id="ans-expo"></span>
143+
</div>
144+
<script type="text/javascript">
145+
...
146+
</script>
147+
</body>
148+
</html>
135149
```
136150
JavaScript:
137151
```js
138152
var input = document.getElementById('calc'),
139153
cart = document.getElementById('ans-cart'),
140154
expo = document.getElementById('ans-expo');
141155

142-
input.addEventListener('change', function(){
156+
input.addEventListener('change', function () {
143157
try {
144-
var
145-
//will throw an error if input is invalid
146-
calc = Complex.parseFunction(input.value),
158+
//will throw an error if input is invalid
159+
var calc = Complex.parseFunction(input.value),
147160
//evaluate the compiled function for the answer
148161
ans = calc();
162+
149163
//use the toString method
150164
cart.innerHTML = ans.toString(true);
151165
expo.innerHTML = ans.toString();
152166
} catch(error) {
153167
//if the parser throws an error, clear outputs and alert error
154-
cart.innerHTML = "";
155-
expo.innerHTML = "";
168+
cart.innerHTML = '';
169+
expo.innerHTML = '';
156170
alert(error.message);
157171
}
158172
});
@@ -168,30 +182,31 @@ for the compiled function:
168182

169183
```js
170184
// Node.js
171-
var Complex = require("complex-js"),
172-
param_a = Complex(5,1),
173-
param_b = Complex(3e-5,0),
174-
param_c = Complex(0,5),
185+
var Complex = require('complex-js'),
186+
paramA = Complex(5, 1),
187+
paramB = Complex(3e-5, 0),
188+
paramC = Complex.Polar(5, Math.PI / 2),
175189
// human-readable variable names in expression
176-
complex_func = "a^(b+10*sin(c))",
190+
complexFunc = 'a^(b+10*sin(c))',
177191
// array of parameters for function is order-dependent
178-
js_func = Complex.parseFunction(complex_func, ["b","a","c"]),
192+
jsFunc = Complex.parseFunction(complexFunc, ['b', 'a', 'c']),
179193
// how to pass parameters to compiled function
180-
output = js_func(param_b, param_a, param_c);
194+
output = jsFunc(paramB, paramA, paramC);
181195

182196
// output cartesian form as string
183197
console.log(output.toString(true));
198+
// -1.85444755246657E-64+1.5844569641866693E-64 i
184199
```
185200

186201
The `Complex.parseFunction` method can also reconstruct a Complex
187202
number from a string created by `Complex.toString`. See below for
188203
a demonstration:
189204

190205
```js
191-
var five_plus_i_str = Complex(5,1).toString(true), //store as cartesian
192-
five_plus_i = (Complex.parseFunction(five_plus_i_str))();
206+
var fivePlusIStr = Complex(5, 1).toString(true), //store as cartesian
207+
fivePlusI = (Complex.parseFunction(fivePlusIStr))();
193208

194-
console.log(Complex(5,1).equals(five_plus_i));
209+
console.log(Complex(5, 1).equals(fivePlusI));
195210
// true
196211
```
197212

@@ -217,8 +232,8 @@ console.log(Complex(5,1).equals(five_plus_i));
217232
* [`sub`](#sub)
218233
* [`rMult`](#r-mult)
219234
* [`mult`](#mult)
220-
* [`rDivBy`](#r-div-by)
221-
* [`divBy`](#div-by)
235+
* [`rDiv`](#r-div)
236+
* [`div`](#div)
222237
* [`rMod`](#r-mod)
223238
* [`mod`](#mod)
224239
* [`rPow`](#r-pow)
@@ -419,7 +434,7 @@ __Arguments__
419434

420435

421436
<a name="add"></a>
422-
### Complex.prototype.add(complex)
437+
### Complex.prototype.add(complex), Complex.prototype\['+'\](complex)
423438

424439
Adds two Complex numbers.
425440

@@ -439,7 +454,7 @@ __Arguments__
439454

440455

441456
<a name="sub"></a>
442-
### Complex.prototype.sub(complex)
457+
### Complex.prototype.sub(complex), Complex.prototype\['-'\](complex)
443458

444459
Subtracts a Complex number from another.
445460

@@ -459,7 +474,7 @@ __Arguments__
459474

460475

461476
<a name="mult"></a>
462-
### Complex.prototype.mult(complex)
477+
### Complex.prototype.mult(complex), Complex.prototype\['*'\](complex)
463478

464479
Multiplies two Complex numbers.
465480

@@ -468,8 +483,8 @@ __Arguments__
468483
* `complex` - An instance of the `Complex` class to multiply.
469484

470485

471-
<a name="r-div-by"></a>
472-
### Complex.prototype.rDivBy(real)
486+
<a name="r-div"></a>
487+
### Complex.prototype.rDiv(real)
473488

474489
Divides a Complex number by a `Number`.
475490

@@ -478,8 +493,8 @@ __Arguments__
478493
* `real` - A `Number` by which to divide.
479494

480495

481-
<a name="div-by"></a>
482-
### Complex.prototype.divBy(complex)
496+
<a name="div"></a>
497+
### Complex.prototype.div(complex), Complex.prototype\['/'\](complex)
483498

484499
Divides a Complex number by another.
485500

@@ -499,7 +514,7 @@ __Arguments__
499514

500515

501516
<a name="mod"></a>
502-
### Complex.prototype.mod(complex)
517+
### Complex.prototype.mod(complex), Complex.prototype\['%'\](complex)
503518

504519
Applies a Complex Modulus to a Complex number by cartesian coordinates.
505520

@@ -519,7 +534,7 @@ __Arguments__
519534

520535

521536
<a name="pow"></a>
522-
### Complex.prototype.pow(complex)
537+
### Complex.prototype.pow(complex), Complex.prototype\['^'\](complex)
523538

524539
Raises a Complex number to a Complex power.
525540

@@ -1042,7 +1057,7 @@ __Arguments__
10421057

10431058

10441059
<a name="parse-function"></a>
1045-
### Complex.parseFunction(string[, params[, skipFormat]])
1060+
### Complex.parseFunction(string[, params][, skipFormat])
10461061

10471062
Returns a JavaScript function bound with pre-compiled constants parsed
10481063
from the human-readable math expression `string`. Optionally, an `Array`

0 commit comments

Comments
 (0)