Comparing version 1.1.8 to 1.1.9
@@ -18,2 +18,3 @@ /** | ||
func: Function; | ||
unparserML?: (tree: any) => string; | ||
}; | ||
@@ -224,2 +225,3 @@ export type TBaseFunctionTable = Record<string, TBaseFunctionTableEntry>; | ||
readonly linearize: typeof MultiArray.linearize; | ||
private readonly unparseMLFunctions; | ||
/** | ||
@@ -390,3 +392,3 @@ * Evaluator object constructor | ||
*/ | ||
private unparserML; | ||
unparserML(tree: any): string; | ||
/** | ||
@@ -393,0 +395,0 @@ * Unparse Expression tree in MathML. |
@@ -156,2 +156,3 @@ import { ComplexDecimal } from './complex-decimal'; | ||
static testIndex(k: ComplexDecimal, bound: number, matrix: MultiArray, input: string): number; | ||
static firstNonSingleDimmension(M: MultiArray): number; | ||
static oneRowToDim(M: ComplexDecimal[] | MultiArray): number[]; | ||
@@ -316,5 +317,34 @@ static setItems(M: MultiArray, id: string, indexList: Array<ComplexDecimal | MultiArray>, value: MultiArray | ComplexDecimal): MultiArray | ComplexDecimal; | ||
*/ | ||
static mean(M: MultiArray): MultiArray | ComplexDecimal; | ||
static sumsq(M: MultiArray, DIM: ComplexDecimal): MultiArray | ComplexDecimal; | ||
static mean(M: MultiArray | ComplexDecimal): MultiArray | ComplexDecimal; | ||
/** | ||
* Calculate sum or product of elements along dimension DIM. | ||
* @param op 'add' for sum or 'mul' for product | ||
* @param mapper function to apply in each element | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with sum or product of elements along dimension DIM. | ||
*/ | ||
private static sumprod; | ||
/** | ||
* Calculate sum of elements along dimension DIM. | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with sum of elements along dimension DIM. | ||
*/ | ||
static sum(M: MultiArray, DIM?: ComplexDecimal): MultiArray | ComplexDecimal; | ||
/** | ||
* Calculate sum of squares of elements along dimension DIM. | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with sum of squares of elements along dimension DIM. | ||
*/ | ||
static sumsq(M: MultiArray, DIM?: ComplexDecimal): MultiArray | ComplexDecimal; | ||
/** | ||
* Calculate product of elements along dimension DIM. | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with product of elements along dimension DIM. | ||
*/ | ||
static prod(M: MultiArray, DIM?: ComplexDecimal): MultiArray | ComplexDecimal; | ||
/** | ||
* Sum of diagonal elements. | ||
@@ -321,0 +351,0 @@ * @param M |
{ | ||
"name": "mathjslab", | ||
"version": "1.1.8", | ||
"description": "MathJSLab - An interpreter with language syntax like MATLAB/Octave. ISBN 978-65-00-82338-7", | ||
"version": "1.1.9", | ||
"description": "MathJSLab - An interpreter with language syntax like MATLAB®/Octave. ISBN 978-65-00-82338-7", | ||
"main": "lib/mathjslab.js", | ||
@@ -6,0 +6,0 @@ "types": "lib/src/lib.d.ts", |
@@ -83,3 +83,3 @@ # MathJSLab | ||
Examples: | ||
### Examples: | ||
@@ -86,0 +86,0 @@ * Parsing |
@@ -457,9 +457,6 @@ import { Decimal } from 'decimal.js'; | ||
public static minMaxArrayReal(cmp: 'lt' | 'gt', ...args: ComplexDecimal[]): ComplexDecimal { | ||
let result = args[0]; | ||
for (let i = 1; i < args.length; i++) { | ||
if (args[i].re[cmp](result.re)) { | ||
result = args[i]; | ||
} | ||
} | ||
return result; | ||
return args.reduce( | ||
(previous: ComplexDecimal, current: ComplexDecimal): ComplexDecimal => (previous.re[cmp](current.re) ? previous : current), | ||
args[0], | ||
); | ||
} | ||
@@ -477,18 +474,11 @@ | ||
public static minMaxArrayComplex(cmp: 'lt' | 'gt', ...args: ComplexDecimal[]): ComplexDecimal { | ||
let result = args[0]; | ||
for (let i = 1; i < args.length; i++) { | ||
const result_abs = ComplexDecimal.abs(result).re; | ||
const arg_abs = ComplexDecimal.abs(args[i]).re; | ||
if (result_abs.eq(arg_abs)) { | ||
result = ComplexDecimal.arg(result).re[cmp](ComplexDecimal.arg(args[i]).re) ? result : args[i]; | ||
if (ComplexDecimal.arg(args[i]).re[cmp](ComplexDecimal.arg(result).re)) { | ||
result = args[i]; | ||
} | ||
return args.reduce((previous: ComplexDecimal, current: ComplexDecimal): ComplexDecimal => { | ||
const previous_abs = ComplexDecimal.abs(previous).re; | ||
const current_abs = ComplexDecimal.abs(current).re; | ||
if (previous_abs.eq(current_abs)) { | ||
return ComplexDecimal.arg(previous).re[cmp](ComplexDecimal.arg(current).re) ? previous : current; | ||
} else { | ||
if (arg_abs[cmp](result_abs)) { | ||
result = args[i]; | ||
} | ||
return previous_abs[cmp](current_abs) ? previous : current; | ||
} | ||
} | ||
return result; | ||
}, args[0]); | ||
} | ||
@@ -495,0 +485,0 @@ |
@@ -25,2 +25,3 @@ /** | ||
func: Function; | ||
unparserML?: (tree: any) => string; | ||
}; | ||
@@ -350,2 +351,16 @@ export type TBaseFunctionTable = Record<string, TBaseFunctionTableEntry>; | ||
private readonly unparseMLFunctions: Record<string, (tree: any) => string> = { | ||
abs: (tree: any) => '<mrow><mo>|</mo>' + this.unparserML(tree.args[0]) + '<mo>|</mo></mrow>', | ||
conj: (tree: any) => '<mover><mrow>' + this.unparserML(tree.args[0]) + '</mrow><mo>‾</mo></mover>', | ||
sqrt: (tree: any) => '<msqrt><mrow>' + this.unparserML(tree.args[0]) + '</mrow></msqrt>', | ||
root: (tree: any) => '<mroot><mrow>' + this.unparserML(tree.args[0]) + '</mrow><mrow>' + this.unparserML(tree.args[1]) + '</mrow></mroot>', | ||
exp: (tree: any) => '<msup><mi>e</mi><mrow>' + this.unparserML(tree.args[0]) + '</mrow></msup>', | ||
logb: (tree: any) => | ||
'<msub><mi>log</mi><mrow>' + this.unparserML(tree.args[0]) + '</mrow></msub><mrow>' + this.unparserML(tree.args[1]) + '</mrow>', | ||
log2: (tree: any) => '<msub><mi>log</mi><mrow>' + '<mn>2</mn>' + '</mrow></msub><mrow>' + this.unparserML(tree.args[0]) + '</mrow>', | ||
log10: (tree: any) => '<msub><mi>log</mi><mrow>' + '<mn>10</mn>' + '</mrow></msub><mrow>' + this.unparserML(tree.args[0]) + '</mrow>', | ||
gamma: (tree: any) => '<mi>Γ</mi><mrow><mo>(</mo>' + this.unparserML(tree.args[0]) + '<mo>)</mo></mrow>', | ||
factorial: (tree: any) => '<mrow><mo>(</mo>' + this.unparserML(tree.args[0]) + '<mo>)</mo></mrow><mo>!</mo>', | ||
}; | ||
/** | ||
@@ -388,2 +403,5 @@ * Evaluator object constructor | ||
} | ||
for (const func in this.unparseMLFunctions) { | ||
this.baseFunctionTable[func].unparserML = this.unparseMLFunctions[func]; | ||
} | ||
} | ||
@@ -911,5 +929,5 @@ | ||
return this.nodeOp('=', left, this.nameTable[id].expr); | ||
} catch (e) { | ||
} catch (error) { | ||
this.nameTable[id] = { args: [], expr: expr }; | ||
throw e; | ||
throw error; | ||
} | ||
@@ -1210,3 +1228,3 @@ } else { | ||
*/ | ||
private unparserML(tree: any): string { | ||
public unparserML(tree: any): string { | ||
try { | ||
@@ -1329,63 +1347,6 @@ if (tree === undefined) { | ||
const aliasTreeName = this.aliasName(tree.expr.id); | ||
switch (aliasTreeName) { | ||
case 'abs': | ||
return '<mrow><mo>|</mo>' + this.unparserML(tree.args[0]) + '<mo>|</mo></mrow>'; | ||
case 'conj': | ||
return '<mover><mrow>' + this.unparserML(tree.args[0]) + '</mrow><mo>‾</mo></mover>'; | ||
case 'sqrt': | ||
return '<msqrt><mrow>' + this.unparserML(tree.args[0]) + '</mrow></msqrt>'; | ||
case 'root': | ||
return ( | ||
'<mroot><mrow>' + | ||
this.unparserML(tree.args[0]) + | ||
'</mrow><mrow>' + | ||
this.unparserML(tree.args[1]) + | ||
'</mrow></mroot>' | ||
); | ||
case 'exp': | ||
return '<msup><mi>e</mi><mrow>' + this.unparserML(tree.args[0]) + '</mrow></msup>'; | ||
case 'log': | ||
return ( | ||
'<msub><mi>log</mi><mrow>' + | ||
this.unparserML(tree.args[0]) + | ||
'</mrow></msub><mrow>' + | ||
this.unparserML(tree.args[1]) + | ||
'</mrow>' | ||
); | ||
case 'sum': | ||
return ( | ||
'<mrow><munderover><mo>∑</mo><mrow>' + | ||
this.unparserML(tree.args[0]) + | ||
'<mo>=</mo>' + | ||
this.unparserML(tree.args[1]) + | ||
'</mrow>' + | ||
this.unparserML(tree.args[2]) + | ||
'</munderover></mrow>' + | ||
this.unparserML(tree.args[3]) | ||
); | ||
case 'prod': | ||
return ( | ||
'<mrow><munderover><mo>∏</mo><mrow>' + | ||
this.unparserML(tree.args[0]) + | ||
'<mo>=</mo>' + | ||
this.unparserML(tree.args[1]) + | ||
'</mrow>' + | ||
this.unparserML(tree.args[2]) + | ||
'</munderover></mrow>' + | ||
this.unparserML(tree.args[3]) | ||
); | ||
case 'gamma': | ||
return '<mi>Γ</mi><mrow><mo>(</mo>' + arglist + '<mo>)</mo></mrow>'; | ||
case 'factorial': | ||
return '<mrow><mo>(</mo>' + this.unparserML(tree.args[0]) + '<mo>)</mo></mrow><mo>!</mo>'; | ||
case 'binomial': | ||
return ( | ||
'<mrow><mo>(</mo><mtable><mtr><mtd>' + | ||
this.unparserML(tree.args[0]) + | ||
'</mtd></mtr><mtr><mtd>' + | ||
this.unparserML(tree.args[1]) + | ||
'</mtd></mtr></mtable><mo>)</mo></mrow>' | ||
); | ||
default: | ||
return '<mi>' + substGreek(tree.expr.id) + '</mi><mrow><mo>(</mo>' + arglist + '<mo>)</mo></mrow>'; | ||
if (aliasTreeName in this.baseFunctionTable && this.baseFunctionTable[aliasTreeName].unparserML) { | ||
return this.baseFunctionTable[aliasTreeName].unparserML!(tree); | ||
} else { | ||
return '<mi>' + substGreek(tree.expr.id) + '</mi><mrow><mo>(</mo>' + arglist + '<mo>)</mo></mrow>'; | ||
} | ||
@@ -1392,0 +1353,0 @@ } else { |
@@ -40,3 +40,5 @@ import { ComplexDecimal } from './complex-decimal'; | ||
mean: MultiArray.mean, | ||
sum: MultiArray.sum, | ||
sumsq: MultiArray.sumsq, | ||
prod: MultiArray.prod, | ||
trace: MultiArray.trace, | ||
@@ -374,2 +376,11 @@ det: MultiArray.det, | ||
public static firstNonSingleDimmension(M: MultiArray): number { | ||
for (let i = 0; i < M.dim.length; i++) { | ||
if (M.dim[i] !== 1) { | ||
return i; | ||
} | ||
} | ||
return M.dim.length - 1; | ||
} | ||
public static oneRowToDim(M: ComplexDecimal[] | MultiArray): number[] { | ||
@@ -1068,20 +1079,77 @@ if (Array.isArray(M)) { | ||
*/ | ||
public static mean(M: MultiArray): MultiArray | ComplexDecimal { | ||
let temp: MultiArray; | ||
if (M.dim[0] === 1) { | ||
temp = MultiArray.transpose(M); | ||
public static mean(M: MultiArray | ComplexDecimal): MultiArray | ComplexDecimal { | ||
if ('array' in M) { | ||
let temp: MultiArray; | ||
if (M.dim[0] === 1) { | ||
temp = MultiArray.transpose(M); | ||
} else { | ||
temp = M; | ||
} | ||
const result = new MultiArray([1, temp.dim[1]]); | ||
result.array = [new Array(temp.dim[1])]; | ||
for (let j = 0; j < temp.dim[1]; j++) { | ||
result.array[0][j] = temp.array[0][j]; | ||
for (let i = 1; i < temp.dim[0]; i++) { | ||
result.array[0][j] = ComplexDecimal.add(result.array[0][j], temp.array[i][j]); | ||
} | ||
result.array[0][j] = ComplexDecimal.rdiv(result.array[0][j], new ComplexDecimal(temp.dim[0], 0)); | ||
} | ||
result.type = Math.max(...result.array.map((row) => ComplexDecimal.maxNumberType(...row))); | ||
if (temp.dim[1] === 1) { | ||
return result.array[0][0]; | ||
} else { | ||
return result; | ||
} | ||
} else { | ||
temp = M; | ||
return M; | ||
} | ||
const result = new MultiArray([1, temp.dim[1]]); | ||
result.array = [new Array(temp.dim[1])]; | ||
for (let j = 0; j < temp.dim[1]; j++) { | ||
result.array[0][j] = temp.array[0][j]; | ||
for (let i = 1; i < temp.dim[0]; i++) { | ||
result.array[0][j] = ComplexDecimal.add(result.array[0][j], temp.array[i][j]); | ||
} | ||
/** | ||
* Calculate sum or product of elements along dimension DIM. | ||
* @param op 'add' for sum or 'mul' for product | ||
* @param mapper function to apply in each element | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with sum or product of elements along dimension DIM. | ||
*/ | ||
private static sumprod( | ||
op: 'add' | 'mul', | ||
mapper: ((value: ComplexDecimal) => ComplexDecimal) | null, | ||
M: MultiArray, | ||
DIM?: ComplexDecimal, | ||
): MultiArray | ComplexDecimal { | ||
if (!mapper) { | ||
mapper = (value) => value; | ||
} | ||
let dim: number; | ||
if (DIM) { | ||
dim = MultiArray.testIndex(DIM, M.dim.length, M, 'sum'); | ||
} else { | ||
dim = MultiArray.firstNonSingleDimmension(M); | ||
} | ||
let result: MultiArray; | ||
if (dim === 0) { | ||
result = new MultiArray([1, M.dim[1]]); | ||
result.array[0] = new Array(M.dim[1]); | ||
for (let i = 0; i < M.dim[1]; i++) { | ||
result.array[0][i] = M.array | ||
.map((line) => line[i]) | ||
.reduce( | ||
(accumulator: ComplexDecimal, current: ComplexDecimal): ComplexDecimal => ComplexDecimal[op](accumulator, mapper!(current)), | ||
op === 'mul' ? ComplexDecimal.one() : ComplexDecimal.zero(), | ||
); | ||
} | ||
result.array[0][j] = ComplexDecimal.rdiv(result.array[0][j], new ComplexDecimal(temp.dim[0], 0)); | ||
} else { | ||
result = new MultiArray([M.dim[0], 1]); | ||
for (let i = 0; i < M.dim[0]; i++) { | ||
result.array[i] = [ | ||
M.array[i].reduce( | ||
(accumulator: ComplexDecimal, current: ComplexDecimal): ComplexDecimal => ComplexDecimal[op](accumulator, mapper!(current)), | ||
op === 'mul' ? ComplexDecimal.one() : ComplexDecimal.zero(), | ||
), | ||
]; | ||
} | ||
} | ||
result.type = Math.max(...result.array.map((row) => ComplexDecimal.maxNumberType(...row))); | ||
if (temp.dim[1] === 1) { | ||
if (result.dim[0] === 1 && result.dim[1] === 1) { | ||
return result.array[0][0]; | ||
@@ -1093,7 +1161,33 @@ } else { | ||
public static sumsq(M: MultiArray, DIM: ComplexDecimal): MultiArray | ComplexDecimal { | ||
return ComplexDecimal.one(); | ||
/** | ||
* Calculate sum of elements along dimension DIM. | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with sum of elements along dimension DIM. | ||
*/ | ||
public static sum(M: MultiArray, DIM?: ComplexDecimal): MultiArray | ComplexDecimal { | ||
return MultiArray.sumprod('add', null, M, DIM); | ||
} | ||
/** | ||
* Calculate sum of squares of elements along dimension DIM. | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with sum of squares of elements along dimension DIM. | ||
*/ | ||
public static sumsq(M: MultiArray, DIM?: ComplexDecimal): MultiArray | ComplexDecimal { | ||
return MultiArray.sumprod('add', (value) => ComplexDecimal.mul(value, ComplexDecimal.conj(value)), M, DIM); | ||
} | ||
/** | ||
* Calculate product of elements along dimension DIM. | ||
* @param M Matrix | ||
* @param DIM Dimension | ||
* @returns One dimensional matrix with product of elements along dimension DIM. | ||
*/ | ||
public static prod(M: MultiArray, DIM?: ComplexDecimal): MultiArray | ComplexDecimal { | ||
return MultiArray.sumprod('mul', null, M, DIM); | ||
} | ||
/** | ||
* Sum of diagonal elements. | ||
@@ -1100,0 +1194,0 @@ * @param M |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
436776
6695