Skip to content

Commit

Permalink
modified: .env
Browse files Browse the repository at this point in the history
	modified:   CeresHelper.js
	modified:   build.sh
	modified:   shortBuild.sh
  • Loading branch information
miner committed Jan 4, 2024
1 parent 139e3cc commit bc934dd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=2.0.2
CERESJS=https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/dist/ceres.js
50 changes: 38 additions & 12 deletions CeresHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Ceres {
this.instance.delete();
}

parseFunctionFromJson(jsonFunction, variablesMapping) {
static parseFunctionFromJson(jsonFunction, variablesMapping) {
let parsedFunction = jsonFunction;

// Replace variables with their corresponding indexed representations
Expand All @@ -127,19 +127,49 @@ export class Ceres {
parsedFunction = parsedFunction.replace(regex, `x[${index}]`);
});

return new Function('x', `return ${parsedFunction}`);
return parsedFunction;
}

sanitizeInput(input) {
// Return input without any potentially harmful JavaScript string
return input.replace(/[<>]/g, "");
}
static sanitizeInput(mathExpression) {
const validExpression = /^[0-9\s+\-/*()\[\]^sincoxetalaqrtPi.E]*$/;
const validStructure = /^[\d\s+\-/*()\[\]^]*$/;

if (!validExpression.test(mathExpression) || !validStructure.test(mathExpression.replace(/sin|cos|tan|cot|sec|csc|log|ln|sqrt|exp|abs|x|E|Pi/g,''))){
throw new Error(`Invalid mathematical expression: ${mathExpression}`);
}

mathExpression = mathExpression.replace('^', '**');

// Translation to Javascript functions
mathExpression = mathExpression
.replace(/sin/g, 'Math.sin')
.replace(/cos/g, 'Math.cos')
.replace(/tan/g, 'Math.tan')
.replace(/cot/g, 'Math.cot')
.replace(/sec/g, 'Math.sec')
.replace(/csc/g, 'Math.csc')
.replace(/log/g, 'Math.log10')
.replace(/ln/g, 'Math.log')
.replace(/sqrt/g, 'Math.sqrt')
.replace(/exp/g, 'Math.exp')
.replace(/abs/g, 'Math.abs')
.replace(/Pi/g, 'Math.PI')
.replace(/E/g, 'Math.E');

return mathExpression;
}

setSystemFromJson(jsonSystem) {

//console.log(jsonSystem.functions)
let variables = jsonSystem.variables
jsonSystem.functions = jsonSystem.functions.map(function(x) { return Ceres.parseFunctionFromJson(x, variables); });

// sanitize the input to prevent injection attacks
jsonSystem.functions = jsonSystem.functions.map(this.sanitizeInput);
jsonSystem.functions = jsonSystem.functions.map(Ceres.sanitizeInput);
//console.log(jsonSystem.functions)

jsonSystem.functions.forEach(jsonFunction => this.addFunction(this.parseFunctionFromJson(jsonFunction, jsonSystem.variables)));
jsonSystem.functions.forEach(jsonFunction => this.addFunction(new Function('x', `return ${jsonFunction}`)));

Object.keys(jsonSystem.variables).forEach((varName, index) => {
let variable = jsonSystem.variables[varName];
Expand All @@ -152,10 +182,6 @@ export class Ceres {
});
}

generateInitialGuess(variablesMapping) {
return Object.keys(variablesMapping).map(varName => variablesMapping[varName].guess);
}

async run(jsonSystem, max_numb_iterations = 2000, parameter_tolerance = 1e-10, function_tolerance = 1e-16, gradient_tolerance = 1e-16, max_solver_time_in_seconds = 100, initial_trust_region_radius = 1e4, max_trust_region_radius = 1e16, max_num_consecutive_invalid_steps = 5) {
this.setSystemFromJson(jsonSystem);
let initial_guess = this.generateInitialGuess(jsonSystem.variables);
Expand Down
3 changes: 1 addition & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ sed -i 's/include(CeresCodeGeneration)/# include(CeresCodeGeneration)/' $bdir/in
#$bdir/emsdk/upstream/emscripten/emconfigure cmake $cwd/ -DCMAKE_INSTALL_PREFIX=$bdir/installpkg
$bdir/emsdk/upstream/emscripten/emcmake cmake $cwd/ -DCMAKE_INSTALL_PREFIX=$bdir/installpkg
$bdir/emsdk/upstream/emscripten/emmake make
cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-v$VERSION.js
cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-latest.mjs
cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/ceres.js

#~/emsdk/upstream/emscripten/emrun --browser "explorer.exe" ~/ceres.js-master/index.html
#~/emsdk/upstream/emscripten/emrun --browser "explorer.exe" ~/ceres.js-master/test.html
Expand Down
3 changes: 1 addition & 2 deletions shortBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ $bdir/emsdk/upstream/emscripten/emcmake cmake $cwd/ -DCMAKE_INSTALL_PREFIX=$bdir
$bdir/emsdk/upstream/emscripten/emmake make
cat $cwd/CeresHelper.js >> $bdir/Ceres.js/Ceres.js

cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-v$VERSION.mjs
cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-latest.mjs
cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/ceres.js
#cp --verbose $bdir/Ceres.js/Ceres.wasm.map $cwd/Ceres.wasm.map

cd $cwd/app
Expand Down

0 comments on commit bc934dd

Please sign in to comment.