Skip to content

Commit

Permalink
resetCaches() + #$
Browse files Browse the repository at this point in the history
  • Loading branch information
GoToLoop committed Sep 6, 2023
1 parent 2f26342 commit 2f7da98
Show file tree
Hide file tree
Showing 20 changed files with 443 additions and 198 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
> Last updated: 16 Aug 2023
> @author Peter Lager (2023)
> Author: Peter Lager (2023)
> https://Discourse.Processing.org/t/
> sketch-tool-linking-js-files-from-another-server/42582
Expand All @@ -36,4 +36,4 @@
https://GitHub.com/GoToLoop/Complex

## Docs
https://GoToLoop.GitHub.io/Complex/docs
https://GoToLoop.GitHub.io/Complex
38 changes: 29 additions & 9 deletions complex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export default class Complex {
*/
#hashCode = null;

/**
* The cached string representation for this complex number.
* @type {string | null}
*/
#$ = null;

/**
* Create a new instance of the Complex class.
* @constructor
Expand Down Expand Up @@ -176,39 +182,39 @@ export default class Complex {
* @readonly
* @const {Complex}
*/
static ZERO = new this;
static ZERO = new this().resetCaches();

/**
* A complex number representing 1 + 0i.
* @static
* @readonly
* @const {Complex}
*/
static _1_0i = new this(1, 0);
static _1_0i = new this(1, 0).resetCaches();

/**
* A complex number representing -1 + 0i.
* @static
* @readonly
* @const {Complex}
*/
static _1_0i_neg = new this(-1, 0);
static _1_0i_neg = new this(-1, 0).resetCaches();

/**
* A complex number representing 0 + 1i.
* @static
* @readonly
* @const {Complex}
*/
static _0_1i = new this(0, 1);
static _0_1i = new this(0, 1).resetCaches();

/**
* A complex number representing 0 - 1i.
* @static
* @readonly
* @const {Complex}
*/
static _0_1i_neg = new this(0, -1);
static _0_1i_neg = new this(0, -1).resetCaches();

/**
* Pi. Ratio of the circumference of a circle to its diameter.
Expand Down Expand Up @@ -596,7 +602,7 @@ export default class Complex {
* values as this instance.
*/
clone() {
return new this.constructor(...this);
return new this.constructor(...this).resetCaches();
}

/**
Expand Down Expand Up @@ -943,15 +949,18 @@ export default class Complex {
/**
* Returns a string representation of this complex number.
* @param {number} [precision=16] Number of significant digits to display.
* @param {string} [$=''] Extra info to append to the string representation.
* @return {string} A string representation of this complex number.
*/
$(precision = 16) {
$(precision = 0, $ = '') {
if (!precision && !$ && this.#$ != null) return this.#$;

const
sgn = this.imag < 0 && '-' || '+',
rep = this.real.toPrecision(precision),
rep = this.real.toPrecision(precision ||= 16),
imp = Math.abs(this.imag).toPrecision(precision);

return `( ${rep} | ${sgn}${imp}j )`;
return this.#$ = `( ${rep} | ${sgn}${imp}j )${$}`;
}

/**
Expand All @@ -962,6 +971,17 @@ export default class Complex {
return this.$();
}

/**
* Resets to default values both #hashCode & #$ internal caches.
* @return {this} This complex number.
* @chainable
*/
resetCaches() {
this.hashCode(17);
this.$(16, '');
return this;
}

/**
* Returns the name of the constructor of this complex number.
* @return {string} The name of the constructor of this complex number.
Expand Down
8 changes: 4 additions & 4 deletions docs/jsdoc/Complex.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ <h4 class="name" id="mod"><span class="type-signature"></span>mod<span class="si

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="complex.mjs.html">complex.mjs</a>, <a href="complex.mjs.html#line995">line 995</a>
<a href="complex.mjs.html">complex.mjs</a>, <a href="complex.mjs.html#line1015">line 1015</a>
</li></ul></dd>


Expand Down Expand Up @@ -311,7 +311,7 @@ <h4 class="name" id="modSq"><span class="type-signature"></span>modSq<span class

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="complex.mjs.html">complex.mjs</a>, <a href="complex.mjs.html#line1004">line 1004</a>
<a href="complex.mjs.html">complex.mjs</a>, <a href="complex.mjs.html#line1024">line 1024</a>
</li></ul></dd>


Expand Down Expand Up @@ -379,13 +379,13 @@ <h5>Returns:</h5>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Complex.html">Complex</a></li><li><a href="module.exports_module.exports.html">exports</a></li></ul><h3>Global</h3><ul><li><a href="global.html#$">$</a></li><li><a href="global.html#_ctor">_ctor</a></li><li><a href="global.html#_isComplex">_isComplex</a></li><li><a href="global.html#_new">_new</a></li><li><a href="global.html#abs">abs</a></li><li><a href="global.html#abs_squared">abs_squared</a></li><li><a href="global.html#acos">acos</a></li><li><a href="global.html#acosh">acosh</a></li><li><a href="global.html#add">add</a></li><li><a href="global.html#arg">arg</a></li><li><a href="global.html#arrClone">arrClone</a></li><li><a href="global.html#asin">asin</a></li><li><a href="global.html#asinh">asinh</a></li><li><a href="global.html#atan">atan</a></li><li><a href="global.html#atanh">atanh</a></li><li><a href="global.html#cbrt">cbrt</a></li><li><a href="global.html#clone">clone</a></li><li><a href="global.html#compare">compare</a></li><li><a href="global.html#compareTo">compareTo</a></li><li><a href="global.html#conj">conj</a></li><li><a href="global.html#cos">cos</a></li><li><a href="global.html#cosec">cosec</a></li><li><a href="global.html#cosech">cosech</a></li><li><a href="global.html#cosh">cosh</a></li><li><a href="global.html#cot">cot</a></li><li><a href="global.html#coth">coth</a></li><li><a href="global.html#cubed">cubed</a></li><li><a href="global.html#div">div</a></li><li><a href="global.html#epsilon">epsilon</a></li><li><a href="global.html#equals">equals</a></li><li><a href="global.html#exp">exp</a></li><li><a href="global.html#filterRealRoots">filterRealRoots</a></li><li><a href="global.html#fromPolar">fromPolar</a></li><li><a href="global.html#fromRI">fromRI</a></li><li><a href="global.html#fromRandom">fromRandom</a></li><li><a href="global.html#fromZ">fromZ</a></li><li><a href="global.html#half">half</a></li><li><a href="global.html#hashCode">hashCode</a></li><li><a href="global.html#isReal">isReal</a></li><li><a href="global.html#isZero">isZero</a></li><li><a href="global.html#log">log</a></li><li><a href="global.html#mult">mult</a></li><li><a href="global.html#negate">negate</a></li><li><a href="global.html#norm">norm</a></li><li><a href="global.html#pow">pow</a></li><li><a href="global.html#print">print</a></li><li><a href="global.html#removeDuplicates">removeDuplicates</a></li><li><a href="global.html#roots">roots</a></li><li><a href="global.html#sin">sin</a></li><li><a href="global.html#sinh">sinh</a></li><li><a href="global.html#sort">sort</a></li><li><a href="global.html#sqrt">sqrt</a></li><li><a href="global.html#squared">squared</a></li><li><a href="global.html#sub">sub</a></li><li><a href="global.html#tan">tan</a></li><li><a href="global.html#tanh">tanh</a></li><li><a href="global.html#toDeg">toDeg</a></li><li><a href="global.html#toRad">toRad</a></li><li><a href="global.html#toString">toString</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Complex.html">Complex</a></li><li><a href="module.exports_module.exports.html">exports</a></li></ul><h3>Global</h3><ul><li><a href="global.html#$">$</a></li><li><a href="global.html#_ctor">_ctor</a></li><li><a href="global.html#_isComplex">_isComplex</a></li><li><a href="global.html#_new">_new</a></li><li><a href="global.html#abs">abs</a></li><li><a href="global.html#abs_squared">abs_squared</a></li><li><a href="global.html#acos">acos</a></li><li><a href="global.html#acosh">acosh</a></li><li><a href="global.html#add">add</a></li><li><a href="global.html#arg">arg</a></li><li><a href="global.html#arrClone">arrClone</a></li><li><a href="global.html#asin">asin</a></li><li><a href="global.html#asinh">asinh</a></li><li><a href="global.html#atan">atan</a></li><li><a href="global.html#atanh">atanh</a></li><li><a href="global.html#cbrt">cbrt</a></li><li><a href="global.html#clone">clone</a></li><li><a href="global.html#compare">compare</a></li><li><a href="global.html#compareTo">compareTo</a></li><li><a href="global.html#conj">conj</a></li><li><a href="global.html#cos">cos</a></li><li><a href="global.html#cosec">cosec</a></li><li><a href="global.html#cosech">cosech</a></li><li><a href="global.html#cosh">cosh</a></li><li><a href="global.html#cot">cot</a></li><li><a href="global.html#coth">coth</a></li><li><a href="global.html#cubed">cubed</a></li><li><a href="global.html#div">div</a></li><li><a href="global.html#epsilon">epsilon</a></li><li><a href="global.html#equals">equals</a></li><li><a href="global.html#exp">exp</a></li><li><a href="global.html#filterRealRoots">filterRealRoots</a></li><li><a href="global.html#fromPolar">fromPolar</a></li><li><a href="global.html#fromRI">fromRI</a></li><li><a href="global.html#fromRandom">fromRandom</a></li><li><a href="global.html#fromZ">fromZ</a></li><li><a href="global.html#half">half</a></li><li><a href="global.html#hashCode">hashCode</a></li><li><a href="global.html#isReal">isReal</a></li><li><a href="global.html#isZero">isZero</a></li><li><a href="global.html#log">log</a></li><li><a href="global.html#mult">mult</a></li><li><a href="global.html#negate">negate</a></li><li><a href="global.html#norm">norm</a></li><li><a href="global.html#pow">pow</a></li><li><a href="global.html#print">print</a></li><li><a href="global.html#removeDuplicates">removeDuplicates</a></li><li><a href="global.html#resetCaches">resetCaches</a></li><li><a href="global.html#roots">roots</a></li><li><a href="global.html#sin">sin</a></li><li><a href="global.html#sinh">sinh</a></li><li><a href="global.html#sort">sort</a></li><li><a href="global.html#sqrt">sqrt</a></li><li><a href="global.html#squared">squared</a></li><li><a href="global.html#sub">sub</a></li><li><a href="global.html#tan">tan</a></li><li><a href="global.html#tanh">tanh</a></li><li><a href="global.html#toDeg">toDeg</a></li><li><a href="global.html#toRad">toRad</a></li><li><a href="global.html#toString">toString</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Mon Sep 04 2023 23:13:01 GMT-0300 (GMT-03:00)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Sep 06 2023 04:55:19 GMT-0300 (GMT-03:00)
</footer>

<script> prettyPrint(); </script>
Expand Down
42 changes: 31 additions & 11 deletions docs/jsdoc/complex.mjs.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ <h1 class="page-title">Source: complex.mjs</h1>
*/
#hashCode = null;

/**
* The cached string representation for this complex number.
* @type {string | null}
*/
#$ = null;

/**
* Create a new instance of the Complex class.
* @constructor
Expand Down Expand Up @@ -204,39 +210,39 @@ <h1 class="page-title">Source: complex.mjs</h1>
* @readonly
* @const {Complex}
*/
static ZERO = new this;
static ZERO = new this().resetCaches();

/**
* A complex number representing 1 + 0i.
* @static
* @readonly
* @const {Complex}
*/
static _1_0i = new this(1, 0);
static _1_0i = new this(1, 0).resetCaches();

/**
* A complex number representing -1 + 0i.
* @static
* @readonly
* @const {Complex}
*/
static _1_0i_neg = new this(-1, 0);
static _1_0i_neg = new this(-1, 0).resetCaches();

/**
* A complex number representing 0 + 1i.
* @static
* @readonly
* @const {Complex}
*/
static _0_1i = new this(0, 1);
static _0_1i = new this(0, 1).resetCaches();

/**
* A complex number representing 0 - 1i.
* @static
* @readonly
* @const {Complex}
*/
static _0_1i_neg = new this(0, -1);
static _0_1i_neg = new this(0, -1).resetCaches();

/**
* Pi. Ratio of the circumference of a circle to its diameter.
Expand Down Expand Up @@ -624,7 +630,7 @@ <h1 class="page-title">Source: complex.mjs</h1>
* values as this instance.
*/
clone() {
return new this.constructor(...this);
return new this.constructor(...this).resetCaches();
}

/**
Expand Down Expand Up @@ -971,15 +977,18 @@ <h1 class="page-title">Source: complex.mjs</h1>
/**
* Returns a string representation of this complex number.
* @param {number} [precision=16] Number of significant digits to display.
* @param {string} [$=''] Extra info to append to the string representation.
* @return {string} A string representation of this complex number.
*/
$(precision = 16) {
$(precision = 0, $ = '') {
if (!precision &amp;&amp; !$ &amp;&amp; this.#$ != null) return this.#$;

const
sgn = this.imag &lt; 0 &amp;&amp; '-' || '+',
rep = this.real.toPrecision(precision),
rep = this.real.toPrecision(precision ||= 16),
imp = Math.abs(this.imag).toPrecision(precision);

return `( ${rep} | ${sgn}${imp}j )`;
return this.#$ = `( ${rep} | ${sgn}${imp}j )${$}`;
}

/**
Expand All @@ -990,6 +999,17 @@ <h1 class="page-title">Source: complex.mjs</h1>
return this.$();
}

/**
* Resets to default values both #hashCode &amp; #$ internal caches.
* @return {this} This complex number.
* @chainable
*/
resetCaches() {
this.hashCode(17);
this.$(16, '');
return this;
}

/**
* Returns the name of the constructor of this complex number.
* @return {string} The name of the constructor of this complex number.
Expand Down Expand Up @@ -1051,13 +1071,13 @@ <h1 class="page-title">Source: complex.mjs</h1>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Complex.html">Complex</a></li><li><a href="module.exports_module.exports.html">exports</a></li></ul><h3>Global</h3><ul><li><a href="global.html#$">$</a></li><li><a href="global.html#_ctor">_ctor</a></li><li><a href="global.html#_isComplex">_isComplex</a></li><li><a href="global.html#_new">_new</a></li><li><a href="global.html#abs">abs</a></li><li><a href="global.html#abs_squared">abs_squared</a></li><li><a href="global.html#acos">acos</a></li><li><a href="global.html#acosh">acosh</a></li><li><a href="global.html#add">add</a></li><li><a href="global.html#arg">arg</a></li><li><a href="global.html#arrClone">arrClone</a></li><li><a href="global.html#asin">asin</a></li><li><a href="global.html#asinh">asinh</a></li><li><a href="global.html#atan">atan</a></li><li><a href="global.html#atanh">atanh</a></li><li><a href="global.html#cbrt">cbrt</a></li><li><a href="global.html#clone">clone</a></li><li><a href="global.html#compare">compare</a></li><li><a href="global.html#compareTo">compareTo</a></li><li><a href="global.html#conj">conj</a></li><li><a href="global.html#cos">cos</a></li><li><a href="global.html#cosec">cosec</a></li><li><a href="global.html#cosech">cosech</a></li><li><a href="global.html#cosh">cosh</a></li><li><a href="global.html#cot">cot</a></li><li><a href="global.html#coth">coth</a></li><li><a href="global.html#cubed">cubed</a></li><li><a href="global.html#div">div</a></li><li><a href="global.html#epsilon">epsilon</a></li><li><a href="global.html#equals">equals</a></li><li><a href="global.html#exp">exp</a></li><li><a href="global.html#filterRealRoots">filterRealRoots</a></li><li><a href="global.html#fromPolar">fromPolar</a></li><li><a href="global.html#fromRI">fromRI</a></li><li><a href="global.html#fromRandom">fromRandom</a></li><li><a href="global.html#fromZ">fromZ</a></li><li><a href="global.html#half">half</a></li><li><a href="global.html#hashCode">hashCode</a></li><li><a href="global.html#isReal">isReal</a></li><li><a href="global.html#isZero">isZero</a></li><li><a href="global.html#log">log</a></li><li><a href="global.html#mult">mult</a></li><li><a href="global.html#negate">negate</a></li><li><a href="global.html#norm">norm</a></li><li><a href="global.html#pow">pow</a></li><li><a href="global.html#print">print</a></li><li><a href="global.html#removeDuplicates">removeDuplicates</a></li><li><a href="global.html#roots">roots</a></li><li><a href="global.html#sin">sin</a></li><li><a href="global.html#sinh">sinh</a></li><li><a href="global.html#sort">sort</a></li><li><a href="global.html#sqrt">sqrt</a></li><li><a href="global.html#squared">squared</a></li><li><a href="global.html#sub">sub</a></li><li><a href="global.html#tan">tan</a></li><li><a href="global.html#tanh">tanh</a></li><li><a href="global.html#toDeg">toDeg</a></li><li><a href="global.html#toRad">toRad</a></li><li><a href="global.html#toString">toString</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Complex.html">Complex</a></li><li><a href="module.exports_module.exports.html">exports</a></li></ul><h3>Global</h3><ul><li><a href="global.html#$">$</a></li><li><a href="global.html#_ctor">_ctor</a></li><li><a href="global.html#_isComplex">_isComplex</a></li><li><a href="global.html#_new">_new</a></li><li><a href="global.html#abs">abs</a></li><li><a href="global.html#abs_squared">abs_squared</a></li><li><a href="global.html#acos">acos</a></li><li><a href="global.html#acosh">acosh</a></li><li><a href="global.html#add">add</a></li><li><a href="global.html#arg">arg</a></li><li><a href="global.html#arrClone">arrClone</a></li><li><a href="global.html#asin">asin</a></li><li><a href="global.html#asinh">asinh</a></li><li><a href="global.html#atan">atan</a></li><li><a href="global.html#atanh">atanh</a></li><li><a href="global.html#cbrt">cbrt</a></li><li><a href="global.html#clone">clone</a></li><li><a href="global.html#compare">compare</a></li><li><a href="global.html#compareTo">compareTo</a></li><li><a href="global.html#conj">conj</a></li><li><a href="global.html#cos">cos</a></li><li><a href="global.html#cosec">cosec</a></li><li><a href="global.html#cosech">cosech</a></li><li><a href="global.html#cosh">cosh</a></li><li><a href="global.html#cot">cot</a></li><li><a href="global.html#coth">coth</a></li><li><a href="global.html#cubed">cubed</a></li><li><a href="global.html#div">div</a></li><li><a href="global.html#epsilon">epsilon</a></li><li><a href="global.html#equals">equals</a></li><li><a href="global.html#exp">exp</a></li><li><a href="global.html#filterRealRoots">filterRealRoots</a></li><li><a href="global.html#fromPolar">fromPolar</a></li><li><a href="global.html#fromRI">fromRI</a></li><li><a href="global.html#fromRandom">fromRandom</a></li><li><a href="global.html#fromZ">fromZ</a></li><li><a href="global.html#half">half</a></li><li><a href="global.html#hashCode">hashCode</a></li><li><a href="global.html#isReal">isReal</a></li><li><a href="global.html#isZero">isZero</a></li><li><a href="global.html#log">log</a></li><li><a href="global.html#mult">mult</a></li><li><a href="global.html#negate">negate</a></li><li><a href="global.html#norm">norm</a></li><li><a href="global.html#pow">pow</a></li><li><a href="global.html#print">print</a></li><li><a href="global.html#removeDuplicates">removeDuplicates</a></li><li><a href="global.html#resetCaches">resetCaches</a></li><li><a href="global.html#roots">roots</a></li><li><a href="global.html#sin">sin</a></li><li><a href="global.html#sinh">sinh</a></li><li><a href="global.html#sort">sort</a></li><li><a href="global.html#sqrt">sqrt</a></li><li><a href="global.html#squared">squared</a></li><li><a href="global.html#sub">sub</a></li><li><a href="global.html#tan">tan</a></li><li><a href="global.html#tanh">tanh</a></li><li><a href="global.html#toDeg">toDeg</a></li><li><a href="global.html#toRad">toRad</a></li><li><a href="global.html#toString">toString</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Mon Sep 04 2023 23:13:01 GMT-0300 (GMT-03:00)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Sep 06 2023 04:55:19 GMT-0300 (GMT-03:00)
</footer>

<script> prettyPrint(); </script>
Expand Down
Loading

0 comments on commit 2f7da98

Please sign in to comment.