@sc-voice/tools 3.23.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.23.0",
3
+ "version": "3.26.0",
4
4
  "description": "Utilities for SC-Voice",
5
5
  "main": "index.mjs",
6
6
  "files": [
package/src/defines.mjs CHANGED
@@ -3,6 +3,7 @@ export const DBG = {
3
3
  ALIGN_LINE: 0,
4
4
  L2T_TO_STRING: 0,
5
5
  COLOR_CONSOLE: 0,
6
+ C10E_INSPECT: 0,
6
7
  I6L_CONTAINS: 0,
7
8
  I6L_OVERLAPS: 0,
8
9
  ML_DOC_VECTORS: 0, // 'mn8:3.4',
@@ -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
+ }
@@ -1,3 +1,4 @@
1
+ import util from 'node:util';
1
2
  import { Unicode } from './unicode.mjs';
2
3
 
3
4
  const {
@@ -62,6 +63,80 @@ export class ColorConsole {
62
63
  return CC;
63
64
  }
64
65
 
66
+ static utilColor(ansiColor) {
67
+ switch (ansiColor) {
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';
102
+ }
103
+ }
104
+
105
+ writeColor(color, rest) {
106
+ let { styles, defaultOptions } = util?.inspect || {};
107
+ if (styles) {
108
+ let oldStyles = Object.assign({}, styles);
109
+ let oldColors = defaultOptions.colors;
110
+ defaultOptions.colors = true;
111
+ let valueColor = ColorConsole.utilColor(this.valueColor);
112
+ let textColor = ColorConsole.utilColor(color);
113
+ styles.bigint = valueColor;
114
+ styles.boolean = valueColor;
115
+ styles.date = valueColor;
116
+ //styles.name = textColor;
117
+ styles.module = 'underline';
118
+ styles.null = valueColor;
119
+ styles.number = valueColor;
120
+ styles.regexp = valueColor;
121
+ styles.special = valueColor;
122
+ styles.string = valueColor;
123
+ //styles.undefined = valueColor;
124
+
125
+ this.write(...this.color(color, ...rest));
126
+
127
+ Object.assign(util.inspect.styles, oldStyles);
128
+ defaultOptions.colors = oldColors;
129
+ } else {
130
+ this.write(...this.color(color, ...rest));
131
+ }
132
+ }
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
+
65
140
  valueOf(thing) {
66
141
  const msg = 'c10e.valueOf';
67
142
  let { precision } = this;
@@ -99,6 +174,7 @@ export class ColorConsole {
99
174
 
100
175
  color(textColor, ...things) {
101
176
  let { valueColor } = this;
177
+ let { styleText } = util;
102
178
  let label = '';
103
179
  let endColor = NO_COLOR;
104
180
  let eol;
@@ -106,16 +182,21 @@ export class ColorConsole {
106
182
  let newLabel = '';
107
183
  let v = this.valueOf(thing);
108
184
  let aLast = a.at(-1);
109
- if (typeof aLast === 'string' && aLast.endsWith('\n' + endColor)) {
185
+ if (
186
+ typeof aLast === 'string' &&
187
+ aLast.endsWith('\n' + endColor)
188
+ ) {
110
189
  if (aLast === textColor + '\n' + endColor) {
111
190
  a.pop();
112
191
  } else {
113
192
  const iLast = aLast.lastIndexOf('\n');
114
193
  a.pop();
115
- a.push(aLast.substring(0, iLast) + aLast.substring(iLast + 1));
194
+ a.push(
195
+ aLast.substring(0, iLast) + aLast.substring(iLast + 1),
196
+ );
116
197
  }
117
198
  v = '\n' + v;
118
- }
199
+ }
119
200
  switch (typeof thing) {
120
201
  case 'object': {
121
202
  if (
@@ -154,11 +235,11 @@ export class ColorConsole {
154
235
  }
155
236
 
156
237
  fyi1(...rest) {
157
- this.write(...this.color(this.fyiColor1, ...rest));
238
+ this.writeColor(this.fyiColor1, rest);
158
239
  }
159
240
 
160
241
  fyi2(...rest) {
161
- this.write(...this.color(this.fyiColor2, ...rest));
242
+ this.writeColor(this.fyiColor2, rest);
162
243
  }
163
244
 
164
245
  ok(...rest) {
@@ -166,11 +247,11 @@ export class ColorConsole {
166
247
  }
167
248
 
168
249
  ok1(...rest) {
169
- this.write(...this.color(this.okColor1, ...rest));
250
+ this.writeColor(this.okColor1, rest);
170
251
  }
171
252
 
172
253
  ok2(...rest) {
173
- this.write(...this.color(this.okColor2, ...rest));
254
+ this.writeColor(this.okColor2, rest);
174
255
  }
175
256
 
176
257
  bad(...rest) {
@@ -178,11 +259,11 @@ export class ColorConsole {
178
259
  }
179
260
 
180
261
  bad1(...rest) {
181
- this.write(...this.color(this.badColor1, ...rest));
262
+ this.writeColor(this.badColor1, rest);
182
263
  }
183
264
 
184
265
  bad2(...rest) {
185
- this.write(...this.color(this.badColor2, ...rest));
266
+ this.writeColor(this.badColor2, rest);
186
267
  }
187
268
 
188
269
  tag(...rest) {
@@ -190,10 +271,10 @@ export class ColorConsole {
190
271
  }
191
272
 
192
273
  tag1(...rest) {
193
- this.write(...this.color(this.tagColor1, ...rest));
274
+ this.writeColor(this.tagColor1, rest);
194
275
  }
195
276
 
196
277
  tag2(...rest) {
197
- this.write(...this.color(this.tagColor2, ...rest));
278
+ this.writeColor(this.tagColor2, rest);
198
279
  }
199
280
  }
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
  }
@@ -281,7 +281,28 @@ export class Unicode {
281
281
  BRIGHT_MAGENTA: '\u001b[95m',
282
282
  BRIGHT_CYAN: '\u001b[96m',
283
283
  BRIGHT_WHITE: '\u001b[97m',
284
- NO_COLOR: '\u001b[0m',
284
+ NO_COLOR: '\u001b[39m',
285
+ };
286
+ }
287
+
288
+ static get LINUX_STYLE() {
289
+ return {
290
+ BOLD: '\u001b[1m',
291
+ DIM: '\u001b[2m',
292
+ NO_BOLD: '\u001b[22m',
293
+ ITALIC: '\u001b[3m',
294
+ NO_ITALIC: '\u001b[23m',
295
+ UNDERLINE: '\u001b[4m',
296
+ NO_UNDERLINE: '\u001b[24m',
297
+ BLINK: '\u001b[5m',
298
+ NO_BLINK: '\u001b[25m',
299
+ INVERSE: '\u001b[7m',
300
+ NO_INVERSE: '\u001b[27m',
301
+ HIDE: '\u001b[8m',
302
+ NO_HIDE: '\u001b[28m',
303
+ STRIKETHROUGH: '\u001b[9m',
304
+ NO_STRIKETHROUGH: '\u001b[29m',
305
+ RESET: '\u001b[0m',
285
306
  };
286
307
  }
287
308