This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.d.ts
430 lines (380 loc) · 14.5 KB
/
index.d.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
export as namespace nerdamer
export = nerdamer
declare function nerdamer(
expression: nerdamer.ExpressionParam,
subs?: { [name: string]: string },
option?: keyof nerdamer.Options | (keyof nerdamer.Options)[],
location?: nerdamer.int): nerdamer.Expression
declare namespace nerdamer {
export type ExpressionParam = Expression | string
export interface Options {
numer: never, expand: never
}
type int = number
/**
* Returns the current version of nerdamer.
*/
export function version(): string
/**
* Sets a constant value which nerdamer will automatically substitute when parsing expression/equation
* @param name The variable to be set as the constant.
* @param value The value for the expression to be set to.
*/
export function setConstant(name: string, value: number | string): typeof nerdamer
/**
* Sets a function which can then be called using nerdamer.
* @param function_name The function name
* @param param_array The parameter array in the order in which the arguments are to be passed
* @param function_body The body of the function
* @example
* nerdamer.setFunction('f', ['x', 'y'], 'x^2+y')
* var x = nerdamer('f(4, 7)').toString()
* console.log(x.toString())
* nerdamer.setFunction('g', ['z', 'x', 'y'], '2*x+3*y+4*z')
* x = nerdamer('g(3, 1, 2)')
* console.log(x.toString())
*/
export function setFunction(function_name: string, param_array: string[], function_body: string): typeof nerdamer
/**
* Returns the nerdamer core object. This object contains all the core functions of nerdamer and houses the parser.
* @example
* Object.keys(nerdamer.getCore())
*/
export function getCore(): any
/**
* Gets the list of reserved names. This is a list of names already in use by nerdamer excluding variable names. This is not a static list.
* @param asArray Pass in true to get the list back as an array instead of as an object.
*/
export function reserved(asArray: true): string[]
export function reserved(asArray?: false): any
/**
* Clears all stored expressions.
* @example
* var x = nerdamer('x*x')
console.log(nerdamer.expressions())
nerdamer.flush() //clear all expressions
console.log(nerdamer.expressions())
*/
export function flush(): typeof nerdamer
/**
* Converts and expression to LaTeX without evaluating expression.
* @param expression The expression being converted.
*/
export function convertToLaTeX(expression: string): string
/**
* Attempts to import a LaTeX string.
* @param TeX The expression being converted.
*/
export function convertFromLaTeX(TeX: string): Expression
/**
* Each time an expression is parsed nerdamer stores the result. Use this method to get back stored expressions.
* @param asObject Pass in true to get expressions as numbered object with 1 as starting index
* @param asLatex Pass in the string "LaTeX" to get the expression to LaTeX, otherwise expressions come back as strings
*/
export function expressions(asObject?: boolean, asLatex?: 'LaTeX'): string[]
/**
* Registers a module function with nerdamer. The object needs to contain at a minimum, a name property (text), a numargs property (int), this is -1 for variable arguments or an array containing the min and max arguments, the visible property (bool) which allows use of this function through nerdamer, defaults to true, and a build property containing a function which returns the function to be used. This function is also handy for creating aliases to functions. See below how the alias D was created for the diff function).
* @param o
*/
export interface ModuleFunction {
/**
* Name of function.
*/
name: string,
/**
* Number of function arguments, -1 for variable arguments or an tuple containing minimum and maximum number of arguments.
*/
numargs: int | [int, int],
/**
* Allows use of this function through nerdamer. Defaults to true.
*/
visible?: boolean,
/**
* Return the function to be used.
*/
build(): (...args: number[]) => number
}
/**
* Registers one or more module functions with nerdamer.
* @param f
* @example
* var core = nerdamer.getCore()
var _ = core.PARSER
function f(a, b) {
//use clone for safety since a or b might be returned
var sum = _.add(a.clone(), b.clone())
var product = _.multiply(a.clone(), b.clone())
return _.multiply(sum, product)
}
//register the function with nerdamer
nerdamer.register({
name: 'myFunction',
numargs: 2,
visible: true,
build: function(){ return f }
})
//create an alias for the diff function
var core = nerdamer.getCore()
nerdamer.register({
name: 'D',
visible: true,
numargs: [1, 3],
build: function(){ return core.Calculus.diff }
})
*/
export function register(f: ModuleFunction | ModuleFunction[]): typeof nerdamer
/**
* This method can be used to check that the variable meets variable name requirements for nerdamer. Variable names Must start with a letter or underscore and may contains any combination of numbers, letters, and underscores after that.
* @param variable_name The variable name being validated.
* @example
* nerdamer.validVarName('cos') // false
* nerdamer.validVarName('chicken1') // true
* nerdamer.validVarName('1chicken') // false
* nerdamer.validVarName('_') // true
*/
export function validVarName(variable_name: string): boolean
/**
* Sets a known value in nerdamer. This differs from setConstant as the value can be overridden trough the scope. See example.
* @param name The known value to be set.
* @param value The value for the expression to be set to
* @example
* nerdamer.setVar('x', '11')
* nerdamer('x*x') // == 121
* // nerdamer will use 13 instead of 11:
* nerdamer('x*x', {x: 13}) // == 169
* // the value will be 121 again since the known value isn't being overridden:
* nerdamer('x*x') // == 121
* nerdamer.setVar('x', 'delete')
* // since there no longer is a known value it will just be evaluated symbolically:
* nerdamer('x*x') // == x^2
*/
export function setVar(name: string, value: number | string | 'delete'): void
/**
* Clears all previously set variables.
*/
export function clearVars(): typeof nerdamer
/**
* Gets all previously set variables.
* @param option Use "LaTeX" to get as LaTeX. Defaults to text.
*/
export function getVars(option: 'LaTeX' | 'text'): { [name: string]: string }
/**
* Sets the value of a nerdamer setting. Currently PARSE2NUMBER and IMAGINARY. Setting PARSE2NUMBER to true will let nerdamer always try to return a number whenenver possible. IMAGINARY allows you to change the variable used for imaginary to j for instance.
* @param setting The setting to be changed
* @param value The value to set the setting to.
* @example
* nerdamer.set('PARSE2NUMBER', true)
* nerdamer('cos(9)+1') // == 14846499/167059106
* nerdamer.set('IMAGINARY', 'j')
* nerdamer('sqrt(-1)') // == j
*/
export function set(setting: 'PARSE2NUMBER', value: boolean): typeof nerdamer
export function set(setting: 'IMAGINARY', value: string | 'i'): typeof nerdamer
export function set(setting: 'POWER_OPERATOR', value: '**' | '^'): typeof nerdamer
export interface Expression {
/**
* Generates a JavaScript function given the expression. This is perfect for plotting and filtering user input. Plotting for the demo is accomplished using this. The order of the parameters is in alphabetical order by default but an argument array can be provided with the desired order.
* @param args_array The argument array with the order in which they are preferred.
*/
buildFunction(args_array?: string[]): (...args: number[]) => number
/**
* Forces evaluation of the expression.
* @example
* const x = nerdamer('sin(9+5)')
* //the expression is simplified but the functions aren't called:
* x.toString() // == sin(14)
* // force function calls with evaluate:
* x.evaluate().toString() // == 127690464/128901187
*/
evaluate(): Expression
/**
* Substitutes a given value for another given value
* @param value The value being substituted.
* @param for_value The value to substitute for.
*/
sub(value: string, for_value: string): Expression
/**
* Get a list of the variables contained within the expression.
*/
variables(): string[]
/**
* Gets expression as LaTeX
*/
toTeX(): string
/**
* Returns the value of the expression as a string or a number
*/
valueOf(): string | number
/**
* Gets the list of reserved names. This is a list of names already in use by nerdamer excluding variable names. This is not a static list.
* @param outputType Pass in the string 'decimals' to always get back numers as decimals. Pass in the string 'fractions' to always get back number as fractions. Defaults to decimals.
*/
text(outputType?: 'decimals' | 'fractions'): string
/**
* This method requires that the Solve, Calculus, and Algebra add-ons are loaded. It will attempt to solve an equation. If solutions no solutions are found then an empty array is returned. It can solve for multivariate polynomials up to the third degree. After which it can solve numerically for polynomials up to the the 100th degree. If it's a univariate equation it will attempt to solve it using numerical methods.
* @param variable The variable to solve for.
* @example
* nerdamer('a*x^2+b*x=y').evaluate({y: 'x-7'}) // == ??
* eq.solveFor('x') // ?? TODO
*/
solveFor(variable: string): Expression
/**
* Forces the expression to displayed with decimals
*/
toDecimal(prec?: number): string
/**
* Checks to see if the expression's value equals a number. Compares the direct value returned.
* The function will not check for all possible cases. To avoid this call evaluate.
* @example
* nerdamer('sqrt(5)').isNumber()
* // false
* nerdamer('sqrt(5)').evaluate().isNumber()
* // true
*/
isNumber(): boolean
/**
* Checks if a number evaluates to an imaginary number
* @example
* nerdamer('sqrt(-5)+8').isImaginary()
* // true
* nerdamer('sqrt(5)+8').isImaginary()
* // false
*/
isImaginary(): boolean
/**
* Adds a value to an expression
* @example
* nerdamer('x').add(3)
*/
add(symbol: number | string | Expression): Expression
/**
* Subtracts a value from an expression
* @example
* nerdamer('x').subtract(3)
*/
subtract(symbol: number | string | Expression): Expression
/**
* Multiplies an expression by a value
* @example
* nerdamer('x').multiply(3)
*/
multiply(symbol: number | string | Expression): Expression
/**
* Divides an expression by a valule
* @example
* nerdamer('9*x').divide(3)
*/
divide(symbol: number | string | Expression): Expression
/**
* Raises an expression to a power
* @example
* nerdamer('x').pow(3)
*/
pow(symbol: number | string | Expression): Expression
/**
* Checks if two values are equal
* @param value The value being tested
* @example
* nerdamer('sqrt(9)').eq(3)
* // true
* nerdamer('x').eq('y')
* // false
*/
eq(value: number | string | Expression): boolean
/**
* Checks if a value is less than another
* @param value The value being tested
* @example
* nerdamer('sqrt(9)').lt(3)
* // false
* nerdamer('8').lt(100)
* // true
*/
lt(value: number | string | Expression): boolean
/**
* Checks if a value is less than or equal to another
* @param value The value being tested
* @example
* nerdamer('sqrt(9)').lte(3)
* // true
* nerdamer('x').lte(100)
* // false
*/
lte(value: number | string | Expression): boolean
/**
* Checks if a value is greater than another
* @param value The value being tested
* @example
* nerdamer('sqrt(9)').gt(3)
* // false
* nerdamer('800').gt(100)
* // true
*/
gt(value: number | string | Expression): boolean
/**
* Checks if a value is greater than or equal to another
* @param value The value being tested
* @example
* nerdamer('sqrt(9)').gte(3)
* // true
* nerdamer('x').gte(100)
* // false
*/
gte(value: number | string | Expression): boolean
/**
* Expands a function or expression.
* @example
* nerdamer('x*(x+1)').expand();
* // x+x^2
* nerdamer('(x+y)*(x-5)*x').expand();
* // -5*x*y-5*x^2+x^3+x^2*y
*/
expand(): Expression
}
////////// CALCULUS
/**
* Gets the GCD of 2 polynomials
* @param expression Returns the appropriate value if possible otherwise it returns the function with the simplified expression.
* @param index The index of summation.
* @param lower Starting index.
* @param upper Ending index.
*/
export function sum(expression: ExpressionParam,
index: string,
lower: ExpressionParam,
upper: ExpressionParam): Expression
/**
*
* @param expression Returns the appropriate value if possible otherwise it returns the function with the simplified expression.
* @param variable The variable with respect to which to integrate.
*/
export function integrate(expression: ExpressionParam, variable: string): Expression
/**
*
* @param expression Returns the appropriate value if possible otherwise it returns the function with the simplified expression.
* @param variable The variable with respect to which to differentiate.
* @param n Calculate the nth derivative.
*/
export function diff(expression: ExpressionParam, variable: string, n?: int): Expression
////////// ALGEBRA
/**
* Divides 2 polynominals.
* @param expression Returns the appropriate value if possible otherwise it returns the function with the simplified expression.
*/
export function divide(expression: ExpressionParam): Expression
/**
* Factor an expression.
* @param expression Returns the appropriate value if possible otherwise it returns the function with the simplified expression.
*/
export function factor(expression: ExpressionParam): Expression
/**
* Gets the GCD of 2 polynomials.
* @param expression Returns the appropriate value if possible otherwise it returns the function with the simplified expression.
*/
export function gcd(expression: ExpressionParam): Expression
/**
* Finds the roots of a univariate polynomial.
* @param expression
*/
export function factor(expression: ExpressionParam): Expression
}