From 02bc717453961545dcd4e17982d040bf85a1dbad Mon Sep 17 00:00:00 2001 From: miner Date: Sat, 20 Jan 2024 11:55:48 -0500 Subject: [PATCH] modified: CeresHelper.js modified: dist/ceres.js modified: docs/index.html --- CeresHelper.js | 117 +++--------------------------------------------- dist/ceres.js | 117 +++--------------------------------------------- docs/index.html | 115 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 125 insertions(+), 224 deletions(-) diff --git a/CeresHelper.js b/CeresHelper.js index fe0dc26..29054e4 100644 --- a/CeresHelper.js +++ b/CeresHelper.js @@ -131,6 +131,12 @@ export class Ceres { } static sanitizeInput(mathExpression) { + if (mathExpression.indexOf('=') > -1) + { + let m_split = mathExpression.split("=") + mathExpression = m_split[0]+"-("+m_split[1]+")" + } + const validExpression = /^[0-9\s+\-/*()\[\]^sincoxetalaqrtPi.E]*$/; const validStructure = /^[\d\s+\-/*()\[\]^]*$/; @@ -201,116 +207,7 @@ export class Ceres { let max_trust_region_radius = (isNumber(jsonSystem.max_trust_region_radius)) ? jsonSystem.max_trust_region_radius : 1e16; let max_num_consecutive_invalid_steps = (isNumber(jsonSystem.max_num_consecutive_invalid_steps)) ? jsonSystem.max_num_consecutive_invalid_steps : 5; const results = await this.solve(initial_guess, max_numb_iterations, parameter_tolerance, function_tolerance, gradient_tolerance, max_solver_time_in_seconds, initial_trust_region_radius, max_trust_region_radius, max_num_consecutive_invalid_steps); - results.report = jsonFunctions.map(i => 'Function: ' + i + ' = 0\n')+"\n\n"+results.report + results.report = jsonFunctions.map(i => 'Function: ' + i + ' = 0\n').join("\r\n")+"\n\n"+results.report return results; } } - -/* -class CeresGrad { - constructor() { - this.loaded = false - this.fxn = [] - this.lowerbound = [] - this.upperbound = [] - this.callback = [] - - // Create example data to test float_multiply_array - this.varLength = 0 - this.maxLength = 1000 - this.data = new Float64Array(this.maxLength); - - this.promise = new Promise(function(resolve, reject){ - CeresModule().then(function(Module){ - this.instance = new Module.CeresjsGrad - - // Get data byte size, allocate memory on Emscripten heap, and get pointer - let nDataBytes = this.data.length * this.data.BYTES_PER_ELEMENT; - let dataPtr = Module._malloc(nDataBytes); - - // Copy data to Emscripten heap (directly accessed from Module.HEAPU8) - this.dataHeap = new Float64Array(Module.HEAPF64.buffer, dataPtr, nDataBytes); - this.dataHeap.set(new Float64Array(this.data.buffer)); - this.loaded = true - resolve() - }.bind(this)) - }.bind(this)) - } - // Method - add_function(fn) { - this.fxn.push(fn) - } - // Method - add_lowerbound(xNumber, lowerBound) { - this.lowerbound.push([xNumber, lowerBound]) - } - // Method - add_upperbound(xNumber, upperBound) { - this.upperbound.push([xNumber, upperBound]) - } - // Method - add_callback(fn) { - this.callback.push(fn) - } - reset(){ - this.instance.reset(); - this.fxn = [] - this.lowerbound = [] - this.upperbound = [] - this.callback = [] - } - //Method - load_fxns(){ - for(let i = 0; i < this.fxn.length; i++){ - let newfunc = function f(){ - let x = new Float64Array(this.dataHeap.buffer, this.dataHeap.byteOffset, this.varLength); - return this.fxn[i](x) - } - this.instance.add_function(newfunc.bind(this)); - } - for(let i = 0; i < this.lowerbound.length; i++){ - this.instance.add_lowerbound(this.lowerbound[i][0], this.lowerbound[i][1]); - } - for(let i = 0; i < this.upperbound.length; i++){ - this.instance.add_upperbound(this.upperbound[i][0], this.upperbound[i][1]); - } - for(let i = 0; i < this.callback.length; i++){ - let newfunc = function f(evaluate_jacobians, new_evaluation_point){ - let x = new Float64Array(this.dataHeap.buffer, this.dataHeap.byteOffset, this.varLength); - return this.callback[i](x, evaluate_jacobians, new_evaluation_point); - } - this.instance.add_callback(newfunc.bind(this)); - } - } - // Method - solve(xi, max_numb_iterations = 2000, parameter_tolerance = 1e-10, function_tolerance = 1e-16, gradient_tolerance = 1e-16, max_solver_time_in_seconds = 100) { - if(this.loaded == true){ - this.varLength = xi.length - this.load_fxns() - - for(let i = 0; i < xi.length; i++){ - this.dataHeap[i] = xi[i]; - } - this.instance.setup_x(this.dataHeap.byteOffset, this.varLength); - - let success = this.instance.solve(max_numb_iterations, parameter_tolerance, function_tolerance, gradient_tolerance, max_solver_time_in_seconds); - let report = this.instance.get_report(); - let message = this.instance.get_message(); - - let x = new Float64Array(this.dataHeap.buffer, this.dataHeap.byteOffset, this.varLength) - let normalArray = [].slice.call(x); - let txt = ""; - for(let i=0; i 'Function: ' + i + ' = 0\n').join("\r\n")+"\n\n"+results.report return results; } } - -/* -class CeresGrad { - constructor() { - this.loaded = false - this.fxn = [] - this.lowerbound = [] - this.upperbound = [] - this.callback = [] - - // Create example data to test float_multiply_array - this.varLength = 0 - this.maxLength = 1000 - this.data = new Float64Array(this.maxLength); - - this.promise = new Promise(function(resolve, reject){ - CeresModule().then(function(Module){ - this.instance = new Module.CeresjsGrad - - // Get data byte size, allocate memory on Emscripten heap, and get pointer - let nDataBytes = this.data.length * this.data.BYTES_PER_ELEMENT; - let dataPtr = Module._malloc(nDataBytes); - - // Copy data to Emscripten heap (directly accessed from Module.HEAPU8) - this.dataHeap = new Float64Array(Module.HEAPF64.buffer, dataPtr, nDataBytes); - this.dataHeap.set(new Float64Array(this.data.buffer)); - this.loaded = true - resolve() - }.bind(this)) - }.bind(this)) - } - // Method - add_function(fn) { - this.fxn.push(fn) - } - // Method - add_lowerbound(xNumber, lowerBound) { - this.lowerbound.push([xNumber, lowerBound]) - } - // Method - add_upperbound(xNumber, upperBound) { - this.upperbound.push([xNumber, upperBound]) - } - // Method - add_callback(fn) { - this.callback.push(fn) - } - reset(){ - this.instance.reset(); - this.fxn = [] - this.lowerbound = [] - this.upperbound = [] - this.callback = [] - } - //Method - load_fxns(){ - for(let i = 0; i < this.fxn.length; i++){ - let newfunc = function f(){ - let x = new Float64Array(this.dataHeap.buffer, this.dataHeap.byteOffset, this.varLength); - return this.fxn[i](x) - } - this.instance.add_function(newfunc.bind(this)); - } - for(let i = 0; i < this.lowerbound.length; i++){ - this.instance.add_lowerbound(this.lowerbound[i][0], this.lowerbound[i][1]); - } - for(let i = 0; i < this.upperbound.length; i++){ - this.instance.add_upperbound(this.upperbound[i][0], this.upperbound[i][1]); - } - for(let i = 0; i < this.callback.length; i++){ - let newfunc = function f(evaluate_jacobians, new_evaluation_point){ - let x = new Float64Array(this.dataHeap.buffer, this.dataHeap.byteOffset, this.varLength); - return this.callback[i](x, evaluate_jacobians, new_evaluation_point); - } - this.instance.add_callback(newfunc.bind(this)); - } - } - // Method - solve(xi, max_numb_iterations = 2000, parameter_tolerance = 1e-10, function_tolerance = 1e-16, gradient_tolerance = 1e-16, max_solver_time_in_seconds = 100) { - if(this.loaded == true){ - this.varLength = xi.length - this.load_fxns() - - for(let i = 0; i < xi.length; i++){ - this.dataHeap[i] = xi[i]; - } - this.instance.setup_x(this.dataHeap.byteOffset, this.varLength); - - let success = this.instance.solve(max_numb_iterations, parameter_tolerance, function_tolerance, gradient_tolerance, max_solver_time_in_seconds); - let report = this.instance.get_report(); - let message = this.instance.get_message(); - - let x = new Float64Array(this.dataHeap.buffer, this.dataHeap.byteOffset, this.varLength) - let normalArray = [].slice.call(x); - let txt = ""; - for(let i=0; iNumerical Optimization Library in JavaScript About | Examples | Documentation | - Contact + Generate Code
@@ -271,9 +271,116 @@

Class: Ceres

-
-

Contact

- +
+

Code Generator

+
+ +

+ + y=2 \cdot x^2-8 \cdot x-3 +

+ +

+ + y = x+2 +

+ +

+ + x = ??; Guess = 4 +

+ +

+ + y = ??; Guess = 1 +

+ + + + + + +
+ + +
+ +