-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.ts
853 lines (812 loc) · 31.7 KB
/
vector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
import { Token, Evaluable, Constant as _Constant, Variable as _Variable, Expression as _Expression, Operator, isConstant, isVariable } from "./core/definitions";
import { BinaryOperator, isBinaryOperator } from "./core/operators/binary";
import { UnaryOperator, isUnaryOperator } from "./core/operators/unary";
import { ExpressionBuilder } from "./core/expression";
import { Scalar } from "./scalar";
import { InvalidIndex } from "./core/errors";
/**
* The double underscore.
*
* Represents any unknown value. When passed in along with other known values
* this gets interpreted as an unknown or a [[Variable]].
* @see [[Vector.variable]] for a use case example.
*/
export const __ = undefined;
/**
* Base class to work with vector quantities.
* @abstract
*/
export abstract class Vector implements Token, Evaluable {
readonly abstract type: "constant" | "variable" | "expression";
readonly abstract X: (i: number) => Scalar;
readonly quantity = "vector";
/**
* Adds two [[Vector]]s together. If `this` and `that` are both constants
* then vectorially adds the two and returns a new [[Vector.Constant]] object
* otherwise creates an [[Expression]] out of them and returns the same.
* @param that The scalar to add `this` with.
* @return The result of algebraic addition.
*/
public abstract add(that: Vector): Vector;
/**
* Subtracts `that` from `this`. If `this` and `that` are both constants
* then vectorially subtracts one from the other and returns a new
* [[Vector.Constant]] object otherwise creates an [[Expression]] out of them
* and returns the same.
* @param that The scalar to subtract from `this`.
* @return The result of algebraic subtraction.
*/
public abstract sub(that: Vector): Vector;
/**
* Evaluates the scalar product of `this` and `that`. If both are constants
* then numerically computes the product and returns a [[Scalar.Constant]] object
* otherwise creates an [[Expression]] out of them and returns the same.
* @param that The scalar to subtract from `this`.
* @return The inner product of `this` and `that`.
*/
public abstract dot(that: Vector): Scalar;
/**
* Evaluates the vector product of `this` and `that`. If both are constants
* then numerically computes the product and returns a [[Vector.Constant]] object
* otherwise creates an [[Expression]] out of them and returns the same.
* @param that The scalar to subtract from `this`.
* @return The vector product of `this` and `that`.
*/
public abstract cross(that: Vector): Vector;
/**
* Scales, or multiplies the "size" (magnitude) of, `this` vector by given
* amount. If `this` and `k` are both constants then numerically calculates
* the scaled vector otherwise creates an [[Expression]] out of them and
* returns the same.
* @param k The scale factor.
* @return The scaled vector.
*/
public abstract scale(k: Scalar): Vector;
/**
* Computes the magnitude of a constant vector numberically.
* @param A The [[Vector]] whose magnitude is to be calculated.
* @return The [[Scalar]] magnitude of the given [[Vector]].
*/
public static mag(A: Vector.Constant): Scalar.Constant;
/**
* Computes the magnitude of a given vector. If `A` vector is a constant
* vector then numerically calculates the magnitude otherwise creates a
* scalar [[Expression]] and returns the same.
* @param A The [[Vector]] whose magnitude is to be calculated.
* @return The [[Scalar]] magnitude of the given [[Vector]].
*/
public static mag(A: Vector): Scalar.Expression;
public static mag(A: Vector) {
if(A instanceof Vector.Constant) {
let m = 0;
for(let i = 1; i <= A.value.length; i++)
m += Math.pow(A.X(i).value, 2);
return Scalar.constant(Math.sqrt(m));
}
return new Scalar.Expression(BinaryOperator.MAG, <Evaluable><unknown>Vector, A);
}
/**
* For a given constant vector `A`, numberically evaluates the unit vector along `A`.
* @param A The [[Vector.Constant]] along which the unit vector is to be calculated.
* @return The unit vector along the given [[Vector]] `A`.
*/
public static unit(A: Vector.Constant): Vector.Constant;
/**
* For a given variable vector `A`, creates an [[Expression]] for the unit vector along `A`.
* @param A The [[Vector.Constant]] along which the unit vector is to be calculated.
* @return The unit vector along the given [[Vector]] `A`.
*/
public static unit(A: Vector): Vector.Expression;
public static unit(A: Vector) {
if(A instanceof Vector.Constant)
return A.scale(Scalar.constant(1).div(Vector.mag(A)));
const m = Vector.mag(A);
return new Vector.Expression(BinaryOperator.UNIT, <Evaluable><unknown>Vector, A, (i: number) => A.X(i).div(m));
}
}
export namespace Vector {
/**
* A mapping from stringified vector constants to [[Vector.Constant]] objects.
* @ignore
*/
const CONSTANTS = new Map<string, Vector.Constant>();
/**
* A mapping from named vector constants to [[Vector.Constant]] objects.
* @ignore
*/
const NAMED_CONSTANTS = new Map<string, Vector.Constant>();
/**
* A mapping from name of vector variables to [[Vector.Variable]] objects.
* @ignore
*/
const VARIABLES = new Map<string, Vector.Variable>();
/**
* @extends [[Vector]]
*/
export class Constant extends Vector implements _Constant {
readonly type = "constant";
/**
* The number of dimensions `this` vector exists in.
* @ignore
*/
private dimension: number;
readonly value: Scalar.Constant[] = [];
/**
* The name by which `this` is identified. This is optional and defaults
* to the empty string `""`.
*/
readonly name: string;
/**
* Creates a [[Vector.Constant]] object from a list of [[Scalar.Constant]]
* objects. One may optionally pass in a string by which `this` object
* may be identified by.
*
* Using the contructor directly for creating vector objects is
* not recommended.
*
* @see [[Vector.constant]]
* @param value The fixed value `this` should represent.
* @param name The name by which `this` is identified.
*/
constructor(value: Scalar.Constant[], name?: string);
/**
* Creates a [[Vector.Constant]] object from a list of numbers.
* One may optionally pass in a string by which `this` object
* may be identified by.
*
* Using the contructor directly for creating vector objects is
* not recommended.
*
* @see [[Vector.constant]]
* @param value The fixed value `this` should represent.
* @param name The name by which `this` is identified.
*/
constructor(value: number[], name?: string);
constructor(value: Scalar.Constant[] | number[], name = "") {
super();
this.name = name;
for(let x of value)
if(x instanceof Scalar.Constant)
this.value.push(x);
else this.value.push(Scalar.constant(x));
this.dimension = this.value.length;
}
/**
* Returns the components of `this` vector. The index values start
* from `1` instead of the commonly used starting index `0`.
* @param i The index of the desired component.
* @return The [[Scalar]] element at given index.
*/
public get X() {
const value = this.value;
return function(i: number) {
if(i <= 0)
throw new InvalidIndex(i, 0);
return (i <= value.length)?value[i - 1]: Scalar.constant(0);
}
}
/**
* Checks for equality of two vector constants. The equality check
* for floating point numbers becomes problematic in the decimal system.
* The binary representation is finite and therefore even if two values
* are in fact equal they may not return true by using the `==` or `===`
* equality. To tackle this problem we use a tolerance value, if the
* difference of the two numerical values is less than that tolerance
* value then we can assume the values to be practically equal. Smaller
* tolerance values will result in more accurate checks.
* This function allows a default tolerance of `1e-14` for floating point numbers.
* @param that The value to check equality with.
*/
public equals(that: Vector.Constant): boolean;
/**
* Checks for equality of two vector constants. The equality check
* for floating point numbers becomes problematic in the decimal system.
* The binary representation is finite and therefore even if two values
* are in fact equal they may not return true by using the `==` or `===`
* equality. To tackle this problem we use a tolerance value, if the
* difference of the two numerical values is less than that tolerance
* value then we can assume the values to be practically equal. Smaller
* tolerance values will result in more accurate checks.
* @param that The value to check equality with.
* @param tolerance The tolerance permitted for floating point numbers.
*/
public equals(that: Vector.Constant, tolerance: number): boolean;
public equals(that: Vector.Constant, tolerance = 1e-14) {
const m = Math.max(this.value.length, that.value.length);
for(let i = 1; i <= m; i++)
if(Math.abs(this.X(i).value - that.X(i).value) >= tolerance)
return false;
return true;
}
/**
* Adds two [[Vector.Constant]] objects numerically.
* @param that The [[Vector.Constant]] to add to `this`.
* @return The vector sum of `this` and `that`.
*/
public add(that: Vector.Constant): Vector.Constant;
/**
* Creates and returns a [[Vector.Expression]] for the addition of
* two [[Vector]] objects. The [[type]] of `this` does not matter because
* adding a variable vector to another vector always results in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for sum of `this` and `that`.
*/
public add(that: Vector.Variable | Vector.Expression): Vector.Expression;
public add(that: Vector) {
if(that instanceof Vector.Constant) {
const m = Math.max(this.value.length, that.value.length);
const vec: number[] = [];
for(let i = 1; i <= m; i++)
vec.push(this.X(i).value + that.X(i).value);
return Vector.constant(vec);
}
return new Vector.Expression(BinaryOperator.ADD, this, that, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return (<Scalar>this.X(i)).add(that.X(i));
});
}
/**
* Subtracts one [[Vector.Constant]] object from another numerically.
* @param that The [[Vector.Constant]] to subtract from `this`.
* @return The vector difference of `this` from `that`.
*/
public sub(that: Vector.Constant): Vector.Constant;
/**
* Creates and returns a [[Vector.Expression]] for the subtraction of
* two [[Vector]] objects. The [[type]] of `this` does not matter because
* subtracting a variable vector from another vector always results in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for subtracting `that` from `this`.
*/
public sub(that: Vector.Variable | Vector.Expression): Vector.Expression;
public sub(that: Vector) {
if(that instanceof Vector.Constant) {
const m = Math.max(this.value.length, that.value.length);
const vec: number[] = [];
for(let i = 1; i <= m; i++)
vec.push(this.X(i).value - that.X(i).value);
return Vector.constant(vec);
}
return new Vector.Expression(BinaryOperator.SUB, this, that, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return (<Scalar>this.X(i)).sub(that.X(i));
});
}
/**
* Calculates the scalar product of two [[Vector.Constant]] objects
* numerically.
* @param that The [[Vector.Constant]] to compute scalar product with `this`.
* @return The inner product of `this` and `that`.
*/
public dot(that: Vector.Constant): Scalar.Constant;
/**
* Creates and returns a [[Vector.Expression]] for the dot product of
* two [[Vector]] objects. The [[type]] of `this` does not matter because
* dot multiplying a variable vector with another vector always results
* in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for inner product of `this` and `that`.
*/
public dot(that: Vector.Variable | Vector.Expression): Scalar.Expression;
public dot(that: Vector) {
if(that instanceof Vector.Constant) {
let parallel = 0;
const m = Math.max(this.value.length, that.value.length);
for(let i = 1; i <= m; i++)
parallel += this.X(i).value * that.X(i).value;
return Scalar.constant(parallel);
}
return new Scalar.Expression(BinaryOperator.DOT, this, that);
}
/**
* Calculates the vector product of two [[Vector.Constant]] objects numerically.
* @param that The [[Vector.Constant]] to compute cross product with `this`.
* @return The vector product of `this` and `that`.
*/
public cross(that: Vector.Constant): Vector.Constant;
/**
* Creates and returns a [[Vector.Expression]] for the cross product of
* two [[Vector]] objects. The [[type]] of `this` does not matter because
* cross multiplying a variable vector to another vector always results
* in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for vector product of `this` and `that`.
*/
public cross(that: Vector.Variable | Vector.Expression): Vector.Expression;
public cross(that: Vector) {
if(this.dimension > 3)
throw new Error("Cross product defined only in 3 dimensions.");
if(that instanceof Vector.Constant) {
if(that.dimension > 3)
throw new Error("Cross product defined only in 3 dimensions.");
const a1 = this.X(1).value, a2 = this.X(2).value, a3 = this.X(3).value;
const b1 = that.X(1).value, b2 = that.X(2).value, b3 = that.X(3).value;
return Vector.constant([
a2 * b3 - a3 * b2,
a3 * b1 - a1 * b3,
a1 * b2 - a2 * b1
]);
}
return new Vector.Expression(BinaryOperator.CROSS, this, that, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
if(this.value.length > 3)
throw new Error("Cross product defined only in 3 dimensions.");
const a1 = <Scalar>this.X(1), a2 = <Scalar>this.X(2), a3 = <Scalar>this.X(3);
const b1 = <Scalar>that.X(1), b2 = <Scalar>that.X(2), b3 = <Scalar>that.X(3);
return (i === 1)? a2.mul(b3).sub(a3.mul(b2)):
(i === 2)? a3.mul(b1).sub(a1.mul(b3)):
a1.mul(b2).sub(a2.mul(b1));
});
}
/**
* Scales `this` [[Vector.Constant]] object numerically.
* @param k The scale factor.
* @return The scaled vector.
*/
public scale(k: Scalar.Constant): Vector.Constant;
/**
* Creates and returns a [[Vector.Expression]] for the scaling of
* `this` [[Vector]] object. The [[type]] of `this` does not matter because
* scaling a variable vector always results in an expresion.
* @param k The scale factor.
* @return Expression for scaling `this`.
*/
public scale(k: Scalar.Variable | Scalar.Expression): Vector.Expression;
public scale(k: Scalar) {
if(k instanceof Scalar.Constant)
return Vector.constant(this.value.map(x => k.mul(x).value));
return new Vector.Expression(BinaryOperator.SCALE, this, k, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return (<Scalar>this.X(i)).mul(k);
});
}
}
/**
* @extends [[Vector]]
*/
export class Variable extends Vector implements _Variable {
readonly type = "variable";
readonly name: string;
readonly value: (Scalar.Variable | Scalar.Constant)[] = [];
/**
* Creates a [[Vector.Variable]] object.
*
* Using the contructor directly for creating vector objects is
* not recommended.
*
* @see [[Vector.variable]]
* @param name The name with which the [[Vector.Variable]] is going to be identified.
*/
constructor(name: string);
/**
* Creates a [[Vector.Variable]] object from an array. The array may
* contain known [[Scalar.Constants]] and, for the components yet unknown,
* [[Scalar.Variable]]. This allows for creation of vectors whose few
* components are known before hand and the rest are not.
*
* Using the contructor directly for creating vector objects is
* not recommended.
*
* @see [[Vector.variable]]
* @param name The name with which the [[Vector.Variable]] is going to be identified.
* @param value The array containing the values with which to initialise the vector variable object.
*/
constructor(name: string, value: (Scalar.Variable | Scalar.Constant)[]);
constructor(a: string, b?: (Scalar.Variable | Scalar.Constant)[]) {
super();
this.name = a;
if(b !== undefined)
this.value = b;
}
/**
* Returns the components of `this` vector. The index values start
* from `1` instead of the commonly used starting index `0`.
* @param i The index of the desired component.
* @return The [[Scalar]] element at given index.
*/
public get X() {
const self = this;
return function(i: number) {
if(i <= 0)
throw new InvalidIndex(i, 0);
if(self.value.length === 0)
return Scalar.variable(self.name + "_" + i);
return (i <= self.value.length)? self.value[i - 1]: Scalar.constant(0);
}
}
/**
* Creates and returns a [[Vector.Expression]] for the addition of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* adding a variable vector to another vector always results in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for sum of `this` and `that`.
*/
public add(that: Vector) {
return new Vector.Expression(BinaryOperator.ADD, this, that, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return (<Scalar>this.X(i)).add(that.X(i));
});
}
/**
* Creates and returns a [[Vector.Expression]] for the subtraction of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* subtracting a variable vector from another vector always results in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for subtracting `that` from `this`.
*/
public sub(that: Vector) {
return new Vector.Expression(BinaryOperator.SUB, this, that, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return (<Scalar>this.X(i)).sub(that.X(i));
});
}
/**
* Creates and returns a [[Vector.Expression]] for the dot product of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* dot multiplying a variable vector with another vector always results
* in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for inner product of `this` and `that`.
*/
public dot(that: Vector) {
return new Scalar.Expression(BinaryOperator.DOT, this, that);
}
/**
* Creates and returns a [[Vector.Expression]] for the cross product of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* cross multiplying a variable vector to another vector always results
* in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for vector product of `this` and `that`.
*/
public cross(that: Vector) {
return new Vector.Expression(BinaryOperator.CROSS, this, that, (i: number) => {
if(i <= 0)
throw new Error("Indexing starts from `1`.");
if(this.value.length > 3)
throw new Error("Cross product defined only in 3 dimensions.");
const a1 = <Scalar>this.X(1), a2 = <Scalar>this.X(2), a3 = <Scalar>this.X(3);
const b1 = <Scalar>that.X(1), b2 = <Scalar>that.X(2), b3 = <Scalar>that.X(3);
return (i === 1)? a2.mul(b3).sub(a3.mul(b2)):
(i === 2)? a3.mul(b1).sub(a1.mul(b3)):
a1.mul(b2).sub(a2.mul(b1));
});
}
/**
* Creates and returns a [[Vector.Expression]] for the scaling of
* `this` [[Vector]] object. The [[type]] of `that` does not matter because
* scaling a variable vector always results in an expresion.
* @param k The scale factor.
* @return Expression for scaling `this`.
*/
public scale(k: Scalar) {
return new Vector.Expression(BinaryOperator.SCALE, this, k, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return (<Scalar>this.X(i)).mul(k);
});
}
}
/**
* @extends [[Vector]]
*/
export class Expression extends Vector implements _Expression {
readonly type = "expression";
readonly arg_list: Set<_Variable>;
readonly operands: Evaluable[] = [];
/**
* Returns the components of `this` vector. The index values start
* from `1` instead of the commonly used starting index `0`.
* @param i The index of the desired component.
* @return The [[Scalar]] element at given index.
*/
readonly X: (i: number) => Scalar;
/**
* Creates a vector expression for a binary operator with left and right
* hand side arguments.
* @param op The root binary operator.
* @param lhs The left hand side argument for the root operator.
* @param rhs The right hand side argument for the root operator.
* @param X The accessor function which defines what the `i`th element should be.
*/
constructor(op: BinaryOperator, lhs: Evaluable, rhs: Evaluable, X: (i: number) => Scalar);
/**
* Creates a vector expression for a binary operator with left and right
* hand side arguments.
* @param op The root unary operator.
* @param arg The argument for the root operator.
* @param X The accessor function which defines what the `i`th element should be.
*/
constructor(op: UnaryOperator, arg: Evaluable, X: (i: number) => Scalar);
constructor(readonly op: Operator, a: Evaluable, b: Evaluable | ((i: number) => Scalar), c?: (i: number) => Scalar) {
super();
if(b instanceof Function && c === undefined) {
this.X = b;
this.arg_list = ExpressionBuilder.createArgList(a);
this.operands.push(a);
} else if(!(b instanceof Function) && c instanceof Function) {
this.X = c;
this.arg_list = ExpressionBuilder.createArgList(a, b);
this.operands.push(a, b);
} else throw new Error("Illegal argument.");
}
/**
* The left hand side operand for `this.op`.
* @throws If `this.op` is a `UnaryOperator`.
*/
public get lhs() {
if(isBinaryOperator(this.op))
return this.operands[0];
throw new Error("Unary operators have no left hand argument.");
}
/**
* The right hand side operand for `this.op`.
* @throws If `this.op` is a `UnaryOperator`.
*/
public get rhs() {
if(isBinaryOperator(this.op))
return this.operands[1];
throw new Error("Unary operators have no right hand argument.");
}
/**
* The argument for `this.op`.
* @throws If `this.op` is a `BinaryOperator`.
*/
public get arg() {
if(isUnaryOperator(this.op))
return this.operands[0];
throw new Error("Binary operators have two arguments.");
}
/**
* Creates and returns a [[Vector.Expression]] for the addition of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* adding an unknown vector/vector expression to another vector always
* results in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for sum of `this` and `that`.
*/
public add(that: Vector) {
return new Vector.Expression(BinaryOperator.ADD, this, that, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return this.X(i).add(that.X(i));
});
}
/**
* Creates and returns a [[Vector.Expression]] for the subtraction of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* subtracting an unknown vector/vector expression from another vector
* always results in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for subtracting `that` from `this`.
*/
public sub(that: Vector) {
return new Vector.Expression(BinaryOperator.SUB, this, that, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return this.X(i).sub(that.X(i));
});
}
/**
* Creates and returns a [[Vector.Expression]] for the dot product of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* dot multiplying an unknown vector/vector expression with another vector
* always results
* in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for inner product of `this` and `that`.
*/
public dot(that: Vector) {
return new Scalar.Expression(BinaryOperator.DOT, this, that);
}
/**
* Creates and returns a [[Vector.Expression]] for the cross product of
* two [[Vector]] objects. The [[type]] of `that` does not matter because
* cross multiplying an unknown vector/vector expression to another vector
* always results
* in an expresion.
* @param that The [[Vector]] to add to `this`.
* @return Expression for vector product of `this` and `that`.
*/
public cross(that: Vector) {
return new Vector.Expression(BinaryOperator.CROSS, this, that, (i: number) => {
if(i <= 0)
throw new Error("Indexing starts from `1`.");
// if(this.value.length > 3)
// throw new Error("Cross product defined only in 3 dimensions.");
const a1 = <Scalar>this.X(1), a2 = <Scalar>this.X(2), a3 = <Scalar>this.X(3);
const b1 = <Scalar>that.X(1), b2 = <Scalar>that.X(2), b3 = <Scalar>that.X(3);
return (i === 1)? a2.mul(b3).sub(a3.mul(b2)):
(i === 2)? a3.mul(b1).sub(a1.mul(b3)):
a1.mul(b2).sub(a2.mul(b1));
});
}
/**
* Creates and returns a [[Vector.Expression]] for the scaling of
* `this` [[Vector]] object. The [[type]] of `that` does not matter because
* scaling an unknown vector/vector expression always results in an expresion.
* @param k The scale factor.
* @return Expression for scaling `this`.
*/
public scale(k: Scalar) {
return new Vector.Expression(BinaryOperator.SCALE, this, k, (i: number) => {
if(i <= 0)
throw new InvalidIndex(i, 0);
return this.X(i).mul(k);
});
}
/**
* Checks whether `this` [[Vector.Expression]] depends on a given
* [[Variable]].
* @param v The [[Variable]] to check against.
*/
public isFunctionOf(v: _Variable) {
return this.arg_list.has(v);
}
/**
* Evaluates this [[Vector.Expression]] at the given values for the
* [[Variable]] objects `this` depends on. In case `this` is not a
* function of any of the variables in the mapping then `this` is returned
* as is.
* @param values A map from the [[Variable]] quantities to [[Constant]] quantities.
* @return The result after evaluating `this` at the given values.
*/
public at(values: Map<_Variable, _Constant>) {
const res = ExpressionBuilder.evaluateAt(this, values);
if(isConstant(res))
return <Vector.Constant>res;
if(isVariable(res))
return <Vector.Variable>res;
return <Vector.Expression>res;
}
}
/**
* Creates a new [[Vector.Constant]] object from a list of numbers
* if it has not been created before.
* Otherwise just returns the previously created object.
*
* This is the recommended way of creating [[Vector.Constant]] objects instead of
* using the constructor.
* @param value The fixed value the [[Vector.Constant]] is supposed to represent.
*/
export function constant(value: number[]): Vector.Constant;
/**
* Defines a named [[Vector.Constant]] object from a list of numbers
* if it has not been created before.
* Otherwise just returns the previously created object.
*
* This is the recommended way of creating named [[Vector.Constant]] objects instead of
* using the constructor.
* @param value The fixed value the [[Vector.Constant]] is supposed to represent.
* @param name The string with which `this` object is identified.
* @throws Throws an error if a [[Vector.Constant]] with the same name has been defined previously.
*/
export function constant(value: number[], name: string): Vector.Constant;
/**
* Creates a new [[Vector.Constant]] object from a list of [[Scalar.Constant]] objects
* if it has not been created before.
* Otherwise just returns the previously created object.
*
* This is the recommended way of creating [[Vector.Constant]] objects instead of
* using the constructor.
* @param value The fixed value the [[Vector.Constant]] is supposed to represent.
*/
export function constant(value: Scalar.Constant[]): Vector.Constant;
/**
* Defines a named [[Vector.Constant]] object from a list of [[Scalar.Constant]] objects
* if it has not been created before.
* Otherwise just returns the previously created object.
*
* This is the recommended way of creating named [[Vector.Constant]] objects instead of
* using the constructor.
* @param value The fixed value the [[Vector.Constant]] is supposed to represent.
* @param name The string with which `this` object is identified.
*
* @throws Throws an error if a [[Vector.Constant]] with the same name has been defined previously.
*/
export function constant(value: Scalar.Constant[], name: string): Vector.Constant;
/**
* Returns a previously declared named [[Vector.Constant]] object.
* @param name The name of the named [[Vector.Constant]] object to be retrieved.
*/
export function constant(name: string): Vector.Constant;
export function constant(a: number[] | Scalar.Constant[] | string, b?: string) {
let c;
if(Array.isArray(a)) {
const values: number[] = [];
if(typeof a[0] === "number")
for(let i = 0; i < a.length; i++)
values.push(<number>a[i]);
else if(a[0] instanceof Scalar.Constant)
for(let i = 0; i < a.length; i++)
values.push((<Scalar.Constant>a[i]).value);
let i = values.length - 1;
for(; i >= 0; i--)
if(values[i] !== 0)
break;
const key = values.slice(0, i+1).join();
if(b === undefined) {
c = CONSTANTS.get(key);
if(c === undefined) {
c = new Vector.Constant(values);
CONSTANTS.set(key, c);
}
} else {
c = NAMED_CONSTANTS.get(b);
if(c !== undefined)
throw new Error("Attempt to redefine a constant: A constant with the same name already exists.");
c = new Vector.Constant(values, b);
NAMED_CONSTANTS.set(b, c);
}
} else {
c = NAMED_CONSTANTS.get(a);
if(c === undefined)
throw new Error("No such constant defined.");
}
return c;
}
/**
* Creates a new [[Vector.Variable]] object if it has not been created before.
* Otherwise just returns the previously created object.
*
* This is the recommended way of creating [[Vector.Variable]] objects instead of
* using the constructor.
* @param name The string with which `this` object will be identified.
*/
export function variable(name: string): Vector.Variable;
/**
* Creates a [[Vector.Variable]] object from an array. The array may
* contain known scalar constants and, for the components yet unknown,
* [\_\_](../globals.html#__). Passing ``__`` as an element of the `value` array automatically
* gets interpreted as having a variable at that index. This allows for
* creation of vectors whose few components are known before hand and
* the rest are not. For example,
* ```javascript
* const A = Vector.variable("A", [1, __, 4, __, 2]);
* console.log(A);
* ```
* This line of code will create a vector whose 2nd and 4th components are
* [[Scalar.Variable]] objects and the remaining will be [[Scalar.Constant]]
* objects.
*
* This is the recommended way of creating [[Vector.Variable]] objects instead of
* using the constructor.
* @param name The name with which the [[Vector.Variable]] is going to be identified.
* @param value The array containing the values with which to initialise the vector variable object.
*/
export function variable(name: string, value: (Scalar.Constant | undefined | number)[]): Vector.Variable;
export function variable(name: string, value?: (Scalar.Constant | undefined | number)[]) {
let v = VARIABLES.get(name);
if(v === undefined) {
if(value === undefined)
v = new Vector.Variable(name);
else {
const arr: (Scalar.Constant | Scalar.Variable)[] = [];
if(value !== undefined) {
let i = value.length - 1;
for(; i >= 0; i--)
if(value[i] !== Scalar.constant(0) || value[i] !== 0)
break;
for(let j = 0; j <= i; j++) {
const x = value[j];
if(x === undefined)
arr.push(Scalar.variable(name + "_" + (j+1)));
else if(x instanceof Scalar.Constant)
arr.push(x);
else
arr.push(Scalar.constant(x));
}
}
v = new Vector.Variable(name, arr);
}
VARIABLES.set(name, v);
}
return v;
}
}