@sc-voice/tools 3.24.0 → 3.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.mjs CHANGED
@@ -3,10 +3,12 @@ export const JS = {
3
3
  Assert,
4
4
  }
5
5
 
6
+ import { Activation } from './src/math/activation.mjs';
6
7
  import { Fraction } from './src/math/fraction.mjs';
7
8
  import { Interval } from './src/math/interval.mjs';
8
9
 
9
10
  export const ScvMath = {
11
+ Activation,
10
12
  Fraction,
11
13
  Interval,
12
14
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sc-voice/tools",
3
- "version": "3.24.0",
3
+ "version": "3.26.0",
4
4
  "description": "Utilities for SC-Voice",
5
5
  "main": "index.mjs",
6
6
  "files": [
@@ -0,0 +1,62 @@
1
+ export class Activation {
2
+ constructor(opts = {}) {
3
+ const msg = 'A8n.ctor';
4
+ let { a, b, c, d, fEval, dEval} = opts;
5
+ if (fEval == null) {
6
+ throw new Error(`${msg} fEval?`);
7
+ }
8
+ this.fEval = fEval;
9
+ if (dEval == null) {
10
+ throw new Error(`${msg} dEval?`);
11
+ }
12
+ this.dEval = dEval;
13
+
14
+ if (a != null) { this.a = a; }
15
+ if (b != null) { this.b = b; }
16
+ if (c != null) { this.c = c; }
17
+ if (d != null) { this.d = d; }
18
+ }
19
+
20
+ f(x) {
21
+ let { fEval, a, b, c, d } = this;
22
+ return fEval(x, a, b, c, d);
23
+ }
24
+
25
+ df(x) {
26
+ let { dEval, a, b, c, d } = this;
27
+ return dEval(x, a, b, c, d);
28
+ }
29
+
30
+ // https://en.wikipedia.org/wiki/Soboleva_modified_hyperbolic_tangent
31
+ static createSoboleva(a = 1, b = 1, c = 1, d = 1) {
32
+ const msg = 'a8n.createSoboleva';
33
+ let fEval = (x, a, b, c, d) => {
34
+ return (Math.exp(a * x) - Math.exp(-b * x)) /
35
+ (Math.exp(c * x) + Math.exp(-d * x))
36
+ }
37
+ let dEval = (x, a, b, c, d) => {
38
+ console.log(msg, "UNTESTED");
39
+ return (a*Math.exp(a * x) + b*Math.exp(-b * x)) /
40
+ (Math.exp(c * x) + Math.exp(-d * x)) -
41
+ fEval(x,a,b,c,d) *
42
+ (c*Math.exp(c * x) - d*Math.exp(-d * x)) /
43
+ (Math.exp(c * x) + Math.exp(-d * x));
44
+ }
45
+
46
+ return new Activation({ a, b, c, d, fEval, dEval });
47
+ }
48
+
49
+ static createRareN(a=100,b=1) {
50
+ let fEval = (x,a) => (x < 1 ? 1 : 1 - Math.exp(((x - a)/x) * b));
51
+ let dEval = (x,a) =>
52
+ (x < 1 ? 0 : - ( a * b * fEval(x,a,b)) / (x*x));
53
+ return new Activation({ a, b, fEval, dEval});
54
+ }
55
+
56
+ static createElu(a=0) {
57
+ let fEval = (x) => (x >= 0 ? x : (a * (Math.exp(x) - 1)));
58
+ let dEval = (x) => "TBD";
59
+ //let dEval = (x) => (x >= 0 ? 1 : fEval(x,a)+a);
60
+ return new Activation({ a, fEval, dEval });
61
+ }
62
+ }
@@ -65,23 +65,40 @@ export class ColorConsole {
65
65
 
66
66
  static utilColor(ansiColor) {
67
67
  switch (ansiColor) {
68
- case BLACK: return 'black';
69
- case WHITE: return 'white';
70
- case RED: return 'red';
71
- case GREEN: return 'green';
72
- case BLUE: return 'blue';
73
- case CYAN: return 'cyan';
74
- case MAGENTA: return 'magenta';
75
- case YELLOW: return 'yellow';
76
- case BRIGHT_BLACK: return 'blackBright';
77
- case BRIGHT_WHITE: return 'whiteBright';
78
- case BRIGHT_RED: return 'redBright';
79
- case BRIGHT_GREEN: return 'greenBright';
80
- case BRIGHT_BLUE: return 'blueBright';
81
- case BRIGHT_CYAN: return 'cyanBright';
82
- case BRIGHT_MAGENTA: return 'magentaBright';
83
- case BRIGHT_YELLOW: return 'yellowBright';
84
- case NO_COLOR: return 'noColor';
68
+ case BLACK:
69
+ return 'black';
70
+ case WHITE:
71
+ return 'white';
72
+ case RED:
73
+ return 'red';
74
+ case GREEN:
75
+ return 'green';
76
+ case BLUE:
77
+ return 'blue';
78
+ case CYAN:
79
+ return 'cyan';
80
+ case MAGENTA:
81
+ return 'magenta';
82
+ case YELLOW:
83
+ return 'yellow';
84
+ case BRIGHT_BLACK:
85
+ return 'blackBright';
86
+ case BRIGHT_WHITE:
87
+ return 'whiteBright';
88
+ case BRIGHT_RED:
89
+ return 'redBright';
90
+ case BRIGHT_GREEN:
91
+ return 'greenBright';
92
+ case BRIGHT_BLUE:
93
+ return 'blueBright';
94
+ case BRIGHT_CYAN:
95
+ return 'cyanBright';
96
+ case BRIGHT_MAGENTA:
97
+ return 'magentaBright';
98
+ case BRIGHT_YELLOW:
99
+ return 'yellowBright';
100
+ case NO_COLOR:
101
+ return 'noColor';
85
102
  }
86
103
  }
87
104
 
@@ -114,6 +131,12 @@ export class ColorConsole {
114
131
  }
115
132
  }
116
133
 
134
+ isOk(thing, tf) {
135
+ let v = this.valueOf(thing);
136
+ let color = tf ? this.okColor2 : this.badColor2;
137
+ return color + v;
138
+ }
139
+
117
140
  valueOf(thing) {
118
141
  const msg = 'c10e.valueOf';
119
142
  let { precision } = this;
@@ -159,16 +182,21 @@ export class ColorConsole {
159
182
  let newLabel = '';
160
183
  let v = this.valueOf(thing);
161
184
  let aLast = a.at(-1);
162
- if (typeof aLast === 'string' && aLast.endsWith('\n' + endColor)) {
185
+ if (
186
+ typeof aLast === 'string' &&
187
+ aLast.endsWith('\n' + endColor)
188
+ ) {
163
189
  if (aLast === textColor + '\n' + endColor) {
164
190
  a.pop();
165
191
  } else {
166
192
  const iLast = aLast.lastIndexOf('\n');
167
193
  a.pop();
168
- a.push(aLast.substring(0, iLast) + aLast.substring(iLast + 1));
194
+ a.push(
195
+ aLast.substring(0, iLast) + aLast.substring(iLast + 1),
196
+ );
169
197
  }
170
198
  v = '\n' + v;
171
- }
199
+ }
172
200
  switch (typeof thing) {
173
201
  case 'object': {
174
202
  if (
package/src/text/list.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import { ColorConsole } from './color-console.mjs';
2
2
  const { cc } = ColorConsole;
3
3
 
4
- var LIST_FACTORY_SINGLETON;
4
+ let LIST_FACTORY_SINGLETON;
5
5
 
6
6
  /* Array decorator
7
7
  */
8
8
  export class ListFactory {
9
- constructor(opts={}) {
9
+ constructor(opts = {}) {
10
10
  let {
11
11
  nColumns = 0,
12
12
  nRows = 0,
@@ -29,15 +29,15 @@ export class ListFactory {
29
29
 
30
30
  static get SINGLETON() {
31
31
  if (LIST_FACTORY_SINGLETON == null) {
32
- LIST_FACTORY_SINGLETON = new ListFactory;
32
+ LIST_FACTORY_SINGLETON = new ListFactory();
33
33
  }
34
34
  return LIST_FACTORY_SINGLETON;
35
35
  }
36
36
 
37
37
  createList(opts = {}) {
38
- let {
38
+ let {
39
39
  name, // title
40
- values=[],
40
+ values = [],
41
41
  separator = ',', // join() separator
42
42
  widths, // element string widths
43
43
  precision = this.precision, // numeric precision
@@ -70,7 +70,7 @@ export class ListFactory {
70
70
  value: () => {
71
71
  let strs = list.toStrings();
72
72
  return strs.join(list.separator);
73
- }
73
+ },
74
74
  });
75
75
  Object.defineProperty(list, 'toStrings', {
76
76
  value: () => {
@@ -95,36 +95,35 @@ export class ListFactory {
95
95
  s = JSON.stringify(v);
96
96
  }
97
97
  break;
98
- case 'number': {
99
- let sRaw = precision ? v.toFixed(precision) : v+'';
100
- let sShort = sRaw.replace(/\.?0+$/, '');
101
- s = Number(sShort) === v ? sShort : sRaw;
102
- }
103
- break;
98
+ case 'number':
99
+ {
100
+ let sRaw = precision ? v.toFixed(precision) : v + '';
101
+ let sShort = sRaw.replace(/\.?0+$/, '');
102
+ s = Number(sShort) === v ? sShort : sRaw;
103
+ }
104
+ break;
104
105
  default:
105
- s += v;
106
+ s += v;
106
107
  break;
107
108
  }
108
109
  let width = widths?.[i];
109
110
  if (width) {
110
- s = s.substring(0,width).padEnd(width);
111
+ s = s.substring(0, width).padEnd(width);
111
112
  }
112
113
  s5s.push(s);
113
114
  }
114
115
 
115
-
116
116
  return s5s;
117
- }
118
-
117
+ },
119
118
  });
120
119
 
121
120
  return list;
122
121
  }
123
122
 
124
123
  createColumn(opts = {}) {
125
- let {
126
- name,
127
- values=[],
124
+ let {
125
+ name,
126
+ values = [],
128
127
  separator = '\n',
129
128
  precision = this.precision,
130
129
  } = opts;
@@ -134,17 +133,17 @@ export class ListFactory {
134
133
  name = 'column' + this.nColumns;
135
134
  }
136
135
  return this.createList({
137
- name,
136
+ name,
138
137
  precision,
139
- separator,
140
- values,
138
+ separator,
139
+ values,
141
140
  });
142
141
  }
143
142
 
144
143
  createRow(opts = {}) {
145
- let {
146
- name,
147
- values=[],
144
+ let {
145
+ name,
146
+ values = [],
148
147
  separator = '\t',
149
148
  widths,
150
149
  precision = this.precision,
@@ -155,10 +154,10 @@ export class ListFactory {
155
154
  name = 'row' + this.nRows;
156
155
  }
157
156
  return this.createList({
158
- name,
157
+ name,
159
158
  precision,
160
- separator,
161
- values,
159
+ separator,
160
+ values,
162
161
  widths,
163
162
  });
164
163
  }
@@ -176,12 +175,13 @@ export class ListFactory {
176
175
  precision = this.precision,
177
176
  } = opts;
178
177
 
179
- let singleList = this.createColumn({
180
- name,
181
- separator:rowSeparator,
178
+ let singleList = this.createColumn({
179
+ name,
180
+ separator: rowSeparator,
182
181
  precision,
183
182
  });
184
- let newRow = (separator) => this.createRow({ separator, precision });
183
+ let newRow = (separator) =>
184
+ this.createRow({ separator, precision });
185
185
  name = name || singleList.name;
186
186
  switch (order) {
187
187
  case 'col-major':
@@ -251,20 +251,23 @@ export class ListFactory {
251
251
 
252
252
  // compute row element widths
253
253
  let widths = new Array(maxValues).fill(0);
254
- singleList.forEach(row => {
254
+ singleList.forEach((row) => {
255
255
  let strs = row.toStrings();
256
- strs.map((s,i)=> {
256
+ strs.map((s, i) => {
257
257
  widths[i] = Math.max(s.length, widths[i]);
258
258
  });
259
259
  });
260
- singleList.forEach(row => row.widths = widths);
261
- dbg && cc.ok1(msg+1, widths[0], maxValues, 'widths:', widths);
260
+ singleList.forEach((row) => {
261
+ row.widths = widths;
262
+ });
263
+ dbg && cc.ok1(msg + 1, widths[0], maxValues, 'widths:', widths);
262
264
 
263
265
  return singleList;
264
266
  }
265
267
  }
266
268
 
267
- export class List { // namespace wrapper for ListFactory
269
+ export class List {
270
+ // namespace wrapper for ListFactory
268
271
  static createColumn(opts = {}) {
269
272
  return ListFactory.SINGLETON.createColumn(opts);
270
273
  }
@@ -274,6 +277,6 @@ export class List { // namespace wrapper for ListFactory
274
277
  }
275
278
 
276
279
  static wrapList(list, opts = {}) {
277
- return ListFactory.SINGLETON.wrapList(list, opts);
280
+ return ListFactory.SINGLETON.wrapList(list, opts);
278
281
  }
279
282
  }
@@ -303,7 +303,7 @@ export class Unicode {
303
303
  STRIKETHROUGH: '\u001b[9m',
304
304
  NO_STRIKETHROUGH: '\u001b[29m',
305
305
  RESET: '\u001b[0m',
306
- }
306
+ };
307
307
  }
308
308
 
309
309
  constructor(opts = {}) {