LogarithmPlotter/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/math/common.js

79 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-03-05 16:35:58 +00:00
/**
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
2023-05-21 22:19:58 +00:00
* Copyright (C) 2023 Ad5001
2022-03-05 16:35:58 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
.pragma library
.import "../expr-eval.js" as ExprEval
.import "../utils.js" as Utils
.import "latex.js" as Latex
var evalVariables = { // Variables not provided by expr-eval.js, needs to be provided manually
2022-03-05 16:35:58 +00:00
"pi": Math.PI,
"PI": Math.PI,
2022-03-05 16:35:58 +00:00
"π": Math.PI,
"inf": Infinity,
2022-10-19 22:37:02 +00:00
"infinity": Infinity,
2022-03-05 16:35:58 +00:00
"Infinity": Infinity,
"∞": Infinity,
"e": Math.E,
"E": Math.E,
"true": true,
"false": false
2022-03-05 16:35:58 +00:00
}
var currentVars = {}
var currentObjectsByName = {} // Mirror of currentObjectsByName in objects.js
2022-03-05 16:35:58 +00:00
const parser = new ExprEval.Parser()
parser.consts = Object.assign({}, parser.consts, evalVariables)
// Function definition
2022-03-05 16:35:58 +00:00
parser.functions.integral = function(a, b, f, variable) {
// https://en.wikipedia.org/wiki/Simpson%27s_rule
// Simpler, faster than tokenizing the expression
2022-03-05 16:35:58 +00:00
f = parser.parse(f).toJSFunction(variable, currentVars)
return (b-a)/6*(f(a)+4*f((a+b)/2)+f(b))
}
parser.functions.derivative = function(...args) {
let f, target, variable, x
if(args.length == 2) {
[f, x] = args
if(typeof f != 'object' || !f.execute)
throw EvalError(qsTranslate('usage', 'Usage: %1')
.arg(qsTranslate('usage', 'derivative(<function: ExecutableObject>, <x: variable>)')))
target = f
f = (x) => target.execute(x)
} else if(args.length == 3) {
[f, variable, x] = args
if(typeof f != 'string')
throw EvalError(qsTranslate('usage', 'Usage: %1')
.arg(qsTranslate('usage', 'derivative(<function: string>, <variable: string>, <x: variable>)')))
f = parser.parse(f).toJSFunction(variable, currentVars)
} else
throw EvalError(qsTranslate('usage', 'Usage: %1 or\n%2')
.arg(qsTranslate('usage', 'derivative(<function: string>, <variable: string>, <x: variable>)')
.arg(qsTranslate('usage', 'derivative(<function: string>, <variable: string>, <x: variable>)'))))
let derivative_precision = x/10
return (f(x+derivative_precision/2)-f(x-derivative_precision/2))/derivative_precision
2022-03-05 16:35:58 +00:00
}