From 9375c14b1f1285b1053aa00de6eec527bdf12aeb Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 21 Jul 2025 23:09:16 +0200 Subject: [PATCH] Mon 21 Jul 22:43:21 CEST 2025 --- js/numerics/vector.js | 608 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 608 insertions(+) create mode 100644 js/numerics/vector.js diff --git a/js/numerics/vector.js b/js/numerics/vector.js new file mode 100644 index 0000000..9d3e78a --- /dev/null +++ b/js/numerics/vector.js @@ -0,0 +1,608 @@ +/** + ** ============================== + ** O O O OOOO + ** O O O O O O + ** O O O O O O + ** OOOO OOOO O OOO OOOO + ** O O O O O O O + ** O O O O O O O + ** OOOO OOOO O O OOOO + ** ============================== + ** Dr. Stefan Bosse http://www.sblab.de + ** + ** COPYRIGHT: THIS SOFTWARE, EXECUTABLE AND SOURCE CODE IS OWNED + ** BY THE AUTHOR(S). + ** THIS SOURCE CODE MAY NOT BE COPIED, EXTRACTED, + ** MODIFIED, OR OTHERWISE USED IN A CONTEXT + ** OUTSIDE OF THE SOFTWARE SYSTEM. + ** + ** $AUTHORS: Stefan Bosse + ** $INITIAL: (C) 2006-2019 bLAB + ** $CREATED: 1-1-19 by sbosse. + ** $VERSION: 1.4.4 + ** + ** $INFO: + ** + ** Vector module supporting typed and generic arrays. + ** + ** + ** $ENDOFINFO + */ + +Require('numerics/polyfill') +var sprintf = Require('com/sprintf'); + +/********** TYPEDARRY/ARRAY Extension for Matrix/Vector compatibility *************/ + +if (typeof Array.prototype.get == 'undefined') { + Object.defineProperty(Array.prototype, 'get', {value:function (i) { + return this[i]; + }, configurable: true}) + + Object.defineProperty(Array.prototype, 'set', {value: function (a,b) { + this[a]=b; + }, configurable: true}) +} + + +if (typeof Array.prototype.print == 'undefined') { + Object.defineProperty(Array.prototype, 'print', {value: function (format) { + var i,s='',sep='', columns=this.length,complex=isArray(this[0]); + if (!format) format = '%4.2f'; + for(i=0;i> 6), + 0x80 | (charcode & 0x3f)); + } + else if (charcode < 0xd800 || charcode >= 0xe000) { + utf8.push(0xe0 | (charcode >> 12), + 0x80 | ((charcode>>6) & 0x3f), + 0x80 | (charcode & 0x3f)); + } + // surrogate pair + else { + i++; + charcode = ((charcode&0x3ff)<<10)|(str.charCodeAt(i)&0x3ff) + utf8.push(0xf0 | (charcode >>18), + 0x80 | ((charcode>>12) & 0x3f), + 0x80 | ((charcode>>6) & 0x3f), + 0x80 | (charcode & 0x3f)); + } + } + return utf8; +} + +function fromUTF8Array(data) { // array of bytes + var str = '', i; + for (i = 0; i < data.length; i++) { + var value = data[i]; + if (value < 0x80) { + str += String.fromCharCode(value); + } else if (value > 0xBF && value < 0xE0) { + str += String.fromCharCode((value & 0x1F) << 6 | data[i + 1] & 0x3F); + i += 1; + } else if (value > 0xDF && value < 0xF0) { + str += String.fromCharCode((value & 0x0F) << 12 | (data[i + 1] & 0x3F) << 6 | data[i + 2] & 0x3F); + i += 2; + } else { + // surrogate pair + var charCode = ((value & 0x07) << 18 | (data[i + 1] & 0x3F) << 12 | (data[i + 2] & 0x3F) << 6 | data[i + 3] & 0x3F) - 0x010000; + + str += String.fromCharCode(charCode >> 10 | 0xD800, charCode & 0x03FF | 0xDC00); + i += 3; + } + } + return str; +} + +var complex = { + //------------------------------------------------- + // Add two complex numbers + //------------------------------------------------- + add : function (a, b) + { + return [a[0] + b[0], a[1] + b[1]]; + }, + + //------------------------------------------------- + // Subtract two complex numbers + //------------------------------------------------- + subtract : function (a, b) + { + return [a[0] - b[0], a[1] - b[1]]; + }, + + //------------------------------------------------- + // Multiply two complex numbers + // + // (a + bi) * (c + di) = (ac - bd) + (ad + bc)i + //------------------------------------------------- + multiply : function (a, b) + { + return [(a[0] * b[0] - a[1] * b[1]), + (a[0] * b[1] + a[1] * b[0])]; + }, + + //------------------------------------------------- + // Calculate |a + bi| + // + // sqrt(a*a + b*b) + //------------------------------------------------- + magnitude : function (offset,c) + { + return Math.sqrt(c[offset]*c[offset] + c[offset+1]*c[offset+1]); + }, + + phase : function (offset,c) + { + return c[offset]!=0?Math.atan(c[offset+1]/c[offset])*180/Math.PI:(c[offset+1]>0?90:-90); + } + +} + +/*********** VECTOR ************/ +function Vector(a,b) { + var self = this; + var i,columns,size,offset=0,dim=1,dtn,dt=Vector.options.dt,data; + + if (!(this instanceof Vector)) return new Vector(a,b); + var options=isObject(b)?b:{}; + + if (isNumber(a)) { + // Create a new empty vector (rows=1) + columns=a; + if (options.type) dt=options.type; + if (options.dtn) dt=options.dtn=='Array'?Array:TypedArrayOfName[options.dtn]; + size=columns; + if (options.complex) size *=2; + if (options.dtn && !dt) throw ("Vector: Unknown array type dtn="+options.dtn) + data=new dt(size); + } else if (isArray(a)) { + size=columns=a.length; + if (options.type) dt=options.type; + if (options.dtn) dt=options.dtn=='Array'?Array:TypedArrayOfName[options.dtn]; + if (options.dtn && !dt) throw ("Vector: Unknown array type dtn="+options.dtn) + if (options.dtn && options.dtn != 'Array') { + // Create typedarray from generic array + data=new dt(a); + } else { + // Matrix wrapper for generic arrays and array arrays + // modify .get .set .getRow prototype ... + // no _Matrix.call + dt=Array; + data=a; + } + } else if (isObject(a)) { + // partial object + columns=a.columns; + size=a.size||columns; + scale=options.scale; + if (options.dtn) dt=options.dtn=='Array'?Array:TypedArrayOfName[options.dtn]; + if (options.dtn && !dt) throw ("Vector: Unknown array type dtn="+options.dtn) + if (options.dtn && a.dtn != options.dtn) { + // convert dtn + if (isArray(a.data) && !scale) + data=new dt(a.data); + else { + data=new dt(size); + if (scale) for(i=0;i