diff --git a/packages/dispatcher/package.json b/packages/dispatcher/package.json index 5975f67a6..715945ce0 100644 --- a/packages/dispatcher/package.json +++ b/packages/dispatcher/package.json @@ -4,7 +4,7 @@ "version": "1.0.0", "author": "Microsoft", "bugs": "https://github.com/microsoft/botframework-cli/issues", - "main": "lib/src/index.js", + "main": "lib/index.js", "dependencies": { "@microsoft/bf-lu": "1.0.0", "@oclif/command": "~1.5.19", diff --git a/packages/dispatcher/src/data/AppColumnarData.ts b/packages/dispatcher/src/data/AppColumnarData.ts index 5bd688852..2f42e2226 100644 --- a/packages/dispatcher/src/data/AppColumnarData.ts +++ b/packages/dispatcher/src/data/AppColumnarData.ts @@ -77,9 +77,9 @@ export function exampleFunctionData(): ColumnarData { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/data/AppEntityAnnotatedCorpusData.ts b/packages/dispatcher/src/data/AppEntityAnnotatedCorpusData.ts index adab30568..75436587a 100644 --- a/packages/dispatcher/src/data/AppEntityAnnotatedCorpusData.ts +++ b/packages/dispatcher/src/data/AppEntityAnnotatedCorpusData.ts @@ -53,9 +53,9 @@ export function exampleFunctionData(): EntityAnnotatedCorpusData { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/data/AppLuData.ts b/packages/dispatcher/src/data/AppLuData.ts index 7c704b8d8..6fb4325ac 100644 --- a/packages/dispatcher/src/data/AppLuData.ts +++ b/packages/dispatcher/src/data/AppLuData.ts @@ -43,9 +43,9 @@ export async function exampleFunctionData(): Promise { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/data/data.ts b/packages/dispatcher/src/data/data.ts index fde0229e1..2750456d2 100644 --- a/packages/dispatcher/src/data/data.ts +++ b/packages/dispatcher/src/data/data.ts @@ -290,13 +290,13 @@ export abstract class Data { public getFeaturizerLabels(): string[] { return this.getFeaturizer().getLabels(); } - public getFeaturizerLabelMap(): { [id: string]: number; } { + public getFeaturizerLabelMap(): { [id: string]: number } { return this.getFeaturizer().getLabelMap(); } public getFeaturizerFeatures(): string[] { return this.getFeaturizer().getFeatures(); } - public getFeaturizerFeatureMap(): { [id: string]: number; } { + public getFeaturizerFeatureMap(): { [id: string]: number } { return this.getFeaturizer().getFeatureMap(); } diff --git a/packages/dispatcher/src/data_structure/DictionaryMapUtility.ts b/packages/dispatcher/src/data_structure/DictionaryMapUtility.ts index ea3ee7519..d716c612e 100644 --- a/packages/dispatcher/src/data_structure/DictionaryMapUtility.ts +++ b/packages/dispatcher/src/data_structure/DictionaryMapUtility.ts @@ -6,24 +6,283 @@ import { TMapGenericKeyGenericArray } from "./TMapGenericKeyGenericArray"; import { TMapGenericKeyGenericArrays } from "./TMapGenericKeyGenericArrays"; import { TMapGenericKeyGenericValue } from "./TMapGenericKeyGenericValue"; +import { TMapGenericKeyGenericSet } from "./TMapGenericKeyGenericSet"; import { TMapStringKeyGenericArray } from "./TMapStringKeyGenericArray"; import { TMapStringKeyGenericArrays } from "./TMapStringKeyGenericArrays"; import { TMapStringKeyGenericValue } from "./TMapStringKeyGenericValue"; +import { TMapStringKeyGenericSet } from "./TMapStringKeyGenericSet"; import { TMapNumberKeyGenericArray } from "./TMapNumberKeyGenericArray"; import { TMapNumberKeyGenericArrays } from "./TMapNumberKeyGenericArrays"; import { TMapNumberKeyGenericValue } from "./TMapNumberKeyGenericValue"; +import { TMapNumberKeyGenericSet } from "./TMapNumberKeyGenericSet"; import { IDictionaryNumberIdGenericArray } from "../data_structure/IDictionaryNumberIdGenericArray"; import { IDictionaryNumberIdGenericArrays } from "../data_structure/IDictionaryNumberIdGenericArrays"; import { IDictionaryNumberIdGenericValue } from "../data_structure/IDictionaryNumberIdGenericValue"; +import { IDictionaryNumberIdGenericSet } from "../data_structure/IDictionaryNumberIdGenericSet"; import { IDictionaryStringIdGenericArray } from "../data_structure/IDictionaryStringIdGenericArray"; import { IDictionaryStringIdGenericArrays } from "../data_structure/IDictionaryStringIdGenericArrays"; import { IDictionaryStringIdGenericValue } from "../data_structure/IDictionaryStringIdGenericValue"; +import { IDictionaryStringIdGenericSet } from "../data_structure/IDictionaryStringIdGenericSet"; import { Utility } from "../utility/Utility"; export class DictionaryMapUtility { + public static convertStringKeyGenericSetNativeMapToDictionary( + stringKeyGenericSetMap: Map>): { [id: string]: Set } { + const stringIdGenericSetDictionary: { [id: string]: Set } = {}; + for (const key in stringKeyGenericSetMap) { + if (key) { + const value: Set | undefined = stringKeyGenericSetMap.get(key); + stringIdGenericSetDictionary[key] = value as Set; + } + } + return stringIdGenericSetDictionary; + } + public static convertStringKeyGenericValueNativeMapToDictionary( + stringKeyGenericValueMap: Map): { [id: string]: T } { + const stringIdGenericValueDictionary: { [id: string]: T } = {}; + for (const key in stringKeyGenericValueMap) { + if (key) { + const value: T | undefined = stringKeyGenericValueMap.get(key); + stringIdGenericValueDictionary[key] = value as T; + } + } + return stringIdGenericValueDictionary; + } + public static convertNumberKeyGenericSetNativeMapToDictionary( + numberKeyGenericSetMap: Map>): { [id: number]: Set } { + const numberIdGenericSetDictionary: { [id: number]: Set } = {}; + for (const key in numberKeyGenericSetMap) { + if (key) { + // ---- key is already a number, tslint is mistaken that it's a string + const keyInNumber: number = Number(key); + const value: Set | undefined = numberKeyGenericSetMap.get(keyInNumber); + numberIdGenericSetDictionary[keyInNumber] = value as Set; + } + } + return numberIdGenericSetDictionary; + } + public static convertNumberKeyGenericValueNativeMapToDictionary( + numberKeyGenericValueMap: Map): { [id: number]: T } { + const numberIdGenericValueDictionary: { [id: number]: T } = {}; + for (const key in numberKeyGenericValueMap) { + if (key) { + // ---- key is already a number, tslint is mistaken that it's a string + const keyInNumber: number = Number(key); + const value: T | undefined = numberKeyGenericValueMap.get(keyInNumber); + numberIdGenericValueDictionary[keyInNumber] = value as T; + } + } + return numberIdGenericValueDictionary; + } + + public static convertStringKeyGenericSetMapToDictionary( + stringKeyGenericSetMap: TMapStringKeyGenericSet): IDictionaryStringIdGenericSet { + const stringIdGenericSetDictionary: IDictionaryStringIdGenericSet = {}; + for (const key in stringKeyGenericSetMap) { + if (key) { + const value: Set | undefined = stringKeyGenericSetMap.get(key); + stringIdGenericSetDictionary[key] = value as Set; + } + } + return stringIdGenericSetDictionary; + } + public static convertStringKeyGenericValueMapToDictionary( + stringKeyGenericValueMap: TMapStringKeyGenericValue): IDictionaryStringIdGenericValue { + const stringIdGenericValueDictionary: IDictionaryStringIdGenericValue = {}; + for (const key in stringKeyGenericValueMap) { + if (key) { + const value: T | undefined = stringKeyGenericValueMap.get(key); + stringIdGenericValueDictionary[key] = value as T; + } + } + return stringIdGenericValueDictionary; + } + public static convertNumberKeyGenericSetMapToDictionary( + numberKeyGenericSetMap: TMapNumberKeyGenericSet): IDictionaryNumberIdGenericSet { + const numberIdGenericSetDictionary: IDictionaryNumberIdGenericSet = {}; + for (const key in numberKeyGenericSetMap) { + if (key) { + // ---- key is already a number, tslint is mistaken that it's a string + const keyInNumber: number = Number(key); + const value: Set | undefined = numberKeyGenericSetMap.get(keyInNumber); + numberIdGenericSetDictionary[keyInNumber] = value as Set; + } + } + return numberIdGenericSetDictionary; + } + public static convertNumberKeyGenericValueMapToDictionary( + numberKeyGenericValueMap: TMapNumberKeyGenericValue): IDictionaryNumberIdGenericValue { + const numberIdGenericValueDictionary: IDictionaryNumberIdGenericValue = {}; + for (const key in numberKeyGenericValueMap) { + if (key) { + // ---- key is already a number, tslint is mistaken that it's a string + const keyInNumber: number = Number(key); + const value: T | undefined = numberKeyGenericValueMap.get(keyInNumber); + numberIdGenericValueDictionary[keyInNumber] = value as T; + } + } + return numberIdGenericValueDictionary; + } + + public static insertStringPairToStringIdStringSetNativeDictionary( + key: string, + value: string, + stringIdStringSetDictionary: { [id: string]: Set }): { [id: string]: Set } { + if (!stringIdStringSetDictionary) { + stringIdStringSetDictionary = {}; + } + if (key in stringIdStringSetDictionary) { + const stringSet: Set = stringIdStringSetDictionary[key]; + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + stringIdStringSetDictionary[key] = stringSet; + stringSet.add(value); + } + return stringIdStringSetDictionary; + } + public static insertNumberStringPairToNumberIdStringSetNativeDictionary( + key: number, + value: string, + numberIdStringSetDictionary: { [id: number]: Set }): { [id: number]: Set } { + if (!numberIdStringSetDictionary) { + numberIdStringSetDictionary = {}; + } + if (key in numberIdStringSetDictionary) { + const stringSet: Set = numberIdStringSetDictionary[key]; + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + numberIdStringSetDictionary[key] = stringSet; + stringSet.add(value); + } + return numberIdStringSetDictionary; + } + + public static insertStringPairToStringIdStringSetDictionary( + key: string, + value: string, + stringIdStringSetDictionary: IDictionaryStringIdGenericSet): IDictionaryStringIdGenericSet { + if (DictionaryMapUtility.isEmptyStringIdGenericSetDictionary(stringIdStringSetDictionary)) { + stringIdStringSetDictionary = {}; + } + if (key in stringIdStringSetDictionary) { + const stringSet: Set = stringIdStringSetDictionary[key]; + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + stringIdStringSetDictionary[key] = stringSet; + stringSet.add(value); + } + return stringIdStringSetDictionary; + } + public static insertNumberStringPairToNumberIdStringSetDictionary( + key: number, + value: string, + numberIdStringSetDictionary: IDictionaryNumberIdGenericSet): IDictionaryNumberIdGenericSet { + if (DictionaryMapUtility.isEmptyNumberIdGenericSetDictionary(numberIdStringSetDictionary)) { + numberIdStringSetDictionary = {}; + } + if (key in numberIdStringSetDictionary) { + const stringSet: Set = numberIdStringSetDictionary[key]; + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + numberIdStringSetDictionary[key] = stringSet; + stringSet.add(value); + } + return numberIdStringSetDictionary; + } + + public static insertStringPairToStringIdStringSetNativeMap( + key: string, + value: string, + stringKeyStringSetMap: Map>): Map> { + if (!stringKeyStringSetMap) { + stringKeyStringSetMap = new Map>(); + } + if (key in stringKeyStringSetMap) { + let stringSet: Set | undefined = stringKeyStringSetMap.get(key); + if (!stringSet) { + stringSet = new Set(); + stringKeyStringSetMap.set(key, stringSet); + } + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + stringKeyStringSetMap.set(key, stringSet); + stringSet.add(value); + } + return stringKeyStringSetMap; + } + public static insertNumberStringPairToNumberIdStringSetNativeMap( + key: number, + value: string, + numberKeyStringSetMap: Map>): Map> { + if (!numberKeyStringSetMap) { + numberKeyStringSetMap = new Map>(); + } + if (key in numberKeyStringSetMap) { + let stringSet: Set | undefined = numberKeyStringSetMap.get(key); + if (!stringSet) { + stringSet = new Set(); + numberKeyStringSetMap.set(key, stringSet); + } + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + numberKeyStringSetMap.set(key, stringSet); + stringSet.add(value); + } + return numberKeyStringSetMap; + } + + public static insertStringPairToStringIdStringSetMap( + key: string, + value: string, + stringKeyStringSetMap: TMapStringKeyGenericSet): TMapStringKeyGenericSet { + if (DictionaryMapUtility.isEmptyStringKeyGenericSetMap(stringKeyStringSetMap)) { + stringKeyStringSetMap = DictionaryMapUtility.newTMapStringKeyGenericSet(); + } + if (key in stringKeyStringSetMap) { + let stringSet: Set | undefined = stringKeyStringSetMap.get(key); + if (!stringSet) { + stringSet = new Set(); + stringKeyStringSetMap.set(key, stringSet); + } + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + stringKeyStringSetMap.set(key, stringSet); + stringSet.add(value); + } + return stringKeyStringSetMap; + } + public static insertNumberStringPairToNumberIdStringSetMap( + key: number, + value: string, + numberKeyStringSetMap: TMapNumberKeyGenericSet): TMapNumberKeyGenericSet { + if (DictionaryMapUtility.isEmptyNumberKeyGenericSetMap(numberKeyStringSetMap)) { + numberKeyStringSetMap = DictionaryMapUtility.newTMapNumberKeyGenericSet(); + } + if (key in numberKeyStringSetMap) { + let stringSet: Set | undefined = numberKeyStringSetMap.get(key); + if (!stringSet) { + stringSet = new Set(); + numberKeyStringSetMap.set(key, stringSet); + } + stringSet.add(value); + } else { + const stringSet: Set = new Set(); + numberKeyStringSetMap.set(key, stringSet); + stringSet.add(value); + } + return numberKeyStringSetMap; + } + public static buildStringIdNumberValueDictionaryFromUniqueStringArrayFile( filename: string, delimiter: string = "\t"): { @@ -116,10 +375,9 @@ export class DictionaryMapUtility { DictionaryMapUtility.getStringIdGenericValueDictionaryLength(stringIdNumberValueDictionary)) { if (throwIfNotLegal) { throw new Error( - "stringArray.length|" + - stringIdNumberValueDictionary.length + - "| !== stringIdNumberValueDictionary.length|" + - DictionaryMapUtility.getStringIdGenericValueDictionaryLength(stringIdNumberValueDictionary) + "|"); + `stringArray.length|${stringArray.length}|` + + "!==stringIdNumberValueDictionary.length" + + `|${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(stringIdNumberValueDictionary)}|`); } return false; } @@ -130,14 +388,14 @@ export class DictionaryMapUtility { const keyId: number = stringIdNumberValueDictionary[key]; if ((keyId < 0) || (keyId > stringArray.length)) { if (throwIfNotLegal) { - throw new Error("(keyId<0)||(keyId>stringArray.length)"); + throw new Error(`(keyId<0)||(keyId|${keyId}|>stringArray.length|${stringArray.length}|)`); } return false; } const keyRetrieved = stringArray[keyId]; if (key !== keyRetrieved) { if (throwIfNotLegal) { - throw new Error("key!==keyRetrieved"); + throw new Error(`key|${key}|!==keyRetrieved|${keyRetrieved}|`); } return false; } @@ -336,7 +594,7 @@ export class DictionaryMapUtility { } else { if (throwIfNotLegal) { Utility.debuggingThrow( - `key=${key}, not int the map: ${Utility.mapToJsonSerialization(stringKeyNumberValueMap)}`); + `key=${key}, not in the map: ${Utility.mapToJsonSerialization(stringKeyNumberValueMap)}`); } return false; } @@ -500,18 +758,18 @@ export class DictionaryMapUtility { stringIdNumberValueDictionaryFirst: IDictionaryStringIdGenericValue, stringIdNumberValueDictionarySecond: IDictionaryStringIdGenericValue, throwIfNotLegal: boolean = true): boolean { - if ((stringIdNumberValueDictionaryFirst == null) && (stringIdNumberValueDictionarySecond == null)) { + if ((stringIdNumberValueDictionaryFirst === null) && (stringIdNumberValueDictionarySecond === null)) { return true; } - if (stringIdNumberValueDictionaryFirst == null) { + if (stringIdNumberValueDictionaryFirst === null) { if (throwIfNotLegal) { - Utility.debuggingThrow("stringKeyNumberValueDictionaryFirst==null"); + Utility.debuggingThrow("stringIdNumberValueDictionaryFirst==null"); } return false; } - if (stringIdNumberValueDictionarySecond == null) { + if (stringIdNumberValueDictionarySecond === null) { if (throwIfNotLegal) { - Utility.debuggingThrow("stringKeyNumberValueDictionarySecond==null"); + Utility.debuggingThrow("stringIdNumberValueDictionarySecond==null"); } return false; } @@ -806,6 +1064,11 @@ export class DictionaryMapUtility { return true; } + public static isEmptyStringIdGenericSetDictionary( + stringIdGenericSetMap: IDictionaryStringIdGenericSet): boolean { + return !(stringIdGenericSetMap && + DictionaryMapUtility.getStringIdGenericSetDictionaryLength(stringIdGenericSetMap) > 0); + } public static isEmptyStringIdGenericValueDictionary( stringIdGenericValueMap: IDictionaryStringIdGenericValue): boolean { return !(stringIdGenericValueMap && @@ -821,6 +1084,11 @@ export class DictionaryMapUtility { return !(stringIdGenericArraysMap && DictionaryMapUtility.getStringIdGenericArraysDictionaryLength(stringIdGenericArraysMap) > 0); } + public static isEmptyNumberIdGenericSetDictionary( + numberIdGenericSetMap: IDictionaryNumberIdGenericSet): boolean { + return !(numberIdGenericSetMap && + DictionaryMapUtility.getNumberIdGenericSetDictionaryLength(numberIdGenericSetMap) > 0); + } public static isEmptyNumberIdGenericValueDictionary( numberIdGenericValueMap: IDictionaryNumberIdGenericValue): boolean { return !(numberIdGenericValueMap && @@ -837,6 +1105,11 @@ export class DictionaryMapUtility { DictionaryMapUtility.getNumberIdGenericArraysDictionaryLength(numberIdGenericArraysMap) > 0); } + public static isEmptyGenericKeyGenericSetMap( + genericKeyGenericSetMap: TMapGenericKeyGenericSet): boolean { + return !(genericKeyGenericSetMap && + DictionaryMapUtility.getGenericKeyGenericSetMapLength(genericKeyGenericSetMap) > 0); + } public static isEmptyGenericKeyGenericValueMap( genericKeyGenericValueMap: TMapGenericKeyGenericValue): boolean { return !(genericKeyGenericValueMap && @@ -852,6 +1125,11 @@ export class DictionaryMapUtility { return !(genericKeyGenericArraysMap && DictionaryMapUtility.getGenericKeyGenericArraysMapLength(genericKeyGenericArraysMap) > 0); } + public static isEmptyStringKeyGenericSetMap( + stringKeyGenericSetMap: TMapStringKeyGenericSet): boolean { + return !(stringKeyGenericSetMap && + DictionaryMapUtility.getStringKeyGenericSetMapLength(stringKeyGenericSetMap) > 0); + } public static isEmptyStringKeyGenericValueMap( stringKeyGenericValueMap: TMapStringKeyGenericValue): boolean { return !(stringKeyGenericValueMap && @@ -867,6 +1145,11 @@ export class DictionaryMapUtility { return !(stringKeyGenericArraysMap && DictionaryMapUtility.getStringKeyGenericArraysMapLength(stringKeyGenericArraysMap) > 0); } + public static isEmptyNumberKeyGenericSetMap( + numberKeyGenericSetMap: TMapNumberKeyGenericSet): boolean { + return !(numberKeyGenericSetMap && + DictionaryMapUtility.getNumberKeyGenericSetMapLength(numberKeyGenericSetMap) > 0); + } public static isEmptyNumberKeyGenericValueMap( numberKeyGenericValueMap: TMapNumberKeyGenericValue): boolean { return !(numberKeyGenericValueMap && @@ -883,6 +1166,12 @@ export class DictionaryMapUtility { DictionaryMapUtility.getNumberKeyGenericArraysMapLength(numberKeyGenericArraysMap) > 0); } + public static getStringIdGenericSetDictionaryLength(map: IDictionaryStringIdGenericSet): number { + return (Object.keys(map).length); + } + public static getNumberIdGenericSetDictionaryLength(map: IDictionaryNumberIdGenericSet): number { + return (Object.keys(map).length); + } public static getStringIdGenericValueDictionaryLength(map: IDictionaryStringIdGenericValue): number { return (Object.keys(map).length); } @@ -902,6 +1191,15 @@ export class DictionaryMapUtility { return (Object.keys(map).length); } + public static getGenericKeyGenericSetMapLength(map: TMapGenericKeyGenericSet): number { + return [...map].length; + } + public static getStringKeyGenericSetMapLength(map: TMapStringKeyGenericSet): number { + return [...map].length; + } + public static getNumberKeyGenericSetMapLength(map: TMapNumberKeyGenericSet): number { + return [...map].length; + } public static getGenericKeyGenericValueMapLength(map: TMapGenericKeyGenericValue): number { return [...map].length; } @@ -930,6 +1228,15 @@ export class DictionaryMapUtility { return [...map].length; } + public static newTMapGenericKeyGenericSet(): TMapGenericKeyGenericSet { + return new Map>(); + } + public static newTMapStringKeyGenericSet(): TMapStringKeyGenericSet { + return new Map>(); + } + public static newTMapNumberKeyGenericSet(): TMapNumberKeyGenericSet { + return new Map>(); + } public static newTMapGenericKeyGenericValue(): TMapGenericKeyGenericValue { return new Map(); } diff --git a/packages/dispatcher/src/data_structure/IDictionaryNumberIdGenericSet.ts b/packages/dispatcher/src/data_structure/IDictionaryNumberIdGenericSet.ts new file mode 100644 index 000000000..bfa7b2ebf --- /dev/null +++ b/packages/dispatcher/src/data_structure/IDictionaryNumberIdGenericSet.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +export interface IDictionaryNumberIdGenericSet { + [id: number]: Set; +} diff --git a/packages/dispatcher/src/data_structure/IDictionaryStringIdGenericSet.ts b/packages/dispatcher/src/data_structure/IDictionaryStringIdGenericSet.ts new file mode 100644 index 000000000..f33c4876e --- /dev/null +++ b/packages/dispatcher/src/data_structure/IDictionaryStringIdGenericSet.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +export interface IDictionaryStringIdGenericSet { + [id: string]: Set; +} diff --git a/packages/dispatcher/src/data_structure/TMapGenericKeyGenericSet.ts b/packages/dispatcher/src/data_structure/TMapGenericKeyGenericSet.ts new file mode 100644 index 000000000..bcb5f0e1a --- /dev/null +++ b/packages/dispatcher/src/data_structure/TMapGenericKeyGenericSet.ts @@ -0,0 +1,6 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +export type TMapGenericKeyGenericSet = Map>; diff --git a/packages/dispatcher/src/data_structure/TMapNumberKeyGenericSet.ts b/packages/dispatcher/src/data_structure/TMapNumberKeyGenericSet.ts new file mode 100644 index 000000000..1c6568383 --- /dev/null +++ b/packages/dispatcher/src/data_structure/TMapNumberKeyGenericSet.ts @@ -0,0 +1,7 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { TMapGenericKeyGenericSet } from "./TMapGenericKeyGenericSet"; +export type TMapNumberKeyGenericSet = TMapGenericKeyGenericSet; diff --git a/packages/dispatcher/src/data_structure/TMapStringKeyGenericSet.ts b/packages/dispatcher/src/data_structure/TMapStringKeyGenericSet.ts new file mode 100644 index 000000000..1f2a5cf11 --- /dev/null +++ b/packages/dispatcher/src/data_structure/TMapStringKeyGenericSet.ts @@ -0,0 +1,7 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { TMapGenericKeyGenericSet } from "./TMapGenericKeyGenericSet"; +export type TMapStringKeyGenericSet = TMapGenericKeyGenericSet; diff --git a/packages/dispatcher/src/index.ts b/packages/dispatcher/src/index.ts index 6e0e4ca09..85d5608d0 100644 --- a/packages/dispatcher/src/index.ts +++ b/packages/dispatcher/src/index.ts @@ -3,32 +3,169 @@ * Licensed under the MIT License. */ -import { AppAutoActiveLearner } from "./model/supervised/classifier/auto_active_learning/AppAutoActiveLearner"; +import { ColumnarData } from "./data/ColumnarData"; +import { Data } from "./data/Data"; +import { DataUtility } from "./data/DataUtility"; +import { EntityAnnotatedCorpusData } from "./data/EntityAnnotatedCorpusData"; +import { LuData } from "./data/LuData"; + +import { DictionaryMapUtility } from "./data_structure/DictionaryMapUtility"; +import { IDictionaryNumberIdGenericArray } from "./data_structure/IDictionaryNumberIdGenericArray"; +import { IDictionaryNumberIdGenericArrays } from "./data_structure/IDictionaryNumberIdGenericArrays"; +import { IDictionaryNumberIdGenericSet } from "./data_structure/IDictionaryNumberIdGenericSet"; +import { IDictionaryNumberIdGenericValue } from "./data_structure/IDictionaryNumberIdGenericValue"; +import { IDictionaryStringIdGenericArray } from "./data_structure/IDictionaryStringIdGenericArray"; +import { IDictionaryStringIdGenericArrays } from "./data_structure/IDictionaryStringIdGenericArrays"; +import { IDictionaryStringIdGenericSet } from "./data_structure/IDictionaryStringIdGenericSet"; +import { IDictionaryStringIdGenericValue } from "./data_structure/IDictionaryStringIdGenericValue"; +import { TMapGenericKeyGenericArray } from "./data_structure/TMapGenericKeyGenericArray"; +import { TMapGenericKeyGenericArrays } from "./data_structure/TMapGenericKeyGenericArrays"; +import { TMapGenericKeyGenericSet } from "./data_structure/TMapGenericKeyGenericSet"; +import { TMapGenericKeyGenericValue } from "./data_structure/TMapGenericKeyGenericValue"; +import { TMapNumberKeyGenericArray } from "./data_structure/TMapNumberKeyGenericArray"; +import { TMapNumberKeyGenericArrays } from "./data_structure/TMapNumberKeyGenericArrays"; +import { TMapNumberKeyGenericSet } from "./data_structure/TMapNumberKeyGenericSet"; +import { TMapNumberKeyGenericValue } from "./data_structure/TMapNumberKeyGenericValue"; +import { TMapStringKeyGenericArray } from "./data_structure/TMapStringKeyGenericArray"; +import { TMapStringKeyGenericArrays } from "./data_structure/TMapStringKeyGenericArrays"; +import { TMapStringKeyGenericSet } from "./data_structure/TMapStringKeyGenericSet"; +import { TMapStringKeyGenericValue } from "./data_structure/TMapStringKeyGenericValue"; import { mainConfusionMatrix } from "./mathematics/confusion_matrix/AppConfusionMatrix"; import { mainConfusionMatrixFunction } from "./mathematics/confusion_matrix/AppConfusionMatrix"; +import { BinaryConfusionMatrix } from "./mathematics/confusion_matrix/BinaryConfusionMatrix"; import { ConfusionMatrix } from "./mathematics/confusion_matrix/ConfusionMatrix"; +import { ConfusionMatrixBase } from "./mathematics/confusion_matrix/ConfusionMatrixBase"; +import { IConfusionMatrix } from "./mathematics/confusion_matrix/IConfusionMatrix"; +import { IMultiLabelConfusionMatrix } from "./mathematics/confusion_matrix/IMultiLabelConfusionMatrix"; +import { MultiLabelConfusionMatrix } from "./mathematics/confusion_matrix/MultiLabelConfusionMatrix"; +import { MultiLabelConfusionMatrixBase } from "./mathematics/confusion_matrix/MultiLabelConfusionMatrixBase"; +import { MultiLabelConfusionMatrixSubset } from "./mathematics/confusion_matrix/MultiLabelConfusionMatrixSubset"; +import { MultiLabelConfusionMatrixWithBinaryArrayBase } from "./mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryArrayBase"; +import { MultiLabelConfusionMatrixWithBinaryBase } from "./mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryBase"; + +import { IMathematicsHelper } from "./mathematics/mathematics_helper/IMathematicsHelper"; +import { MathematicsHelper } from "./mathematics/mathematics_helper/MathematicsHelper"; + +import { AbstractBaseBootstrapSampler } from "./mathematics/sampler/AbstractBaseBootstrapSampler"; +import { AbstractBaseReservoirSampler } from "./mathematics/sampler/AbstractBaseReservoirSampler"; +import { AbstractBaseSampler } from "./mathematics/sampler/AbstractBaseSampler"; +import { BootstrapSampler } from "./mathematics/sampler/BootstrapSampler"; +import { BootstrapSamplerDistribution } from "./mathematics/sampler/BootstrapSamplerDistribution"; +import { BootstrapSamplerKeyMap } from "./mathematics/sampler/BootstrapSamplerKeyMap"; +import { BootstrapSamplerKeyMapDistribution } from "./mathematics/sampler/BootstrapSamplerKeyMapDistribution"; +import { ReservoirArraySampler } from "./mathematics/sampler/ReservoirArraySampler"; +import { ReservoirSampler } from "./mathematics/sampler/ReservoirSampler"; +import { ReservoirSamplerKeyMap } from "./mathematics/sampler/ReservoirSamplerKeyMap"; import { mainCrossValidatorWithColumnarContent } from "./model/evaluation/cross_validation/AppCrossValidator"; import { mainCrossValidatorWithLuContent } from "./model/evaluation/cross_validation/AppCrossValidator"; import { mainCrossValidator } from "./model/evaluation/cross_validation/AppCrossValidator"; +import { CrossValidator } from "./model/evaluation/cross_validation/CrossValidator"; import { mainPredictor } from "./model/evaluation/predict/AppPredictor"; +import { Predictor } from "./model/evaluation/predict/Predictor"; import { mainDataProfileReporter } from "./model/evaluation/report/AppDataProfileReporter"; +import { DataProfileReporter } from "./model/evaluation/report/DataProfileReporter"; import { mainModelMetaDataProfileReporter } from "./model/evaluation/report/AppModelMetaDataProfileReporter"; +import { ModelMetaDataProfileReporter } from "./model/evaluation/report/ModelMetaDataProfileReporter"; import { mainThresholdReporter } from "./model/evaluation/report/AppThresholdReporter"; +import { ThresholdReporter } from "./model/evaluation/report/ThresholdReporter"; + +import { mainTester } from "./model/evaluation/test/AppTester"; +import { Tester } from "./model/evaluation/test/Tester"; + +import { AppAutoActiveLearner } from "./model/supervised/classifier/auto_active_learning/AppAutoActiveLearner"; +import { AutoActiveLearner } from "./model/supervised/classifier/auto_active_learning/AutoActiveLearner"; + +import { AppSoftmaxRegressionSparse } from "./model/supervised/classifier/neural_network/learner/AppSoftmaxRegressionSparse"; +import { SoftmaxRegressionSparse } from "./model/supervised/classifier/neural_network/learner/SoftmaxRegressionSparse"; + +import { Utility } from "./utility/Utility"; +import { ListArrayUtility } from "./utility/ListArrayUtility"; export default { - AppAutoActiveLearner, + ColumnarData, + Data, + DataUtility, + EntityAnnotatedCorpusData, + LuData + , + DictionaryMapUtility + // IDictionaryNumberIdGenericArray, + // IDictionaryNumberIdGenericArrays, + // IDictionaryNumberIdGenericSet, + // IDictionaryNumberIdGenericValue, + // IDictionaryStringIdGenericArray, + // IDictionaryStringIdGenericArrays, + // IDictionaryStringIdGenericSet, + // IDictionaryStringIdGenericValue, + // TMapGenericKeyGenericArray, + // TMapGenericKeyGenericArrays, + // TMapGenericKeyGenericSet, + // TMapGenericKeyGenericValue, + // TMapNumberKeyGenericArray, + // TMapNumberKeyGenericArrays, + // TMapNumberKeyGenericSet, + // TMapNumberKeyGenericValue, + // TMapStringKeyGenericArray, + // TMapStringKeyGenericArrays, + // TMapStringKeyGenericSet, + // TMapStringKeyGenericValue + , mainConfusionMatrix, mainConfusionMatrixFunction, + BinaryConfusionMatrix, ConfusionMatrix, - mainCrossValidator, - mainCrossValidatorWithLuContent, + ConfusionMatrixBase, + // IConfusionMatrix, + // IMultiLabelConfusionMatrix, + MultiLabelConfusionMatrix, + MultiLabelConfusionMatrixBase, + MultiLabelConfusionMatrixSubset, + MultiLabelConfusionMatrixWithBinaryArrayBase, + MultiLabelConfusionMatrixWithBinaryBase + , + // IMathematicsHelper, + MathematicsHelper + , + AbstractBaseBootstrapSampler, + AbstractBaseReservoirSampler, + AbstractBaseSampler, + BootstrapSampler, + BootstrapSamplerDistribution, + BootstrapSamplerKeyMap, + BootstrapSamplerKeyMapDistribution, + ReservoirArraySampler, + ReservoirSampler, + ReservoirSamplerKeyMap + , mainCrossValidatorWithColumnarContent, + mainCrossValidatorWithLuContent, + mainCrossValidator, + CrossValidator + , mainPredictor, + Predictor + , mainDataProfileReporter, + DataProfileReporter, mainModelMetaDataProfileReporter, + ModelMetaDataProfileReporter, mainThresholdReporter, + ThresholdReporter + , + mainTester, + Tester + , + AppAutoActiveLearner, + AutoActiveLearner + , + AppSoftmaxRegressionSparse, + SoftmaxRegressionSparse + , + Utility, + ListArrayUtility + , }; diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/AppConfusionMatrix.ts b/packages/dispatcher/src/mathematics/confusion_matrix/AppConfusionMatrix.ts index 6a9eb03ec..2b6215fb6 100644 --- a/packages/dispatcher/src/mathematics/confusion_matrix/AppConfusionMatrix.ts +++ b/packages/dispatcher/src/mathematics/confusion_matrix/AppConfusionMatrix.ts @@ -27,7 +27,7 @@ export function mainConfusionMatrixFunction( revisedTextColumnIndex: number, lineIndexToStart: number): { "labels": string[], - "labelMap": { [id: string]: number; }, + "labelMap": { [id: string]: number }, "binaryConfusionMatrices": BinaryConfusionMatrix[], "confusionMatrix": ConfusionMatrix } { // ----------------------------------------------------------------------- @@ -39,9 +39,9 @@ export function mainConfusionMatrixFunction( } // ----------------------------------------------------------------------- let labels: string[] = []; - let labelMap: { [id: string]: number; } = {}; + let labelMap: { [id: string]: number } = {}; if (!Utility.isEmptyString(labelFilename)) { - const labelsAndLabelMap: { "stringArray": string[], "stringMap": { [id: string]: number; } } = + const labelsAndLabelMap: { "stringArray": string[], "stringMap": { [id: string]: number } } = DictionaryMapUtility.buildStringIdNumberValueDictionaryFromUniqueStringArrayFile(labelFilename); labels = labelsAndLabelMap.stringArray; labelMap = labelsAndLabelMap.stringMap; @@ -90,7 +90,7 @@ export function mainConfusionMatrixFunction( if (label === predictedLabel) { numberMatches++; } - confusionMatrix.addInstance(label, predictedLabel); + confusionMatrix.addInstanceByLabel(label, predictedLabel); } // ----------------------------------------------------------------------- Utility.debuggingLog( @@ -103,7 +103,7 @@ export function mainConfusionMatrixFunction( Utility.debuggingLog( "labels=" + confusionMatrix.getLabels()); Utility.debuggingLog( - Utility.JSONstringify(confusionMatrix.getLabelMap())); + Utility.jsonStringify(confusionMatrix.getLabelMap())); Utility.debuggingLog( "rows=" + confusionMatrix.getConfusionMatrixRows()); Utility.debuggingLog( @@ -246,9 +246,9 @@ export function mainConfusionMatrix(): void { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); @@ -302,7 +302,7 @@ export function mainConfusionMatrix(): void { // ----------------------------------------------------------------------- const mainConfusionMatrixFunctionResult: { "labels": string[], - "labelMap": { [id: string]: number; }, + "labelMap": { [id: string]: number }, "confusionMatrix": ConfusionMatrix } = mainConfusionMatrixFunction( scoreFilename, labelFilename, @@ -326,24 +326,24 @@ export function mainConfusionMatrix(): void { export function exampleFunctionConfusionMatrix(): void { const labels: string[] = [ "label0", "label1", "label2" ]; - const labelMap: { [id: string]: number; } = { }; + const labelMap: { [id: string]: number } = { }; labelMap.label0 = 0; labelMap.label1 = 1; labelMap.label2 = 2; const confusionMatrix = new ConfusionMatrix(labels, labelMap); - confusionMatrix.addInstance("label0", "label0"); - confusionMatrix.addInstance("label0", "label1"); - confusionMatrix.addInstance("label0", "label2"); - confusionMatrix.addInstance("label1", "label0"); - confusionMatrix.addInstance("label1", "label1"); - confusionMatrix.addInstance("label1", "label2"); - confusionMatrix.addInstance("label2", "label0"); - confusionMatrix.addInstance("label2", "label1"); - confusionMatrix.addInstance("label2", "label2"); + confusionMatrix.addInstanceByLabel("label0", "label0"); + confusionMatrix.addInstanceByLabel("label0", "label1"); + confusionMatrix.addInstanceByLabel("label0", "label2"); + confusionMatrix.addInstanceByLabel("label1", "label0"); + confusionMatrix.addInstanceByLabel("label1", "label1"); + confusionMatrix.addInstanceByLabel("label1", "label2"); + confusionMatrix.addInstanceByLabel("label2", "label0"); + confusionMatrix.addInstanceByLabel("label2", "label1"); + confusionMatrix.addInstanceByLabel("label2", "label2"); Utility.debuggingLog( "labels=" + confusionMatrix.getLabels()); Utility.debuggingLog( - Utility.JSONstringify(confusionMatrix.getLabelMap())); + Utility.jsonStringify(confusionMatrix.getLabelMap())); Utility.debuggingLog( "rows=" + confusionMatrix.getConfusionMatrixRows()); Utility.debuggingLog( diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/BinaryConfusionMatrix.ts b/packages/dispatcher/src/mathematics/confusion_matrix/BinaryConfusionMatrix.ts index fa0f6afcc..552ac45e9 100644 --- a/packages/dispatcher/src/mathematics/confusion_matrix/BinaryConfusionMatrix.ts +++ b/packages/dispatcher/src/mathematics/confusion_matrix/BinaryConfusionMatrix.ts @@ -3,10 +3,20 @@ * Licensed under the MIT License. */ +import { IMathematicsHelper } from "../mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../mathematics_helper/MathematicsHelper"; +import { IDictionaryStringIdGenericArray } from "../../data_structure/IDictionaryStringIdGenericArray"; +import { IDictionaryStringIdGenericValue } from "../../data_structure/IDictionaryStringIdGenericValue"; + +import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; + // tslint:disable: max-line-length export class BinaryConfusionMatrix { + + public static readonly MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + // // Initialize an BinaryConfusionMatrix object. // @@ -40,10 +50,10 @@ export class BinaryConfusionMatrix { protected ratioCell22: number = 0; constructor( - total: number, - cell11: number, - row1: number, - column1: number, + total: number = 0, + cell11: number = 0, + row1: number = 0, + column1: number = 0, potentialRow1: number = 0, potentialRow2: number = 0, throwIfNotProper: boolean = true) { @@ -52,12 +62,12 @@ export class BinaryConfusionMatrix { // cells defined below: // // || Predicted Positive | Predicted Negative | - // ---------------------------------------------------------------------------------- - // ---------------------------------------------------------------------------------- + // ---------------------------------------------------------------------------------------------- + // ---------------------------------------------------------------------------------------------- // Ground - True Positive || True Positive | False Nagative | Ground - True Positive - // ---------------------------------------------------------------------------------- + // ---------------------------------------------------------------------------------------------- // Ground - True Negative || False Positive | True Nagative | Ground - True Negative - // ---------------------------------------------------------------------------------- + // ---------------------------------------------------------------------------------------------- // || Predicted Positive | Predicted Negative | Total // this.total = total; @@ -81,17 +91,87 @@ export class BinaryConfusionMatrix { this.getThrowIfNotProper()); } + public addToTruePositives(value: number = 1, recalculate: boolean = true) { + this.addToCell11(value, recalculate); + } + public addToFalseNegatives(value: number = 1, recalculate: boolean = true) { + this.addToCell12(value, recalculate); + } + public addToFalsePositives(value: number = 1, recalculate: boolean = true) { + this.addToCell21(value, recalculate); + } + public addToTrueNegatives(value: number = 1, recalculate: boolean = true) { + this.addToCell22(value, recalculate); + } + + public addToCell11(value: number = 1, recalculate: boolean = true) { + this.total += value; + this.cell11 += value; + this.row1 += value; + this.column1 += value; + if (recalculate) { + this.calculateDerivedCells(); + } + } + public addToCell12(value: number = 1, recalculate: boolean = true) { + this.total += value; + this.row1 += value; + this.column2 += value; + if (recalculate) { + this.calculateDerivedCells(); + } + } + public addToCell21(value: number = 1, recalculate: boolean = true) { + this.total += value; + this.row2 += value; + this.column1 += value; + if (recalculate) { + this.calculateDerivedCells(); + } + } + public addToCell22(value: number = 1, recalculate: boolean = true) { + this.total += value; + this.row2 += value; + this.column2 += value; + if (recalculate) { + this.calculateDerivedCells(); + } + } + + public addToPotentialRow1(value: number = 1, recalculate: boolean = true) { + this.potentialRow1 += value; + if (recalculate) { + this.calculateDerivedCells(); + } + } + public addToPotentialRow2(value: number = 1, recalculate: boolean = true) { + this.potentialRow2 += value; + if (recalculate) { + this.calculateDerivedCells(); + } + } + + public addFrom(other: BinaryConfusionMatrix) { + this.total += other.total; + this.cell11 += other.cell11; + this.row1 += other.row1; + this.column1 += other.column1; + this.potentialRow1 += other.potentialRow1; + this.potentialRow2 += other.potentialRow2; + this.calculateDerivedCells(); + } + public moveFromPredictedNegativeToPositive( groundTruthIsPositive: boolean, createNewBinaryConfusionMatrix: boolean = true, value: number = 1): BinaryConfusionMatrix { - const binaryConfusionMatrix: BinaryConfusionMatrix = - createNewBinaryConfusionMatrix ? this.clone() : this; if (groundTruthIsPositive) { - binaryConfusionMatrix.cell11 += value; + this.cell11 += value; } - binaryConfusionMatrix.column1 += value; - binaryConfusionMatrix.calculateDerivedCells(); + this.column1 += value; + this.calculateDerivedCells(); + const binaryConfusionMatrix: BinaryConfusionMatrix = + createNewBinaryConfusionMatrix ? this.clone() : this; return binaryConfusionMatrix; } @@ -101,19 +181,19 @@ export class BinaryConfusionMatrix { this.cell12 = this.row1 - this.cell11; this.cell21 = this.column1 - this.cell11; this.cell22 = this.total - this.cell11 - this.cell12 - this.cell21; - this.expectedCell11 = MathematicsHelper.safeDivide(this.row1 * this.column1, this.total); - this.expectedCell12 = MathematicsHelper.safeDivide(this.row1 * this.column2, this.total); - this.expectedCell21 = MathematicsHelper.safeDivide(this.row2 * this.column1, this.total); - this.expectedCell22 = MathematicsHelper.safeDivide(this.row2 * this.column2, this.total); + this.expectedCell11 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.row1 * this.column1, this.total); + this.expectedCell12 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.row1 * this.column2, this.total); + this.expectedCell21 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.row2 * this.column1, this.total); + this.expectedCell22 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.row2 * this.column2, this.total); this.isProper = this.validate(this.throwIfNotProper); - this.ratioCell11 = MathematicsHelper.safeDivide(this.cell11, this.total); - this.ratioRow1 = MathematicsHelper.safeDivide(this.row1, this.total); - this.ratioColumn1 = MathematicsHelper.safeDivide(this.column1, this.total); - this.ratioRow2 = MathematicsHelper.safeDivide(this.row2, this.total); - this.ratioColumn2 = MathematicsHelper.safeDivide(this.column2, this.total); - this.ratioCell12 = MathematicsHelper.safeDivide(this.cell12, this.total); - this.ratioCell21 = MathematicsHelper.safeDivide(this.cell21, this.total); - this.ratioCell22 = MathematicsHelper.safeDivide(this.cell22, this.total); + this.ratioCell11 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.cell11, this.total); + this.ratioRow1 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.row1, this.total); + this.ratioColumn1 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.column1, this.total); + this.ratioRow2 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.row2, this.total); + this.ratioColumn2 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.column2, this.total); + this.ratioCell12 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.cell12, this.total); + this.ratioCell21 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.cell21, this.total); + this.ratioCell22 = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.cell22, this.total); } public getTotal(): number { @@ -268,8 +348,7 @@ export class BinaryConfusionMatrix { } public validate(throwIfNotProper: boolean = true) { - let isProper: boolean; - isProper = false; + let isProper: boolean = false; if (this.cell11 < 0) { throw new Error("this.cell11 < 0"); } @@ -440,7 +519,7 @@ export class BinaryConfusionMatrix { // // Retrieve the ratio of positives vs. negatives in the confusion matrix. // - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getPositives(), this.getNegatives()); } @@ -448,7 +527,7 @@ export class BinaryConfusionMatrix { // // Retrieve the ratio of negatives vs. positives in the confusion matrix. // - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getNegatives(), this.getPositives()); } @@ -470,7 +549,7 @@ export class BinaryConfusionMatrix { // // Retrieve the precision in the confusion matrix. // - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getTruePositives(), this.getTruePositives() + this.getFalsePositives()); } @@ -478,7 +557,7 @@ export class BinaryConfusionMatrix { // // Retrieve the precision in the confusion matrix. // - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getTruePositives(), this.getPositives()); } @@ -486,7 +565,7 @@ export class BinaryConfusionMatrix { // // Retrieve the precision in the confusion matrix. // - return 2 * MathematicsHelper.safeDivide( + return 2 * BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getPrecision() * this.getRecall(), this.getPrecision() + this.getRecall()); } @@ -498,8 +577,8 @@ export class BinaryConfusionMatrix { return this.getPositives(); } - public getBasicMetrics(): { [id: string]: number; } { - const metrics: { [id: string]: number; } = {}; + public getBasicMetrics(): { [id: string]: number } { + const metrics: { [id: string]: number } = {}; metrics.positives = this.getPositives(); metrics.negatives = this.getNegatives(); metrics.positiveRatio = this.getPositiveRatio(); @@ -545,108 +624,108 @@ export class BinaryConfusionMatrix { } public getTruePositiveRate(): number { - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getTruePositives(), this.getPositives()); } public getTrueNegativeRate(): number { - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getTrueNegatives(), this.getNegatives()); } public getPositivePredictiveValue(): number { - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getTruePositives(), (this.getTruePositives() + this.getFalsePositives())); } public getNegativePredictiveValue(): number { - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getTrueNegatives(), (this.getTrueNegatives() + this.getFalseNegatives())); } public getFalsePositiveRate(): number { - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getFalsePositives(), this.getNegatives()); } public getFalseDiscoveryRate(): number { - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getFalsePositives(), (this.getFalsePositives() + this.getTruePositives())); } public getFalseNegativeRate(): number { - return MathematicsHelper.safeDivide( + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( this.getFalseNegatives(), this.getPositives()); } public getTruePositiveOverTotalRate(): number { - return MathematicsHelper.safeDivide(this.getTruePositives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTruePositives(), this.getTotal()); } public getTrueNegativeOverTotalRate(): number { - return MathematicsHelper.safeDivide(this.getTrueNegatives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTrueNegatives(), this.getTotal()); } public getFalsePositiveOverTotalRate(): number { - return MathematicsHelper.safeDivide(this.getFalsePositives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalsePositives(), this.getTotal()); } public getFalseNegativeOverTotalRate(): number { - return MathematicsHelper.safeDivide(this.getFalseNegatives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalseNegatives(), this.getTotal()); } public getTruePositiveOverExplicitTotalRate(explicitTotal: number = 0): number { if (explicitTotal <= 0) { explicitTotal = this.getTotal(); } - return MathematicsHelper.safeDivide(this.getTruePositives(), explicitTotal); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTruePositives(), explicitTotal); } public getTrueNegativeOverExplicitTotalRate(explicitTotal: number = 0): number { if (explicitTotal <= 0) { explicitTotal = this.getTotal(); } - return MathematicsHelper.safeDivide(this.getTrueNegatives(), explicitTotal); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTrueNegatives(), explicitTotal); } public getFalsePositiveOverExplicitTotalRate(explicitTotal: number = 0): number { if (explicitTotal <= 0) { explicitTotal = this.getTotal(); } - return MathematicsHelper.safeDivide(this.getFalsePositives(), explicitTotal); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalsePositives(), explicitTotal); } public getFalseNegativeOverExplicitTotalRate(explicitTotal: number = 0): number { if (explicitTotal <= 0) { explicitTotal = this.getTotal(); } - return MathematicsHelper.safeDivide(this.getFalseNegatives(), explicitTotal); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalseNegatives(), explicitTotal); } public getConditionPositiveRate(): number { - return MathematicsHelper.safeDivide(this.getPositives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getPositives(), this.getTotal()); } public getConditionNegativeRate(): number { - return MathematicsHelper.safeDivide(this.getNegatives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getNegatives(), this.getTotal()); } public getPredictedPositiveRate(): number { - return MathematicsHelper.safeDivide(this.getPredictedPositives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getPredictedPositives(), this.getTotal()); } public getPredictedNegativeRate(): number { - return MathematicsHelper.safeDivide(this.getPredictedNegatives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getPredictedNegatives(), this.getTotal()); } public getConditionPositiveCaughtRate(explicitTotalPositives: number = -1): number { if (explicitTotalPositives <= 0) { explicitTotalPositives = this.getPositives(); } - return MathematicsHelper.safeDivide(this.getTruePositives(), explicitTotalPositives); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTruePositives(), explicitTotalPositives); } public getConditionPositiveMissedRate(explicitTotalPositives: number = -1): number { if (explicitTotalPositives <= 0) { explicitTotalPositives = this.getPositives(); } - return MathematicsHelper.safeDivide(this.getFalseNegatives(), explicitTotalPositives); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalseNegatives(), explicitTotalPositives); } public getConditionNegativeCaughtRate(explicitTotalNegatives: number = -1): number { if (explicitTotalNegatives <= 0) { explicitTotalNegatives = this.getNegatives(); } - return MathematicsHelper.safeDivide(this.getTrueNegatives(), explicitTotalNegatives); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTrueNegatives(), explicitTotalNegatives); } public getConditionNegativeMissedRate(explicitTotalNegatives: number = -1): number { if (explicitTotalNegatives <= 0) { explicitTotalNegatives = this.getNegatives(); } - return MathematicsHelper.safeDivide(this.getFalsePositives(), explicitTotalNegatives); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalsePositives(), explicitTotalNegatives); } public getConditionPositiveDecidedRate(explicitTotalPositives: number = -1): number { @@ -666,19 +745,19 @@ export class BinaryConfusionMatrix { if (explicitTotal <= 0) { explicitTotal = this.getTotal(); } - return MathematicsHelper.safeDivide(this.getPredictedPositives(), explicitTotal); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getPredictedPositives(), explicitTotal); } public getPredictedNegativeDecidedRate(explicitTotal: number = -1): number { if (explicitTotal <= 0) { explicitTotal = this.getTotal(); } - return MathematicsHelper.safeDivide(this.getPredictedNegatives(), explicitTotal); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getPredictedNegatives(), explicitTotal); } public getTotalDecidedRate(explicitTotal: number = -1): number { if (explicitTotal <= 0) { explicitTotal = this.getTotal(); } - return MathematicsHelper.safeDivide((this.getPredictedPositives() + this.getPredictedNegatives()), explicitTotal); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((this.getPredictedPositives() + this.getPredictedNegatives()), explicitTotal); } public getTotalUnDecidedRate(explicitTotal: number = -1): number { return (1 - this.getTotalDecidedRate(explicitTotal)); @@ -732,12 +811,12 @@ export class BinaryConfusionMatrix { } public getAccuracy(): number { - return MathematicsHelper.safeDivide((this.getTruePositives() + this.getTrueNegatives()), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((this.getTruePositives() + this.getTrueNegatives()), this.getTotal()); } // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- public getF1Score(): number { // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- double truePositiveFactor = (2 * this.getTruePositives()); // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- return - // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- MathematicsHelper.safeDivide( + // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- BinaryConfusionMatrix.MathematicsHelperObject.safeDivide( // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- truePositiveFactor, // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- (truePositiveFactor + this.getFalsePositives() + this.getFalseNegatives())); // ---- NOTE-BASIC-METRIC-DEFINED-ALREADY-FOR-COMPLETENESS ---- } @@ -764,32 +843,32 @@ export class BinaryConfusionMatrix { } public getFalseOmissionRate(): number { - return MathematicsHelper.safeDivide(this.getFalseNegatives(), (this.getFalseNegatives() + this.getTrueNegatives())); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalseNegatives(), (this.getFalseNegatives() + this.getTrueNegatives())); } public getPrevalence(): number { - return MathematicsHelper.safeDivide(this.getPositives(), this.getTotal()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getPositives(), this.getTotal()); } public getTruePositiveRateOverFalsePositiveRate(): number { - return MathematicsHelper.safeDivide(this.getTruePositiveRate(), this.getFalsePositiveRate()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTruePositiveRate(), this.getFalsePositiveRate()); } public getPositiveLikelihoodRatio(): number { return this.getTruePositiveRateOverFalsePositiveRate(); } public getFalseNegativeRateOverTrueNegativeRate(): number { - return MathematicsHelper.safeDivide(this.getFalseNegativeRate(), this.getTrueNegativeRate()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalseNegativeRate(), this.getTrueNegativeRate()); } public getNegativeLikelihoodRatio(): number { return this.getFalseNegativeRateOverTrueNegativeRate(); } public getDiagnosticOddsRatio(): number { - return MathematicsHelper.safeDivide(this.getPositiveLikelihoodRatio(), this.getNegativeLikelihoodRatio()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getPositiveLikelihoodRatio(), this.getNegativeLikelihoodRatio()); } public getFMeasure(fMeasureBeta: number = 1): number { const beta2: number = fMeasureBeta * fMeasureBeta; const precision: number = this.getPrecision(); const recall: number = this.getRecall(); - return MathematicsHelper.safeDivide(((1 + beta2) * (precision * recall)), ((beta2 * precision) + recall)); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(((1 + beta2) * (precision * recall)), ((beta2 * precision) + recall)); } public getF1Measure(): number { return this.getFMeasure(1); @@ -815,29 +894,29 @@ export class BinaryConfusionMatrix { } public getRisk(): number { - return MathematicsHelper.safeDivide(this.getTruePositives(), this.getPredictedPositives()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTruePositives(), this.getPredictedPositives()); } public getRiskPredictedNegative(): number { - return MathematicsHelper.safeDivide(this.getFalseNegatives(), this.getPredictedNegatives()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getFalseNegatives(), this.getPredictedNegatives()); } public getOdds(): number { - return MathematicsHelper.safeDivide(this.getTruePositives(), this.getFalsePositives()); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTruePositives(), this.getFalsePositives()); } public getOddsRatio(): number { - return MathematicsHelper.safeDivide(MathematicsHelper.safeDivide(this.getTruePositives(), this.getFalsePositives()), MathematicsHelper.safeDivide(this.getTrueNegatives(), this.getFalseNegatives())); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTruePositives(), this.getFalsePositives()), BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(this.getTrueNegatives(), this.getFalseNegatives())); } public getRelativeRisk(): number { - return MathematicsHelper.safeDivide((this.getRisk()), (this.getRiskPredictedNegative())); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((this.getRisk()), (this.getRiskPredictedNegative())); } public getWeightOfEvidence(): number { - return MathematicsHelper.safeLog(this.getOdds()) * 100; + return BinaryConfusionMatrix.MathematicsHelperObject.safeLog(this.getOdds()) * 100; } public getPositiveNegativePotentialRatio(): number { const negativePotential: number = this.getPotentialNegatives(); const positivePotential: number = this.getPotentialPositives(); - const R: number = ((negativePotential === 0) ? 1 : MathematicsHelper.safeDivide(positivePotential, negativePotential)); + const R: number = ((negativePotential === 0) ? 1 : BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(positivePotential, negativePotential)); return R; } @@ -853,7 +932,7 @@ export class BinaryConfusionMatrix { public getPE(): number { const trueNegatives: number = this.getTrueNegatives(); - return MathematicsHelper.safeDivide((trueNegatives - this.getFalseNegatives()), (this.getFalsePositives() + trueNegatives)); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((trueNegatives - this.getFalseNegatives()), (this.getFalsePositives() + trueNegatives)); } public getPETruePositiveExtreme(): number { return 1; @@ -873,7 +952,7 @@ export class BinaryConfusionMatrix { public getPEWorst(): number { const R: number = this.getPositiveNegativePotentialRatio(); const fraudRate: number = this.getFraudRate(); - return MathematicsHelper.safeDivide(-(fraudRate * R), (1 - fraudRate)); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(-(fraudRate * R), (1 - fraudRate)); } public getPEAlwaysReject(): number { return 0; @@ -881,7 +960,7 @@ export class BinaryConfusionMatrix { public getPEAlwaysApprove(): number { const R: number = this.getPositiveNegativePotentialRatio(); const fraudRate: number = this.getFraudRate(); - return MathematicsHelper.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate)); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate)); } public getPERandomBaseline(): number { const R: number = this.getPositiveNegativePotentialRatio(); @@ -895,7 +974,7 @@ export class BinaryConfusionMatrix { if ((rejectRate < 0) || (rejectRate > 1)) { rejectRate = fraudRate; } - return ((1 - rejectRate) * (1 - (MathematicsHelper.safeDivide(fraudRate, (1 - fraudRate) * R)))); + return ((1 - rejectRate) * (1 - (BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(fraudRate, (1 - fraudRate) * R)))); } public getOPE(): number { @@ -905,7 +984,7 @@ export class BinaryConfusionMatrix { const falseNegatives: number = this.getFalseNegatives(); const numerator: number = ((trueNegatives - falsePositives) + (truePositives - falseNegatives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getOPETruePositiveExtreme(): number { return 1; @@ -928,17 +1007,17 @@ export class BinaryConfusionMatrix { public getOPEAlwaysReject(): number { const R: number = this.getPositiveNegativePotentialRatio(); const fraudRate: number = this.getFraudRate(); - return MathematicsHelper.safeDivide(-(1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(-(1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); } public getOPEAlwaysApprove(): number { const R: number = this.getPositiveNegativePotentialRatio(); const fraudRate: number = this.getFraudRate(); - return MathematicsHelper.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); } public getOPERandomBaseline(): number { const R: number = this.getPositiveNegativePotentialRatio(); const fraudRate: number = this.getFraudRate(); - const term: number = MathematicsHelper.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); + const term: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); return (-((2 * fraudRate) - 1) * term); } public getOPEGenericRandomBaseline( @@ -948,7 +1027,7 @@ export class BinaryConfusionMatrix { if ((rejectRate < 0) || (rejectRate > 1)) { rejectRate = fraudRate; } - const term: number = MathematicsHelper.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); + const term: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((1 - fraudRate - (fraudRate * R)), (1 - fraudRate + (fraudRate * R))); return (-((2 * rejectRate) - 1) * term); } @@ -961,15 +1040,15 @@ export class BinaryConfusionMatrix { const trueNegatives: number = this.getTrueNegatives(); let falsePositives: number = this.getFalsePositives(); const falseNegatives: number = this.getFalseNegatives(); - const chargeBackRate: number = MathematicsHelper.safeDivide(falseNegatives, (falseNegatives + trueNegatives)); // ---- this.getChargeBackRate(); - const falsePositiveRate: number = MathematicsHelper.safeDivide(falsePositives, (falsePositives + trueNegatives)); // ---- this.getFalsePositiveRate(); - const truePositiveDiminishingFactor: number = A * Math.exp(-alpha * (MathematicsHelper.safeDivide((chargeBackRate), (1 - chargeBackRate)))); - const falsePositiveAmplifyingFactor: number = B * Math.exp(-beta * MathematicsHelper.safeDivide((falsePositiveRate), (1 - falsePositiveRate))); + const chargeBackRate: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(falseNegatives, (falseNegatives + trueNegatives)); // ---- this.getChargeBackRate(); + const falsePositiveRate: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(falsePositives, (falsePositives + trueNegatives)); // ---- this.getFalsePositiveRate(); + const truePositiveDiminishingFactor: number = A * Math.exp(-alpha * (BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((chargeBackRate), (1 - chargeBackRate)))); + const falsePositiveAmplifyingFactor: number = B * Math.exp(-beta * BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((falsePositiveRate), (1 - falsePositiveRate))); truePositives *= truePositiveDiminishingFactor; falsePositives *= falsePositiveAmplifyingFactor; const numerator: number = ((trueNegatives - falsePositives) + (truePositives - falseNegatives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getOPEsharpRandomBaseline( alpha: number = 1, @@ -982,15 +1061,15 @@ export class BinaryConfusionMatrix { const trueNegatives: number = (1 - fraudRate) * (1 - fraudRate); // ---- this.getTrueNegatives(); let falsePositives: number = fraudRate * (1 - fraudRate); // ---- this.getFalsePositives(); const falseNegatives: number = R * (fraudRate * (1 - fraudRate)); // ---- this.getFalseNegatives(); - const chargeBackRate: number = MathematicsHelper.safeDivide(falseNegatives, (falseNegatives + trueNegatives)); // ---- MathematicsHelper.safeDivide((R * fraudRate), (1 - fraudRate - (R * fraudRate))); // ---- MathematicsHelper.safeDivide((R * (fraudRate * (1 - fraudRate))), ((R * (fraudRate * (1 - fraudRate))) + ((1-fraudRate) * (1-fraudRate)))); // ---- this.getChargeBackRate(); - const falsePositiveRate: number = MathematicsHelper.safeDivide(falsePositives, (falsePositives + trueNegatives)); // ---- fraudRate; // ---- MathematicsHelper.safeDivide(((fraudRate * (1 - fraudRate))), (((fraudRate * (1 - fraudRate))) + ((1 - fraudRate) * (1 - fraudRate)))); // ---- this.getFalsePositiveRate(); - const truePositiveDiminishingFactor: number = A * Math.exp(-alpha * (MathematicsHelper.safeDivide((chargeBackRate), (1 - chargeBackRate)))); - const falsePositiveAmplifyingFactor: number = B * Math.exp(-beta * (MathematicsHelper.safeDivide((falsePositiveRate), (1 - falsePositiveRate)))); + const chargeBackRate: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(falseNegatives, (falseNegatives + trueNegatives)); // ---- BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((R * fraudRate), (1 - fraudRate - (R * fraudRate))); // ---- BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((R * (fraudRate * (1 - fraudRate))), ((R * (fraudRate * (1 - fraudRate))) + ((1-fraudRate) * (1-fraudRate)))); // ---- this.getChargeBackRate(); + const falsePositiveRate: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(falsePositives, (falsePositives + trueNegatives)); // ---- fraudRate; // ---- BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(((fraudRate * (1 - fraudRate))), (((fraudRate * (1 - fraudRate))) + ((1 - fraudRate) * (1 - fraudRate)))); // ---- this.getFalsePositiveRate(); + const truePositiveDiminishingFactor: number = A * Math.exp(-alpha * (BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((chargeBackRate), (1 - chargeBackRate)))); + const falsePositiveAmplifyingFactor: number = B * Math.exp(-beta * (BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((falsePositiveRate), (1 - falsePositiveRate)))); truePositives *= truePositiveDiminishingFactor; falsePositives *= falsePositiveAmplifyingFactor; const numerator: number = ((trueNegatives - falsePositives) + (truePositives - falseNegatives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getOPEsharpGenericRandomBaseline( rejectRate: number = -1, @@ -1007,15 +1086,15 @@ export class BinaryConfusionMatrix { const trueNegatives: number = (1 - rejectRate) * (1 - fraudRate); // ---- (1 - fraudRate) * (1 - fraudRate); // ---- this.getTrueNegatives(); let falsePositives: number = rejectRate * (1 - fraudRate); // ---- fraudRate * (1 - fraudRate); // ---- this.getFalsePositives(); const falseNegatives: number = R * (fraudRate * (1 - rejectRate)); // ---- R * (fraudRate * (1 - fraudRate)); // ---- this.getFalseNegatives(); - const chargeBackRate: number = MathematicsHelper.safeDivide(falseNegatives, (falseNegatives + trueNegatives)); // ---- MathematicsHelper.safeDivide((R * fraudRate), (1 - fraudRate - (R * fraudRate))); // ---- MathematicsHelper.safeDivide((R * (fraudRate * (1 - fraudRate))), ((R * (fraudRate * (1 - fraudRate))) + ((1-fraudRate) * (1-fraudRate)))); // ---- this.getChargeBackRate(); - const falsePositiveRate: number = MathematicsHelper.safeDivide(falsePositives, (falsePositives + trueNegatives)); // ---- fraudRate; // ---- MathematicsHelper.safeDivide(((fraudRate * (1 - fraudRate))), (((fraudRate * (1 - fraudRate))) + ((1 - fraudRate) * (1 - fraudRate)))); // ---- this.getFalsePositiveRate(); - const truePositiveDiminishingFactor: number = A * Math.exp(-alpha * (MathematicsHelper.safeDivide((chargeBackRate), (1 - chargeBackRate)))); - const falsePositiveAmplifyingFactor: number = B * Math.exp(-beta * (MathematicsHelper.safeDivide((falsePositiveRate), (1 - falsePositiveRate)))); + const chargeBackRate: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(falseNegatives, (falseNegatives + trueNegatives)); // ---- BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((R * fraudRate), (1 - fraudRate - (R * fraudRate))); // ---- BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((R * (fraudRate * (1 - fraudRate))), ((R * (fraudRate * (1 - fraudRate))) + ((1-fraudRate) * (1-fraudRate)))); // ---- this.getChargeBackRate(); + const falsePositiveRate: number = BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(falsePositives, (falsePositives + trueNegatives)); // ---- fraudRate; // ---- BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(((fraudRate * (1 - fraudRate))), (((fraudRate * (1 - fraudRate))) + ((1 - fraudRate) * (1 - fraudRate)))); // ---- this.getFalsePositiveRate(); + const truePositiveDiminishingFactor: number = A * Math.exp(-alpha * (BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((chargeBackRate), (1 - chargeBackRate)))); + const falsePositiveAmplifyingFactor: number = B * Math.exp(-beta * (BinaryConfusionMatrix.MathematicsHelperObject.safeDivide((falsePositiveRate), (1 - falsePositiveRate)))); truePositives *= truePositiveDiminishingFactor; falsePositives *= falsePositiveAmplifyingFactor; const numerator: number = ((trueNegatives - falsePositives) + (truePositives - falseNegatives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getNetPE(): number { @@ -1056,7 +1135,7 @@ export class BinaryConfusionMatrix { const falseNegatives: number = this.getFalseNegatives(); const numerator: number = ((trueNegatives - falsePositives) + (truePositives - falseNegatives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getOPEPrime(): number { const truePositives: number = 0; // ---- this.getDiagonalCellsSum(); @@ -1065,7 +1144,7 @@ export class BinaryConfusionMatrix { const falseNegatives: number = this.getFalseNegatives(); const numerator: number = ((trueNegatives - falsePositives) + (truePositives - falseNegatives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getOLR(): number { const truePositives: number = this.getTruePositives(); @@ -1074,7 +1153,7 @@ export class BinaryConfusionMatrix { const falseNegatives: number = this.getFalseNegatives(); const numerator: number = ((falsePositives) + (falseNegatives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getOGR(): number { const truePositives: number = this.getTruePositives(); @@ -1083,16 +1162,16 @@ export class BinaryConfusionMatrix { const falseNegatives: number = this.getFalseNegatives(); const numerator: number = ((trueNegatives) + (truePositives)); const denominator: number = ((trueNegatives + falsePositives) + (truePositives + falseNegatives)); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getInverseDocumentFrequencyRaw(): number { const numerator: number = this.getTotal(); const denominator: number = this.getPositives(); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getInverseDocumentFrequency(): number { - return MathematicsHelper.safeLog(this.getInverseDocumentFrequencyRaw()); // ---- NOTE: better to use the smooth version as it's possible that positives can be zero! + return BinaryConfusionMatrix.MathematicsHelperObject.safeLog(this.getInverseDocumentFrequencyRaw()); // ---- NOTE: better to use the smooth version as it's possible that positives can be zero! } public getInverseDocumentFrequencyUnary(): number { return ((this.getPositives() > 0) && (this.getTotal() > 0)) ? 1 : 0; @@ -1100,29 +1179,29 @@ export class BinaryConfusionMatrix { public getInverseDocumentFrequencySmooth(): number { const inverseDocumentFrequencyRaw: number = this.getInverseDocumentFrequencyRaw(); - return MathematicsHelper.safeLog(1 + inverseDocumentFrequencyRaw); + return BinaryConfusionMatrix.MathematicsHelperObject.safeLog(1 + inverseDocumentFrequencyRaw); } public getInverseDocumentFrequencyMax(explicitMaxPositives: number = -1): number { if (explicitMaxPositives <= 0) { return this.getInverseDocumentFrequency(); } - return MathematicsHelper.safeLog(MathematicsHelper.safeDivide(explicitMaxPositives, (1 + this.getPositives()))); + return BinaryConfusionMatrix.MathematicsHelperObject.safeLog(BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(explicitMaxPositives, (1 + this.getPositives()))); } public getInverseDocumentFrequencyProbabilistic(): number { const denominator: number = this.getPositives(); const numerator: number = this.getTotal() - denominator; - return MathematicsHelper.safeLog(MathematicsHelper.safeDivide(numerator, denominator)); + return BinaryConfusionMatrix.MathematicsHelperObject.safeLog(BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator)); } public getJaccardCoefficient(): number { const denominator: number = this.getTruePositives() + this.getFalsePositives() + this.getFalseNegatives(); const numerator: number = this.getTruePositives(); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getSimpleMatchingCoefficient(): number { const denominator: number = this.getTotal(); const numerator: number = this.getTruePositives(); - return MathematicsHelper.safeDivide(numerator, denominator); + return BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator); } public getJaccardDenominatorMinCoefficient(): number { const positives: number = this.getPositives(); @@ -1132,7 +1211,7 @@ export class BinaryConfusionMatrix { denominator = predictedPositives; } const numerator: number = this.getTruePositives(); - return MathematicsHelper.safeLog(MathematicsHelper.safeDivide(numerator, denominator)); + return BinaryConfusionMatrix.MathematicsHelperObject.safeLog(BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator)); } public getJaccardDenominatorMaxCoefficient(): number { const positives: number = this.getPositives(); @@ -1142,7 +1221,7 @@ export class BinaryConfusionMatrix { denominator = predictedPositives; } const numerator: number = this.getTruePositives(); - return MathematicsHelper.safeLog(MathematicsHelper.safeDivide(numerator, denominator)); + return BinaryConfusionMatrix.MathematicsHelperObject.safeLog(BinaryConfusionMatrix.MathematicsHelperObject.safeDivide(numerator, denominator)); } public getMetrics( @@ -1156,8 +1235,8 @@ export class BinaryConfusionMatrix { beta: number = 1, A: number = 1, B: number = 1, - explicitMaxPositives: number = -1): { [id: string]: number; } { - const metrics: { [id: string]: number; } = {}; + explicitMaxPositives: number = -1): { [id: string]: number } { + const metrics: { [id: string]: number } = {}; const metricNames: string[] = this.getMetricNames(); const metricValues: number[] = @@ -1178,6 +1257,14 @@ export class BinaryConfusionMatrix { } return metrics; } + public getMetricNameMap(): IDictionaryStringIdGenericValue { + const metricNames: string[] = + this.getMetricNames(); + const metricNameMap: IDictionaryStringIdGenericValue = + DictionaryMapUtility.buildStringIdNumberValueDictionaryFromUniqueStringArray( + metricNames); + return metricNameMap; + } public getMetricNames(): string[] { const metricNames: string[] = [ "Total", @@ -1380,7 +1467,7 @@ export class BinaryConfusionMatrix { A: number = 1, B: number = 1, explicitMaxPositives: number = -1): number[] { - const metrics: number[] = [ + const metricValues: number[] = [ this.getTotal(), this.getCell11(), this.getRow1(), @@ -1566,6 +1653,6 @@ export class BinaryConfusionMatrix { B, explicitMaxPositives, ]; - return metrics; + return metricValues; } } diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/ConfusionMatrix.ts b/packages/dispatcher/src/mathematics/confusion_matrix/ConfusionMatrix.ts index 6552deb80..141652b56 100644 --- a/packages/dispatcher/src/mathematics/confusion_matrix/ConfusionMatrix.ts +++ b/packages/dispatcher/src/mathematics/confusion_matrix/ConfusionMatrix.ts @@ -3,133 +3,16 @@ * Licensed under the MIT License. */ +import { ConfusionMatrixBase } from "./ConfusionMatrixBase"; + import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; import { Utility } from "../../utility/Utility"; -export class ConfusionMatrix { - - public static generateConfusionMatrixMetricStructure( - confusionMatrix: ConfusionMatrix): { - "confusionMatrix": ConfusionMatrix, - "labelBinaryConfusionMatrixDerivedMetricMap": { [id: string]: { [id: string]: number }; }, - "labelBinaryConfusionMatrixMetricMap": { [id: string]: BinaryConfusionMatrix; }, - "macroAverageMetrics": { "averagePrecision": number, - "averageRecall": number, - "averageF1Score": number, - "support": number }, - "microAverageMetrics": { "accuracy": number, - "truePositives": number, - "support": number }, - "weightedMacroAverageMetrics": { "weightedAveragePrecision": number, - "weightedAverageRecall": number, - "weightedAverageF1Score": number, - "support": number } } { - const crossValidationBinaryConfusionMatrix: BinaryConfusionMatrix[] = - confusionMatrix.getBinaryConfusionMatrices(); - const labelMap: { [id: string]: number; } = - confusionMatrix.getLabelMap(); - const labelBinaryConfusionMatrixDerivedMetricMap: { [id: string]: { [id: string]: number }; } = - Object.entries(labelMap).reduce( - (accumulant, [id, value]) => - ({...accumulant, [id]: crossValidationBinaryConfusionMatrix[value].getBasicMetrics()}), {}); - const labelBinaryConfusionMatrixMetricMap: { [id: string]: BinaryConfusionMatrix; } = - Object.entries(labelMap).reduce( - (accumulant, [id, value]) => - ({...accumulant, [id]: crossValidationBinaryConfusionMatrix[value]}), {}); - const microAverageMetricArray: [number, number, number] = - confusionMatrix.getMicroAverageMetrics(); - const accuracy: number = - microAverageMetricArray[0]; - const truePositives: number = - microAverageMetricArray[1]; - const supportMicroAverage: number = - microAverageMetricArray[2]; - const microAverageMetrics: { - "accuracy": number, - "truePositives": number, - "support": number } = { - // tslint:disable-next-line: object-literal-key-quotes - "accuracy": accuracy, - // tslint:disable-next-line: object-literal-key-quotes - "truePositives": truePositives, - // tslint:disable-next-line: object-literal-key-quotes - "support": supportMicroAverage }; - const macroAverageMetricArray: [number, number, number, number] = - confusionMatrix.getMacroAverageMetrics(); - const averagePrecision: number = - macroAverageMetricArray[0]; - const averageRecall: number = - macroAverageMetricArray[1]; - const averageF1Score: number = - macroAverageMetricArray[2]; - const supportMacroAverage: number = - macroAverageMetricArray[3]; - const macroAverageMetrics: { - "averagePrecision": number, - "averageRecall": number, - "averageF1Score": number, - "support": number } = { - // tslint:disable-next-line: object-literal-key-quotes - "averagePrecision": averagePrecision, - // tslint:disable-next-line: object-literal-key-quotes - "averageRecall": averageRecall, - // tslint:disable-next-line: object-literal-key-quotes - "averageF1Score": averageF1Score, - // tslint:disable-next-line: object-literal-key-quotes - "support": supportMacroAverage }; - const weightedMacroAverageMetricArray: [number, number, number, number] = - confusionMatrix.getWeightedMacroAverageMetrics(); - const weightedAveragePrecision: number = - weightedMacroAverageMetricArray[0]; - const weightedAverageRecall: number = - weightedMacroAverageMetricArray[1]; - const weightedAverageF1Score: number = - weightedMacroAverageMetricArray[2]; - const supportWeightedMacroAverage: number = - weightedMacroAverageMetricArray[3]; - const weightedMacroAverageMetrics: { - "weightedAveragePrecision": number, - "weightedAverageRecall": number, - "weightedAverageF1Score": number, - "support": number } = { - // tslint:disable-next-line: object-literal-key-quotes - "weightedAveragePrecision": weightedAveragePrecision, - // tslint:disable-next-line: object-literal-key-quotes - "weightedAverageRecall": weightedAverageRecall, - // tslint:disable-next-line: object-literal-key-quotes - "weightedAverageF1Score": weightedAverageF1Score, - // tslint:disable-next-line: object-literal-key-quotes - "support": supportWeightedMacroAverage }; - const confusionMatrixMetricStructure: { - "confusionMatrix": ConfusionMatrix, - "labelBinaryConfusionMatrixDerivedMetricMap": { [id: string]: { [id: string]: number }; }, - "labelBinaryConfusionMatrixMetricMap": { [id: string]: BinaryConfusionMatrix; }, - "macroAverageMetrics": { "averagePrecision": number, - "averageRecall": number, - "averageF1Score": number, - "support": number }, - "microAverageMetrics": { "accuracy": number, - "truePositives": number, - "support": number }, - "weightedMacroAverageMetrics": { "weightedAveragePrecision": number, - "weightedAverageRecall": number, - "weightedAverageF1Score": number, - "support": number } } = { - confusionMatrix, - labelBinaryConfusionMatrixDerivedMetricMap, - labelBinaryConfusionMatrixMetricMap, - macroAverageMetrics, - microAverageMetrics, - weightedMacroAverageMetrics, - }; - return confusionMatrixMetricStructure; - } +export class ConfusionMatrix extends ConfusionMatrixBase { - protected labels: string[] = []; - protected labelMap: { [id: string]: number; } = {}; protected confusionMatrix: number[][] = []; protected confusionMatrixRows: number[] = []; protected confusionMatrixColumns: number[] = []; @@ -137,10 +20,12 @@ export class ConfusionMatrix { constructor( labels: string[], - labelMap: { [id: string]: number; }) { - DictionaryMapUtility.validateStringArrayAndStringIdNumberValueDictionary(labels, labelMap); - this.labels = labels; - this.labelMap = labelMap; + labelMap: { [id: string]: number }) { + super(labels, labelMap); + this.reset(); + } + + public reset(): void { this.confusionMatrix = []; this.confusionMatrixRows = []; this.confusionMatrixColumns = []; @@ -156,29 +41,20 @@ export class ConfusionMatrix { } } - public addFrom(confusionMatrix: ConfusionMatrix): void { - Utility.validateStringArrayPairEquality(this.labels, confusionMatrix.labels); - DictionaryMapUtility.validateStringIdNumberValueDictionaryPair(this.labelMap, confusionMatrix.labelMap); - this.confusionMatrixTotal += confusionMatrix.confusionMatrixTotal; + public addFrom(other: ConfusionMatrix): void { + Utility.validateStringArrayPairEquality(this.labels, other.labels); + DictionaryMapUtility.validateStringIdNumberValueDictionaryPair(this.labelMap, other.labelMap); + this.confusionMatrixTotal += other.confusionMatrixTotal; const numberLabels: number = this.getNumberLabels(); for (let row: number = 0; row < numberLabels; row++) { - this.confusionMatrixRows[row] += confusionMatrix.confusionMatrixRows[row]; - this.confusionMatrixColumns[row] += confusionMatrix.confusionMatrixColumns[row]; + this.confusionMatrixRows[row] += other.confusionMatrixRows[row]; + this.confusionMatrixColumns[row] += other.confusionMatrixColumns[row]; for (let column: number = 0; column < numberLabels; column++) { - this.confusionMatrix[row][column] += confusionMatrix.confusionMatrix[row][column]; + this.confusionMatrix[row][column] += other.confusionMatrix[row][column]; } } } - public getNumberLabels(): number { - return this.labels.length; - } - public getLabels(): string[] { - return this.labels; - } - public getLabelMap(): { [id: string]: number; } { - return this.labelMap; - } public getConfusionMatrixRows(): number[] { return this.confusionMatrixRows; } @@ -204,7 +80,7 @@ export class ConfusionMatrix { this.confusionMatrix[groundTrueLabelId][predictedLabelId] += value; } - public addInstance( + public addInstanceByLabel( groundTrueLabel: string, predictedLabel: string, value: number = 1): void { @@ -229,92 +105,4 @@ export class ConfusionMatrix { } return binaryConfusionMatrices; } - - public getMicroAverageMetrics(): [number, number, number] { - const binaryConfusionMatrices: BinaryConfusionMatrix[] = - this.getBinaryConfusionMatrices(); - const total: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getPositives(), 0); - const truePositives: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getTruePositives(), 0); - const microAverageMetrics: [number, number, number] = - [truePositives / total, truePositives, total]; - return microAverageMetrics; - } - - public getMacroAverageMetrics(): [number, number, number, number] { - const binaryConfusionMatrices: BinaryConfusionMatrix[] = - this.getBinaryConfusionMatrices(); - const numberLabels: number = - binaryConfusionMatrices.length; - const averagePrecision: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getPrecision(), 0) / numberLabels; - const averageRecall: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getRecall(), 0) / numberLabels; - const averageF1Score: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getF1Score(), 0) / numberLabels; - const total: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getPositives(), 0); - const macroAverageMetrics: [number, number, number, number] = - [averagePrecision, averageRecall, averageF1Score, total]; - return macroAverageMetrics; - } - - public getWeightedMacroAverageMetrics(): [number, number, number, number] { - const binaryConfusionMatrices: BinaryConfusionMatrix[] = - this.getBinaryConfusionMatrices(); - const total: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getPositives(), 0); - const averagePrecision: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getPrecision() * entry.getPositives(), 0) / total; - const averageRecall: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getRecall() * entry.getPositives(), 0) / total; - const averageF1Score: number = - binaryConfusionMatrices.reduce( - (accumulation, entry) => accumulation + entry.getF1Score() * entry.getPositives(), 0) / total; - const macroAverageMetrics: [number, number, number, number] = - [averagePrecision, averageRecall, averageF1Score, total]; - return macroAverageMetrics; - } - - public validateLabelId( - labelId: number, - throwIfNotLegal: boolean = true): boolean { - if (labelId < 0) { - if (throwIfNotLegal) { - Utility.debuggingThrow( - `labelId=${labelId}, small than 0`); - } - return false; - } - if (labelId >= this.getNumberLabels()) { - if (throwIfNotLegal) { - Utility.debuggingThrow( - `labelId=${labelId}, greater or equal to number of labels, ${this.getNumberLabels()}`); - } - return false; - } - return true; - } - public validateLabel( - label: string, - throwIfNotLegal: boolean = true): boolean { - if (!(label in this.getLabelMap())) { - if (throwIfNotLegal) { - Utility.debuggingThrow( - `label=${label}, not int the label map=${Utility.JSONstringify(this.getLabelMap())}`); - } - return false; - } - return true; - } } diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/ConfusionMatrixBase.ts b/packages/dispatcher/src/mathematics/confusion_matrix/ConfusionMatrixBase.ts new file mode 100644 index 000000000..2cf85b46b --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/ConfusionMatrixBase.ts @@ -0,0 +1,308 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { IConfusionMatrix } from "./IConfusionMatrix"; + +import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; + +import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; + +import { Utility } from "../../utility/Utility"; + +export abstract class ConfusionMatrixBase implements IConfusionMatrix { + + protected labels: string[] = []; + protected labelMap: { [id: string]: number } = {}; + + constructor( + labels: string[], + labelMap: { [id: string]: number }) { + this.resetLabelsAndMap(labels, labelMap); + } + + public abstract reset(): void; + + public generateConfusionMatrixMetricStructure(): { + "confusionMatrix": IConfusionMatrix, + "labelBinaryConfusionMatrixBasicMetricMap": { [id: string]: { [id: string]: number } }, + "labelBinaryConfusionMatrixMap": { [id: string]: BinaryConfusionMatrix }, + "macroAverageMetrics": { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number }, + "microAverageMetrics": { + "accuracy": number, + "truePositives": number, + "support": number }, + "weightedMacroAverageMetrics": { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } } { + const confusionMatrix: IConfusionMatrix = this; + const crossValidationBinaryConfusionMatrix: BinaryConfusionMatrix[] = + confusionMatrix.getBinaryConfusionMatrices(); + const labelMap: { [id: string]: number } = + confusionMatrix.getLabelMap(); + const labelBinaryConfusionMatrixBasicMetricMap: { [id: string]: { [id: string]: number } } = + Object.entries(labelMap).reduce( + (accumulant: { [id: string]: { [id: string]: number } }, [id, value]) => + ({...accumulant, [id]: crossValidationBinaryConfusionMatrix[value].getBasicMetrics()}), {}); + const labelBinaryConfusionMatrixMap: { [id: string]: BinaryConfusionMatrix } = + Object.entries(labelMap).reduce( + (accumulant: { [id: string]: BinaryConfusionMatrix }, [id, value]) => + ({...accumulant, [id]: crossValidationBinaryConfusionMatrix[value]}), {}); + const binaryConfusionMatrices: BinaryConfusionMatrix[] = + confusionMatrix.getBinaryConfusionMatrices(); + const microAverageMetricArray: { + "averagePrecisionRecallF1Accuracy": number, + "truePositives": number, + "total": number } = + confusionMatrix.getMicroAverageMetrics(binaryConfusionMatrices); + const accuracy: number = + microAverageMetricArray.averagePrecisionRecallF1Accuracy; + const truePositives: number = + microAverageMetricArray.truePositives; + const supportMicroAverage: number = + microAverageMetricArray.total; + const microAverageMetrics: { + "accuracy": number, + "truePositives": number, + "support": number } = { + // tslint:disable-next-line: object-literal-key-quotes + "accuracy": accuracy, + // tslint:disable-next-line: object-literal-key-quotes + "truePositives": truePositives, + // tslint:disable-next-line: object-literal-key-quotes + "support": supportMicroAverage }; + const macroAverageMetricArray: { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } = + confusionMatrix.getMacroAverageMetrics(binaryConfusionMatrices); + const averagePrecision: number = + macroAverageMetricArray.averagePrecision; + const averageRecall: number = + macroAverageMetricArray.averageRecall; + const averageF1Score: number = + macroAverageMetricArray.averageF1Score; + const supportMacroAverage: number = + macroAverageMetricArray.total; + const macroAverageMetrics: { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number } = { + // tslint:disable-next-line: object-literal-key-quotes + "averagePrecision": averagePrecision, + // tslint:disable-next-line: object-literal-key-quotes + "averageRecall": averageRecall, + // tslint:disable-next-line: object-literal-key-quotes + "averageF1Score": averageF1Score, + // tslint:disable-next-line: object-literal-key-quotes + "support": supportMacroAverage }; + const weightedMacroAverageMetricArray: { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } = + confusionMatrix.getWeightedMacroAverageMetrics(binaryConfusionMatrices); + const weightedAveragePrecision: number = + weightedMacroAverageMetricArray.averagePrecision; + const weightedAverageRecall: number = + weightedMacroAverageMetricArray.averageRecall; + const weightedAverageF1Score: number = + weightedMacroAverageMetricArray.averageF1Score; + const supportWeightedMacroAverage: number = + weightedMacroAverageMetricArray.total; + const weightedMacroAverageMetrics: { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } = { + // tslint:disable-next-line: object-literal-key-quotes + "weightedAveragePrecision": weightedAveragePrecision, + // tslint:disable-next-line: object-literal-key-quotes + "weightedAverageRecall": weightedAverageRecall, + // tslint:disable-next-line: object-literal-key-quotes + "weightedAverageF1Score": weightedAverageF1Score, + // tslint:disable-next-line: object-literal-key-quotes + "support": supportWeightedMacroAverage }; + const confusionMatrixMetricStructure: { + "confusionMatrix": IConfusionMatrix, + "labelBinaryConfusionMatrixBasicMetricMap": { [id: string]: { [id: string]: number } }, + "labelBinaryConfusionMatrixMap": { [id: string]: BinaryConfusionMatrix }, + "macroAverageMetrics": { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number }, + "microAverageMetrics": { + "accuracy": number, + "truePositives": number, + "support": number }, + "weightedMacroAverageMetrics": { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } } = { + confusionMatrix, + labelBinaryConfusionMatrixBasicMetricMap, + labelBinaryConfusionMatrixMap, + macroAverageMetrics, + microAverageMetrics, + weightedMacroAverageMetrics, + }; + return confusionMatrixMetricStructure; + } + + public getNumberLabels(): number { + return this.labels.length; + } + public getLabels(): string[] { + return this.labels; + } + public getLabelMap(): { [id: string]: number } { + return this.labelMap; + } + + public abstract getBinaryConfusionMatrices(): BinaryConfusionMatrix[]; + + public getMicroAverageMetrics(binaryConfusionMatrices: BinaryConfusionMatrix[] = []): { + "averagePrecisionRecallF1Accuracy": number, + "truePositives": number, + "total": number } { + if (Utility.isEmptyArray(binaryConfusionMatrices)) { + binaryConfusionMatrices = + this.getBinaryConfusionMatrices(); + } + const total: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getPositives(), 0); + const truePositives: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getTruePositives(), 0); + const averagePrecisionRecallF1Accuracy: number = + truePositives / total; + const microAverageMetrics: { + "averagePrecisionRecallF1Accuracy": number, + "truePositives": number, + "total": number } = { + averagePrecisionRecallF1Accuracy, + truePositives, + total }; + return microAverageMetrics; + } + + public getMacroAverageMetrics(binaryConfusionMatrices: BinaryConfusionMatrix[] = []): { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } { + if (Utility.isEmptyArray(binaryConfusionMatrices)) { + binaryConfusionMatrices = + this.getBinaryConfusionMatrices(); + } + const numberLabels: number = + binaryConfusionMatrices.length; + const averagePrecision: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getPrecision(), 0) / numberLabels; + const averageRecall: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getRecall(), 0) / numberLabels; + const averageF1Score: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getF1Score(), 0) / numberLabels; + const total: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getPositives(), 0); + const macroAverageMetrics: { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } = { + averagePrecision, + averageRecall, + averageF1Score, + total }; + return macroAverageMetrics; + } + + public getWeightedMacroAverageMetrics(binaryConfusionMatrices: BinaryConfusionMatrix[] = []): { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } { + if (Utility.isEmptyArray(binaryConfusionMatrices)) { + binaryConfusionMatrices = + this.getBinaryConfusionMatrices(); + } + const total: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getPositives(), 0); + const averagePrecision: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getPrecision() * entry.getPositives(), 0) / total; + const averageRecall: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getRecall() * entry.getPositives(), 0) / total; + const averageF1Score: number = + binaryConfusionMatrices.reduce( + (accumulation, entry) => accumulation + entry.getF1Score() * entry.getPositives(), 0) / total; + const macroAverageMetrics: { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } = { + averagePrecision, + averageRecall, + averageF1Score, + total }; + return macroAverageMetrics; + } + + public validateLabelId( + labelId: number, + throwIfNotLegal: boolean = true): boolean { + if (labelId < 0) { + if (throwIfNotLegal) { + Utility.debuggingThrow( + `labelId=${labelId}, small than 0`); + } + return false; + } + if (labelId >= this.getNumberLabels()) { + if (throwIfNotLegal) { + Utility.debuggingThrow( + `labelId=${labelId}, greater or equal to number of labels, ${this.getNumberLabels()}`); + } + return false; + } + return true; + } + public validateLabel( + label: string, + throwIfNotLegal: boolean = true): boolean { + if (!(label in this.getLabelMap())) { + if (throwIfNotLegal) { + Utility.debuggingThrow( + `label=${label}, not int the label map=${Utility.jsonStringify(this.getLabelMap())}`); + } + return false; + } + return true; + } + + protected resetLabelsAndMap( + labels: string[], + labelMap: { [id: string]: number }): void { + DictionaryMapUtility.validateStringArrayAndStringIdNumberValueDictionary(labels, labelMap); + this.labels = labels; + this.labelMap = labelMap; + } +} diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/IConfusionMatrix.ts b/packages/dispatcher/src/mathematics/confusion_matrix/IConfusionMatrix.ts new file mode 100644 index 000000000..ab738d2ee --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/IConfusionMatrix.ts @@ -0,0 +1,60 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; + +export interface IConfusionMatrix { + + reset(): void; + + generateConfusionMatrixMetricStructure(): { + "confusionMatrix": IConfusionMatrix, + "labelBinaryConfusionMatrixBasicMetricMap": { [id: string]: { [id: string]: number } }, + "labelBinaryConfusionMatrixMap": { [id: string]: BinaryConfusionMatrix }, + "macroAverageMetrics": { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number }, + "microAverageMetrics": { + "accuracy": number, + "truePositives": number, + "support": number }, + "weightedMacroAverageMetrics": { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } }; + + getNumberLabels(): number; + getLabels(): string[]; + getLabelMap(): { [id: string]: number }; + + getBinaryConfusionMatrices(): BinaryConfusionMatrix[]; + + getMicroAverageMetrics(binaryConfusionMatrices: BinaryConfusionMatrix[]): { + "averagePrecisionRecallF1Accuracy": number, + "truePositives": number, + "total": number }; + + getMacroAverageMetrics(binaryConfusionMatrices: BinaryConfusionMatrix[]): { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number }; + + getWeightedMacroAverageMetrics(binaryConfusionMatrices: BinaryConfusionMatrix[]): { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number }; + + validateLabelId( + labelId: number, + throwIfNotLegal: boolean): boolean; + validateLabel( + label: string, + throwIfNotLegal: boolean): boolean; +} diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/IMultiLabelConfusionMatrix.ts b/packages/dispatcher/src/mathematics/confusion_matrix/IMultiLabelConfusionMatrix.ts new file mode 100644 index 000000000..7d84e3e32 --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/IMultiLabelConfusionMatrix.ts @@ -0,0 +1,21 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { IConfusionMatrix } from "./IConfusionMatrix"; + +export interface IMultiLabelConfusionMatrix extends IConfusionMatrix { + + addInstanceByLabelIndexes( + groundTrueLabelIds: number[], + predictedLabelIds: number[], + value: number): void; + + validateLabelIds( + labelIds: number[], + throwIfNotLegal: boolean): boolean; + validateLabels( + labels: string[], + throwIfNotLegal: boolean): boolean; +} diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrix.ts b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrix.ts new file mode 100644 index 000000000..3435a3073 --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrix.ts @@ -0,0 +1,50 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { IMultiLabelConfusionMatrix } from "./IMultiLabelConfusionMatrix"; +import { MultiLabelConfusionMatrixWithBinaryArrayBase } from "./MultiLabelConfusionMatrixWithBinaryArrayBase"; +import { ConfusionMatrixBase } from "./ConfusionMatrixBase"; +import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; + +import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; + +import { Utility } from "../../utility/Utility"; + +export class MultiLabelConfusionMatrix extends MultiLabelConfusionMatrixWithBinaryArrayBase { + + constructor( + labels: string[], + labelMap: { [id: string]: number }) { + super(labels, labelMap); + } + + public addInstanceByLabelIndexes( + groundTrueLabelIds: number[], + predictedLabelIds: number[], + value: number = 1): void { + this.validateLabelIds(groundTrueLabelIds); + this.validateLabelIds(predictedLabelIds); + for (let labelId: number = 0; labelId < this.getNumberLabels(); labelId++) { + const isInGroundTruthLabelIds: boolean = this.isLabelIdInArray(groundTrueLabelIds, labelId); + const isInPredictedLabelIds: boolean = this.isLabelIdInArray(predictedLabelIds, labelId); + if (isInGroundTruthLabelIds) { + if (isInPredictedLabelIds) { + this.getBinaryConfusionMatrices()[labelId].addToTruePositives(value, false); + } else { + this.getBinaryConfusionMatrices()[labelId].addToFalseNegatives(value, false); + } + } else { + if (isInPredictedLabelIds) { + this.getBinaryConfusionMatrices()[labelId].addToFalsePositives(value, false); + } else { + this.getBinaryConfusionMatrices()[labelId].addToTrueNegatives(value, false); + } + } + } + for (let labelId: number = 0; labelId < this.getNumberLabels(); labelId++) { + this.getBinaryConfusionMatrices()[labelId].calculateDerivedCells(); + } + } +} diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixBase.ts b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixBase.ts new file mode 100644 index 000000000..d42bebcfc --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixBase.ts @@ -0,0 +1,79 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { IMultiLabelConfusionMatrix } from "./IMultiLabelConfusionMatrix"; +import { ConfusionMatrixBase } from "./ConfusionMatrixBase"; +import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; + +import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; + +import { Utility } from "../../utility/Utility"; + +export abstract class MultiLabelConfusionMatrixBase +extends ConfusionMatrixBase implements IMultiLabelConfusionMatrix { + + protected binaryConfusionMatrices: BinaryConfusionMatrix[] = []; + + constructor( + labels: string[], + labelMap: { [id: string]: number }) { + super(labels, labelMap); + } + + public addFrom(other: MultiLabelConfusionMatrixBase): void { + Utility.validateStringArrayPairEquality(this.labels, other.labels); + DictionaryMapUtility.validateStringIdNumberValueDictionaryPair(this.labelMap, other.labelMap); + const numbergetBinaryConfusionMatrices: number = this.getBinaryConfusionMatrices().length; + for (let l: number = 0; l < numbergetBinaryConfusionMatrices; l++) { + this.binaryConfusionMatrices[l].addFrom(other.binaryConfusionMatrices[l]); + } + } + + public abstract addInstanceByLabelIndexes( + groundTrueLabelIds: number[], + predictedLabelIds: number[], + value: number): void; + + public addInstanceByLabels( + groundTrueLabels: string[], + predictedLabels: string[], + value: number = 1): void { + this.validateLabels(groundTrueLabels); + this.validateLabels(predictedLabels); + const groundTrueLabelIds: number[] = groundTrueLabels.map((x: string) => this.labelMap[x]); + const predictedLabelIds: number[] = predictedLabels.map((x: string) => this.labelMap[x]); + this.addInstanceByLabelIndexes(groundTrueLabelIds, predictedLabelIds, value); + } + + public getBinaryConfusionMatrices(): BinaryConfusionMatrix[] { + return this.binaryConfusionMatrices; + } + + public validateLabelIds( + labelIds: number[], + throwIfNotLegal: boolean = true): boolean { + for (const labelId of labelIds) { + if (!this.validateLabelId(labelId, throwIfNotLegal)) { + return false; + } + } + return true; + } + public validateLabels( + labels: string[], + throwIfNotLegal: boolean = true): boolean { + for (const label of labels) { + if (!this.validateLabel(label, throwIfNotLegal)) { + return false; + } + } + return true; + } + + // ---- NOTE ---- label id set is usually very small, mostly 1, so a linear search is sufficiently fast. + public isLabelIdInArray(labelIds: number[], labelId: number): boolean { + return labelIds.includes(labelId); + } +} diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixSubset.ts b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixSubset.ts new file mode 100644 index 000000000..50b61f323 --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixSubset.ts @@ -0,0 +1,49 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { IMultiLabelConfusionMatrix } from "./IMultiLabelConfusionMatrix"; +import { MultiLabelConfusionMatrixWithBinaryBase } from "./MultiLabelConfusionMatrixWithBinaryBase"; +import { ConfusionMatrixBase } from "./ConfusionMatrixBase"; +import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; + +import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; + +import { Utility } from "../../utility/Utility"; + +export class MultiLabelConfusionMatrixSubset extends MultiLabelConfusionMatrixWithBinaryBase { + + constructor( + labels: string[], + labelMap: { [id: string]: number }) { + super(labels, labelMap); + } + + public addInstanceByLabelIndexes( + groundTrueLabelIds: number[], + predictedLabelIds: number[], + value: number = 1): void { + this.validateLabelIds(groundTrueLabelIds); + this.validateLabelIds(predictedLabelIds); + if (Utility.isEmptyNumberArray(predictedLabelIds)) { + if (Utility.isEmptyNumberArray(groundTrueLabelIds)) { + this.getBinaryConfusionMatrix().addToTruePositives(value, true); + } + this.getBinaryConfusionMatrix().addToFalseNegatives(value, true); + return; + } + let isPredictionSubsetOfGroundTruthLabelIds: boolean = true; + for (const predictedLabelId of predictedLabelIds) { + if (!this.isLabelIdInArray(groundTrueLabelIds, predictedLabelId)) { + isPredictionSubsetOfGroundTruthLabelIds = false; + break; + } + } + if (isPredictionSubsetOfGroundTruthLabelIds) { + this.getBinaryConfusionMatrix().addToTruePositives(value, true); + } else { + this.getBinaryConfusionMatrix().addToFalsePositives(value, true); + } + } +} diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryArrayBase.ts b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryArrayBase.ts new file mode 100644 index 000000000..7c93c0947 --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryArrayBase.ts @@ -0,0 +1,33 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { IMultiLabelConfusionMatrix } from "./IMultiLabelConfusionMatrix"; +import { MultiLabelConfusionMatrixBase } from "./MultiLabelConfusionMatrixBase"; +import { ConfusionMatrixBase } from "./ConfusionMatrixBase"; +import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; + +import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; + +import { Utility } from "../../utility/Utility"; + +export abstract class MultiLabelConfusionMatrixWithBinaryArrayBase +extends MultiLabelConfusionMatrixBase { + + constructor( + labels: string[], + labelMap: { [id: string]: number }) { + super(labels, labelMap); + this.reset(); + } + + public reset(): void { + const numberLabels: number = this.getNumberLabels(); + this.binaryConfusionMatrices = + new Array(numberLabels); + for (let l: number = 0; l < numberLabels; l++) { + this.binaryConfusionMatrices[l] = new BinaryConfusionMatrix(); + } + } +} diff --git a/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryBase.ts b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryBase.ts new file mode 100644 index 000000000..9e48ae273 --- /dev/null +++ b/packages/dispatcher/src/mathematics/confusion_matrix/MultiLabelConfusionMatrixWithBinaryBase.ts @@ -0,0 +1,40 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { IMultiLabelConfusionMatrix } from "./IMultiLabelConfusionMatrix"; +import { MultiLabelConfusionMatrixBase } from "./MultiLabelConfusionMatrixBase"; +import { ConfusionMatrixBase } from "./ConfusionMatrixBase"; +import { BinaryConfusionMatrix } from "./BinaryConfusionMatrix"; + +import { DictionaryMapUtility } from "../../data_structure/DictionaryMapUtility"; + +import { Utility } from "../../utility/Utility"; + +export abstract class MultiLabelConfusionMatrixWithBinaryBase +extends MultiLabelConfusionMatrixBase { + + protected binaryConfusionMatrix: BinaryConfusionMatrix = new BinaryConfusionMatrix(); + + constructor( + labels: string[], + labelMap: { [id: string]: number }) { + super(labels, labelMap); + this.reset(); + } + + public reset(): void { + const numberLabels: number = this.getNumberLabels(); + this.binaryConfusionMatrix = + new BinaryConfusionMatrix(); + this.binaryConfusionMatrices = + new Array(1); + this.binaryConfusionMatrices[0] = + this.binaryConfusionMatrix; + } + + public getBinaryConfusionMatrix(): BinaryConfusionMatrix { + return this.binaryConfusionMatrix; + } +} diff --git a/packages/dispatcher/src/mathematics/mathematics_helper/AppMathematicsHelper.ts b/packages/dispatcher/src/mathematics/mathematics_helper/AppMathematicsHelper.ts index 3f187f8ca..845235c43 100644 --- a/packages/dispatcher/src/mathematics/mathematics_helper/AppMathematicsHelper.ts +++ b/packages/dispatcher/src/mathematics/mathematics_helper/AppMathematicsHelper.ts @@ -3,21 +3,26 @@ * Licensed under the MIT License. */ +import { IMathematicsHelper } from "./IMathematicsHelper"; import { MathematicsHelper } from "./MathematicsHelper"; import { Utility } from "../../utility/Utility"; +const MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + export function exampleFunctionMathematicsHelper(): void { // ----------------------------------------------------------------------- const numerator = 1; const denominator = 2; Utility.debuggingLog( - MathematicsHelper.safeDivide(numerator, denominator)); - Utility.debuggingLog(MathematicsHelper.safeLog( - MathematicsHelper.safeDivide(numerator, denominator))); + MathematicsHelperObject.safeDivide(numerator, denominator)); + Utility.debuggingLog( + MathematicsHelperObject.safeLog( + MathematicsHelperObject.safeDivide(numerator, denominator))); // ----------------------------------------------------------------------- const inputValue: number = 0.5; - const inputValueSigmoid: number = MathematicsHelper.sigmoidLogisticFunction(inputValue); + const inputValueSigmoid: number = MathematicsHelperObject.sigmoidLogisticFunction(inputValue); Utility.debuggingLog(inputValueSigmoid); // ----------------------------------------------------------------------- } @@ -27,12 +32,13 @@ export function exampleFunctionMathematicsHelperSoftmax(): void { const numerator = 1; const denominator = 2; Utility.debuggingLog( - MathematicsHelper.safeDivide(numerator, denominator)); - Utility.debuggingLog(MathematicsHelper.safeLog( - MathematicsHelper.safeDivide(numerator, denominator))); + MathematicsHelperObject.safeDivide(numerator, denominator)); + Utility.debuggingLog( + MathematicsHelperObject.safeLog( + MathematicsHelperObject.safeDivide(numerator, denominator))); // ----------------------------------------------------------------------- const inputValue: number = 0.5; - const inputValueSigmoid: number = MathematicsHelper.sigmoidLogisticFunction(inputValue); + const inputValueSigmoid: number = MathematicsHelperObject.sigmoidLogisticFunction(inputValue); Utility.debuggingLog(inputValueSigmoid); // ----------------------------------------------------------------------- /* diff --git a/packages/dispatcher/src/mathematics/mathematics_helper/IMathematicsHelper.ts b/packages/dispatcher/src/mathematics/mathematics_helper/IMathematicsHelper.ts new file mode 100644 index 000000000..56386d0bf --- /dev/null +++ b/packages/dispatcher/src/mathematics/mathematics_helper/IMathematicsHelper.ts @@ -0,0 +1,1136 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import { Utility } from "../../utility/Utility"; + +export interface IMathematicsHelper { + + // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/Softmax_function + softmaxSingleFunction(inputs: number[], index: number): number; + smoothArgmaxApproximationSingleFunction(inputs: number[], index: number): number; + // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/Softmax_function + softmaxFunction(inputs: number[]): number[]; + smoothArgmaxApproximationFunction(inputs: number[]): number[]; + + // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/LogSumExp + logsumexpStrictConvexSingleFunction(inputs: number[]): number; + smoothMaxApproximationStrictConvexFunction(inputs: number[]): number; + // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/LogSumExp + logsumexpSingleFunction(inputs: number[]): number; + smoothMaxApproximationFunction(inputs: number[]): number; + + sigmoidLogisticGradientFunction(input: number): number; + + sigmoidLogisticFunction(input: number): number; + sigmoidHyperbolicTangentFunction(input: number): number; + sigmoidArctangentFunction(input: number): number; + sigmoidGudermannianFunction(input: number): number; + sigmoidGeneralizedLogisticFunction(input: number, alpha: number): number; + sigmoidAlgebraicFunction(input: number): number; + + getL1Regularized(weight: number, l1Regularization: number): number; + getL2Regularized(weight: number, l2Regularization: number): number; + getL1l2RegularizedWeightOptimizedSparse( + weight: number, + l1Regularization: number, + l2Regularization: number): number; + getL1l2RegularizedWeightOptimizedDense( + weight: number, + l1Regularization: number, + l2Regularization: number): number; + + /* + * return: + * softmaxVectors: number[][]: + * update: + * matrixWeightDenseArrays + * biasVectorDenseValueArray + * input: + * instanceGroundTruthPositiveLabelIndexes: + * Each element is a label index. + * Dimension: N, N: #instances. + * instanceFeatureVectorSparseIndexArrays: + * Each row represents a sparse feature, one-hot-encoder vector for an input instance. + * #rows is the number of input instances + * #columns is the number of space feature index for that row/instance. + * There is no limit to the length of each row, as long as the elements, feature indexes, + * fall in the feature range [0, #features). + * Dimension: N X iF, N: #instances, iF: indefinite #features. + * matrixWeightDenseArrays: + * Each row represents a dense feature, floating-point weight vector for a label. + * Row length is equal to #features. + * Dimension: L x F, L: #labels, F: #features. + * biasVectorDenseValueArray: + * A bias vector, each element is for a label. + * #biases is equal to #labels. + * Dimension: L, L: #labels. + * learningRate: + * learning rate for SGD. + * l1Regularization: + * l1 regularization coefficient. + * l2Regularization: + * l2 regularization coefficient. + * instanceFeatureVectorIndexBegin: + * The begin index for a mini batch. + * instanceFeatureVectorIndexEnd: + * The end index for a mini batch. + * internal data structure: + * matrixWeightGradientDenseArrays: + * Each row represents a dense feature gradient vector for a label. + * Row length is equal to #features. + * Dimension: L x F, L: #labels, F: #features. + * biasVectorGradientDenseValueArray: + * Each element represents a bias-term gradient for a label. + * #biases is equal to #labels. + * Dimension: L, L: #labels. + */ + softmaxLogLossGradientUpdate( + instanceGroundTruthPositiveLabelIndexes: number[], + instanceFeatureVectorSparseIndexArrays: number[][], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[], + learningRate: number, + l1Regularization: number, + l2Regularization: number, + instanceFeatureVectorIndexBegin: number, + instanceFeatureVectorIndexEnd: number): number[][]; + + logLoss( + probabilityVector: number[], + instanceGroundTruthPositiveLabelIndex: number): number; + logLossGeneric( + probabilityVector: number[], + labelVector: number[]): number; + + softmaxLogLoss( + softmaxVectors: number[][], + instanceGroundTruthPositiveLabelIndexes: number[]): number; + softmaxLogLossGeneric( + softmaxVectors: number[][], + labelVectors: number[][]): number; + + matrixVectorProductSoftmaxSparseIndexesValues( + instanceFeatureVectorSparseIndexArrays: number[][], + instanceFeatureVectorSparseValueArrays: number[][], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[], + instanceFeatureVectorIndexBegin: number, + instanceFeatureVectorIndexEnd: number): number[][]; + /* + * return: + * softmaxVectors: + * Each row is a softmax vector for an input instance. + * #rows is equivalent to #labels. + * Dimension: N X L, N: #instances, L: #labels. + * inputs: + * instanceFeatureVectorSparseIndexArrays: + * Each row represents a sparse feature, one-hot-encoder vector for an input instance. + * #rows is the number of input instances + * #columns is the number of space feature index for that row/instance. + * There is no limit to the length of each row, as long as the elements, feature indexes, + * fall in the feature range [0, #features). + * Dimension: N X iF, N: #instances, iF: indefinite #features. + * matrixWeightDenseArrays: + * Each row represents a dense feature, floating-point weight vector for a label. + * Row length is equal to #features. + * Dimension: L x F, L: #labels, F: #features. + * biasVectorDenseValueArray: + * A bias vector, each element is for a label. + * #biases is equal to #labels. + * Dimension: L, L: #labels. + * instanceFeatureVectorIndexBegin: + * The begin index for a mini batch. + * instanceFeatureVectorIndexEnd: + * The end index for a mini batch. + */ + matrixVectorProductSoftmaxSparseIndexes( + instanceFeatureVectorSparseIndexArrays: number[][], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[], + instanceFeatureVectorIndexBegin: number, + instanceFeatureVectorIndexEnd: number): number[][]; + /* + * return: + * softmaxVectors: + * Each row is a softmax vector for an input instance. + * #rows is equivalent to #labels. + * Dimension: N X L, N: #instances, L: #labels. + * inputs: + * instanceFeatureVectorDenseValueArrays: + * Each row represents a dense feature value vector for an input instance. + * #rows is the number of input instances + * #columns is the number of dense features for that row/instance. + * There is no limit to the length of each row should be equal to the number of features. + * Dimension: N X F, N: #instances, F: #features. + * matrixWeightDenseArrays: + * Each row represents a dense feature, floating-point weight vector for a label. + * Row length is equal to #features. + * Dimension: L x F, L: #labels, F: #features. + * biasVectorDenseValueArray: + * A bias vector, each element is for a label. + * #biases is equal to #labels. + * Dimension: L, L: #labels. + * instanceFeatureVectorIndexBegin: + * The begin index for a mini batch. + * instanceFeatureVectorIndexEnd: + * The end index for a mini batch. + */ + matrixVectorProductSoftmaxDenseValues( + instanceFeatureVectorDenseValueArrays: number[][], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[], + instanceFeatureVectorIndexBegin: number, + instanceFeatureVectorIndexEnd: number): number[][]; + + matrixVectorProductSparseIndexesValues( + instanceFeatureVectorSparseIndexArray: number[], + instanceFeatureVectorSparseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + matrixVectorProductSparseIndexes( + instanceFeatureVectorSparseIndexArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + matrixVectorProductDenseValues( + vectorDenseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + + matrixVectorProductSparseIndexesValuesTo( + matrixVectorProduct: number[], + instanceFeatureVectorSparseIndexArray: number[], + instanceFeatureVectorSparseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + matrixVectorProductSparseIndexesTo( + matrixVectorProduct: number[], + instanceFeatureVectorSparseIndexArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + matrixVectorProductDenseValuesTo( + matrixVectorProduct: number[], + vectorDenseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + + vectorMatrixProductSparseIndexesValues( + instanceFeatureVectorSparseIndexArray: number[], + instanceFeatureVectorSparseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + vectorMatrixProductSparseIndexes( + instanceFeatureVectorSparseIndexArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + vectorMatrixProductDenseValues( + vectorDenseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + + vectorMatrixProductSparseIndexesValuesTo( + vectorMatrixProduct: number[], + instanceFeatureVectorSparseIndexArray: number[], + instanceFeatureVectorSparseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + vectorMatrixProductSparseIndexesTo( + vectorMatrixProduct: number[], + instanceFeatureVectorSparseIndexArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + vectorMatrixProductDenseValuesTo( + vectorMatrixProduct: number[], + vectorDenseValueArray: number[], + matrixWeightDenseArrays: number[][], + biasVectorDenseValueArray: number[]): number[]; + + dotProductSparseIndexesValues( + sparseIndexArray: number[], + sparseValueArray: number[], + weights: number[], + weightBias: number): number; + dotProductSparseIndexes( + sparseIndexArray: number[], + weights: number[], + weightBias: number): number; + dotProductDenseValues( + denseValueArray: number[], + weights: number[], + weightBias: number): number; + + matrixDenseSubtractScaledFromAndL1l2RegularizedSparseTo( + denseValueArray0: number[][], + denseValueArray1: number[][], + constant: number, + l1Regularization: number, + l2Regularization: number): number[][]; + matrixDenseSubtractScaledFromAndL1l2RegularizedDenseTo( + denseValueArray0: number[][], + denseValueArray1: number[][], + constant: number, + l1Regularization: number, + l2Regularization: number): number[][]; + vectorDenseSubtractScaledFromAndL1l2RegularizedSparseTo( + denseValueArray0: number[], + denseValueArray1: number[], + constant: number, + l1Regularization: number, + l2Regularization: number): number[]; + vectorDenseSubtractScaledFromAndL1l2RegularizedDenseTo( + denseValueArray0: number[], + denseValueArray1: number[], + constant: number, + l1Regularization: number, + l2Regularization: number): number[]; + + matrixDenseL1l2RegularizedSparseTo( + denseValueArray: number[][], + l1Regularization: number, + l2Regularization: number): number[][]; + matrixDenseL1l2RegularizedDenseTo( + denseValueArray: number[][], + l1Regularization: number, + l2Regularization: number): number[][]; + + vectorDenseL1l2RegularizedSparseTo( + denseValueArray: number[], + l1Regularization: number, + l2Regularization: number): number[]; + vectorDenseL1l2RegularizedDenseTo( + denseValueArray: number[], + l1Regularization: number, + l2Regularization: number): number[]; + + tensor4dDenseAssignRandomTo( + denseValueArray0: number[][][][]): number[][][][]; + + tensor4dDenseAssignConstantTo( + denseValueArray0: number[][][][], + constant: number): number[][][][]; + tensor4dDenseAddConstantTo( + denseValueArray0: number[][][][], + constant: number): number[][][][]; + tensor4dDenseMultiplyConstantTo( + denseValueArray0: number[][][][], + constant: number): number[][][][]; + tensor4dDenseSubtractConstantFrom( + denseValueArray0: number[][][][], + constant: number): number[][][][]; + tensor4dDenseDivideConstantFrom( + denseValueArray0: number[][][][], + constant: number): number[][][][]; + + tensor4dDenseAssignTo( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][]): number[][][][]; + tensor4dDenseAddTo( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][]): number[][][][]; + tensor4dDenseMultiplyTo( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][]): number[][][][]; + tensor4dDenseSubtractFrom( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][]): number[][][][]; + tensor4dDenseDivideFrom( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][]): number[][][][]; + + tensor4dDenseAssignScaledTo( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][], + constant: number): number[][][][]; + tensor4dDenseAddScaledTo( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][], + constant: number): number[][][][]; + tensor4dDenseMultiplyScaledTo( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][], + constant: number): number[][][][]; + tensor4dDenseSubtractScaledFrom( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][], + constant: number): number[][][][]; + tensor4dDenseDivideScaledFrom( + denseValueArray0: number[][][][], + denseValueArray1: number[][][][], + constant: number): number[][][][]; + + tensor3dDenseAssignRandomTo( + denseValueArray0: number[][][]): number[][][]; + + tensor3dDenseAssignConstantTo( + denseValueArray0: number[][][], + constant: number): number[][][]; + tensor3dDenseAddConstantTo( + denseValueArray0: number[][][], + constant: number): number[][][]; + tensor3dDenseMultiplyConstantTo( + denseValueArray0: number[][][], + constant: number): number[][][]; + tensor3dDenseSubtractConstantFrom( + denseValueArray0: number[][][], + constant: number): number[][][]; + tensor3dDenseDivideConstantFrom( + denseValueArray0: number[][][], + constant: number): number[][][]; + + tensor3dDenseAssignTo( + denseValueArray0: number[][][], + denseValueArray1: number[][][]): number[][][]; + tensor3dDenseAddTo( + denseValueArray0: number[][][], + denseValueArray1: number[][][]): number[][][]; + tensor3dDenseMultiplyTo( + denseValueArray0: number[][][], + denseValueArray1: number[][][]): number[][][]; + tensor3dDenseSubtractFrom( + denseValueArray0: number[][][], + denseValueArray1: number[][][]): number[][][]; + tensor3dDenseDivideFrom( + denseValueArray0: number[][][], + denseValueArray1: number[][][]): number[][][]; + + tensor3dDenseAssignScaledTo( + denseValueArray0: number[][][], + denseValueArray1: number[][][], + constant: number): number[][][]; + tensor3dDenseAddScaledTo( + denseValueArray0: number[][][], + denseValueArray1: number[][][], + constant: number): number[][][]; + tensor3dDenseMultiplyScaledTo( + denseValueArray0: number[][][], + denseValueArray1: number[][][], + constant: number): number[][][]; + tensor3dDenseSubtractScaledFrom( + denseValueArray0: number[][][], + denseValueArray1: number[][][], + constant: number): number[][][]; + tensor3dDenseDivideScaledFrom( + denseValueArray0: number[][][], + denseValueArray1: number[][][], + constant: number): number[][][]; + + matrixDenseAssignRandomTo( + denseValueArray0: number[][]): number[][]; + + matrixDenseAssignConstantTo( + denseValueArray0: number[][], + constant: number): number[][]; + matrixDenseAddConstantTo( + denseValueArray0: number[][], + constant: number): number[][]; + matrixDenseMultiplyConstantTo( + denseValueArray0: number[][], + constant: number): number[][]; + matrixDenseSubtractConstantFrom( + denseValueArray0: number[][], + constant: number): number[][]; + matrixDenseDivideConstantFrom( + denseValueArray0: number[][], + constant: number): number[][]; + + matrixDenseAssignTo( + denseValueArray0: number[][], + denseValueArray1: number[][]): number[][]; + matrixDenseAddTo( + denseValueArray0: number[][], + denseValueArray1: number[][]): number[][]; + matrixDenseMultiplyTo( + denseValueArray0: number[][], + denseValueArray1: number[][]): number[][]; + matrixDenseSubtractFrom( + denseValueArray0: number[][], + denseValueArray1: number[][]): number[][]; + matrixDenseDivideFrom( + denseValueArray0: number[][], + denseValueArray1: number[][]): number[][]; + + matrixDenseAssignScaledTo( + denseValueArray0: number[][], + denseValueArray1: number[][], + constant: number): number[][]; + matrixDenseAddScaledTo( + denseValueArray0: number[][], + denseValueArray1: number[][], + constant: number): number[][]; + matrixDenseMultiplyScaledTo( + denseValueArray0: number[][], + denseValueArray1: number[][], + constant: number): number[][]; + matrixDenseSubtractScaledFrom( + denseValueArray0: number[][], + denseValueArray1: number[][], + constant: number): number[][]; + matrixDenseDivideScaledFrom( + denseValueArray0: number[][], + denseValueArray1: number[][], + constant: number): number[][]; + + vectorDenseAssignRandomTo( + denseValueArray0: number[]): number[]; + + vectorDenseAssignConstantTo( + denseValueArray0: number[], + constant: number): number[]; + vectorDenseAddConstantTo( + denseValueArray0: number[], + constant: number): number[]; + vectorDenseMultiplyConstantTo( + denseValueArray0: number[], + constant: number): number[]; + vectorDenseSubtractConstantFrom( + denseValueArray0: number[], + constant: number): number[]; + vectorDenseDivideConstantFrom( + denseValueArray0: number[], + constant: number): number[]; + + vectorDenseAssignTo( + denseValueArray0: number[], + denseValueArray1: number[]): number[]; + vectorDenseAddTo( + denseValueArray0: number[], + denseValueArray1: number[]): number[]; + vectorDenseMultiplyTo( + denseValueArray0: number[], + denseValueArray1: number[]): number[]; + vectorDenseSubtractFrom( + denseValueArray0: number[], + denseValueArray1: number[]): number[]; + vectorDenseDivideFrom( + denseValueArray0: number[], + denseValueArray1: number[]): number[]; + + vectorDenseAssignScaledTo( + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): number[]; + vectorDenseAddScaledTo( + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): number[]; + vectorDenseMultiplyScaledTo( + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): number[]; + vectorDenseSubtractScaledFrom( + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): number[]; + vectorDenseDivideScaledFrom( + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): number[]; + + vectorSparseAssignRandomTo( + sparseIndexArray0: number[], + sparseValueArray0: number[]): [number[], number[]]; + + vectorSparseAssignConstantTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseAddConstantTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseMultiplyConstantTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseSubtractConstantFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseDivideConstantFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + constant: number): [number[], number[]]; + + vectorSparseAssignTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[]): [number[], number[]]; + vectorSparseAddTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[]): [number[], number[]]; + vectorSparseMultiplyTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[]): [number[], number[]]; + vectorSparseSubtractFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[]): [number[], number[]]; + vectorSparseDivideFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[]): [number[], number[]]; + + vectorSparseAssignScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseAddScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseMultiplyScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseSubtractScaledFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseDivideScaledFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + constant: number): [number[], number[]]; + + vectorSparseIndexDenseArrayAssignRandomTo( + sparseIndexArray0: number[], + denseValueArray0: number[]): [number[], number[]]; + + vectorSparseIndexDenseArrayAssignConstantTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArrayAddConstantTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArrayMultiplyConstantTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArraySubtractConstantFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArrayDivideConstantFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + + vectorSparseIndexDenseArrayAssignTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseIndexDenseArrayAddTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseIndexDenseArrayMultiplyTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseIndexDenseArraySubtractFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseIndexDenseArrayDivideFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + + vectorSparseIndexDenseArrayAssignScaledTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArrayAddScaledTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArrayMultiplyScaledTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArraySubtractScaledFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseIndexDenseArrayDivideScaledFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + + vectorIndependentSparseIndexDenseArrayAssignTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseIndexDenseArrayAddTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseIndexDenseArrayMultiplyTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseIndexDenseArraySubtractFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseIndexDenseArrayDivideFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + + vectorIndependentSparseIndexDenseArrayAssignScaledTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseIndexDenseArrayAddScaledTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseIndexDenseArrayMultiplyScaledTo( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseIndexDenseArraySubtractScaledFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseIndexDenseArrayDivideScaledFrom( + sparseIndexArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + + vectorSparseMapDenseArrayAssignRandomTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[]): [number[], number[]]; + + vectorSparseMapDenseArrayAssignConstantTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArrayAddConstantTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArrayMultiplyConstantTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArraySubtractConstantFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArrayDivideConstantFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + constant: number): [number[], number[]]; + + vectorSparseMapDenseArrayAssignTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseMapDenseArrayAddTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseMapDenseArrayMultiplyTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseMapDenseArraySubtractFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorSparseMapDenseArrayDivideFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[]): [number[], number[]]; + + vectorSparseMapDenseArrayAssignScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArrayAddScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArrayMultiplyScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArraySubtractScaledFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorSparseMapDenseArrayDivideScaledFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + + vectorIndependentSparseMapDenseArrayAssignTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseMapDenseArrayAddTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseMapDenseArrayMultiplyTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseMapDenseArraySubtractFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + vectorIndependentSparseMapDenseArrayDivideFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[]): [number[], number[]]; + + vectorIndependentSparseMapDenseArrayAssignScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseMapDenseArrayAddScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseMapDenseArrayMultiplyScaledTo( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseMapDenseArraySubtractScaledFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + vectorIndependentSparseMapDenseArrayDivideScaledFrom( + sparseIndexArray0: number[], + sparseValueArray0: number[], + denseValueArray0: number[], + sparseIndexArray1: number[], + sparseValueArray1: number[], + denseValueArray1: number[], + constant: number): [number[], number[]]; + + tensor4dNewLikeWithRandomCells( + tensor4d: number[][][][]): number[][][][]; + tensor4dNewLikeWithRandomCellsScaled( + tensor4d: number[][][][], + scale: number): number[][][][]; + tensor4dNewLikeWithZeroCells( + tensor4d: number[][][][]): number[][][][]; + tensor4dNewLikeWithConstantCells( + tensor4d: number[][][][], + constant: number): number[][][][]; + tensor4dNewLikeWithScaledCells( + tensor4d: number[][][][], + scale: number): number[][][][]; + tensor4dNewLikeWithL1l2RegularizedSparseCells( + tensor4d: number[][][][], + l1Regularization: number, + l2Regularization: number): number[][][][]; + tensor4dNewLikeWithL1l2RegularizedDenseCells( + tensor4d: number[][][][], + l1Regularization: number, + l2Regularization: number): number[][][][]; + + tensor3dNewLikeWithRandomCells( + tensor3d: number[][][]): number[][][]; + tensor3dNewLikeWithRandomCellsScaled( + tensor3d: number[][][], + scale: number): number[][][]; + tensor3dNewLikeWithZeroCells( + tensor3d: number[][][]): number[][][]; + tensor3dNewLikeWithConstantCells( + tensor3d: number[][][], + constant: number): number[][][]; + tensor3dNewLikeWithScaledCells( + tensor3d: number[][][], + scale: number): number[][][]; + tensor3dNewLikeWithL1l2RegularizedSparseCells( + tensor3d: number[][][], + l1Regularization: number, + l2Regularization: number): number[][][]; + tensor3dNewLikeWithL1l2RegularizedDenseCells( + tensor3d: number[][][], + l1Regularization: number, + l2Regularization: number): number[][][]; + + matrixNewLikeWithRandomCells( + matrix: number[][]): number[][]; + matrixNewLikeWithRandomCellsScaled( + matrix: number[][], + scale: number): number[][]; + matrixNewLikeWithZeroCells( + matrix: number[][]): number[][]; + matrixNewLikeWithConstantCells( + matrix: number[][], + constant: number): number[][]; + matrixNewLikeWithScaledCells( + matrix: number[][], + scale: number): number[][]; + matrixNewLikeWithL1l2RegularizedSparseCells( + matrix: number[][], + l1Regularization: number, + l2Regularization: number): number[][]; + matrixNewLikeWithL1l2RegularizedDenseCells( + matrix: number[][], + l1Regularization: number, + l2Regularization: number): number[][]; + + vectorNewLikeWithRandomElements( + vector: number[]): number[]; + vectorNewLikeWithRandomElementsScaled( + vector: number[], + scale: number): number[]; + vectorNewLikeWithZeroElements( + vector: number[]): number[]; + vectorNewLikeWithConstantElements( + vector: number[], + constant: number): number[]; + vectorNewLikeWithScaledElements( + vector: number[], + scale: number): number[]; + vectorNewLikeWithL1l2RegularizedSparseElements( + vector: number[], + l1Regularization: number, + l2Regularization: number): number[]; + vectorNewLikeWithL1l2RegularizedDenseElements( + vector: number[], + l1Regularization: number, + l2Regularization: number): number[]; + + tensor4dNewWithRandomCells( + rows: number, + columns: number, + dimension3ds: number, + dimension4ds: number): number[][][][]; + tensor4dNewWithRandomCellsScaled( + rows: number, + columns: number, + dimension3ds: number, + dimension4ds: number, + scale: number): number[][][][]; + tensor4dNewWithZeroCells( + rows: number, + columns: number, + dimension3ds: number, + dimension4ds: number): number[][][][]; + tensor4dNewWithConstantCells( + rows: number, + columns: number, + dimension3ds: number, + dimension4ds: number, + constant: number): number[][][][]; + tensor4dNewWithScaledCells( + existingTensor4d: number[][][][], + scale: number): number[][][][]; + tensor4dNewWithL1l2RegularizedSparseCells( + existingTensor4d: number[][][][], + l1Regularization: number, + l2Regularization: number): number[][][][]; + tensor4dNewWithL1l2RegularizedDenseCells( + existingTensor4d: number[][][][], + l1Regularization: number, + l2Regularization: number): number[][][][]; + + tensor3dNewWithRandomCells( + rows: number, + columns: number, + dimension3ds: number): number[][][]; + tensor3dNewWithRandomCellsScaled( + rows: number, + columns: number, + dimension3ds: number, + scale: number): number[][][]; + tensor3dNewWithZeroCells( + rows: number, + columns: number, + dimension3ds: number): number[][][]; + tensor3dNewWithConstantCells( + rows: number, + columns: number, + dimension3ds: number, + constant: number): number[][][]; + tensor3dNewWithScaledCells( + existingTensor3d: number[][][], + scale: number): number[][][]; + tensor3dNewWithL1l2RegularizedSparseCells( + existingTensor3d: number[][][], + l1Regularization: number, + l2Regularization: number): number[][][]; + tensor3dNewWithL1l2RegularizedDenseCells( + existingTensor3d: number[][][], + l1Regularization: number, + l2Regularization: number): number[][][]; + + matrixNewWithRandomCells( + rows: number, + columns: number): number[][]; + matrixNewWithRandomCellsScaled( + rows: number, + columns: number, + scale: number): number[][]; + matrixNewWithZeroCells( + rows: number, + columns: number): number[][]; + matrixNewWithConstantCells( + rows: number, + columns: number, + constant: number): number[][]; + matrixNewWithScaledCells( + existingMatrix: number[][], + scale: number): number[][]; + matrixNewWithL1l2RegularizedSparseCells( + existingMatrix: number[][], + l1Regularization: number, + l2Regularization: number): number[][]; + matrixNewWithL1l2RegularizedDenseCells( + existingMatrix: number[][], + l1Regularization: number, + l2Regularization: number): number[][]; + + vectorNewWithRandomElements( + length: number): number[]; + vectorNewWithRandomElementsScaled( + length: number, + scale: number): number[]; + vectorNewWithZeroElements( + length: number): number[]; + vectorNewWithConstantElements( + length: number, + constant: number): number[]; + vectorNewWithScaledElements( + existingVector: number[], + scale: number): number[]; + vectorNewWithL1l2RegularizedSparseElements( + existingVector: number[], + l1Regularization: number, + l2Regularization: number): number[]; + vectorNewWithL1l2RegularizedDenseElements( + existingVector: number[], + l1Regularization: number, + l2Regularization: number): number[]; + + getIndexOnFirstMaxEntryOnArray( + inputArray: Float32Array | Int32Array | Uint8Array): + { "indexMax": number, "max": number }; + getIndexOnLastMaxEntryOnArray( + inputArray: Float32Array | Int32Array | Uint8Array): + { "indexMax": number, "max": number }; + getIndexOnFirstMaxEntry( + inputArray: number[]): + { "indexMax": number, "max": number }; + getIndexOnLastMaxEntry( + inputArray: number[]): + { "indexMax": number, "max": number }; + + getIndexOnFirstMinEntryOnArray( + inputArray: Float32Array | Int32Array | Uint8Array): + { "indexMin": number, "min": number }; + getIndexOnLastMinEntryOnArray( + inputArray: Float32Array | Int32Array | Uint8Array): + { "indexMin": number, "min": number }; + getIndexOnFirstMinEntry( + inputArray: number[]): + { "indexMin": number, "min": number }; + getIndexOnLastMinEntry( + inputArray: number[]): + { "indexMin": number, "min": number }; + + safeDivide(numerator: number, denominator: number): number; + safeLog(value: number): number; + + clipValue(value: number): number; +} diff --git a/packages/dispatcher/src/mathematics/mathematics_helper/MathematicsHelper.ts b/packages/dispatcher/src/mathematics/mathematics_helper/MathematicsHelper.ts index 91cec81b8..783554435 100644 --- a/packages/dispatcher/src/mathematics/mathematics_helper/MathematicsHelper.ts +++ b/packages/dispatcher/src/mathematics/mathematics_helper/MathematicsHelper.ts @@ -3,17 +3,25 @@ * Licensed under the MIT License. */ +import { IMathematicsHelper } from "./IMathematicsHelper"; + import { Utility } from "../../utility/Utility"; -export class MathematicsHelper { +export class MathematicsHelper implements IMathematicsHelper { public static epsilon: number = Utility.epsilon; public static epsilonUp: number = 1 - MathematicsHelper.epsilon; + public static readonly mathematicsHelperObject: IMathematicsHelper = new MathematicsHelper(); + + public static GetMathematicsHelperObject(): IMathematicsHelper { + return MathematicsHelper.mathematicsHelperObject; + } + // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/Softmax_function - public static softmaxSingleFunction(inputs: number[], index: number): number { + public softmaxSingleFunction(inputs: number[], index: number): number { const max: number = - MathematicsHelper.getIndexOnFirstMaxEntry(inputs).max; + this.getIndexOnFirstMaxEntry(inputs).max; const inputShifts: number[] = inputs.map((entry: number) => entry - max); const inputShiftExps: number[] = @@ -23,13 +31,13 @@ export class MathematicsHelper { (accumulation: number, entry: number) => accumulation + entry, 0); return inputShiftExps[index] / inputShiftExpsSum; } - public static smoothArgmaxApproximationSingleFunction(inputs: number[], index: number): number { - return MathematicsHelper.softmaxSingleFunction(inputs, index); + public smoothArgmaxApproximationSingleFunction(inputs: number[], index: number): number { + return this.softmaxSingleFunction(inputs, index); } // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/Softmax_function - public static softmaxFunction(inputs: number[]): number[] { + public softmaxFunction(inputs: number[]): number[] { const max: number = - MathematicsHelper.getIndexOnFirstMaxEntry(inputs).max; + this.getIndexOnFirstMaxEntry(inputs).max; const inputShifts: number[] = inputs.map((entry: number) => entry - max); const inputShiftExps: number[] = @@ -39,14 +47,14 @@ export class MathematicsHelper { (accumulation: number, entry: number) => accumulation + entry, 0); return inputShiftExps.map((entry: number) => entry / inputShiftExpsSum); } - public static smoothArgmaxApproximationFunction(inputs: number[]): number[] { - return MathematicsHelper.softmaxFunction(inputs); + public smoothArgmaxApproximationFunction(inputs: number[]): number[] { + return this.softmaxFunction(inputs); } // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/LogSumExp - public static logsumexpStrictConvexSingleFunction(inputs: number[]): number { + public logsumexpStrictConvexSingleFunction(inputs: number[]): number { let max: number = - MathematicsHelper.getIndexOnFirstMaxEntry(inputs).max; + this.getIndexOnFirstMaxEntry(inputs).max; if (max < 0) { max = 0; } @@ -58,15 +66,15 @@ export class MathematicsHelper { const inputShiftExpsSum: number = inputShiftExps.reduce( (accumulation: number, entry: number) => accumulation + entry, 0); - return max + Math.log(inputShiftExpsSum); + return max + this.safeLog(inputShiftExpsSum); } - public static smoothMaxApproximationStrictConvexFunction(inputs: number[]): number { - return MathematicsHelper.logsumexpStrictConvexSingleFunction(inputs); + public smoothMaxApproximationStrictConvexFunction(inputs: number[]): number { + return this.logsumexpStrictConvexSingleFunction(inputs); } // ---- NOTE-REFERENCE ---- https://en.wikipedia.org/wiki/LogSumExp - public static logsumexpSingleFunction(inputs: number[]): number { + public logsumexpSingleFunction(inputs: number[]): number { const max: number = - MathematicsHelper.getIndexOnFirstMaxEntry(inputs).max; + this.getIndexOnFirstMaxEntry(inputs).max; const inputShifts: number[] = inputs.map((entry: number) => entry - max); const inputShiftExps: number[] = @@ -74,47 +82,47 @@ export class MathematicsHelper { const inputShiftExpsSum: number = inputShiftExps.reduce( (accumulation: number, entry: number) => accumulation + entry, 0); - return max + Math.log(inputShiftExpsSum); + return max + this.safeLog(inputShiftExpsSum); } - public static smoothMaxApproximationFunction(inputs: number[]): number { - return MathematicsHelper.logsumexpSingleFunction(inputs); + public smoothMaxApproximationFunction(inputs: number[]): number { + return this.logsumexpSingleFunction(inputs); } - public static sigmoidLogisticGradientFunction(input: number): number { - const logisticValue = MathematicsHelper.sigmoidLogisticFunction(input); + public sigmoidLogisticGradientFunction(input: number): number { + const logisticValue: number = this.sigmoidLogisticFunction(input); return logisticValue * (1 - logisticValue); } - public static sigmoidLogisticFunction(input: number): number { + public sigmoidLogisticFunction(input: number): number { return 1 / (1 + Math.exp(-input)); } - public static sigmoidHyperbolicTangentFunction(input: number): number { + public sigmoidHyperbolicTangentFunction(input: number): number { const exponent: number = Math.exp(input); const exponentNegative: number = Math.exp(-input); return (exponent - exponentNegative) / (exponent + exponentNegative); } - public static sigmoidArctangentFunction(input: number): number { + public sigmoidArctangentFunction(input: number): number { return Math.atan(input); } - public static sigmoidGudermannianFunction(input: number): number { + public sigmoidGudermannianFunction(input: number): number { return 2 * Math.atan(Math.tanh(input / 2)); } - public static sigmoidGeneralizedLogisticFunction(input: number, alpha: number = 1): number { + public sigmoidGeneralizedLogisticFunction(input: number, alpha: number = 1): number { return Math.pow(1 + Math.exp(-input), -alpha); } - public static sigmoidAlgebraicFunction(input: number): number { + public sigmoidAlgebraicFunction(input: number): number { return input / Math.sqrt(1 + input * input); } - public static getL1Regularized(weight: number, l1Regularization: number): number { + public getL1Regularized(weight: number, l1Regularization: number): number { if (weight > 0) { return l1Regularization; } if (weight < 0) { return -l1Regularization; } return 0; } - public static getL2Regularized(weight: number, l2Regularization: number): number { + public getL2Regularized(weight: number, l2Regularization: number): number { return (weight * l2Regularization); } - public static getL1l2RegularizedWeightOptimizedSparse( + public getL1l2RegularizedWeightOptimizedSparse( weight: number, l1Regularization: number, l2Regularization: number): number { @@ -136,7 +144,7 @@ export class MathematicsHelper { weight -= regularized; return weight; } - public static getL1l2RegularizedWeightOptimizedDense( + public getL1l2RegularizedWeightOptimizedDense( weight: number, l1Regularization: number, l2Regularization: number): number { @@ -158,16 +166,10 @@ export class MathematicsHelper { /* * return: - * softmaxVectors: number[][]: + * softmaxVectors: number[][] * update: - * matrixWeightGradientDenseArrays: - * Each row represents a dense feature gradient vector for a label. - * Row length is equal to #features. - * Dimension: L X F, L: #features, F: #features. - * biasVectorDenseValueArray: - * A bias vector, each element is for a label. - * #biases is equal to #labels. - * Dimension: L, L: #labels. + * matrixWeightDenseArrays + * biasVectorDenseValueArray * input: * instanceGroundTruthPositiveLabelIndexes: * Each element is a label index. @@ -180,9 +182,9 @@ export class MathematicsHelper { * fall in the feature range [0, #features). * Dimension: N X iF, N: #instances, iF: indefinite #features. * matrixWeightDenseArrays: - * Each row represents a dense feature, float weigh vector for a label. + * Each row represents a dense feature, floating-point weight vector for a label. * Row length is equal to #features. - * Dimension: L X F, L: #features, F: #features. + * Dimension: L x F, L: #labels, F: #features. * biasVectorDenseValueArray: * A bias vector, each element is for a label. * #biases is equal to #labels. @@ -197,8 +199,17 @@ export class MathematicsHelper { * The begin index for a mini batch. * instanceFeatureVectorIndexEnd: * The end index for a mini batch. + * internal data structure: + * matrixWeightGradientDenseArrays: + * Each row represents a dense feature gradient vector for a label. + * Row length is equal to #features. + * Dimension: L x F, L: #labels, F: #features. + * biasVectorGradientDenseValueArray: + * Each element represents a bias-term gradient for a label. + * #biases is equal to #labels. + * Dimension: L, L: #labels. */ - public static softmaxLogLossGradientUpdate( + public softmaxLogLossGradientUpdate( instanceGroundTruthPositiveLabelIndexes: number[], instanceFeatureVectorSparseIndexArrays: number[][], matrixWeightDenseArrays: number[][], @@ -228,7 +239,7 @@ export class MathematicsHelper { Utility.debuggingThrow( `biasVectorDenseValueArray is empty`); } - const softmaxVectors: number[][] = MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + const softmaxVectors: number[][] = this.matrixVectorProductSoftmaxSparseIndexes( instanceFeatureVectorSparseIndexArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -238,9 +249,9 @@ export class MathematicsHelper { const numberLabels: number = matrixWeightDenseArrays.length; // const numberFeatures: number = matrixWeightDenseArrays[0].length; const matrixWeightGradientDenseArrays: number[][] = - MathematicsHelper.matrixNewLikeWithZeroCells(matrixWeightDenseArrays); + this.matrixNewLikeWithZeroCells(matrixWeightDenseArrays); const biasVectorGradientDenseValueArray: number[] = - MathematicsHelper.vectorNewLikeWithZeroElements(biasVectorDenseValueArray); + this.vectorNewLikeWithZeroElements(biasVectorDenseValueArray); let instanceFeatureVectorIndex = instanceFeatureVectorIndexBegin; for (let instance: number = 0; instance < numberInstances; instance++) { @@ -251,7 +262,7 @@ export class MathematicsHelper { const softmaxVector: number[] = softmaxVectors[instance]; /* ---- NOTE-FOR-REFERENCE ---- the loop for calculating gradients. - for (let label = 0; label < numberLabels; label++) { + for (let label: number = 0; label < numberLabels; label++) { const probability: number = softmaxVector[label]; const labelWeightGradientDenseArray: number[] = matrixWeightGradientDenseArrays[label]; @@ -291,7 +302,7 @@ export class MathematicsHelper { } } } - for (let label = 0; label < instanceLabel; label++) { + for (let label: number = 0; label < instanceLabel; label++) { // ---- NOTE: un-looping for optimizing the gradient computation process. const probability: number = softmaxVector[label]; const labelWeightGradientDenseArray: number[] = @@ -311,7 +322,7 @@ export class MathematicsHelper { } } } - for (let label = instanceLabel + 1; label < numberLabels; label++) { + for (let label: number = instanceLabel + 1; label < numberLabels; label++) { // ---- NOTE: un-looping for optimizing the gradient computation process. const probability: number = softmaxVector[label]; const labelWeightGradientDenseArray: number[] = @@ -337,24 +348,24 @@ export class MathematicsHelper { const factorConstant: number = learningRate / numberInstances; if ((l1Regularization > 0) || (l2Regularization > 0)) { - MathematicsHelper.matrixDenseSubtractScaledFromAndL1l2RegularizedSparseTo( + this.matrixDenseSubtractScaledFromAndL1l2RegularizedSparseTo( matrixWeightDenseArrays, matrixWeightGradientDenseArrays, factorConstant, l1Regularization, l2Regularization); - MathematicsHelper.vectorDenseSubtractScaledFromAndL1l2RegularizedSparseTo( + this.vectorDenseSubtractScaledFromAndL1l2RegularizedSparseTo( biasVectorDenseValueArray, biasVectorGradientDenseValueArray, factorConstant, l1Regularization, l2Regularization); } else { - MathematicsHelper.matrixDenseSubtractScaledFrom( + this.matrixDenseSubtractScaledFrom( matrixWeightDenseArrays, matrixWeightGradientDenseArrays, factorConstant); - MathematicsHelper.vectorDenseSubtractScaledFrom( + this.vectorDenseSubtractScaledFrom( biasVectorDenseValueArray, biasVectorGradientDenseValueArray, factorConstant); @@ -362,11 +373,11 @@ export class MathematicsHelper { return softmaxVectors; } - public static logLoss( + public logLoss( probabilityVector: number[], instanceGroundTruthPositiveLabelIndex: number): number { let probability = probabilityVector[instanceGroundTruthPositiveLabelIndex]; - probability = MathematicsHelper.clipValue(probability); + probability = this.clipValue(probability); if (!probability || (probability <= 0)) { Utility.debuggingThrow( `probability=${probability}, instanceGroundTruthPositiveLabelIndex=${instanceGroundTruthPositiveLabelIndex}`); @@ -374,14 +385,14 @@ export class MathematicsHelper { const loss: number = Math.log(probability); return -loss; } - public static logLossGeneric( + public logLossGeneric( probabilityVector: number[], labelVector: number[]): number { let loss: number = 0; for (let i: number = 0; i < labelVector.length; i++) { const label = labelVector[i]; let probability = probabilityVector[i]; - probability = MathematicsHelper.clipValue(probability); + probability = this.clipValue(probability); if (!probability || (probability <= 0)) { Utility.debuggingThrow( `probability=${probability}, labelVector=${labelVector}, probabilityVector=${probabilityVector}`); @@ -393,28 +404,28 @@ export class MathematicsHelper { return -loss; } - public static softmaxLogLoss( + public softmaxLogLoss( softmaxVectors: number[][], instanceGroundTruthPositiveLabelIndexes: number[]): number { let softmaxLogLossSum: number = 0; for (let i: number = 0; i < softmaxVectors.length; i++) { softmaxLogLossSum += - MathematicsHelper.logLoss(softmaxVectors[i], instanceGroundTruthPositiveLabelIndexes[i]); + this.logLoss(softmaxVectors[i], instanceGroundTruthPositiveLabelIndexes[i]); } return softmaxLogLossSum / softmaxVectors.length; } - public static softmaxLogLossGeneric( + public softmaxLogLossGeneric( softmaxVectors: number[][], labelVectors: number[][]): number { let softmaxLogLossSum: number = 0; for (let i: number = 0; i < softmaxVectors.length; i++) { softmaxLogLossSum += - MathematicsHelper.logLossGeneric(softmaxVectors[i], labelVectors[i]); + this.logLossGeneric(softmaxVectors[i], labelVectors[i]); } return softmaxLogLossSum / softmaxVectors.length; } - public static matrixVectorProductSoftmaxSparseIndexesValues( + public matrixVectorProductSoftmaxSparseIndexesValues( instanceFeatureVectorSparseIndexArrays: number[][], instanceFeatureVectorSparseValueArrays: number[][], matrixWeightDenseArrays: number[][], @@ -469,7 +480,7 @@ export class MathematicsHelper { const instanceFeatureVectorSparseValueArray: number[] = instanceFeatureVectorSparseValueArrays[i]; const matrixVectorProduct: number[] = - MathematicsHelper.matrixVectorProductSparseIndexesValues( + this.matrixVectorProductSparseIndexesValues( instanceFeatureVectorSparseIndexArray, instanceFeatureVectorSparseValueArray, matrixWeightDenseArrays, @@ -485,7 +496,7 @@ export class MathematicsHelper { Utility.debuggingLog( `i=${i}, matrixVectorProduct=${matrixVectorProduct}`); const softmaxVector: number[] = - MathematicsHelper.softmaxFunction(matrixVectorProduct); + this.softmaxFunction(matrixVectorProduct); softmaxVectors[indexSoftmaxVectors++] = softmaxVector; } @@ -506,9 +517,9 @@ export class MathematicsHelper { * fall in the feature range [0, #features). * Dimension: N X iF, N: #instances, iF: indefinite #features. * matrixWeightDenseArrays: - * Each row represents a dense feature, float weigh vector for a label. + * Each row represents a dense feature, floating-point weight vector for a label. * Row length is equal to #features. - * Dimension: L X F, L: #features, F: #features. + * Dimension: L x F, L: #labels, F: #features. * biasVectorDenseValueArray: * A bias vector, each element is for a label. * #biases is equal to #labels. @@ -518,7 +529,7 @@ export class MathematicsHelper { * instanceFeatureVectorIndexEnd: * The end index for a mini batch. */ - public static matrixVectorProductSoftmaxSparseIndexes( + public matrixVectorProductSoftmaxSparseIndexes( instanceFeatureVectorSparseIndexArrays: number[][], matrixWeightDenseArrays: number[][], biasVectorDenseValueArray: number[], @@ -560,33 +571,59 @@ export class MathematicsHelper { const instanceFeatureVectorSparseIndexArray: number[] = instanceFeatureVectorSparseIndexArrays[i]; const matrixVectorProduct: number[] = - MathematicsHelper.matrixVectorProductSparseIndexes( + this.matrixVectorProductSparseIndexes( instanceFeatureVectorSparseIndexArray, matrixWeightDenseArrays, biasVectorDenseValueArray); const softmaxVector: number[] = - MathematicsHelper.softmaxFunction(matrixVectorProduct); + this.softmaxFunction(matrixVectorProduct); softmaxVectors[indexSoftmaxVectors++] = softmaxVector; } return softmaxVectors; } - public static matrixVectorProductSoftmaxDenseValues( - vectorDenseValueArrays: number[][], + /* + * return: + * softmaxVectors: + * Each row is a softmax vector for an input instance. + * #rows is equivalent to #labels. + * Dimension: N X L, N: #instances, L: #labels. + * inputs: + * instanceFeatureVectorDenseValueArrays: + * Each row represents a dense feature value vector for an input instance. + * #rows is the number of input instances + * #columns is the number of dense features for that row/instance. + * There is no limit to the length of each row should be equal to the number of features. + * Dimension: N X F, N: #instances, F: #features. + * matrixWeightDenseArrays: + * Each row represents a dense feature, floating-point weight vector for a label. + * Row length is equal to #features. + * Dimension: L x F, L: #labels, F: #features. + * biasVectorDenseValueArray: + * A bias vector, each element is for a label. + * #biases is equal to #labels. + * Dimension: L, L: #labels. + * instanceFeatureVectorIndexBegin: + * The begin index for a mini batch. + * instanceFeatureVectorIndexEnd: + * The end index for a mini batch. + */ + public matrixVectorProductSoftmaxDenseValues( + instanceFeatureVectorDenseValueArrays: number[][], matrixWeightDenseArrays: number[][], biasVectorDenseValueArray: number[], instanceFeatureVectorIndexBegin: number = 0, instanceFeatureVectorIndexEnd: number = 0): number[][] { - if (Utility.isEmptyNumberArrays(vectorDenseValueArrays)) { + if (Utility.isEmptyNumberArrays(instanceFeatureVectorDenseValueArrays)) { Utility.debuggingThrow( - "vectorDenseValueArrays is empty."); + "instanceFeatureVectorDenseValueArrays is empty."); } if (Utility.isEmptyNumberArrays(matrixWeightDenseArrays)) { Utility.debuggingThrow( "matrixWeightDenseArrays is empty."); } const numberVectors = - vectorDenseValueArrays.length; + instanceFeatureVectorDenseValueArrays.length; if (numberVectors <= 0) { Utility.debuggingThrow( `numberVectors is empty`); @@ -610,22 +647,22 @@ export class MathematicsHelper { new Array(numberSoftmaxVectors); let indexSoftmaxVectors: number = 0; for (let i: number = instanceFeatureVectorIndexBegin; i < instanceFeatureVectorIndexEnd; i++) { - const vectorDenseValueArray: number[] = - vectorDenseValueArrays[i]; + const instanceFeatureVectorDenseValueArray: number[] = + instanceFeatureVectorDenseValueArrays[i]; const matrixVectorProduct: number[] = - MathematicsHelper.matrixVectorProductDenseValues( - vectorDenseValueArray, + this.matrixVectorProductDenseValues( + instanceFeatureVectorDenseValueArray, matrixWeightDenseArrays, biasVectorDenseValueArray); const softmaxVector: number[] = - MathematicsHelper.softmaxFunction(matrixVectorProduct); + this.softmaxFunction(matrixVectorProduct); softmaxVectors[indexSoftmaxVectors++] = softmaxVector; } return softmaxVectors; } - public static matrixVectorProductSparseIndexesValues( + public matrixVectorProductSparseIndexesValues( instanceFeatureVectorSparseIndexArray: number[], instanceFeatureVectorSparseValueArray: number[], matrixWeightDenseArrays: number[][], @@ -633,44 +670,44 @@ export class MathematicsHelper { const lengthRowMatrix: number = matrixWeightDenseArrays.length; const matrixVectorProduct: number[] = - MathematicsHelper.vectorNewWithZeroElements(lengthRowMatrix); - return MathematicsHelper.matrixVectorProductSparseIndexesValuesTo( + this.vectorNewWithZeroElements(lengthRowMatrix); + return this.matrixVectorProductSparseIndexesValuesTo( matrixVectorProduct, instanceFeatureVectorSparseIndexArray, instanceFeatureVectorSparseValueArray, matrixWeightDenseArrays, biasVectorDenseValueArray); } - public static matrixVectorProductSparseIndexes( + public matrixVectorProductSparseIndexes( instanceFeatureVectorSparseIndexArray: number[], matrixWeightDenseArrays: number[][], biasVectorDenseValueArray: number[]): number[] { const lengthRowMatrix: number = matrixWeightDenseArrays.length; const matrixVectorProduct: number[] = - MathematicsHelper.vectorNewWithZeroElements(lengthRowMatrix); - return MathematicsHelper.matrixVectorProductSparseIndexesTo( + this.vectorNewWithZeroElements(lengthRowMatrix); + return this.matrixVectorProductSparseIndexesTo( matrixVectorProduct, instanceFeatureVectorSparseIndexArray, matrixWeightDenseArrays, biasVectorDenseValueArray); } - public static matrixVectorProductDenseValues( + public matrixVectorProductDenseValues( vectorDenseValueArray: number[], matrixWeightDenseArrays: number[][], biasVectorDenseValueArray: number[]): number[] { const lengthRowMatrix: number = matrixWeightDenseArrays.length; const matrixVectorProduct: number[] = - MathematicsHelper.vectorNewWithZeroElements(lengthRowMatrix); - return MathematicsHelper.matrixVectorProductDenseValuesTo( + this.vectorNewWithZeroElements(lengthRowMatrix); + return this.matrixVectorProductDenseValuesTo( matrixVectorProduct, vectorDenseValueArray, matrixWeightDenseArrays, biasVectorDenseValueArray); } - public static matrixVectorProductSparseIndexesValuesTo( + public matrixVectorProductSparseIndexesValuesTo( matrixVectorProduct: number[], instanceFeatureVectorSparseIndexArray: number[], instanceFeatureVectorSparseValueArray: number[], @@ -687,7 +724,7 @@ export class MathematicsHelper { biasVectorDenseValue = biasVectorDenseValueArray[row]; } const dotProduct: number = - MathematicsHelper.dotProductSparseIndexesValues( + this.dotProductSparseIndexesValues( instanceFeatureVectorSparseIndexArray, instanceFeatureVectorSparseValueArray, matrixWeightDenseArray, @@ -697,7 +734,7 @@ export class MathematicsHelper { } return matrixVectorProduct; } - public static matrixVectorProductSparseIndexesTo( + public matrixVectorProductSparseIndexesTo( matrixVectorProduct: number[], instanceFeatureVectorSparseIndexArray: number[], matrixWeightDenseArrays: number[][], @@ -713,7 +750,7 @@ export class MathematicsHelper { biasVectorDenseValue = biasVectorDenseValueArray[row]; } const dotProduct: number = - MathematicsHelper.dotProductSparseIndexes( + this.dotProductSparseIndexes( instanceFeatureVectorSparseIndexArray, matrixWeightDenseArray, biasVectorDenseValue); @@ -722,7 +759,7 @@ export class MathematicsHelper { } return matrixVectorProduct; } - public static matrixVectorProductDenseValuesTo( + public matrixVectorProductDenseValuesTo( matrixVectorProduct: number[], vectorDenseValueArray: number[], matrixWeightDenseArrays: number[][], @@ -738,7 +775,7 @@ export class MathematicsHelper { biasVectorDenseValue = biasVectorDenseValueArray[row]; } const dotProduct: number = - MathematicsHelper.dotProductDenseValues( + this.dotProductDenseValues( vectorDenseValueArray, matrixWeightDenseArray, biasVectorDenseValue); @@ -748,7 +785,7 @@ export class MathematicsHelper { return matrixVectorProduct; } - public static vectorMatrixProductSparseIndexesValues( + public vectorMatrixProductSparseIndexesValues( instanceFeatureVectorSparseIndexArray: number[], instanceFeatureVectorSparseValueArray: number[], matrixWeightDenseArrays: number[][], @@ -758,15 +795,15 @@ export class MathematicsHelper { const lengthColumnMatrix: number = matrixWeightDenseArrays[0].length; const vectorMatrixProduct: number[] = - MathematicsHelper.vectorNewWithZeroElements(lengthColumnMatrix); - return MathematicsHelper.vectorMatrixProductSparseIndexesValuesTo( + this.vectorNewWithZeroElements(lengthColumnMatrix); + return this.vectorMatrixProductSparseIndexesValuesTo( vectorMatrixProduct, instanceFeatureVectorSparseIndexArray, instanceFeatureVectorSparseValueArray, matrixWeightDenseArrays, biasVectorDenseValueArray); } - public static vectorMatrixProductSparseIndexes( + public vectorMatrixProductSparseIndexes( instanceFeatureVectorSparseIndexArray: number[], matrixWeightDenseArrays: number[][], biasVectorDenseValueArray: number[]): number[] { @@ -775,14 +812,14 @@ export class MathematicsHelper { const lengthColumnMatrix: number = matrixWeightDenseArrays[0].length; const vectorMatrixProduct: number[] = - MathematicsHelper.vectorNewWithZeroElements(lengthColumnMatrix); - return MathematicsHelper.vectorMatrixProductSparseIndexesTo( + this.vectorNewWithZeroElements(lengthColumnMatrix); + return this.vectorMatrixProductSparseIndexesTo( vectorMatrixProduct, instanceFeatureVectorSparseIndexArray, matrixWeightDenseArrays, biasVectorDenseValueArray); } - public static vectorMatrixProductDenseValues( + public vectorMatrixProductDenseValues( vectorDenseValueArray: number[], matrixWeightDenseArrays: number[][], biasVectorDenseValueArray: number[]): number[] { @@ -791,15 +828,15 @@ export class MathematicsHelper { const lengthColumnMatrix: number = matrixWeightDenseArrays[0].length; const vectorMatrixProduct: number[] = - MathematicsHelper.vectorNewWithZeroElements(lengthColumnMatrix); - return MathematicsHelper.vectorMatrixProductDenseValuesTo( + this.vectorNewWithZeroElements(lengthColumnMatrix); + return this.vectorMatrixProductDenseValuesTo( vectorMatrixProduct, vectorDenseValueArray, matrixWeightDenseArrays, biasVectorDenseValueArray); } - public static vectorMatrixProductSparseIndexesValuesTo( + public vectorMatrixProductSparseIndexesValuesTo( vectorMatrixProduct: number[], instanceFeatureVectorSparseIndexArray: number[], instanceFeatureVectorSparseValueArray: number[], @@ -813,19 +850,19 @@ export class MathematicsHelper { const vectorSparseIndex: number = instanceFeatureVectorSparseIndexArray[i]; const vectorSparseValue: number = instanceFeatureVectorSparseValueArray[i]; const matrixWeightDenseArray: number[] = matrixWeightDenseArrays[vectorSparseIndex]; - MathematicsHelper.vectorDenseAddScaledTo( + this.vectorDenseAddScaledTo( vectorMatrixProduct, matrixWeightDenseArray, vectorSparseValue); } if (!Utility.isEmptyNumberArray(biasVectorDenseValueArray)) { - MathematicsHelper.vectorDenseAddTo( + this.vectorDenseAddTo( vectorMatrixProduct, biasVectorDenseValueArray); } return vectorMatrixProduct; } - public static vectorMatrixProductSparseIndexesTo( + public vectorMatrixProductSparseIndexesTo( vectorMatrixProduct: number[], instanceFeatureVectorSparseIndexArray: number[], matrixWeightDenseArrays: number[][], @@ -837,19 +874,19 @@ export class MathematicsHelper { for (const vectorSparseIndex of instanceFeatureVectorSparseIndexArray) { const vectorSparseValue: number = 1; const matrixWeightDenseArray: number[] = matrixWeightDenseArrays[vectorSparseIndex]; - MathematicsHelper.vectorDenseAddScaledTo( + this.vectorDenseAddScaledTo( vectorMatrixProduct, matrixWeightDenseArray, vectorSparseValue); } if (!Utility.isEmptyNumberArray(biasVectorDenseValueArray)) { - MathematicsHelper.vectorDenseAddTo( + this.vectorDenseAddTo( vectorMatrixProduct, biasVectorDenseValueArray); } return vectorMatrixProduct; } - public static vectorMatrixProductDenseValuesTo( + public vectorMatrixProductDenseValuesTo( vectorMatrixProduct: number[], vectorDenseValueArray: number[], matrixWeightDenseArrays: number[][], @@ -861,20 +898,47 @@ export class MathematicsHelper { for (let row: number = 0; row < lengthRowMatrix; row++) { const vectorDenseValue: number = vectorDenseValueArray[row]; const matrixWeightDenseArray: number[] = matrixWeightDenseArrays[row]; - MathematicsHelper.vectorDenseAddScaledTo( + this.vectorDenseAddScaledTo( vectorMatrixProduct, matrixWeightDenseArray, vectorDenseValue); } if (!Utility.isEmptyNumberArray(biasVectorDenseValueArray)) { - MathematicsHelper.vectorDenseAddTo( + this.vectorDenseAddTo( + vectorMatrixProduct, + biasVectorDenseValueArray); + } + return vectorMatrixProduct; + } + public vectorMatrixProductPartialDenseValuesTo( // ---- TODO + vectorMatrixProduct: number[], + vectorDenseValueArray: number[], + matrixWeightDenseArrays: number[][], + offsetDenseValueArray: number = 0, + offsetMatrixWeightDenseArrays: number = 0, + length: number = 0, + biasVectorDenseValueArray: number[] = []): number[] { + const lengthRowMatrix: number = + matrixWeightDenseArrays.length; + // const lengthColumnMatrix: number = + // matrixWeightDenseArrays[0].length; + for (let row: number = 0; row < lengthRowMatrix; row++) { + const vectorDenseValue: number = vectorDenseValueArray[row]; + const matrixWeightDenseArray: number[] = matrixWeightDenseArrays[row]; + this.vectorDenseAddScaledTo( + vectorMatrixProduct, + matrixWeightDenseArray, + vectorDenseValue); + } + if (!Utility.isEmptyNumberArray(biasVectorDenseValueArray)) { + this.vectorDenseAddTo( vectorMatrixProduct, biasVectorDenseValueArray); } return vectorMatrixProduct; } - public static dotProductSparseIndexesValues( + public dotProductSparseIndexesValues( sparseIndexArray: number[], sparseValueArray: number[], weights: number[], @@ -886,7 +950,7 @@ export class MathematicsHelper { 0); return (dotProduct + weightBias); } - public static dotProductSparseIndexes( + public dotProductSparseIndexes( sparseIndexArray: number[], weights: number[], weightBias: number = 0): number { @@ -897,7 +961,7 @@ export class MathematicsHelper { 0); return (dotProduct + weightBias); } - public static dotProductDenseValues( + public dotProductDenseValues( denseValueArray: number[], weights: number[], weightBias: number = 0): number { @@ -907,8 +971,28 @@ export class MathematicsHelper { } return (dotProduct + weightBias); } + public dotProductPartialDenseValues( // ---- TODO + denseValueArray: number[], + weights: number[], + offsetDenseValueArray: number = 0, + offsetWeights: number = 0, + length: number = 0, + weightBias: number = 0): number { + if (length === 0) { + length = denseValueArray.length; + } + let dotProduct: number = 0; + let indexDenseValueArray: number = offsetDenseValueArray; + let indexWeights: number = offsetWeights; + for (let i: number = 0; i < length; i++) { + dotProduct += denseValueArray[indexDenseValueArray] * weights[indexWeights]; + indexDenseValueArray++; + indexWeights++; + } + return (dotProduct + weightBias); + } - public static matrixDenseSubtractScaledFromAndL1l2RegularizedSparseTo( + public matrixDenseSubtractScaledFromAndL1l2RegularizedSparseTo( denseValueArray0: number[][], denseValueArray1: number[][], constant: number, @@ -921,7 +1005,7 @@ export class MathematicsHelper { const adjustment: number = denseValueArray1[row][column]; denseValueArray0[row][column] -= (constant * adjustment); if (adjustment !== 0) { - denseValueArray0[row][column] = MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse( + denseValueArray0[row][column] = this.getL1l2RegularizedWeightOptimizedSparse( denseValueArray0[row][column], l1Regularization, l2Regularization); @@ -930,7 +1014,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static matrixDenseSubtractScaledFromAndL1l2RegularizedDenseTo( + public matrixDenseSubtractScaledFromAndL1l2RegularizedDenseTo( denseValueArray0: number[][], denseValueArray1: number[][], constant: number, @@ -943,7 +1027,7 @@ export class MathematicsHelper { const adjustment: number = denseValueArray1[row][column]; denseValueArray0[row][column] -= (constant * adjustment); if (adjustment !== 0) { - denseValueArray0[row][column] = MathematicsHelper.getL1l2RegularizedWeightOptimizedDense( + denseValueArray0[row][column] = this.getL1l2RegularizedWeightOptimizedDense( denseValueArray0[row][column], l1Regularization, l2Regularization); @@ -952,7 +1036,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseSubtractScaledFromAndL1l2RegularizedSparseTo( + public vectorDenseSubtractScaledFromAndL1l2RegularizedSparseTo( denseValueArray0: number[], denseValueArray1: number[], constant: number, @@ -962,7 +1046,7 @@ export class MathematicsHelper { const adjustment: number = denseValueArray1[i]; denseValueArray0[i] -= (constant * adjustment); if (adjustment !== 0) { - denseValueArray0[i] = MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse( + denseValueArray0[i] = this.getL1l2RegularizedWeightOptimizedSparse( denseValueArray0[i], l1Regularization, l2Regularization); @@ -970,7 +1054,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseSubtractScaledFromAndL1l2RegularizedDenseTo( + public vectorDenseSubtractScaledFromAndL1l2RegularizedDenseTo( denseValueArray0: number[], denseValueArray1: number[], constant: number, @@ -980,7 +1064,7 @@ export class MathematicsHelper { const adjustment: number = denseValueArray1[i]; denseValueArray0[i] -= (constant * adjustment); if (adjustment !== 0) { - denseValueArray0[i] = MathematicsHelper.getL1l2RegularizedWeightOptimizedDense( + denseValueArray0[i] = this.getL1l2RegularizedWeightOptimizedDense( denseValueArray0[i], l1Regularization, l2Regularization); @@ -989,7 +1073,7 @@ export class MathematicsHelper { return denseValueArray0; } - public static matrixDenseL1l2RegularizedSparseTo( + public matrixDenseL1l2RegularizedSparseTo( denseValueArray: number[][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][] { @@ -997,7 +1081,7 @@ export class MathematicsHelper { const columns: number = denseValueArray[0].length; for (let row: number = 0; row < rows; row++) { for (let column: number = 0; column < columns; column++) { - denseValueArray[row][column] = MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse( + denseValueArray[row][column] = this.getL1l2RegularizedWeightOptimizedSparse( denseValueArray[row][column], l1Regularization, l2Regularization); @@ -1005,7 +1089,7 @@ export class MathematicsHelper { } return denseValueArray; } - public static matrixDenseL1l2RegularizedDenseTo( + public matrixDenseL1l2RegularizedDenseTo( denseValueArray: number[][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][] { @@ -1013,7 +1097,7 @@ export class MathematicsHelper { const columns: number = denseValueArray[0].length; for (let row: number = 0; row < rows; row++) { for (let column: number = 0; column < columns; column++) { - denseValueArray[row][column] = MathematicsHelper.getL1l2RegularizedWeightOptimizedDense( + denseValueArray[row][column] = this.getL1l2RegularizedWeightOptimizedDense( denseValueArray[row][column], l1Regularization, l2Regularization); @@ -1022,24 +1106,24 @@ export class MathematicsHelper { return denseValueArray; } - public static vectorDenseL1l2RegularizedSparseTo( + public vectorDenseL1l2RegularizedSparseTo( denseValueArray: number[], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[] { for (let i: number = 0; i < denseValueArray.length; i++) { - denseValueArray[i] = MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse( + denseValueArray[i] = this.getL1l2RegularizedWeightOptimizedSparse( denseValueArray[i], l1Regularization, l2Regularization); } return denseValueArray; } - public static vectorDenseL1l2RegularizedDenseTo( + public vectorDenseL1l2RegularizedDenseTo( denseValueArray: number[], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[] { for (let i: number = 0; i < denseValueArray.length; i++) { - denseValueArray[i] = MathematicsHelper.getL1l2RegularizedWeightOptimizedDense( + denseValueArray[i] = this.getL1l2RegularizedWeightOptimizedDense( denseValueArray[i], l1Regularization, l2Regularization); @@ -1047,187 +1131,187 @@ export class MathematicsHelper { return denseValueArray; } - public static tensor4dDenseAssignRandomTo( + public tensor4dDenseAssignRandomTo( denseValueArray0: number[][][][]): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseAssignRandomTo( + this.tensor3dDenseAssignRandomTo( denseValueArray0[row]); } return denseValueArray0; } - public static tensor4dDenseAssignConstantTo( + public tensor4dDenseAssignConstantTo( denseValueArray0: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseAssignConstantTo( + this.tensor3dDenseAssignConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor4dDenseAddConstantTo( + public tensor4dDenseAddConstantTo( denseValueArray0: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseAddConstantTo( + this.tensor3dDenseAddConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor4dDenseMultiplyConstantTo( + public tensor4dDenseMultiplyConstantTo( denseValueArray0: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseMultiplyConstantTo( + this.tensor3dDenseMultiplyConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor4dDenseSubtractConstantFrom( + public tensor4dDenseSubtractConstantFrom( denseValueArray0: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseSubtractConstantFrom( + this.tensor3dDenseSubtractConstantFrom( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor4dDenseDivideConstantFrom( + public tensor4dDenseDivideConstantFrom( denseValueArray0: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseDivideConstantFrom( + this.tensor3dDenseDivideConstantFrom( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor4dDenseAssignTo( + public tensor4dDenseAssignTo( denseValueArray0: number[][][][], denseValueArray1: number[][][][]): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseAssignTo( + this.tensor3dDenseAssignTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor4dDenseAddTo( + public tensor4dDenseAddTo( denseValueArray0: number[][][][], denseValueArray1: number[][][][]): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseAddTo( + this.tensor3dDenseAddTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor4dDenseMultiplyTo( + public tensor4dDenseMultiplyTo( denseValueArray0: number[][][][], denseValueArray1: number[][][][]): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseMultiplyTo( + this.tensor3dDenseMultiplyTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor4dDenseSubtractFrom( + public tensor4dDenseSubtractFrom( denseValueArray0: number[][][][], denseValueArray1: number[][][][]): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseSubtractFrom( + this.tensor3dDenseSubtractFrom( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor4dDenseDivideFrom( + public tensor4dDenseDivideFrom( denseValueArray0: number[][][][], denseValueArray1: number[][][][]): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseDivideFrom( + this.tensor3dDenseDivideFrom( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor4dDenseAssignScaledTo( + public tensor4dDenseAssignScaledTo( denseValueArray0: number[][][][], denseValueArray1: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseAssignScaledTo( + this.tensor3dDenseAssignScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor4dDenseAddScaledTo( + public tensor4dDenseAddScaledTo( denseValueArray0: number[][][][], denseValueArray1: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseAddScaledTo( + this.tensor3dDenseAddScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor4dDenseMultiplyScaledTo( + public tensor4dDenseMultiplyScaledTo( denseValueArray0: number[][][][], denseValueArray1: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseMultiplyScaledTo( + this.tensor3dDenseMultiplyScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor4dDenseSubtractScaledFrom( + public tensor4dDenseSubtractScaledFrom( denseValueArray0: number[][][][], denseValueArray1: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseSubtractScaledFrom( + this.tensor3dDenseSubtractScaledFrom( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor4dDenseDivideScaledFrom( + public tensor4dDenseDivideScaledFrom( denseValueArray0: number[][][][], denseValueArray1: number[][][][], constant: number): number[][][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.tensor3dDenseDivideScaledFrom( + this.tensor3dDenseDivideScaledFrom( denseValueArray0[row], denseValueArray1[row], constant); @@ -1235,189 +1319,189 @@ export class MathematicsHelper { return denseValueArray0; } - public static tensor3dDenseAssignRandomTo( + public tensor3dDenseAssignRandomTo( denseValueArray0: number[][][]): number[][][] { const rows: number = denseValueArray0.length; const columns: number = denseValueArray0[0].length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseAssignRandomTo( + this.matrixDenseAssignRandomTo( denseValueArray0[row]); } return denseValueArray0; } - public static tensor3dDenseAssignConstantTo( + public tensor3dDenseAssignConstantTo( denseValueArray0: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseAssignConstantTo( + this.matrixDenseAssignConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor3dDenseAddConstantTo( + public tensor3dDenseAddConstantTo( denseValueArray0: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseAddConstantTo( + this.matrixDenseAddConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor3dDenseMultiplyConstantTo( + public tensor3dDenseMultiplyConstantTo( denseValueArray0: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseMultiplyConstantTo( + this.matrixDenseMultiplyConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor3dDenseSubtractConstantFrom( + public tensor3dDenseSubtractConstantFrom( denseValueArray0: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseSubtractConstantFrom( + this.matrixDenseSubtractConstantFrom( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor3dDenseDivideConstantFrom( + public tensor3dDenseDivideConstantFrom( denseValueArray0: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseDivideConstantFrom( + this.matrixDenseDivideConstantFrom( denseValueArray0[row], constant); } return denseValueArray0; } - public static tensor3dDenseAssignTo( + public tensor3dDenseAssignTo( denseValueArray0: number[][][], denseValueArray1: number[][][]): number[][][] { const rows: number = denseValueArray0.length; const columns: number = denseValueArray0[0].length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseAssignTo( + this.matrixDenseAssignTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor3dDenseAddTo( + public tensor3dDenseAddTo( denseValueArray0: number[][][], denseValueArray1: number[][][]): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseAddTo( + this.matrixDenseAddTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor3dDenseMultiplyTo( + public tensor3dDenseMultiplyTo( denseValueArray0: number[][][], denseValueArray1: number[][][]): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseMultiplyTo( + this.matrixDenseMultiplyTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor3dDenseSubtractFrom( + public tensor3dDenseSubtractFrom( denseValueArray0: number[][][], denseValueArray1: number[][][]): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseSubtractFrom( + this.matrixDenseSubtractFrom( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor3dDenseDivideFrom( + public tensor3dDenseDivideFrom( denseValueArray0: number[][][], denseValueArray1: number[][][]): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseDivideFrom( + this.matrixDenseDivideFrom( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static tensor3dDenseAssignScaledTo( + public tensor3dDenseAssignScaledTo( denseValueArray0: number[][][], denseValueArray1: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseAssignScaledTo( + this.matrixDenseAssignScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor3dDenseAddScaledTo( + public tensor3dDenseAddScaledTo( denseValueArray0: number[][][], denseValueArray1: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseAddScaledTo( + this.matrixDenseAddScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor3dDenseMultiplyScaledTo( + public tensor3dDenseMultiplyScaledTo( denseValueArray0: number[][][], denseValueArray1: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseMultiplyScaledTo( + this.matrixDenseMultiplyScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor3dDenseSubtractScaledFrom( + public tensor3dDenseSubtractScaledFrom( denseValueArray0: number[][][], denseValueArray1: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseSubtractScaledFrom( + this.matrixDenseSubtractScaledFrom( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static tensor3dDenseDivideScaledFrom( + public tensor3dDenseDivideScaledFrom( denseValueArray0: number[][][], denseValueArray1: number[][][], constant: number): number[][][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.matrixDenseDivideScaledFrom( + this.matrixDenseDivideScaledFrom( denseValueArray0[row], denseValueArray1[row], constant); @@ -1425,187 +1509,187 @@ export class MathematicsHelper { return denseValueArray0; } - public static matrixDenseAssignRandomTo( + public matrixDenseAssignRandomTo( denseValueArray0: number[][]): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseAssignRandomTo( + this.vectorDenseAssignRandomTo( denseValueArray0[row]); } return denseValueArray0; } - public static matrixDenseAssignConstantTo( + public matrixDenseAssignConstantTo( denseValueArray0: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseAssignConstantTo( + this.vectorDenseAssignConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static matrixDenseAddConstantTo( + public matrixDenseAddConstantTo( denseValueArray0: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseAddConstantTo( + this.vectorDenseAddConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static matrixDenseMultiplyConstantTo( + public matrixDenseMultiplyConstantTo( denseValueArray0: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseMultiplyConstantTo( + this.vectorDenseMultiplyConstantTo( denseValueArray0[row], constant); } return denseValueArray0; } - public static matrixDenseSubtractConstantFrom( + public matrixDenseSubtractConstantFrom( denseValueArray0: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseSubtractConstantFrom( + this.vectorDenseSubtractConstantFrom( denseValueArray0[row], constant); } return denseValueArray0; } - public static matrixDenseDivideConstantFrom( + public matrixDenseDivideConstantFrom( denseValueArray0: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseDivideConstantFrom( + this.vectorDenseDivideConstantFrom( denseValueArray0[row], constant); } return denseValueArray0; } - public static matrixDenseAssignTo( + public matrixDenseAssignTo( denseValueArray0: number[][], denseValueArray1: number[][]): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseAssignTo( + this.vectorDenseAssignTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static matrixDenseAddTo( + public matrixDenseAddTo( denseValueArray0: number[][], denseValueArray1: number[][]): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseAddTo( + this.vectorDenseAddTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static matrixDenseMultiplyTo( + public matrixDenseMultiplyTo( denseValueArray0: number[][], denseValueArray1: number[][]): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseMultiplyTo( + this.vectorDenseMultiplyTo( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static matrixDenseSubtractFrom( + public matrixDenseSubtractFrom( denseValueArray0: number[][], denseValueArray1: number[][]): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseSubtractFrom( + this.vectorDenseSubtractFrom( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static matrixDenseDivideFrom( + public matrixDenseDivideFrom( denseValueArray0: number[][], denseValueArray1: number[][]): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseDivideFrom( + this.vectorDenseDivideFrom( denseValueArray0[row], denseValueArray1[row]); } return denseValueArray0; } - public static matrixDenseAssignScaledTo( + public matrixDenseAssignScaledTo( denseValueArray0: number[][], denseValueArray1: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseAssignScaledTo( + this.vectorDenseAssignScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static matrixDenseAddScaledTo( + public matrixDenseAddScaledTo( denseValueArray0: number[][], denseValueArray1: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseAddScaledTo( + this.vectorDenseAddScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static matrixDenseMultiplyScaledTo( + public matrixDenseMultiplyScaledTo( denseValueArray0: number[][], denseValueArray1: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseMultiplyScaledTo( + this.vectorDenseMultiplyScaledTo( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static matrixDenseSubtractScaledFrom( + public matrixDenseSubtractScaledFrom( denseValueArray0: number[][], denseValueArray1: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseSubtractScaledFrom( + this.vectorDenseSubtractScaledFrom( denseValueArray0[row], denseValueArray1[row], constant); } return denseValueArray0; } - public static matrixDenseDivideScaledFrom( + public matrixDenseDivideScaledFrom( denseValueArray0: number[][], denseValueArray1: number[][], constant: number): number[][] { const rows: number = denseValueArray0.length; for (let row: number = 0; row < rows; row++) { - MathematicsHelper.vectorDenseDivideScaledFrom( + this.vectorDenseDivideScaledFrom( denseValueArray0[row], denseValueArray1[row], constant); @@ -1613,7 +1697,7 @@ export class MathematicsHelper { return denseValueArray0; } - public static vectorDenseAssignRandomTo( + public vectorDenseAssignRandomTo( denseValueArray0: number[]): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { denseValueArray0[i] = Utility.getRandomNumber(); @@ -1621,7 +1705,7 @@ export class MathematicsHelper { return denseValueArray0; } - public static vectorDenseAssignConstantTo( + public vectorDenseAssignConstantTo( denseValueArray0: number[], constant: number): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1629,7 +1713,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseAddConstantTo( + public vectorDenseAddConstantTo( denseValueArray0: number[], constant: number): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1637,7 +1721,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseMultiplyConstantTo( + public vectorDenseMultiplyConstantTo( denseValueArray0: number[], constant: number): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1645,7 +1729,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseSubtractConstantFrom( + public vectorDenseSubtractConstantFrom( denseValueArray0: number[], constant: number): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1653,7 +1737,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseDivideConstantFrom( + public vectorDenseDivideConstantFrom( denseValueArray0: number[], constant: number): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1662,7 +1746,7 @@ export class MathematicsHelper { return denseValueArray0; } - public static vectorDenseAssignTo( + public vectorDenseAssignTo( denseValueArray0: number[], denseValueArray1: number[]): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1670,7 +1754,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseAddTo( + public vectorDenseAddTo( denseValueArray0: number[], denseValueArray1: number[]): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1678,7 +1762,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseMultiplyTo( + public vectorDenseMultiplyTo( denseValueArray0: number[], denseValueArray1: number[]): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1686,7 +1770,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseSubtractFrom( + public vectorDenseSubtractFrom( denseValueArray0: number[], denseValueArray1: number[]): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1694,7 +1778,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseDivideFrom( + public vectorDenseDivideFrom( denseValueArray0: number[], denseValueArray1: number[]): number[] { for (let i: number = 0; i < denseValueArray0.length; i++) { @@ -1703,7 +1787,7 @@ export class MathematicsHelper { return denseValueArray0; } - public static vectorDenseAssignScaledTo( + public vectorDenseAssignScaledTo( denseValueArray0: number[], denseValueArray1: number[], constant: number): number[] { @@ -1712,7 +1796,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseAddScaledTo( + public vectorDenseAddScaledTo( denseValueArray0: number[], denseValueArray1: number[], constant: number): number[] { @@ -1721,7 +1805,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseMultiplyScaledTo( + public vectorDenseMultiplyScaledTo( denseValueArray0: number[], denseValueArray1: number[], constant: number): number[] { @@ -1730,7 +1814,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseSubtractScaledFrom( + public vectorDenseSubtractScaledFrom( denseValueArray0: number[], denseValueArray1: number[], constant: number): number[] { @@ -1739,7 +1823,7 @@ export class MathematicsHelper { } return denseValueArray0; } - public static vectorDenseDivideScaledFrom( + public vectorDenseDivideScaledFrom( denseValueArray0: number[], denseValueArray1: number[], constant: number): number[] { @@ -1749,7 +1833,7 @@ export class MathematicsHelper { return denseValueArray0; } - public static vectorSparseAssignRandomTo( + public vectorSparseAssignRandomTo( sparseIndexArray0: number[], sparseValueArray0: number[]): [number[], number[]] { for (let i: number = 0; i < sparseValueArray0.length; i++) { @@ -1758,7 +1842,7 @@ export class MathematicsHelper { return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseAssignConstantTo( + public vectorSparseAssignConstantTo( sparseIndexArray0: number[], sparseValueArray0: number[], constant: number): [number[], number[]] { @@ -1767,7 +1851,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseAddConstantTo( + public vectorSparseAddConstantTo( sparseIndexArray0: number[], sparseValueArray0: number[], constant: number): [number[], number[]] { @@ -1776,7 +1860,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseMultiplyConstantTo( + public vectorSparseMultiplyConstantTo( sparseIndexArray0: number[], sparseValueArray0: number[], constant: number): [number[], number[]] { @@ -1785,7 +1869,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseSubtractConstantFrom( + public vectorSparseSubtractConstantFrom( sparseIndexArray0: number[], sparseValueArray0: number[], constant: number): [number[], number[]] { @@ -1794,7 +1878,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseDivideConstantFrom( + public vectorSparseDivideConstantFrom( sparseIndexArray0: number[], sparseValueArray0: number[], constant: number): [number[], number[]] { @@ -1804,114 +1888,293 @@ export class MathematicsHelper { return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseAssignTo( + public vectorSparseAssignTo( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[]): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] = sparseValueArray1[i]; + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseIndexArray1) { + sparseIndexArray0.push(x); + } + for (const x of sparseValueArray1) { + sparseValueArray0.push(x); } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseAddTo( + public vectorSparseAddTo( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[]): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] += sparseValueArray1[i]; + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + if (value !== 0) { // ---- NOTE ---- should not be zero anyway as it's sparse structure! + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) + value); + } else { + sparseArrayMap.set(index, value); + } + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseMultiplyTo( + public vectorSparseMultiplyTo( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[]): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] *= sparseValueArray1[i]; + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) * value); + // ---- NOTE ---- multiply to 0 is still 0 ---- } else { + // ---- NOTE ---- multiply to 0 is still 0 ---- sparseArrayMap.set(index, value); + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseSubtractFrom( + public vectorSparseSubtractFrom( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[]): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] -= sparseValueArray1[i]; + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + if (value !== 0) { // ---- NOTE ---- should not be zero anyway as it's sparse structure! + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) - value); + } else { + sparseArrayMap.set(index, -value); + } + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseDivideFrom( + public vectorSparseDivideFrom( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[]): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] /= sparseValueArray1[i]; + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + if (value !== 0) { // ---- NOTE ---- should not be zero anyway as it's sparse structure! + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) / value); + // ---- NOTE ---- 0 divided is still 0 ---- } else { + // ---- NOTE ---- 0 divided is still 0 ---- sparseArrayMap.set(index, value); + } + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseAssignScaledTo( + public vectorSparseAssignScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[], constant: number): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] = (constant * sparseValueArray1[i]); + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseIndexArray1) { + sparseIndexArray0.push(x); + } + for (const x of sparseValueArray1) { + sparseValueArray0.push(constant * x); } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseAddScaledTo( + public vectorSparseAddScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[], constant: number): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] += (constant * sparseValueArray1[i]); + if (constant === 0) { // ---- NOTE ---- no effect if constant is zero + return [sparseIndexArray0, sparseValueArray0]; + } + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + if (value !== 0) { // ---- NOTE ---- should not be zero anyway as it's sparse structure! + const scaledValue: number = constant * value; + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) + scaledValue); + } else { + sparseArrayMap.set(index, scaledValue); + } + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseMultiplyScaledTo( + public vectorSparseMultiplyScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[], constant: number): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] *= (constant * sparseValueArray1[i]); + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + const scaledValue: number = constant * value; + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) * scaledValue); + // ---- NOTE ---- multiply to 0 is still 0 ---- } else { + // ---- NOTE ---- multiply to 0 is still 0 ---- sparseArrayMap.set(index, scaledValue); + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseSubtractScaledFrom( + public vectorSparseSubtractScaledFrom( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[], constant: number): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] -= (constant * sparseValueArray1[i]); + if (constant === 0) { // ---- NOTE ---- no effect if constant is zero + return [sparseIndexArray0, sparseValueArray0]; + } + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + if (value !== 0) { // ---- NOTE ---- should not be zero anyway as it's sparse structure! + const scaledValue: number = constant * value; + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) - scaledValue); + } else { + sparseArrayMap.set(index, -scaledValue); + } + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseDivideScaledFrom( + public vectorSparseDivideScaledFrom( sparseIndexArray0: number[], sparseValueArray0: number[], sparseIndexArray1: number[], sparseValueArray1: number[], constant: number): [number[], number[]] { - for (let i: number = 0; i < sparseValueArray0.length; i++) { - sparseValueArray0[i] /= (constant * sparseValueArray1[i]); + if (constant === 0) { // ---- NOTE ---- divided by 0 is not a good idea ---- + return [sparseIndexArray0, sparseValueArray0]; + } + const sparseArrayMap: Map = + Utility.sparseArrayPairToMap(sparseIndexArray0, sparseValueArray0); + for (let i: number = 0; i < sparseIndexArray1.length; i++) { + const index: number = sparseIndexArray1[i]; + const value: number = sparseValueArray1[i]; + if (value !== 0) { // ---- NOTE ---- should not be zero anyway as it's sparse structure! + const scaledValue: number = constant * value; + if (sparseArrayMap.has(index)) { + sparseArrayMap.set(index, (sparseArrayMap.get(index) as number) / scaledValue); + // ---- NOTE ---- 0 divided is still 0 ---- } else { + // ---- NOTE ---- 0 divided is still 0 ---- sparseArrayMap.set(index, scaledValue); + } + } + } + sparseIndexArray0.length = 0; + sparseValueArray0.length = 0; + for (const x of sparseArrayMap.entries()) { + const key: number = x[0]; + const value: number = x[1]; + if (value !== 0) { + sparseIndexArray0.push(key); + sparseValueArray0.push(value); + } } return [sparseIndexArray0, sparseValueArray0]; } - public static vectorSparseIndexDenseArrayAssignRandomTo( + public vectorSparseIndexDenseArrayAssignRandomTo( sparseIndexArray0: number[], denseValueArray0: number[]): [number[], number[]] { for (const index of sparseIndexArray0) { @@ -1920,7 +2183,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayAssignConstantTo( + public vectorSparseIndexDenseArrayAssignConstantTo( sparseIndexArray0: number[], denseValueArray0: number[], constant: number): [number[], number[]] { @@ -1929,7 +2192,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayAddConstantTo( + public vectorSparseIndexDenseArrayAddConstantTo( sparseIndexArray0: number[], denseValueArray0: number[], constant: number): [number[], number[]] { @@ -1938,7 +2201,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayMultiplyConstantTo( + public vectorSparseIndexDenseArrayMultiplyConstantTo( sparseIndexArray0: number[], denseValueArray0: number[], constant: number): [number[], number[]] { @@ -1947,7 +2210,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArraySubtractConstantFrom( + public vectorSparseIndexDenseArraySubtractConstantFrom( sparseIndexArray0: number[], denseValueArray0: number[], constant: number): [number[], number[]] { @@ -1956,7 +2219,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayDivideConstantFrom( + public vectorSparseIndexDenseArrayDivideConstantFrom( sparseIndexArray0: number[], denseValueArray0: number[], constant: number): [number[], number[]] { @@ -1966,7 +2229,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayAssignTo( + public vectorSparseIndexDenseArrayAssignTo( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[]): [number[], number[]] { @@ -1975,7 +2238,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayAddTo( + public vectorSparseIndexDenseArrayAddTo( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[]): [number[], number[]] { @@ -1984,7 +2247,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayMultiplyTo( + public vectorSparseIndexDenseArrayMultiplyTo( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[]): [number[], number[]] { @@ -1993,7 +2256,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArraySubtractFrom( + public vectorSparseIndexDenseArraySubtractFrom( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[]): [number[], number[]] { @@ -2002,7 +2265,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayDivideFrom( + public vectorSparseIndexDenseArrayDivideFrom( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[]): [number[], number[]] { @@ -2012,7 +2275,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayAssignScaledTo( + public vectorSparseIndexDenseArrayAssignScaledTo( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[], @@ -2022,7 +2285,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayAddScaledTo( + public vectorSparseIndexDenseArrayAddScaledTo( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[], @@ -2032,7 +2295,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayMultiplyScaledTo( + public vectorSparseIndexDenseArrayMultiplyScaledTo( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[], @@ -2042,7 +2305,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArraySubtractScaledFrom( + public vectorSparseIndexDenseArraySubtractScaledFrom( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[], @@ -2052,7 +2315,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseIndexDenseArrayDivideScaledFrom( + public vectorSparseIndexDenseArrayDivideScaledFrom( sparseIndexArray0: number[], denseValueArray0: number[], denseValueArray1: number[], @@ -2063,7 +2326,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayAssignTo( + public vectorIndependentSparseIndexDenseArrayAssignTo( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2075,7 +2338,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayAddTo( + public vectorIndependentSparseIndexDenseArrayAddTo( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2087,7 +2350,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayMultiplyTo( + public vectorIndependentSparseIndexDenseArrayMultiplyTo( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2099,7 +2362,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArraySubtractFrom( + public vectorIndependentSparseIndexDenseArraySubtractFrom( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2111,7 +2374,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayDivideFrom( + public vectorIndependentSparseIndexDenseArrayDivideFrom( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2124,7 +2387,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayAssignScaledTo( + public vectorIndependentSparseIndexDenseArrayAssignScaledTo( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2137,7 +2400,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayAddScaledTo( + public vectorIndependentSparseIndexDenseArrayAddScaledTo( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2150,7 +2413,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayMultiplyScaledTo( + public vectorIndependentSparseIndexDenseArrayMultiplyScaledTo( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2163,7 +2426,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArraySubtractScaledFrom( + public vectorIndependentSparseIndexDenseArraySubtractScaledFrom( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2176,7 +2439,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseIndexDenseArrayDivideScaledFrom( + public vectorIndependentSparseIndexDenseArrayDivideScaledFrom( sparseIndexArray0: number[], denseValueArray0: number[], sparseIndexArray1: number[], @@ -2190,7 +2453,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayAssignRandomTo( + public vectorSparseMapDenseArrayAssignRandomTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[]): [number[], number[]] { @@ -2200,7 +2463,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayAssignConstantTo( + public vectorSparseMapDenseArrayAssignConstantTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2210,7 +2473,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayAddConstantTo( + public vectorSparseMapDenseArrayAddConstantTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2220,7 +2483,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayMultiplyConstantTo( + public vectorSparseMapDenseArrayMultiplyConstantTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2230,7 +2493,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArraySubtractConstantFrom( + public vectorSparseMapDenseArraySubtractConstantFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2240,7 +2503,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayDivideConstantFrom( + public vectorSparseMapDenseArrayDivideConstantFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2251,7 +2514,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayAssignTo( + public vectorSparseMapDenseArrayAssignTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2261,7 +2524,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayAddTo( + public vectorSparseMapDenseArrayAddTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2271,7 +2534,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayMultiplyTo( + public vectorSparseMapDenseArrayMultiplyTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2281,7 +2544,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArraySubtractFrom( + public vectorSparseMapDenseArraySubtractFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2291,7 +2554,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayDivideFrom( + public vectorSparseMapDenseArrayDivideFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2302,7 +2565,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayAssignScaledTo( + public vectorSparseMapDenseArrayAssignScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2313,7 +2576,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayAddScaledTo( + public vectorSparseMapDenseArrayAddScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2324,7 +2587,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayMultiplyScaledTo( + public vectorSparseMapDenseArrayMultiplyScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2335,7 +2598,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArraySubtractScaledFrom( + public vectorSparseMapDenseArraySubtractScaledFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2346,7 +2609,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorSparseMapDenseArrayDivideScaledFrom( + public vectorSparseMapDenseArrayDivideScaledFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2358,7 +2621,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayAssignTo( + public vectorIndependentSparseMapDenseArrayAssignTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2372,7 +2635,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayAddTo( + public vectorIndependentSparseMapDenseArrayAddTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2386,7 +2649,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayMultiplyTo( + public vectorIndependentSparseMapDenseArrayMultiplyTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2400,7 +2663,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArraySubtractFrom( + public vectorIndependentSparseMapDenseArraySubtractFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2414,7 +2677,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayDivideFrom( + public vectorIndependentSparseMapDenseArrayDivideFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2429,7 +2692,7 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayAssignScaledTo( + public vectorIndependentSparseMapDenseArrayAssignScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2444,7 +2707,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayAddScaledTo( + public vectorIndependentSparseMapDenseArrayAddScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2459,7 +2722,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayMultiplyScaledTo( + public vectorIndependentSparseMapDenseArrayMultiplyScaledTo( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2474,7 +2737,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArraySubtractScaledFrom( + public vectorIndependentSparseMapDenseArraySubtractScaledFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2489,7 +2752,7 @@ export class MathematicsHelper { } return [sparseIndexArray0, denseValueArray0]; } - public static vectorIndependentSparseMapDenseArrayDivideScaledFrom( + public vectorIndependentSparseMapDenseArrayDivideScaledFrom( sparseIndexArray0: number[], sparseValueArray0: number[], denseValueArray0: number[], @@ -2505,229 +2768,229 @@ export class MathematicsHelper { return [sparseIndexArray0, denseValueArray0]; } - public static tensor4dNewLikeWithRandomCells( + public tensor4dNewLikeWithRandomCells( tensor4d: number[][][][]): number[][][][] { - return MathematicsHelper.tensor4dNewWithRandomCells( + return this.tensor4dNewWithRandomCells( tensor4d.length, tensor4d[0].length, tensor4d[0][0].length, tensor4d[0][0][0].length); } - public static tensor4dNewLikeWithRandomCellsScaled( + public tensor4dNewLikeWithRandomCellsScaled( tensor4d: number[][][][], scale: number = 1): number[][][][] { - return MathematicsHelper.tensor4dNewWithRandomCellsScaled( + return this.tensor4dNewWithRandomCellsScaled( tensor4d.length, tensor4d[0].length, tensor4d[0][0].length, tensor4d[0][0][0].length, scale); } - public static tensor4dNewLikeWithZeroCells( + public tensor4dNewLikeWithZeroCells( tensor4d: number[][][][]): number[][][][] { - return MathematicsHelper.tensor4dNewWithZeroCells( + return this.tensor4dNewWithZeroCells( tensor4d.length, tensor4d[0].length, tensor4d[0][0].length, tensor4d[0][0][0].length); } - public static tensor4dNewLikeWithConstantCells( + public tensor4dNewLikeWithConstantCells( tensor4d: number[][][][], constant: number = 1): number[][][][] { - return MathematicsHelper.tensor4dNewWithConstantCells( + return this.tensor4dNewWithConstantCells( tensor4d.length, tensor4d[0].length, tensor4d[0][0].length, tensor4d[0][0][0].length, constant); } - public static tensor4dNewLikeWithScaledCells( + public tensor4dNewLikeWithScaledCells( tensor4d: number[][][][], scale: number = 1): number[][][][] { - return MathematicsHelper.tensor4dNewWithScaledCells(tensor4d, scale); + return this.tensor4dNewWithScaledCells(tensor4d, scale); } - public static tensor4dNewLikeWithL1l2RegularizedSparseCells( + public tensor4dNewLikeWithL1l2RegularizedSparseCells( tensor4d: number[][][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][][] { - return MathematicsHelper.tensor4dNewWithL1l2RegularizedSparseCells( + return this.tensor4dNewWithL1l2RegularizedSparseCells( tensor4d, l1Regularization, l2Regularization); } - public static tensor4dNewLikeWithL1l2RegularizedDenseCells( + public tensor4dNewLikeWithL1l2RegularizedDenseCells( tensor4d: number[][][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][][] { - return MathematicsHelper.tensor4dNewWithL1l2RegularizedDenseCells( + return this.tensor4dNewWithL1l2RegularizedDenseCells( tensor4d, l1Regularization, l2Regularization); } - public static tensor3dNewLikeWithRandomCells( + public tensor3dNewLikeWithRandomCells( tensor3d: number[][][]): number[][][] { - return MathematicsHelper.tensor3dNewWithRandomCells( + return this.tensor3dNewWithRandomCells( tensor3d.length, tensor3d[0].length, tensor3d[0][0].length); } - public static tensor3dNewLikeWithRandomCellsScaled( + public tensor3dNewLikeWithRandomCellsScaled( tensor3d: number[][][], scale: number = 1): number[][][] { - return MathematicsHelper.tensor3dNewWithRandomCellsScaled( + return this.tensor3dNewWithRandomCellsScaled( tensor3d.length, tensor3d[0].length, tensor3d[0][0].length, scale); } - public static tensor3dNewLikeWithZeroCells( + public tensor3dNewLikeWithZeroCells( tensor3d: number[][][]): number[][][] { - return MathematicsHelper.tensor3dNewWithZeroCells( + return this.tensor3dNewWithZeroCells( tensor3d.length, tensor3d[0].length, tensor3d[0][0].length); } - public static tensor3dNewLikeWithConstantCells( + public tensor3dNewLikeWithConstantCells( tensor3d: number[][][], constant: number = 1): number[][][] { - return MathematicsHelper.tensor3dNewWithConstantCells( + return this.tensor3dNewWithConstantCells( tensor3d.length, tensor3d[0].length, tensor3d[0][0].length, constant); } - public static tensor3dNewLikeWithScaledCells( + public tensor3dNewLikeWithScaledCells( tensor3d: number[][][], scale: number = 1): number[][][] { - return MathematicsHelper.tensor3dNewWithScaledCells(tensor3d, scale); + return this.tensor3dNewWithScaledCells(tensor3d, scale); } - public static tensor3dNewLikeWithL1l2RegularizedSparseCells( + public tensor3dNewLikeWithL1l2RegularizedSparseCells( tensor3d: number[][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][] { - return MathematicsHelper.tensor3dNewWithL1l2RegularizedSparseCells( + return this.tensor3dNewWithL1l2RegularizedSparseCells( tensor3d, l1Regularization, l2Regularization); } - public static tensor3dNewLikeWithL1l2RegularizedDenseCells( + public tensor3dNewLikeWithL1l2RegularizedDenseCells( tensor3d: number[][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][] { - return MathematicsHelper.tensor3dNewWithL1l2RegularizedDenseCells( + return this.tensor3dNewWithL1l2RegularizedDenseCells( tensor3d, l1Regularization, l2Regularization); } - public static matrixNewLikeWithRandomCells( + public matrixNewLikeWithRandomCells( matrix: number[][]): number[][] { - return MathematicsHelper.matrixNewWithRandomCells(matrix.length, matrix[0].length); + return this.matrixNewWithRandomCells(matrix.length, matrix[0].length); } - public static matrixNewLikeWithRandomCellsScaled( + public matrixNewLikeWithRandomCellsScaled( matrix: number[][], scale: number = 1): number[][] { - return MathematicsHelper.matrixNewWithRandomCellsScaled(matrix.length, matrix[0].length, scale); + return this.matrixNewWithRandomCellsScaled(matrix.length, matrix[0].length, scale); } - public static matrixNewLikeWithZeroCells( + public matrixNewLikeWithZeroCells( matrix: number[][]): number[][] { - return MathematicsHelper.matrixNewWithZeroCells(matrix.length, matrix[0].length); + return this.matrixNewWithZeroCells(matrix.length, matrix[0].length); } - public static matrixNewLikeWithConstantCells( + public matrixNewLikeWithConstantCells( matrix: number[][], constant: number = 1): number[][] { - return MathematicsHelper.matrixNewWithConstantCells(matrix.length, matrix[0].length, constant); + return this.matrixNewWithConstantCells(matrix.length, matrix[0].length, constant); } - public static matrixNewLikeWithScaledCells( + public matrixNewLikeWithScaledCells( matrix: number[][], scale: number = 1): number[][] { - return MathematicsHelper.matrixNewWithScaledCells(matrix, scale); + return this.matrixNewWithScaledCells(matrix, scale); } - public static matrixNewLikeWithL1l2RegularizedSparseCells( + public matrixNewLikeWithL1l2RegularizedSparseCells( matrix: number[][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][] { - return MathematicsHelper.matrixNewWithL1l2RegularizedSparseCells( + return this.matrixNewWithL1l2RegularizedSparseCells( matrix, l1Regularization, l2Regularization); } - public static matrixNewLikeWithL1l2RegularizedDenseCells( + public matrixNewLikeWithL1l2RegularizedDenseCells( matrix: number[][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][] { - return MathematicsHelper.matrixNewWithL1l2RegularizedDenseCells( + return this.matrixNewWithL1l2RegularizedDenseCells( matrix, l1Regularization, l2Regularization); } - public static vectorNewLikeWithRandomElements( + public vectorNewLikeWithRandomElements( vector: number[]): number[] { - return MathematicsHelper.vectorNewWithRandomElements(vector.length); + return this.vectorNewWithRandomElements(vector.length); } - public static vectorNewLikeWithRandomElementsScaled( + public vectorNewLikeWithRandomElementsScaled( vector: number[], scale: number = 1): number[] { - return MathematicsHelper.vectorNewWithRandomElementsScaled(vector.length, scale); + return this.vectorNewWithRandomElementsScaled(vector.length, scale); } - public static vectorNewLikeWithZeroElements( + public vectorNewLikeWithZeroElements( vector: number[]): number[] { - return MathematicsHelper.vectorNewWithZeroElements(vector.length); + return this.vectorNewWithZeroElements(vector.length); } - public static vectorNewLikeWithConstantElements( + public vectorNewLikeWithConstantElements( vector: number[], constant: number = 1): number[] { - return MathematicsHelper.vectorNewWithConstantElements(vector.length, constant); + return this.vectorNewWithConstantElements(vector.length, constant); } - public static vectorNewLikeWithScaledElements( + public vectorNewLikeWithScaledElements( vector: number[], scale: number = 1): number[] { - return MathematicsHelper.vectorNewWithScaledElements(vector, scale); + return this.vectorNewWithScaledElements(vector, scale); } - public static vectorNewLikeWithL1l2RegularizedSparseElements( + public vectorNewLikeWithL1l2RegularizedSparseElements( vector: number[], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[] { - return MathematicsHelper.vectorNewWithL1l2RegularizedSparseElements( + return this.vectorNewWithL1l2RegularizedSparseElements( vector, l1Regularization, l2Regularization); } - public static vectorNewLikeWithL1l2RegularizedDenseElements( + public vectorNewLikeWithL1l2RegularizedDenseElements( vector: number[], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[] { - return MathematicsHelper.vectorNewWithL1l2RegularizedDenseElements( + return this.vectorNewWithL1l2RegularizedDenseElements( vector, l1Regularization, l2Regularization); } - public static tensor4dNewWithRandomCells( + public tensor4dNewWithRandomCells( rows: number, columns: number, dimension3ds: number, dimension4ds: number): number[][][][] { const tensor4d: number[][][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor4d[row] = MathematicsHelper.tensor3dNewWithRandomCells( + for (let row: number = 0; row < rows; row++) { + tensor4d[row] = this.tensor3dNewWithRandomCells( columns, dimension3ds, dimension4ds); } return tensor4d; } - public static tensor4dNewWithRandomCellsScaled( + public tensor4dNewWithRandomCellsScaled( rows: number, columns: number, dimension3ds: number, dimension4ds: number, scale: number = 1): number[][][][] { const tensor4d: number[][][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor4d[row] = MathematicsHelper.tensor3dNewWithRandomCellsScaled( + for (let row: number = 0; row < rows; row++) { + tensor4d[row] = this.tensor3dNewWithRandomCellsScaled( columns, dimension3ds, dimension4ds, @@ -2735,29 +2998,29 @@ export class MathematicsHelper { } return tensor4d; } - public static tensor4dNewWithZeroCells( + public tensor4dNewWithZeroCells( rows: number, columns: number, dimension3ds: number, dimension4ds: number): number[][][][] { const tensor4d: number[][][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor4d[row] = MathematicsHelper.tensor3dNewWithZeroCells( + for (let row: number = 0; row < rows; row++) { + tensor4d[row] = this.tensor3dNewWithZeroCells( columns, dimension3ds, dimension4ds); } return tensor4d; } - public static tensor4dNewWithConstantCells( + public tensor4dNewWithConstantCells( rows: number, columns: number, dimension3ds: number, dimension4ds: number, constant: number = 1): number[][][][] { const tensor4d: number[][][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor4d[row] = MathematicsHelper.tensor3dNewWithConstantCells( + for (let row: number = 0; row < rows; row++) { + tensor4d[row] = this.tensor3dNewWithConstantCells( columns, dimension3ds, dimension4ds, @@ -2765,38 +3028,38 @@ export class MathematicsHelper { } return tensor4d; } - public static tensor4dNewWithScaledCells( + public tensor4dNewWithScaledCells( existingTensor4d: number[][][][], scale: number = 1): number[][][][] { const rows: number = existingTensor4d.length; const tensor4d: number[][][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor4d[row] = MathematicsHelper.tensor3dNewWithScaledCells(existingTensor4d[row], scale); + for (let row: number = 0; row < rows; row++) { + tensor4d[row] = this.tensor3dNewWithScaledCells(existingTensor4d[row], scale); } return tensor4d; } - public static tensor4dNewWithL1l2RegularizedSparseCells( + public tensor4dNewWithL1l2RegularizedSparseCells( existingTensor4d: number[][][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][][] { const rows: number = existingTensor4d.length; const tensor4d: number[][][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor4d[row] = MathematicsHelper.tensor3dNewWithL1l2RegularizedSparseCells( + for (let row: number = 0; row < rows; row++) { + tensor4d[row] = this.tensor3dNewWithL1l2RegularizedSparseCells( existingTensor4d[row], l1Regularization, l2Regularization); } return tensor4d; } - public static tensor4dNewWithL1l2RegularizedDenseCells( + public tensor4dNewWithL1l2RegularizedDenseCells( existingTensor4d: number[][][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][][] { const rows: number = existingTensor4d.length; const tensor4d: number[][][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor4d[row] = MathematicsHelper.tensor3dNewWithL1l2RegularizedDenseCells( + for (let row: number = 0; row < rows; row++) { + tensor4d[row] = this.tensor3dNewWithL1l2RegularizedDenseCells( existingTensor4d[row], l1Regularization, l2Regularization); @@ -2804,80 +3067,80 @@ export class MathematicsHelper { return tensor4d; } - public static tensor3dNewWithRandomCells( + public tensor3dNewWithRandomCells( rows: number, columns: number, dimension3ds: number): number[][][] { const tensor3d: number[][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor3d[row] = MathematicsHelper.matrixNewWithRandomCells(columns, dimension3ds); + for (let row: number = 0; row < rows; row++) { + tensor3d[row] = this.matrixNewWithRandomCells(columns, dimension3ds); } return tensor3d; } - public static tensor3dNewWithRandomCellsScaled( + public tensor3dNewWithRandomCellsScaled( rows: number, columns: number, dimension3ds: number, scale: number = 1): number[][][] { const tensor3d: number[][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor3d[row] = MathematicsHelper.matrixNewWithRandomCellsScaled(columns, dimension3ds, scale); + for (let row: number = 0; row < rows; row++) { + tensor3d[row] = this.matrixNewWithRandomCellsScaled(columns, dimension3ds, scale); } return tensor3d; } - public static tensor3dNewWithZeroCells( + public tensor3dNewWithZeroCells( rows: number, columns: number, dimension3ds: number): number[][][] { const tensor3d: number[][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor3d[row] = MathematicsHelper.matrixNewWithZeroCells(columns, dimension3ds); + for (let row: number = 0; row < rows; row++) { + tensor3d[row] = this.matrixNewWithZeroCells(columns, dimension3ds); } return tensor3d; } - public static tensor3dNewWithConstantCells( + public tensor3dNewWithConstantCells( rows: number, columns: number, dimension3ds: number, constant: number = 1): number[][][] { const tensor3d: number[][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor3d[row] = MathematicsHelper.matrixNewWithConstantCells(columns, dimension3ds, constant); + for (let row: number = 0; row < rows; row++) { + tensor3d[row] = this.matrixNewWithConstantCells(columns, dimension3ds, constant); } return tensor3d; } - public static tensor3dNewWithScaledCells( + public tensor3dNewWithScaledCells( existingTensor3d: number[][][], scale: number = 1): number[][][] { const rows: number = existingTensor3d.length; const tensor3d: number[][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor3d[row] = MathematicsHelper.matrixNewWithScaledCells(existingTensor3d[row], scale); + for (let row: number = 0; row < rows; row++) { + tensor3d[row] = this.matrixNewWithScaledCells(existingTensor3d[row], scale); } return tensor3d; } - public static tensor3dNewWithL1l2RegularizedSparseCells( + public tensor3dNewWithL1l2RegularizedSparseCells( existingTensor3d: number[][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][] { const rows: number = existingTensor3d.length; const tensor3d: number[][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor3d[row] = MathematicsHelper.matrixNewWithL1l2RegularizedSparseCells( + for (let row: number = 0; row < rows; row++) { + tensor3d[row] = this.matrixNewWithL1l2RegularizedSparseCells( existingTensor3d[row], l1Regularization, l2Regularization); } return tensor3d; } - public static tensor3dNewWithL1l2RegularizedDenseCells( + public tensor3dNewWithL1l2RegularizedDenseCells( existingTensor3d: number[][][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][][] { const rows: number = existingTensor3d.length; const tensor3d: number[][][] = new Array(rows); - for (let row = 0; row < rows; row++) { - tensor3d[row] = MathematicsHelper.matrixNewWithL1l2RegularizedDenseCells( + for (let row: number = 0; row < rows; row++) { + tensor3d[row] = this.matrixNewWithL1l2RegularizedDenseCells( existingTensor3d[row], l1Regularization, l2Regularization); @@ -2885,76 +3148,76 @@ export class MathematicsHelper { return tensor3d; } - public static matrixNewWithRandomCells( + public matrixNewWithRandomCells( rows: number, columns: number): number[][] { const matrix: number[][] = new Array(rows); - for (let row = 0; row < rows; row++) { - matrix[row] = MathematicsHelper.vectorNewWithRandomElements(columns); + for (let row: number = 0; row < rows; row++) { + matrix[row] = this.vectorNewWithRandomElements(columns); } return matrix; } - public static matrixNewWithRandomCellsScaled( + public matrixNewWithRandomCellsScaled( rows: number, columns: number, scale: number = 1): number[][] { const matrix: number[][] = new Array(rows); - for (let row = 0; row < rows; row++) { - matrix[row] = MathematicsHelper.vectorNewWithRandomElementsScaled(columns, scale); + for (let row: number = 0; row < rows; row++) { + matrix[row] = this.vectorNewWithRandomElementsScaled(columns, scale); } return matrix; } - public static matrixNewWithZeroCells( + public matrixNewWithZeroCells( rows: number, columns: number): number[][] { const matrix: number[][] = new Array(rows); - for (let row = 0; row < rows; row++) { - matrix[row] = MathematicsHelper.vectorNewWithZeroElements(columns); + for (let row: number = 0; row < rows; row++) { + matrix[row] = this.vectorNewWithZeroElements(columns); } return matrix; } - public static matrixNewWithConstantCells( + public matrixNewWithConstantCells( rows: number, columns: number, constant: number = 1): number[][] { const matrix: number[][] = new Array(rows); - for (let row = 0; row < rows; row++) { - matrix[row] = MathematicsHelper.vectorNewWithConstantElements(columns, constant); + for (let row: number = 0; row < rows; row++) { + matrix[row] = this.vectorNewWithConstantElements(columns, constant); } return matrix; } - public static matrixNewWithScaledCells( + public matrixNewWithScaledCells( existingMatrix: number[][], scale: number = 1): number[][] { const rows: number = existingMatrix.length; const matrix: number[][] = new Array(rows); - for (let row = 0; row < rows; row++) { - matrix[row] = MathematicsHelper.vectorNewWithScaledElements(existingMatrix[row], scale); + for (let row: number = 0; row < rows; row++) { + matrix[row] = this.vectorNewWithScaledElements(existingMatrix[row], scale); } return matrix; } - public static matrixNewWithL1l2RegularizedSparseCells( + public matrixNewWithL1l2RegularizedSparseCells( existingMatrix: number[][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][] { const rows: number = existingMatrix.length; const matrix: number[][] = new Array(rows); - for (let row = 0; row < rows; row++) { - matrix[row] = MathematicsHelper.vectorNewWithL1l2RegularizedSparseElements( + for (let row: number = 0; row < rows; row++) { + matrix[row] = this.vectorNewWithL1l2RegularizedSparseElements( existingMatrix[row], l1Regularization, l2Regularization); } return matrix; } - public static matrixNewWithL1l2RegularizedDenseCells( + public matrixNewWithL1l2RegularizedDenseCells( existingMatrix: number[][], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[][] { const rows: number = existingMatrix.length; const matrix: number[][] = new Array(rows); - for (let row = 0; row < rows; row++) { - matrix[row] = MathematicsHelper.vectorNewWithL1l2RegularizedDenseElements( + for (let row: number = 0; row < rows; row++) { + matrix[row] = this.vectorNewWithL1l2RegularizedDenseElements( existingMatrix[row], l1Regularization, l2Regularization); @@ -2962,7 +3225,7 @@ export class MathematicsHelper { return matrix; } - public static vectorNewWithRandomElements( + public vectorNewWithRandomElements( length: number): number[] { const vector: number[] = new Array(length); for (let i: number = 0; i < length; i++) { @@ -2970,7 +3233,7 @@ export class MathematicsHelper { } return vector; } - public static vectorNewWithRandomElementsScaled( + public vectorNewWithRandomElementsScaled( length: number, scale: number = 1): number[] { const vector: number[] = new Array(length); @@ -2979,7 +3242,7 @@ export class MathematicsHelper { } return vector; } - public static vectorNewWithZeroElements( + public vectorNewWithZeroElements( length: number): number[] { const vector: number[] = new Array(length); for (let i: number = 0; i < length; i++) { @@ -2987,7 +3250,7 @@ export class MathematicsHelper { } return vector; } - public static vectorNewWithConstantElements( + public vectorNewWithConstantElements( length: number, constant: number = 1): number[] { const vector: number[] = new Array(length); @@ -2996,7 +3259,7 @@ export class MathematicsHelper { } return vector; } - public static vectorNewWithScaledElements( + public vectorNewWithScaledElements( existingVector: number[], scale: number = 1): number[] { const length: number = existingVector.length; @@ -3006,14 +3269,14 @@ export class MathematicsHelper { } return vector; } - public static vectorNewWithL1l2RegularizedSparseElements( + public vectorNewWithL1l2RegularizedSparseElements( existingVector: number[], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[] { const length: number = existingVector.length; const vector: number[] = new Array(length); for (let i: number = 0; i < length; i++) { - const regularized: number = MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse( + const regularized: number = this.getL1l2RegularizedWeightOptimizedSparse( existingVector[i], l1Regularization, l2Regularization); @@ -3021,14 +3284,14 @@ export class MathematicsHelper { } return vector; } - public static vectorNewWithL1l2RegularizedDenseElements( + public vectorNewWithL1l2RegularizedDenseElements( existingVector: number[], l1Regularization: number = 0.01, l2Regularization: number = 0.01): number[] { const length: number = existingVector.length; const vector: number[] = new Array(length); for (let i: number = 0; i < length; i++) { - const regularized: number = MathematicsHelper.getL1l2RegularizedWeightOptimizedDense( + const regularized: number = this.getL1l2RegularizedWeightOptimizedDense( existingVector[i], l1Regularization, l2Regularization); @@ -3037,14 +3300,57 @@ export class MathematicsHelper { return vector; } - public static getIndexOnFirstMaxEntryOnArray( + public getIndexesOnMaxEntriesOnArray( + inputArray: Float32Array | Int32Array | Uint8Array): + { "indexesMax": number[], "max": number } { + if (Utility.isEmptyNumberF32I32U8Array(inputArray)) { + Utility.debuggingThrow("inputArray is empty"); + } + let indexesMax: number[] = [0]; + let max: number = inputArray[0]; + for (let i: number = 1; i < inputArray.length; i++) { + const inputCurrent: number = inputArray[i]; + if (inputCurrent > max) { + max = inputCurrent; + indexesMax = [i]; + continue; + } + if (inputCurrent === max) { + indexesMax.push(i); + } + } + return { indexesMax, max }; + } + public getIndexesOnMaxEntries( + inputArray: number[]): + { "indexesMax": number[], "max": number } { + if (Utility.isEmptyNumberArray(inputArray)) { + Utility.debuggingThrow("inputArray is empty"); + } + let indexesMax: number[] = [0]; + let max: number = inputArray[0]; + for (let i: number = 1; i < inputArray.length; i++) { + const inputCurrent: number = inputArray[i]; + if (inputCurrent > max) { + max = inputCurrent; + indexesMax = [i]; + continue; + } + if (inputCurrent === max) { + indexesMax.push(i); + } + } + return { indexesMax, max }; + } + + public getIndexOnFirstMaxEntryOnArray( inputArray: Float32Array | Int32Array | Uint8Array): { "indexMax": number, "max": number } { if (Utility.isEmptyNumberF32I32U8Array(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMax: number = 0; - let max = inputArray[0]; + let max: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent > max) { @@ -3054,14 +3360,14 @@ export class MathematicsHelper { } return { indexMax, max }; } - public static getIndexOnLastMaxEntryOnArray( + public getIndexOnLastMaxEntryOnArray( inputArray: Float32Array | Int32Array | Uint8Array): { "indexMax": number, "max": number } { if (Utility.isEmptyNumberF32I32U8Array(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMax: number = 0; - let max = inputArray[0]; + let max: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent >= max) { @@ -3071,14 +3377,14 @@ export class MathematicsHelper { } return { indexMax, max }; } - public static getIndexOnFirstMaxEntry( + public getIndexOnFirstMaxEntry( inputArray: number[]): { "indexMax": number, "max": number } { if (Utility.isEmptyNumberArray(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMax: number = 0; - let max = inputArray[0]; + let max: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent > max) { @@ -3088,14 +3394,14 @@ export class MathematicsHelper { } return { indexMax, max }; } - public static getIndexOnLastMaxEntry( + public getIndexOnLastMaxEntry( inputArray: number[]): { "indexMax": number, "max": number } { if (Utility.isEmptyNumberArray(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMax: number = 0; - let max = inputArray[0]; + let max: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent >= max) { @@ -3106,14 +3412,57 @@ export class MathematicsHelper { return { indexMax, max }; } - public static getIndexOnFirstMinEntryOnArray( + public getIndexesOnMinEntriesOnArray( + inputArray: Float32Array | Int32Array | Uint8Array): + { "indexesMin": number[], "min": number } { + if (Utility.isEmptyNumberF32I32U8Array(inputArray)) { + Utility.debuggingThrow("inputArray is empty"); + } + let indexesMin: number[] = [0]; + let min: number = inputArray[0]; + for (let i: number = 1; i < inputArray.length; i++) { + const inputCurrent: number = inputArray[i]; + if (inputCurrent < min) { + min = inputCurrent; + indexesMin = [i]; + continue; + } + if (inputCurrent === min) { + indexesMin.push(i); + } + } + return { indexesMin, min }; + } + public getIndexesOnMinEntries( + inputArray: number[]): + { "indexesMin": number[], "min": number } { + if (Utility.isEmptyNumberArray(inputArray)) { + Utility.debuggingThrow("inputArray is empty"); + } + let indexesMin: number[] = [0]; + let min: number = inputArray[0]; + for (let i: number = 1; i < inputArray.length; i++) { + const inputCurrent: number = inputArray[i]; + if (inputCurrent < min) { + min = inputCurrent; + indexesMin = [i]; + continue; + } + if (inputCurrent === min) { + indexesMin.push(i); + } + } + return { indexesMin, min }; + } + + public getIndexOnFirstMinEntryOnArray( inputArray: Float32Array | Int32Array | Uint8Array): { "indexMin": number, "min": number } { if (Utility.isEmptyNumberF32I32U8Array(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMin: number = 0; - let min = inputArray[0]; + let min: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent < min) { @@ -3123,14 +3472,14 @@ export class MathematicsHelper { } return { indexMin, min }; } - public static getIndexOnLastMinEntryOnArray( + public getIndexOnLastMinEntryOnArray( inputArray: Float32Array | Int32Array | Uint8Array): { "indexMin": number, "min": number } { if (Utility.isEmptyNumberF32I32U8Array(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMin: number = 0; - let min = inputArray[0]; + let min: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent <= min) { @@ -3140,14 +3489,14 @@ export class MathematicsHelper { } return { indexMin, min }; } - public static getIndexOnFirstMinEntry( + public getIndexOnFirstMinEntry( inputArray: number[]): { "indexMin": number, "min": number } { if (Utility.isEmptyNumberArray(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMin: number = 0; - let min = inputArray[0]; + let min: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent < min) { @@ -3157,14 +3506,14 @@ export class MathematicsHelper { } return { indexMin, min }; } - public static getIndexOnLastMinEntry( + public getIndexOnLastMinEntry( inputArray: number[]): { "indexMin": number, "min": number } { if (Utility.isEmptyNumberArray(inputArray)) { Utility.debuggingThrow("inputArray is empty"); } let indexMin: number = 0; - let min = inputArray[0]; + let min: number = inputArray[0]; for (let i: number = 1; i < inputArray.length; i++) { const inputCurrent: number = inputArray[i]; if (inputCurrent <= min) { @@ -3175,13 +3524,13 @@ export class MathematicsHelper { return { indexMin, min }; } - public static safeDivide(numerator: number, denominator: number): number { + public safeDivide(numerator: number, denominator: number): number { if (numerator === 0) { return 0; } - return numerator / denominator; + return (numerator / denominator); } - public static safeLog(value: number): number { + public safeLog(value: number): number { if (value < 0) { return Number.NaN; } @@ -3191,7 +3540,7 @@ export class MathematicsHelper { return Math.log(value); } - public static clipValue(value: number): number { + public clipValue(value: number): number { if (value <= 0) { return MathematicsHelper.epsilon; } diff --git a/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseEvaluator.ts b/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseEvaluator.ts index aa3496f3e..b2a5e49ea 100644 --- a/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseEvaluator.ts +++ b/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseEvaluator.ts @@ -53,7 +53,7 @@ export abstract class AbstractBaseEvaluator { } return Utility.dumpFile( outputReportFilename, - Utility.JSONstringify(outputReportContent), + Utility.jsonStringify(outputReportContent), encoding); } public dumpEvaluationDirectReportToFile( diff --git a/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseModelFeaturizerEvaluator.ts b/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseModelFeaturizerEvaluator.ts index 2f9765f2d..3122dad9f 100644 --- a/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseModelFeaturizerEvaluator.ts +++ b/packages/dispatcher/src/model/evaluation/abstract_base_evaluator/AbstractBaseModelFeaturizerEvaluator.ts @@ -22,7 +22,7 @@ export abstract class AbstractBaseModelFeaturizerEvaluator extends AbstractBaseE protected featurizerNullable: NgramSubwordFeaturizer|null = null; protected labels: string[] = []; - protected labelMap: { [id: string]: number; } = {}; + protected labelMap: { [id: string]: number } = {}; constructor( modelFilename: string, @@ -30,7 +30,7 @@ export abstract class AbstractBaseModelFeaturizerEvaluator extends AbstractBaseE modelNullable: SoftmaxRegressionSparse|null, featurizerNullable: NgramSubwordFeaturizer|null, labels: string[], - labelMap: { [id: string]: number; }) { + labelMap: { [id: string]: number }) { super(); if (!Utility.isEmptyString(modelFilename)) { this.modelNullable = this.deserializeModel(modelFilename); @@ -100,7 +100,7 @@ export abstract class AbstractBaseModelFeaturizerEvaluator extends AbstractBaseE } return this.labels; } - public getLabelMap(): { [id: string]: number; } { + public getLabelMap(): { [id: string]: number } { if (DictionaryMapUtility.isEmptyStringIdGenericValueDictionary(this.labelMap)) { Utility.debuggingThrow("this.labelMap is empty"); } diff --git a/packages/dispatcher/src/model/evaluation/cross_validation/AppCrossValidator.ts b/packages/dispatcher/src/model/evaluation/cross_validation/AppCrossValidator.ts index 14a31934f..57a885d94 100644 --- a/packages/dispatcher/src/model/evaluation/cross_validation/AppCrossValidator.ts +++ b/packages/dispatcher/src/model/evaluation/cross_validation/AppCrossValidator.ts @@ -468,9 +468,9 @@ export async function mainCrossValidator(): Promise<{ const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/model/evaluation/cross_validation/CrossValidator.ts b/packages/dispatcher/src/model/evaluation/cross_validation/CrossValidator.ts index 6f36fbb07..4edecaafa 100644 --- a/packages/dispatcher/src/model/evaluation/cross_validation/CrossValidator.ts +++ b/packages/dispatcher/src/model/evaluation/cross_validation/CrossValidator.ts @@ -6,10 +6,11 @@ import { AppSoftmaxRegressionSparse } from "../../supervised/classifier/neural_network/learner/AppSoftmaxRegressionSparse"; import { SoftmaxRegressionSparse } from "../../supervised/classifier/neural_network/learner/SoftmaxRegressionSparse"; +import { IMathematicsHelper } from "../../../mathematics/mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../../../mathematics/mathematics_helper/MathematicsHelper"; +import { IConfusionMatrix } from "../../../mathematics/confusion_matrix/IConfusionMatrix"; import { ConfusionMatrix } from "../../../mathematics/confusion_matrix/ConfusionMatrix"; - import { BinaryConfusionMatrix } from "../../../mathematics/confusion_matrix/BinaryConfusionMatrix"; import { ThresholdReporter } from "../report/ThresholdReporter"; @@ -24,13 +25,16 @@ import { Utility } from "../../../utility/Utility"; export class CrossValidator extends AbstractBaseEvaluator { + public static readonly MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + public static defaultNumberOfCrossValidationFolds: number = 5; protected numberOfCrossValidationFolds: number = CrossValidator.defaultNumberOfCrossValidationFolds; protected labelsCachedAfterCrossValidation: string[] = []; - protected labelMapCachedAfterCrossValidation: { [id: string]: number; } = {}; + protected labelMapCachedAfterCrossValidation: { [id: string]: number } = {}; protected numberLabelsCachedAfterCrossValidation: number = -1; protected numberFeaturesCachedAfterCrossValidation: number = -1; protected intentsCachedAfterCrossValidation: string[] = []; @@ -230,22 +234,24 @@ export class CrossValidator extends AbstractBaseEvaluator { const confusionMatrixCrossValidation: ConfusionMatrix = this.crossValidationResultCachedAfterCrossValidation.confusionMatrixCrossValidation; const confusionMatrixMetricStructure: { - "confusionMatrix": ConfusionMatrix, - "labelBinaryConfusionMatrixDerivedMetricMap": { [id: string]: { [id: string]: number }; }, - "labelBinaryConfusionMatrixMetricMap": { [id: string]: BinaryConfusionMatrix; }, - "macroAverageMetrics": { "averagePrecision": number, - "averageRecall": number, - "averageF1Score": number, - "support": number }, - "microAverageMetrics": { "accuracy": number, - "truePositives": number, - "support": number }, - "weightedMacroAverageMetrics": { "weightedAveragePrecision": number, - "weightedAverageRecall": number, - "weightedAverageF1Score": number, - "support": number } } = - ConfusionMatrix.generateConfusionMatrixMetricStructure( - confusionMatrixCrossValidation); + "confusionMatrix": IConfusionMatrix, + "labelBinaryConfusionMatrixBasicMetricMap": { [id: string]: { [id: string]: number } }, + "labelBinaryConfusionMatrixMap": { [id: string]: BinaryConfusionMatrix }, + "macroAverageMetrics": { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number }, + "microAverageMetrics": { + "accuracy": number, + "truePositives": number, + "support": number }, + "weightedMacroAverageMetrics": { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } } = + confusionMatrixCrossValidation.generateConfusionMatrixMetricStructure(); Utility.debuggingLog( `confusionMatrixCrossValidation.getMicroAverageMetrics()=` + `${confusionMatrixCrossValidation.getMicroAverageMetrics()}` + @@ -295,7 +301,7 @@ export class CrossValidator extends AbstractBaseEvaluator { public crossValidate( labels: string[], - labelMap: { [id: string]: number; }, + labelMap: { [id: string]: number }, numberLabels: number, numberFeatures: number, intents: string[], @@ -519,7 +525,7 @@ export class CrossValidator extends AbstractBaseEvaluator { learner.predict([instanceFeatureIndexArray])[0]; predictions.push(prediction); const argMax: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntry(prediction); + CrossValidator.MathematicsHelperObject.getIndexOnFirstMaxEntry(prediction); const predictionLabelIndex: number = argMax.indexMax; predictionLabelIndexes.push(predictionLabelIndex); diff --git a/packages/dispatcher/src/model/evaluation/predict/AppPredictor.ts b/packages/dispatcher/src/model/evaluation/predict/AppPredictor.ts index 8ec934bcf..a0295f733 100644 --- a/packages/dispatcher/src/model/evaluation/predict/AppPredictor.ts +++ b/packages/dispatcher/src/model/evaluation/predict/AppPredictor.ts @@ -82,9 +82,9 @@ export function mainPredictor(): void { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); @@ -146,7 +146,7 @@ export function mainPredictor(): void { featurizerNullable as NgramSubwordFeaturizer; // ------------------------------------------------------------------- const labels: string[] = featurizer.getLabels(); - const labelMap: { [id: string]: number; } = featurizer.getLabelMap(); + const labelMap: { [id: string]: number } = featurizer.getLabelMap(); // const numberLabels: number = featurizer.getNumberLabels(); // const numberFeatures: number = featurizer.getNumberFeatures(); // ----------------------------------------------------------------------- diff --git a/packages/dispatcher/src/model/evaluation/predict/Predictor.ts b/packages/dispatcher/src/model/evaluation/predict/Predictor.ts index a41bbc8b1..f9bd98d22 100644 --- a/packages/dispatcher/src/model/evaluation/predict/Predictor.ts +++ b/packages/dispatcher/src/model/evaluation/predict/Predictor.ts @@ -5,10 +5,11 @@ import { SoftmaxRegressionSparse } from "../../supervised/classifier/neural_network/learner/SoftmaxRegressionSparse"; +import { IMathematicsHelper } from "../../../mathematics/mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../../../mathematics/mathematics_helper/MathematicsHelper"; +import { IConfusionMatrix } from "../../../mathematics/confusion_matrix/IConfusionMatrix"; import { ConfusionMatrix } from "../../../mathematics/confusion_matrix/ConfusionMatrix"; - import { BinaryConfusionMatrix } from "../../../mathematics/confusion_matrix/BinaryConfusionMatrix"; import { ThresholdReporter } from "../report/ThresholdReporter"; @@ -25,8 +26,11 @@ import { Utility } from "../../../utility/Utility"; export class Predictor extends AbstractBaseModelFeaturizerEvaluator { + public static readonly MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + protected labels: string[] = []; - protected labelMap: { [id: string]: number; } = {}; + protected labelMap: { [id: string]: number } = {}; protected intents: string[] = []; protected utterances: string[] = []; @@ -169,22 +173,24 @@ export class Predictor extends AbstractBaseModelFeaturizerEvaluator { const confusionMatrixPrediction: ConfusionMatrix = predictionResult.confusionMatrixPrediction; const confusionMatrixMetricStructure: { - "confusionMatrix": ConfusionMatrix, - "labelBinaryConfusionMatrixDerivedMetricMap": { [id: string]: { [id: string]: number }; }, - "labelBinaryConfusionMatrixMetricMap": { [id: string]: BinaryConfusionMatrix; }, - "macroAverageMetrics": { "averagePrecision": number, - "averageRecall": number, - "averageF1Score": number, - "support": number }, - "microAverageMetrics": { "accuracy": number, - "truePositives": number, - "support": number }, - "weightedMacroAverageMetrics": { "weightedAveragePrecision": number, - "weightedAverageRecall": number, - "weightedAverageF1Score": number, - "support": number } } = - ConfusionMatrix.generateConfusionMatrixMetricStructure( - confusionMatrixPrediction); + "confusionMatrix": IConfusionMatrix, + "labelBinaryConfusionMatrixBasicMetricMap": { [id: string]: { [id: string]: number } }, + "labelBinaryConfusionMatrixMap": { [id: string]: BinaryConfusionMatrix }, + "macroAverageMetrics": { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number }, + "microAverageMetrics": { + "accuracy": number, + "truePositives": number, + "support": number }, + "weightedMacroAverageMetrics": { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } } = + confusionMatrixPrediction.generateConfusionMatrixMetricStructure(); Utility.debuggingLog( `confusionMatrixPrediction.getMicroAverageMetrics()=` + `${confusionMatrixPrediction.getMicroAverageMetrics()}` + @@ -253,7 +259,7 @@ export class Predictor extends AbstractBaseModelFeaturizerEvaluator { this.getFeaturizer(); // ------------------------------------------------------------------- const labels: string[] = this.getLabels(); - const labelMap: { [id: string]: number; } = this.getLabelMap(); + const labelMap: { [id: string]: number } = this.getLabelMap(); // const numberLabels: number = featurizer.getNumberLabels(); // const numberFeatures: number = featurizer.getNumberFeatures(); // ------------------------------------------------------------------- @@ -270,7 +276,7 @@ export class Predictor extends AbstractBaseModelFeaturizerEvaluator { const prediction: number[] = predictions[0]; // --------------------------------------------------------------- const argMax: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntry(prediction); + Predictor.MathematicsHelperObject.getIndexOnFirstMaxEntry(prediction); const predictionLabelIndex: number = argMax.indexMax; let groundTruthLabelIndex: number = labelMap[intent]; diff --git a/packages/dispatcher/src/model/evaluation/report/AppDataProfileReporter.ts b/packages/dispatcher/src/model/evaluation/report/AppDataProfileReporter.ts index d94f87a76..ac3fb1de2 100644 --- a/packages/dispatcher/src/model/evaluation/report/AppDataProfileReporter.ts +++ b/packages/dispatcher/src/model/evaluation/report/AppDataProfileReporter.ts @@ -93,9 +93,9 @@ export function mainDataProfileReporter(): void { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/model/evaluation/report/AppModelMetaDataProfileReporter.ts b/packages/dispatcher/src/model/evaluation/report/AppModelMetaDataProfileReporter.ts index 7c084e927..0a53e4c06 100644 --- a/packages/dispatcher/src/model/evaluation/report/AppModelMetaDataProfileReporter.ts +++ b/packages/dispatcher/src/model/evaluation/report/AppModelMetaDataProfileReporter.ts @@ -107,9 +107,9 @@ export function mainModelMetaDataProfileReporter(): void { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/model/evaluation/report/AppThresholdReporter.ts b/packages/dispatcher/src/model/evaluation/report/AppThresholdReporter.ts index 1ca180885..c64345dc3 100644 --- a/packages/dispatcher/src/model/evaluation/report/AppThresholdReporter.ts +++ b/packages/dispatcher/src/model/evaluation/report/AppThresholdReporter.ts @@ -131,9 +131,9 @@ export function mainThresholdReporter(): void { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); @@ -196,9 +196,9 @@ export function mainThresholdReporter(): void { `lineIndexToStart=${lineIndexToStart}`); // ----------------------------------------------------------------------- let labels: string[] = []; - let labelMap: { [id: string]: number; } = {}; + let labelMap: { [id: string]: number } = {}; if (!Utility.isEmptyString(labelFilename)) { - const labelsAndLabelMap: { "stringArray": string[], "stringMap": { [id: string]: number; } } = + const labelsAndLabelMap: { "stringArray": string[], "stringMap": { [id: string]: number } } = DictionaryMapUtility.buildStringIdNumberValueDictionaryFromUniqueStringArrayFile(labelFilename); labels = labelsAndLabelMap.stringArray; labelMap = labelsAndLabelMap.stringMap; diff --git a/packages/dispatcher/src/model/evaluation/report/ModelMetaDataProfileReporter.ts b/packages/dispatcher/src/model/evaluation/report/ModelMetaDataProfileReporter.ts index 870302fe6..df4969c14 100644 --- a/packages/dispatcher/src/model/evaluation/report/ModelMetaDataProfileReporter.ts +++ b/packages/dispatcher/src/model/evaluation/report/ModelMetaDataProfileReporter.ts @@ -29,7 +29,7 @@ export class ModelMetaDataProfileReporter extends AbstractBaseModelFeaturizerEva modelNullable: SoftmaxRegressionSparse|null, featurizerNullable: NgramSubwordFeaturizer|null, labels: string[], - labelMap: { [id: string]: number; }) { + labelMap: { [id: string]: number }) { super( modelFilename, featurizerFilename, diff --git a/packages/dispatcher/src/model/evaluation/report/ThresholdReporter.ts b/packages/dispatcher/src/model/evaluation/report/ThresholdReporter.ts index f4d7d5e95..b5ca7c060 100644 --- a/packages/dispatcher/src/model/evaluation/report/ThresholdReporter.ts +++ b/packages/dispatcher/src/model/evaluation/report/ThresholdReporter.ts @@ -19,6 +19,9 @@ import { DictionaryMapUtility } from "../../../data_structure/DictionaryMapUtili import { Utility } from "../../../utility/Utility"; +const NumberOfLabelsPerBatchToReport: number = 8; +const EnableDebuggingInstrumentation: boolean = false; + export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { protected instancePredictedScoreArrays: number[][] = []; @@ -28,7 +31,7 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { protected instanceWeights: number[] = []; protected targetLabelBatchesToReport: string[][] = []; - protected numberOfLabelsPerBatch: number = 8; + protected numberOfLabelsPerBatch: number = NumberOfLabelsPerBatchToReport; protected explicitTotal: number = -1; protected explicitTotalPositives: number = -1; protected explicitTotalNegatives: number = -1; @@ -47,9 +50,9 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { modelNullable: SoftmaxRegressionSparse|null, featurizerNullable: NgramSubwordFeaturizer|null, labels: string[], - labelMap: { [id: string]: number; }, + labelMap: { [id: string]: number }, targetLabelBatchesToReport: string[][] = [], - numberOfLabelsPerBatch: number = 8, + numberOfLabelsPerBatch: number = NumberOfLabelsPerBatchToReport, explicitTotal: number = -1, explicitTotalPositives: number = -1, explicitTotalNegatives: number = -1, @@ -165,11 +168,12 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { } public reportToDataArrays(): string[][] { - const report: Array, "metricValues": number[][], }>> = this.report( this.targetLabelBatchesToReport, @@ -185,14 +189,31 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { this.A, this.B, this.explicitMaxPositives); + let cell11MetricIndexForDebugInstrumentation: number = -1; + let ratioCell11MetricIndexForDebugInstrumentation: number = -1; + let cell11MetricIndexInOutputArrayForDebugInstrumentation: number = -1; + let ratioCell11MetricIndexInOutputArrayForDebugInstrumentation: number = -1; const outputEvaluationReportDataArrayHeader: string[] = []; const outputEvaluationReportDataArrays: string[][] = []; - for (const reportPerBatch of report) { - for (const reportPerLabel of reportPerBatch) { - const tarrgetLabel: string = reportPerLabel.targetLabel; - const scoreThresholds: number[] = reportPerLabel.scoreThresholds; - const metricNames: string[] = reportPerLabel.metricNames; - const metricValues: number[][] = reportPerLabel.metricValues; + for (const reportResultsPerBatch of reportResults) { + for (const reportResultPerLabel of reportResultsPerBatch) { + const tarrgetLabel: string = reportResultPerLabel.targetLabel; + const scoreThresholds: number[] = reportResultPerLabel.scoreThresholds; + const metricNames: string[] = reportResultPerLabel.metricNames; + const metricNameMap: IDictionaryStringIdGenericValue = reportResultPerLabel.metricNameMap; + const metricValues: number[][] = reportResultPerLabel.metricValues; + if (EnableDebuggingInstrumentation) { // ---- NOTE-DEBUG-INSTRUMENTATION ---- + // const binaryConfusionMatrices: BinaryConfusionMatrix[] = + // reportResultPerLabel.binaryConfusionMatrices; + cell11MetricIndexForDebugInstrumentation = + metricNameMap.Cell11; + ratioCell11MetricIndexForDebugInstrumentation = + metricNameMap.RatioCell11; + cell11MetricIndexInOutputArrayForDebugInstrumentation = + cell11MetricIndexForDebugInstrumentation + 2; + ratioCell11MetricIndexInOutputArrayForDebugInstrumentation = + ratioCell11MetricIndexForDebugInstrumentation + 2; + } if (Utility.isEmptyStringArray(outputEvaluationReportDataArrayHeader)) { outputEvaluationReportDataArrayHeader.push("TargetLabel"); outputEvaluationReportDataArrayHeader.push("ScoreThreshold"); @@ -201,8 +222,8 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { } outputEvaluationReportDataArrays.push(outputEvaluationReportDataArrayHeader); } - const numberInstancs: number = scoreThresholds.length; - for (let indexInstance: number = 0; indexInstance < numberInstancs; indexInstance++) { + const numberInstances: number = scoreThresholds.length; + for (let indexInstance: number = 0; indexInstance < numberInstances; indexInstance++) { const outputEvaluationReportDataArray: string[] = []; const scoreThreshold: number = scoreThresholds[indexInstance]; const metricValueArray: number[] = metricValues[indexInstance]; @@ -212,6 +233,26 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { outputEvaluationReportDataArray.push(metricValue.toString()); } outputEvaluationReportDataArrays.push(outputEvaluationReportDataArray); + if (EnableDebuggingInstrumentation) { // ---- NOTE-DEBUG-INSTRUMENTATION ---- + const cell1MetricValue: number = + metricValueArray[cell11MetricIndexForDebugInstrumentation]; + const ratioCell1MetricValue: number = + metricValueArray[ratioCell11MetricIndexForDebugInstrumentation]; + const cell1MetricValueInString: string = + outputEvaluationReportDataArray[cell11MetricIndexInOutputArrayForDebugInstrumentation]; + const ratioCell1MetricValueInString: string = + outputEvaluationReportDataArray[ratioCell11MetricIndexInOutputArrayForDebugInstrumentation]; + if (cell1MetricValue > 0) { + Utility.debuggingLog( + `cell1MetricValue=${cell1MetricValue}`); + Utility.debuggingLog( + `cell1MetricValueInString=${cell1MetricValueInString}`); + Utility.debuggingLog( + `ratioCell1MetricValue=${ratioCell1MetricValue}`); + Utility.debuggingLog( + `ratioCell1MetricValueInString=${ratioCell1MetricValueInString}`); + } + } } } } @@ -219,7 +260,7 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { } public report( targetLabelBatchesToReport: string[][] = [], - numberOfLabelsPerBatch: number = 8, + numberOfLabelsPerBatch: number = NumberOfLabelsPerBatchToReport, explicitTotal: number = -1, explicitTotalPositives: number = -1, explicitTotalNegatives: number = -1, @@ -235,6 +276,7 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { "scoreThresholds": number[], "binaryConfusionMatrices": BinaryConfusionMatrix[], "metricNames": string[], + "metricNameMap": IDictionaryStringIdGenericValue, "metricValues": number[][], }>> { this.validate(); @@ -243,7 +285,7 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { if (Utility.isEmptyStringArrays(targetLabelBatchesToReport)) { targetLabelBatchesToReport = []; if (numberOfLabelsPerBatch <= 0) { - numberOfLabelsPerBatch = 8; + numberOfLabelsPerBatch = NumberOfLabelsPerBatchToReport; } let numberLabelBatches: number = Math.floor(numberLabels / numberOfLabelsPerBatch); if ((numberLabelBatches * numberOfLabelsPerBatch) < numberLabels) { @@ -264,21 +306,25 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { } } } - const labelMap: { [id: string]: number; } = this.getLabelMap(); + const labelMap: { [id: string]: number } = this.getLabelMap(); + let metricNames: string[] = []; + let metricNameMap: IDictionaryStringIdGenericValue = {}; const numberInstances: number = this.instanceGroudTruthLabelIds.length; - const report: Array, "metricValues": number[][], }>> = []; for (const targetLabelsPerBatchToReport of targetLabelBatchesToReport) { - const reportPerBatch: Array<{ + const reportResultsPerBatch: Array<{ "targetLabel": string, "scoreThresholds": number[], "binaryConfusionMatrices": BinaryConfusionMatrix[], "metricNames": string[], + "metricNameMap": IDictionaryStringIdGenericValue, "metricValues": number[][], }> = []; for (const targetLabel of targetLabelsPerBatchToReport) { @@ -288,23 +334,31 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { Utility.debuggingThrow( `targetLabel|${targetLabel}| is not defined.`); } - const targetLabelIndexScores: number[] = this.instancePredictedScoreArrays.map( + let targetLabelIndexScores: number[] = this.instancePredictedScoreArrays.map( (scoreArray) => scoreArray[targetLabelIndex]); const targetLabelPositives: number = this.instanceGroudTruthLabelIds.reduce( (total, instanceGroudTruthLabelId) => (instanceGroudTruthLabelId === targetLabelIndex ? total + 1 : total), 0); - const orderSequence: number[] = ListArrayUtility.sortGenerateOrderSequence( + let orderSequence: number[] = ListArrayUtility.sortGenerateOrderSequence( targetLabelIndexScores); + { + orderSequence = orderSequence.reverse(); + targetLabelIndexScores = targetLabelIndexScores.reverse(); + } const binaryConfusionMatrixBase: BinaryConfusionMatrix = new BinaryConfusionMatrix( numberInstances, 0, targetLabelPositives, 0); + if (Utility.isEmptyStringArray(metricNames)) { + metricNames = binaryConfusionMatrixBase.getMetricNames(); + } + if (DictionaryMapUtility.isEmptyStringIdGenericValueDictionary(metricNameMap)) { + metricNameMap = binaryConfusionMatrixBase.getMetricNameMap(); + } const binaryConfusionMatrices: BinaryConfusionMatrix[] = orderSequence.map( (instanceIndex) => binaryConfusionMatrixBase.moveFromPredictedNegativeToPositive( this.instanceGroudTruthLabelIds[instanceIndex] === targetLabelIndex)); - const metricNames: string[] = - binaryConfusionMatrixBase.getMetricNames(); const metricValues: number[][] = binaryConfusionMatrices.map((binaryConfusionMatrix) => binaryConfusionMatrix.getMetricValues( explicitTotal, @@ -318,17 +372,46 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { A, B, explicitMaxPositives)); - reportPerBatch.push({ + if (EnableDebuggingInstrumentation) { // ---- NOTE-DEBUG-INSTRUMENTATION ---- + const totalMetricIndex: number = + metricNameMap.Total; + const cell11MetricIndex: number = + metricNameMap.Cell11; + const ratioCell11MetricIndex: number = + metricNameMap.RatioCell11; + const totalMatricValues: number[] = + metricValues.map((x: number[]) => x[totalMetricIndex]); + const cell11MatricValues: number[] = + metricValues.map((x: number[]) => x[cell11MetricIndex]); + const ratioCell11MatricValues: number[] = + metricValues.map((x: number[]) => x[ratioCell11MetricIndex]); + Utility.debuggingLog( + `cell11MatricValues=${cell11MatricValues}`); + Utility.debuggingLog( + `ratioCell11MatricValues=${ratioCell11MatricValues}`); + Utility.debuggingLog( + `totalMatricValues=${totalMatricValues}`); + } + const reportResultPerLabel: { + "targetLabel": string, + "scoreThresholds": number[], + "binaryConfusionMatrices": BinaryConfusionMatrix[], + "metricNames": string[], + "metricNameMap": IDictionaryStringIdGenericValue, + "metricValues": number[][], + } = { targetLabel, scoreThresholds: targetLabelIndexScores, binaryConfusionMatrices, metricNames, + metricNameMap, metricValues, - }); + }; + reportResultsPerBatch.push(reportResultPerLabel); } - report.push(reportPerBatch); + reportResults.push(reportResultsPerBatch); } - return report; + return reportResults; } public validate(): void { @@ -395,7 +478,7 @@ export class ThresholdReporter extends AbstractBaseModelFeaturizerEvaluator { predictedLabelColumnIndex, revisedTextColumnIndex, lineIndexToStart); - const labelMap: { [id: string]: number; } = + const labelMap: { [id: string]: number } = this.getLabelMap(); const numberInstances: number = scoreDataStructure.labels.length; for (let i = 0; i < numberInstances; i++) { diff --git a/packages/dispatcher/src/model/evaluation/test/AppTester.ts b/packages/dispatcher/src/model/evaluation/test/AppTester.ts index a778771bd..6bcc95178 100644 --- a/packages/dispatcher/src/model/evaluation/test/AppTester.ts +++ b/packages/dispatcher/src/model/evaluation/test/AppTester.ts @@ -116,9 +116,9 @@ export async function mainTester(): Promise { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); @@ -180,7 +180,7 @@ export async function mainTester(): Promise { const featurizer: NgramSubwordFeaturizer = tester.getFeaturizer(); // Utility.debuggingLog( - // `featurizer.getLabelMap()=${Utility.JSONstringify(featurizer.getLabelMap())}`); + // `featurizer.getLabelMap()=${Utility.jsonStringify(featurizer.getLabelMap())}`); // ----------------------------------------------------------------------- let intentsUtterancesWeights: { "intents": string[], diff --git a/packages/dispatcher/src/model/evaluation/test/Tester.ts b/packages/dispatcher/src/model/evaluation/test/Tester.ts index b923ab57b..1890102af 100644 --- a/packages/dispatcher/src/model/evaluation/test/Tester.ts +++ b/packages/dispatcher/src/model/evaluation/test/Tester.ts @@ -7,10 +7,11 @@ // import { AppSoftmaxRegressionSparse } from "../../supervised/classifier/neural_network/learner/AppSoftmaxRegressionSparse"; import { SoftmaxRegressionSparse } from "../../supervised/classifier/neural_network/learner/SoftmaxRegressionSparse"; +import { IMathematicsHelper } from "../../../mathematics/mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../../../mathematics/mathematics_helper/MathematicsHelper"; +import { IConfusionMatrix } from "../../../mathematics/confusion_matrix/IConfusionMatrix"; import { ConfusionMatrix } from "../../../mathematics/confusion_matrix/ConfusionMatrix"; - import { BinaryConfusionMatrix } from "../../../mathematics/confusion_matrix/BinaryConfusionMatrix"; import { ThresholdReporter } from "../report/ThresholdReporter"; @@ -30,6 +31,9 @@ import { Utility } from "../../../utility/Utility"; export class Tester extends AbstractBaseModelFeaturizerEvaluator { + public static readonly MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + protected intentsCachedAfterTest: string[] = []; protected utterancesCachedAfterTest: string[] = []; protected labelIndexArrayCachedAfterTest: number[] = []; @@ -144,22 +148,24 @@ export class Tester extends AbstractBaseModelFeaturizerEvaluator { const confusionMatrixTest: ConfusionMatrix = this.testResultCachedAfterTest.confusionMatrixTest; const confusionMatrixMetricStructure: { - "confusionMatrix": ConfusionMatrix, - "labelBinaryConfusionMatrixDerivedMetricMap": { [id: string]: { [id: string]: number }; }, - "labelBinaryConfusionMatrixMetricMap": { [id: string]: BinaryConfusionMatrix; }, - "macroAverageMetrics": { "averagePrecision": number, - "averageRecall": number, - "averageF1Score": number, - "support": number }, - "microAverageMetrics": { "accuracy": number, - "truePositives": number, - "support": number }, - "weightedMacroAverageMetrics": { "weightedAveragePrecision": number, - "weightedAverageRecall": number, - "weightedAverageF1Score": number, - "support": number } } = - ConfusionMatrix.generateConfusionMatrixMetricStructure( - confusionMatrixTest); + "confusionMatrix": IConfusionMatrix, + "labelBinaryConfusionMatrixBasicMetricMap": { [id: string]: { [id: string]: number } }, + "labelBinaryConfusionMatrixMap": { [id: string]: BinaryConfusionMatrix }, + "macroAverageMetrics": { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number }, + "microAverageMetrics": { + "accuracy": number, + "truePositives": number, + "support": number }, + "weightedMacroAverageMetrics": { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } } = + confusionMatrixTest.generateConfusionMatrixMetricStructure(); Utility.debuggingLog( `confusionMatrixTest.getMicroAverageMetrics()=` + `${confusionMatrixTest.getMicroAverageMetrics()}` + @@ -233,7 +239,7 @@ export class Tester extends AbstractBaseModelFeaturizerEvaluator { this.getFeaturizer(); // ------------------------------------------------------------------- const labels: string[] = this.getLabels(); - const labelMap: { [id: string]: number; } = this.getLabelMap(); + const labelMap: { [id: string]: number } = this.getLabelMap(); // const numberLabels: number = featurizer.getNumberLabels(); // const numberFeatures: number = featurizer.getNumberFeatures(); // ------------------------------------------------------------------- @@ -301,7 +307,7 @@ export class Tester extends AbstractBaseModelFeaturizerEvaluator { groundTruthLabelIndexes[index] = labelIndex; // --------------------------------------------------------------- const argMax: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntry(prediction); + Tester.MathematicsHelperObject.getIndexOnFirstMaxEntry(prediction); const predictionLabelIndex: number = argMax.indexMax; predictionLabelIndexes[index] = diff --git a/packages/dispatcher/src/model/language_understanding/featurizer/AppNgramSubwordFeaturizer.ts b/packages/dispatcher/src/model/language_understanding/featurizer/AppNgramSubwordFeaturizer.ts index 3b0f966fd..15096d187 100644 --- a/packages/dispatcher/src/model/language_understanding/featurizer/AppNgramSubwordFeaturizer.ts +++ b/packages/dispatcher/src/model/language_understanding/featurizer/AppNgramSubwordFeaturizer.ts @@ -17,7 +17,7 @@ export function exampleFunctionNgramSubwordFeaturizer(): void { Utility.debuggingLog(result); Utility.debuggingLog("hashing code = " + Utility.getPositiveStringHashCode(input)); assert.throws(() => { - Utility.debuggingLog("hashing index = " + featurizer.getHashingFeatureIndex(input)); }, + Utility.debuggingLog(`hashing index = ${featurizer.getHashingFeatureIndex(input)}`); }, `featurizer.getNumberHashingFeatures()=${featurizer.getNumberHashingFeatures()}`); } diff --git a/packages/dispatcher/src/model/language_understanding/featurizer/ISparseTextFeaturizer.ts b/packages/dispatcher/src/model/language_understanding/featurizer/ISparseTextFeaturizer.ts new file mode 100644 index 000000000..955988d2d --- /dev/null +++ b/packages/dispatcher/src/model/language_understanding/featurizer/ISparseTextFeaturizer.ts @@ -0,0 +1,34 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ +import { ITextFeaturizer } from "./ITextFeaturizer"; + +export interface ISparseTextFeaturizer extends ITextFeaturizer { + + createFeatureSparseIndexArray(input: string): number[]; + + createFeatureSparseIndexArrays(inputs: string[]): number[][]; + createIntentUtteranceSparseIndexArrays( + intentsUtterancesWeights: { "intents": string[], "utterances": string[], "weights": number[] }): + { "intentLabelIndexArray": number[], "utteranceFeatureIndexArrays": number[][] }; + + createFeatureMiniBatchingSparseIndexArrays( + inputs: string[], + miniBatchIndexBegin: number, + miniBatchIndexEnd: number): number[][]; + createIntentUtteranceMiniBatchingSparseIndexArrays( + intentsUtterancesWeights: { "intents": string[], "utterances": string[], "weights": number[] }, + miniBatchIndexBegin: number, + miniBatchIndexEnd: number): + { "intentLabelIndexArray": number[], "utteranceFeatureIndexArrays": number[][] }; + + createFeatureHashingSparseIndexArray( + input: string): number[]; + + createFeatureHashingSparseIndexArrays( + inputs: string[]): number[][]; + createIntentUtteranceHashingSparseIndexArrays( + intentsUtterancesWeights: { "intents": string[], "utterances": string[], "weights": number[] }): + { "intentLabelIndexArray": number[], "utteranceFeatureIndexArrays": number[][] }; +} diff --git a/packages/dispatcher/src/model/language_understanding/featurizer/ITextFeaturizer.ts b/packages/dispatcher/src/model/language_understanding/featurizer/ITextFeaturizer.ts new file mode 100644 index 000000000..3294df516 --- /dev/null +++ b/packages/dispatcher/src/model/language_understanding/featurizer/ITextFeaturizer.ts @@ -0,0 +1,28 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +export interface ITextFeaturizer { + + getIntentsUtterancesWeights(): { "intents": string[], "utterances": string[], "weights": number[] }; + + getLabels(): string[]; + getLabelMap(): { [id: string]: number }; + getFeatures(): string[]; + getFeatureMap(): { [id: string]: number }; + getHashingFeatureArrays(): Array>; + + getNumberHashingFeaturesSetting(): number; + + getNumberLabels(): number; + getNumberFeatures(): number; + getNumberHashingFeatures(): number; + + getHashingFeatureIndex(feature: string): number; + + getLabelIndex(label: string, throwIfNonExistentLabel: boolean): number; + getFeatureIndex(feature: string, throwIfNonExistentLabel: boolean): number; + + featurize(input: string): string[]; +} diff --git a/packages/dispatcher/src/model/language_understanding/featurizer/NgramSubwordFeaturizer.ts b/packages/dispatcher/src/model/language_understanding/featurizer/NgramSubwordFeaturizer.ts index d9755f6e6..d7422a93d 100644 --- a/packages/dispatcher/src/model/language_understanding/featurizer/NgramSubwordFeaturizer.ts +++ b/packages/dispatcher/src/model/language_understanding/featurizer/NgramSubwordFeaturizer.ts @@ -3,11 +3,14 @@ * Licensed under the MIT License. */ +import { ITextFeaturizer } from "./ITextFeaturizer"; +import { ISparseTextFeaturizer } from "./ISparseTextFeaturizer"; + import { DictionaryMapUtility } from "../../../data_structure/DictionaryMapUtility"; import { Utility } from "../../../utility/Utility"; -export class NgramSubwordFeaturizer { +export class NgramSubwordFeaturizer implements ISparseTextFeaturizer { protected numberHashingFeaturesSetting: number = 0; @@ -25,9 +28,9 @@ export class NgramSubwordFeaturizer { weights: [] }; protected labels: string[] = []; - protected labelMap: { [id: string]: number; } = {}; + protected labelMap: { [id: string]: number } = {}; protected features: string[] = []; - protected featureMap: { [id: string]: number; } = {}; + protected featureMap: { [id: string]: number } = {}; protected hashingFeatureArrays: Array> = []; constructor( @@ -54,13 +57,13 @@ export class NgramSubwordFeaturizer { public getLabels(): string[] { return this.labels; } - public getLabelMap(): { [id: string]: number; } { + public getLabelMap(): { [id: string]: number } { return this.labelMap; } public getFeatures(): string[] { return this.features; } - public getFeatureMap(): { [id: string]: number; } { + public getFeatureMap(): { [id: string]: number } { return this.featureMap; } public getHashingFeatureArrays(): Array> { @@ -105,7 +108,7 @@ export class NgramSubwordFeaturizer { } if (labelId < 0) { if (throwIfNonExistentLabel) { - Utility.debuggingThrow(`label=${label} does not exist in this.labelMap=${Utility.JSONstringify(this.labelMap)}`); + Utility.debuggingThrow(`label=${label} does not exist in this.labelMap=${Utility.jsonStringify(this.labelMap)}`); } } return labelId; @@ -267,7 +270,7 @@ export class NgramSubwordFeaturizer { labelArray[labelId] = true; } else { if (throwIfNonExistentLabel) { - Utility.debuggingThrow(`label=${label} does not exist in this.labelMap=${Utility.JSONstringify(this.labelMap)}`); + Utility.debuggingThrow(`label=${label} does not exist in this.labelMap=${Utility.jsonStringify(this.labelMap)}`); } } return labelArray; @@ -291,7 +294,7 @@ export class NgramSubwordFeaturizer { labelArray[labelId] = 1; } else { if (throwIfNonExistentLabel) { - Utility.debuggingThrow(`label=${label} does not exist in this.labelMap=${Utility.JSONstringify(this.labelMap)}`); + Utility.debuggingThrow(`label=${label} does not exist in this.labelMap=${Utility.jsonStringify(this.labelMap)}`); } } return labelArray; @@ -573,7 +576,7 @@ export class NgramSubwordFeaturizer { // ------------------------------------------------------------------- const intents: string[] = intentsUtterancesWeights.intents; - const intentLabels: { "stringArray": string[], "stringMap": { [id: string]: number; } } = + const intentLabels: { "stringArray": string[], "stringMap": { [id: string]: number } } = DictionaryMapUtility.buildStringIdNumberValueDictionaryFromStringArray(intents); this.labels = intentLabels.stringArray; @@ -596,12 +599,12 @@ export class NgramSubwordFeaturizer { // ---- NOTE-FOR-REFERENCE ---- NOTE ---- RangeError: Maximum call stack size exceeded! // ---- NOTE-FOR-REFERENCE ---- const utteranceTexts: { // ---- NOTE-FOR-REFERENCE ---- "stringArray": string[], - // ---- NOTE-FOR-REFERENCE ---- "stringMap": { [id: string]: number; } } = + // ---- NOTE-FOR-REFERENCE ---- "stringMap": { [id: string]: number } } = // tslint:disable-next-line: max-line-length // ---- NOTE-FOR-REFERENCE ---- DictionaryMapUtility.buildStringIdNumberValueDictionaryFromStringArray(featureArrayFlattened); const utteranceTexts: { "stringArray": string[], - "stringMap": { [id: string]: number; } } = + "stringMap": { [id: string]: number } } = DictionaryMapUtility.buildStringIdNumberValueDictionaryFromStringArrays(featureArray); this.features = utteranceTexts.stringArray; diff --git a/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AppAutoActiveLearner.ts b/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AppAutoActiveLearner.ts index b78158a71..e47caef16 100644 --- a/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AppAutoActiveLearner.ts +++ b/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AppAutoActiveLearner.ts @@ -953,9 +953,9 @@ export class AppAutoActiveLearner { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AutoActiveLearner.ts b/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AutoActiveLearner.ts index 7241f6c98..80c78279b 100644 --- a/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AutoActiveLearner.ts +++ b/packages/dispatcher/src/model/supervised/classifier/auto_active_learning/AutoActiveLearner.ts @@ -6,6 +6,7 @@ import { AppSoftmaxRegressionSparse } from "../neural_network/learner/AppSoftmaxRegressionSparse"; import { SoftmaxRegressionSparse } from "../neural_network/learner/SoftmaxRegressionSparse"; +import { IMathematicsHelper } from "../../../../mathematics/mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../../../../mathematics/mathematics_helper/MathematicsHelper"; import { ConfusionMatrix } from "../../../../mathematics/confusion_matrix/ConfusionMatrix"; @@ -14,6 +15,9 @@ import { Utility } from "../../../../utility/Utility"; export class AutoActiveLearner { + public static readonly MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + public static defaultDoAutoActiveLearning: boolean = false; public static defaultAalLimitInitialNumberOfInstancesPerCategory: number = 10; public static defaultAalNumberOfInstancesPerIteration: number = 100; @@ -116,7 +120,7 @@ export class AutoActiveLearner { public learn( labels: string[], - labelMap: { [id: string]: number; }, + labelMap: { [id: string]: number }, numberLabels: number, numberFeatures: number, labelIndexArray: number[], @@ -212,7 +216,7 @@ export class AutoActiveLearner { // `, prediction=` + // `${prediction}`); const argMax: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntry(prediction); + AutoActiveLearner.MathematicsHelperObject.getIndexOnFirstMaxEntry(prediction); const predictionLabelId: number = argMax.indexMax; confusionMatrixCurrentIteration.addInstanceByLabelIndex( diff --git a/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/AppSoftmaxRegressionSparse.ts b/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/AppSoftmaxRegressionSparse.ts index fd747c24d..59951acf3 100644 --- a/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/AppSoftmaxRegressionSparse.ts +++ b/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/AppSoftmaxRegressionSparse.ts @@ -55,15 +55,18 @@ export class AppSoftmaxRegressionSparse { textColumnIndex, weightColumnIndex, lineIndexToStart); - const intentsUtterancesWeights: { "intents": string[], "utterances": string[], "weights": number[] } = + const intentsUtterancesWeights: { + "intents": string[], + "utterances": string[], + "weights": number[] } = featurizer.getIntentsUtterancesWeights(); const labels: string[] = featurizer.getLabels(); - const labelMap: { [id: string]: number; } = + const labelMap: { [id: string]: number } = featurizer.getLabelMap(); const features: string[] = featurizer.getFeatures(); - const featureMap: { [id: string]: number; } = + const featureMap: { [id: string]: number } = featurizer.getFeatureMap(); // ------------------------------------------------------------------- const numberFeatures: number = featurizer.getNumberFeatures(); // ==== featurizer.getNumberHashingFeatures(); @@ -85,7 +88,9 @@ export class AppSoftmaxRegressionSparse { // --------------------------------------------------------------- { // ----------------------------------------------------------- - const intentUtteranceSparseIndexArrays = + const intentUtteranceSparseIndexArrays: { + "intentLabelIndexArray": number[], + "utteranceFeatureIndexArrays": number[][] } = featurizer.createIntentUtteranceSparseIndexArrays( intentsUtterancesWeights); const intentLabelIndexArray: number[] = diff --git a/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/SoftmaxRegressionSparse.ts b/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/SoftmaxRegressionSparse.ts index 3039f8b2e..634ff1ecd 100644 --- a/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/SoftmaxRegressionSparse.ts +++ b/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/SoftmaxRegressionSparse.ts @@ -3,12 +3,16 @@ * Licensed under the MIT License. */ +import { IMathematicsHelper } from "../../../../../mathematics/mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../../../../../mathematics/mathematics_helper/MathematicsHelper"; import { Utility } from "../../../../../utility/Utility"; export class SoftmaxRegressionSparse { + public static readonly MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + protected modelWeights: number[][] = []; protected modelBiases: number[] = []; @@ -27,10 +31,10 @@ export class SoftmaxRegressionSparse { this.modelWeights = new Array(numberOutputUnits); this.modelBiases = - MathematicsHelper.vectorNewWithConstantElements(numberOutputUnits, 0); + SoftmaxRegressionSparse.MathematicsHelperObject.vectorNewWithConstantElements(numberOutputUnits, 0); for (let i = 0; i < numberOutputUnits; i++) { this.modelWeights[i] = - MathematicsHelper.vectorNewWithConstantElements(numberInputUnits, 0); + SoftmaxRegressionSparse.MathematicsHelperObject.vectorNewWithConstantElements(numberInputUnits, 0); } } this.l1Regularization = l1Regularization; @@ -83,16 +87,19 @@ export class SoftmaxRegressionSparse { const logLossVectorAfterEpoch: number[] = new Array(); const logLossVectorInGradientUpdate: number[] = new Array(); for (let epoch: number = 0; epoch < epochs; epoch++) { - const softmaxVectors: number[][] = MathematicsHelper.softmaxLogLossGradientUpdate( - groundTruthPositiveLabelIndexes, - featureVectorSparseIndexArrays, - this.modelWeights, - this.modelBiases, - learningRate, - this.l1Regularization, - this.l2Regularization); + const softmaxVectors: number[][] = + SoftmaxRegressionSparse.MathematicsHelperObject.softmaxLogLossGradientUpdate( + groundTruthPositiveLabelIndexes, + featureVectorSparseIndexArrays, + this.modelWeights, + this.modelBiases, + learningRate, + this.l1Regularization, + this.l2Regularization, + 0, + 0); if (toCalculateOverallLossInGradientUpdate) { - const logLossInGradientUpdate: number = MathematicsHelper.softmaxLogLoss( + const logLossInGradientUpdate: number = SoftmaxRegressionSparse.MathematicsHelperObject.softmaxLogLoss( softmaxVectors, groundTruthPositiveLabelIndexes); Utility.debuggingLog( @@ -104,11 +111,13 @@ export class SoftmaxRegressionSparse { if (toCalculateOverallLossAfterEpoch) { // ----------------------------------------------------------- const softmaxVectorsAfterGradientUpdate: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( - featureVectorSparseIndexArrays, - this.modelWeights, - this.modelBiases); - const logLossAfterEpoch: number = MathematicsHelper.softmaxLogLoss( + SoftmaxRegressionSparse.MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( + featureVectorSparseIndexArrays, + this.modelWeights, + this.modelBiases, + 0, + 0); + const logLossAfterEpoch: number = SoftmaxRegressionSparse.MathematicsHelperObject.softmaxLogLoss( softmaxVectorsAfterGradientUpdate, groundTruthPositiveLabelIndexes); Utility.debuggingLog( @@ -197,20 +206,22 @@ export class SoftmaxRegressionSparse { // `, miniBatchIndexEnd=${miniBatchIndexEnd}` + // `, miniBatchSize=${miniBatchSize}` + // `, numberInstances=${numberInstances}`); - const softmaxVectors: number[][] = MathematicsHelper.softmaxLogLossGradientUpdate( - groundTruthPositiveLabelIndexes, - featureVectorSparseIndexArrays, - this.modelWeights, - this.modelBiases, - learningRate, - this.l1Regularization, - this.l2Regularization, - miniBatchIndexBegin, - miniBatchIndexEnd); + const softmaxVectors: number[][] = + SoftmaxRegressionSparse.MathematicsHelperObject.softmaxLogLossGradientUpdate( + groundTruthPositiveLabelIndexes, + featureVectorSparseIndexArrays, + this.modelWeights, + this.modelBiases, + learningRate, + this.l1Regularization, + this.l2Regularization, + miniBatchIndexBegin, + miniBatchIndexEnd); if (toCalculateOverallLossInGradientUpdate) { - const logLossInGradientUpdate: number = MathematicsHelper.softmaxLogLoss( - softmaxVectors, - groundTruthPositiveLabelIndexes); + const logLossInGradientUpdate: number = + SoftmaxRegressionSparse.MathematicsHelperObject.softmaxLogLoss( + softmaxVectors, + groundTruthPositiveLabelIndexes); // Utility.debuggingLog( // `B-epoch=${epoch}/${epochs}` + // `, miniBatchIndexBegin=${miniBatchIndexBegin}` + @@ -222,13 +233,16 @@ export class SoftmaxRegressionSparse { } if (toCalculateOverallLossAfterGradientUpdate) { const softmaxVectorsAfterGradientUpdate: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + SoftmaxRegressionSparse.MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, this.modelWeights, - this.modelBiases); - const logLossAfterGradientUpdate: number = MathematicsHelper.softmaxLogLoss( - softmaxVectorsAfterGradientUpdate, - groundTruthPositiveLabelIndexes); + this.modelBiases, + 0, + 0); + const logLossAfterGradientUpdate: number = + SoftmaxRegressionSparse.MathematicsHelperObject.softmaxLogLoss( + softmaxVectorsAfterGradientUpdate, + groundTruthPositiveLabelIndexes); // Utility.debuggingLog( // `C-epoch=${epoch}/${epochs}` + // `, miniBatchIndexBegin=${miniBatchIndexBegin}` + @@ -257,11 +271,13 @@ export class SoftmaxRegressionSparse { if (toCalculateOverallLossAfterEpoch) { // ----------------------------------------------------------- const softmaxVectorsAfterGradientUpdate: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + SoftmaxRegressionSparse.MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, this.modelWeights, - this.modelBiases); - const logLossAfterEpoch: number = MathematicsHelper.softmaxLogLoss( + this.modelBiases, + 0, + 0); + const logLossAfterEpoch: number = SoftmaxRegressionSparse.MathematicsHelperObject.softmaxLogLoss( softmaxVectorsAfterGradientUpdate, groundTruthPositiveLabelIndexes); Utility.debuggingLog( @@ -295,10 +311,13 @@ export class SoftmaxRegressionSparse { } public predict(featureVectorSparseIndexArrays: number[][]): number[][] { - const softmaxVectors: number[][] = MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( - featureVectorSparseIndexArrays, - this.modelWeights, - this.modelBiases); + const softmaxVectors: number[][] = + SoftmaxRegressionSparse.MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( + featureVectorSparseIndexArrays, + this.modelWeights, + this.modelBiases, + 0, + 0); return softmaxVectors; } diff --git a/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/UtilityLearner.ts b/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/UtilityLearner.ts index 06ca9b3b4..a387e8955 100644 --- a/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/UtilityLearner.ts +++ b/packages/dispatcher/src/model/supervised/classifier/neural_network/learner/UtilityLearner.ts @@ -7,12 +7,16 @@ import { SoftmaxRegressionSparse } from "./SoftmaxRegressionSparse"; import { NgramSubwordFeaturizer } from "../../../../language_understanding/featurizer/NgramSubwordFeaturizer"; +import { IMathematicsHelper } from "../../../../../mathematics/mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../../../../../mathematics/mathematics_helper/MathematicsHelper"; import { Utility } from "../../../../../utility/Utility"; export class LearnerUtility { + public static readonly MathematicsHelperObject: IMathematicsHelper = + MathematicsHelper.GetMathematicsHelperObject(); + public static exampleFunctionPredictAndEvaluateTestDataset( featurizer: NgramSubwordFeaturizer, model: SoftmaxRegressionSparse, @@ -57,7 +61,7 @@ export class LearnerUtility { const predictionsDataArray: number[][] = predictions; const predictionLabelIndexMax: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntry(predictionsDataArray[0]); + LearnerUtility.MathematicsHelperObject.getIndexOnFirstMaxEntry(predictionsDataArray[0]); const predictionLabelIndex: number = predictionLabelIndexMax.indexMax; const predictionLabel: string = @@ -126,7 +130,7 @@ export class LearnerUtility { const predictionsDataArray: number[][] = predictions; const predictionLabelIndexMax: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntry(predictionsDataArray[0]); + LearnerUtility.MathematicsHelperObject.getIndexOnFirstMaxEntry(predictionsDataArray[0]); const predictionLabelIndex: number = predictionLabelIndexMax.indexMax; const predictionLabel: string = diff --git a/packages/dispatcher/src/utility/AppUtility.ts b/packages/dispatcher/src/utility/AppUtility.ts index a3d188750..4838406dc 100644 --- a/packages/dispatcher/src/utility/AppUtility.ts +++ b/packages/dispatcher/src/utility/AppUtility.ts @@ -23,7 +23,7 @@ export function exampleFunctionUtilityWithFilename( Utility.debuggingLog(`linesToSkip=${linesToSkip}`); // ----------------------------------------------------------------------- const labels: string[] = [ "label0", "label1", "label2" ]; - const labelMap: { [id: string]: number; } = {}; + const labelMap: { [id: string]: number } = {}; labelMap.label0 = 0; labelMap.label1 = 1; labelMap.label2 = 2; @@ -110,9 +110,9 @@ export function exampleFunctionUtility(): void { const args: any = parsedKnownArgs[0]; const unknownArgs: any = parsedKnownArgs[1]; Utility.debuggingLog( - `args=${Utility.JSONstringify(args)}`); + `args=${Utility.jsonStringify(args)}`); Utility.debuggingLog( - `unknownArgs=${Utility.JSONstringify(unknownArgs)}`); + `unknownArgs=${Utility.jsonStringify(unknownArgs)}`); const debugFlag: boolean = Utility.toBoolean(args.debug); Utility.toPrintDebuggingLogToConsole = debugFlag; // ---- NOTE-FOR-DEBUGGING ---- console.dir(args); diff --git a/packages/dispatcher/src/utility/utility.ts b/packages/dispatcher/src/utility/utility.ts index 384b8a669..c18564185 100644 --- a/packages/dispatcher/src/utility/utility.ts +++ b/packages/dispatcher/src/utility/utility.ts @@ -309,20 +309,20 @@ export class Utility { } public static mapToJsonSerialization(map: Map): string { - return Utility.JSONstringify([...map]); + return Utility.jsonStringify([...map]); } public static jsonSerializationToMap(jsonString: string): Map { const jsonParsedObject: any = JSON.parse(jsonString); return new Map(jsonParsedObject); } public static setToJsonSerialization(set: Set): string { - return Utility.JSONstringify([...set]); + return Utility.jsonStringify([...set]); } public static jsonSerializationToSet(jsonString: string): Set { return new Set(JSON.parse(jsonString)); } public static arrayToJsonSerialization(set: T[]): string { - return Utility.JSONstringify([...set]); + return Utility.jsonStringify([...set]); } public static jsonSerializationToArray(jsonString: string): T[] { return new Array(JSON.parse(jsonString)); @@ -388,13 +388,13 @@ export class Utility { } public static stringMapSetToJson(stringMapSet: Map>): string { - return Utility.JSONstringify(Utility.stringMapSetToObject(stringMapSet)); + return Utility.jsonStringify(Utility.stringMapSetToObject(stringMapSet)); } public static jsonToStringMapSet(jsonString: string): Map> { return Utility.objectToStringMapSet(JSON.parse(jsonString)); } public static stringMapArrayToJson(stringMapArray: Map): string { - return Utility.JSONstringify(Utility.stringMapArrayToObject(stringMapArray)); + return Utility.jsonStringify(Utility.stringMapArrayToObject(stringMapArray)); } public static jsonToStringMapArray(jsonString: string): Map { return Utility.objectToStringMapArray(JSON.parse(jsonString)); @@ -427,6 +427,7 @@ export class Utility { let s1: number = Utility.xorshift128plusState0; const s0: number = Utility.xorshift128plusState1; Utility.xorshift128plusState0 = s0; + Utility.xorshift128plusState0 %= Number.MAX_SAFE_INTEGER; // tslint:disable-next-line: no-bitwise s1 ^= s1 << 23; // tslint:disable-next-line: no-bitwise @@ -436,7 +437,10 @@ export class Utility { // tslint:disable-next-line: no-bitwise s1 ^= s0 >> 26; Utility.xorshift128plusState1 = s1; - return Utility.xorshift128plusState0 + Utility.xorshift128plusState1; + Utility.xorshift128plusState1 %= Number.MAX_SAFE_INTEGER; + let result: number = Utility.xorshift128plusState0 + Utility.xorshift128plusState1; + result %= Number.MAX_SAFE_INTEGER; + return result; } public static rngNextXorshift128plus( inputRngBurninIterations: number = -1): number { @@ -451,20 +455,67 @@ export class Utility { } return Utility.rngNextXorshift128plusDirect(); } + public static rngNextXorshift128plusWithNumberStates(): number { + if (!Utility.rngBurninDone) { + for (let i: number = 0; i < Utility.rngBurninIterations; i++) { + Utility.rngNextXorshift128plusDirect(); + } + Utility.rngBurninDone = true; + } + return Utility.rngNextXorshift128plusDirect(); + } + public static rngNextXorshift128plusFloatWithNumberStates(): number { + const result: number = Utility.rngNextXorshift128plus(); + return (result / Number.MAX_SAFE_INTEGER); + } + public static seedRandomNumberGeneratorWithNumberStates( + newState0: number, + newState1: number, + inputRngBurninIterationsForBigInt: number = -1): void { + Utility.rngSeedXorshift128plus( + newState0, + newState1, + inputRngBurninIterationsForBigInt); + } + public static getRandomNumberWithNumberStates(): number { + return Utility.rngNextXorshift128plusFloatWithNumberStates(); + } + + public static getRandomIntWithNumberStates(limit: number): number { + return Utility.getRandomIntFromIntLimitWithNumberStates(limit); + } + public static getRandomIntFromFloatLimitWithNumberStates(limit: number): number { + let randomFloat: number = + Utility.getRandomNumberWithNumberStates() * limit; + if (randomFloat >= limit) { + randomFloat = limit - Utility.epsilon; + } + const randomInt: number = Math.floor( + randomFloat); + return randomInt; + } + public static getRandomIntFromIntLimitWithNumberStates(limit: number): number { + let randomInt: number = Math.floor( + Utility.getRandomNumberWithNumberStates() * Math.floor(limit)); + if (randomInt >= limit) { + randomInt = limit - 1; + } + return randomInt; + } - public static getXorshift128plusState0BigInt(): bigint { + public static getXorshift128plusState0WithBigIntStates(): bigint { return Utility.xorshift128plusState0BigInt; } - public static getXorshift128plusState1BigInt(): bigint { + public static getXorshift128plusState1WithBigIntStates(): bigint { return Utility.xorshift128plusState1BigInt; } - public static getXorshift128plusCycleBigInt(): bigint { + public static getXorshift128plusCycleWithBigIntStates(): bigint { return Utility.xorshift128plusCycleBigInt; } - public static getXorshift128plusCycleBigIntFloat(): number { - return Utility.xorshift128plusCycleBigIntFloat; + public static getXorshift128plusCycleFloatWithBigIntStates(): number { + return Utility.xorshift128plusCycleFloatBigInt; } - public static rngSeedXorshift128plusBigIntWithNumber( + public static rngSeedXorshift128plusWithBigIntStatesWithNumberArguments( newState0: number, newState1: number, inputRngBurninIterationsForBigInt: number = -1): void { @@ -474,11 +525,11 @@ export class Utility { Utility.xorshift128plusState0BigInt = BigInt(newState0); Utility.xorshift128plusState1BigInt = BigInt(newState1); for (let i: number = 0; i < inputRngBurninIterationsForBigInt; i++) { - Utility.rngNextXorshift128plusBigIntDirect(); + Utility.rngNextXorshift128plusWithBigIntStatesDirect(); } Utility.rngBurninDoneForBigInt = true; } - public static rngSeedXorshift128plusBigInt( + public static rngSeedXorshift128plusWithBigIntStates( newState0BigInt: bigint, newState1BigInt: bigint, newCycleBigInt: bigint, @@ -490,11 +541,11 @@ export class Utility { Utility.xorshift128plusState1BigInt = newState1BigInt; Utility.xorshift128plusCycleBigInt = newCycleBigInt; for (let i: number = 0; i < inputRngBurninIterationsForBigInt; i++) { - Utility.rngNextXorshift128plusBigIntDirect(); + Utility.rngNextXorshift128plusWithBigIntStatesDirect(); } Utility.rngBurninDoneForBigInt = true; } - public static rngNextXorshift128plusBigIntDirect(): bigint { + public static rngNextXorshift128plusWithBigIntStatesDirect(): bigint { let s1: bigint = Utility.xorshift128plusState0BigInt; const s0: bigint = @@ -523,31 +574,31 @@ export class Utility { result %= Utility.xorshift128plusCycleBigInt; return result; } - public static rngNextXorshift128plusBigInt(): bigint { + public static rngNextXorshift128plusWithBigIntStates(): bigint { if (!Utility.rngBurninDoneForBigInt) { for (let i: number = 0; i < Utility.rngBurninIterationsForBigInt; i++) { - Utility.rngNextXorshift128plusBigIntDirect(); + Utility.rngNextXorshift128plusWithBigIntStatesDirect(); } Utility.rngBurninDoneForBigInt = true; } - return Utility.rngNextXorshift128plusBigIntDirect(); + return Utility.rngNextXorshift128plusWithBigIntStatesDirect(); } - public static rngNextXorshift128plusBigIntFloat(): number { - const resultBigInt: bigint = Utility.rngNextXorshift128plusBigInt(); + public static rngNextXorshift128plusFloatWithBigIntStates(): number { + const resultBigInt: bigint = Utility.rngNextXorshift128plusWithBigIntStates(); const resultNumber: number = Number(resultBigInt); - return (resultNumber / Utility.xorshift128plusCycleBigIntFloat); + return (resultNumber / Utility.xorshift128plusCycleFloatBigInt); } public static seedRandomNumberGenerator( newState0: number, newState1: number, inputRngBurninIterationsForBigInt: number = -1): void { - Utility.rngSeedXorshift128plusBigIntWithNumber( + Utility.rngSeedXorshift128plusWithBigIntStatesWithNumberArguments( newState0, newState1, inputRngBurninIterationsForBigInt); } public static getRandomNumber(): number { - return Utility.rngNextXorshift128plusBigIntFloat(); + return Utility.rngNextXorshift128plusFloatWithBigIntStates(); } public static getRandomInt(limit: number): number { @@ -1506,7 +1557,7 @@ export class Utility { Utility.debuggingThrow( `lineColumns.length|${lineColumns.length}|!=4` + `,line=$${line}$` + - `,lineColumns=$${Utility.JSONstringify(lineColumns)}$`); + `,lineColumns=$${Utility.jsonStringify(lineColumns)}$`); } const id: string = lineColumns[0]; const word: string = lineColumns[1]; @@ -1636,7 +1687,7 @@ export class Utility { } public static getObjectMd5Hash(objectValue: object): string|Int32Array { - return Utility.getStringMd5Hash(Utility.JSONstringify(objectValue)); + return Utility.getStringMd5Hash(Utility.jsonStringify(objectValue)); } public static getStringMd5Hash(feature: string): string | Int32Array { return md5.Md5.hashStr(feature); @@ -1646,7 +1697,7 @@ export class Utility { return Math.abs(Utility.getObjectHashCode(objectValue)); } public static getObjectHashCode(objectValue: object): number { - return Utility.getStringHashCode(Utility.JSONstringify(objectValue).toString()); + return Utility.getStringHashCode(Utility.jsonStringify(objectValue).toString()); } public static getPositiveStringHashCode(feature: string): number { return Math.abs(Utility.getStringHashCode(feature)); @@ -1732,7 +1783,7 @@ export class Utility { message: any): void { const dateTimeString: string = (new Date()).toISOString(); const logMessage: string = `[${dateTimeString}] ERROR-MESSAGE: ${message}`; - throw new Error(Utility.JSONstringify(logMessage)); + throw new Error(Utility.jsonStringify(logMessage)); } public static almostEqual(first: number, second: number): boolean { @@ -1805,12 +1856,26 @@ export class Utility { } } + public static iterableIteratorToArray(iterator: IterableIterator): K[] { + const anArray: K[] = []; + for (const element of iterator) { + anArray.push(element); + } + return anArray; + } + public static splitByPunctuation( input: string, splitDelimiter: string = " ", - toRemoveEmptyElements: boolean = true): string[] { - const delimiters: string[] = Utility.LanguageTokenPunctuationDelimiters; - const replacementDelimiters: string[] = Utility.LanguageTokenPunctuationReplacementDelimiters; + toRemoveEmptyElements: boolean = true, + delimiters: string[] = Utility.LanguageTokenPunctuationDelimiters, + replacementDelimiters: string[] = Utility.LanguageTokenPunctuationReplacementDelimiters): string[] { + if (Utility.isEmptyStringArray(delimiters)) { + delimiters = Utility.LanguageTokenPunctuationDelimiters; + } + if (Utility.isEmptyStringArray(replacementDelimiters)) { + delimiters = Utility.LanguageTokenPunctuationReplacementDelimiters; + } const numberDelimiters: number = delimiters.length; for (let i = 0; i < numberDelimiters; i++) { input = input.replace(delimiters[i], replacementDelimiters[i]); @@ -1824,14 +1889,6 @@ export class Utility { return result; } - public static iterableIteratorToArray(iterator: IterableIterator): K[] { - const anArray: K[] = []; - for (const element of iterator) { - anArray.push(element); - } - return anArray; - } - public static splitStringWithCommaDelimitersFilteredByDoubleQuotes(input: string): string[] { return Utility.splitStringWithColumnDelimitersFilteredByQuotingDelimiters( input, @@ -1845,6 +1902,7 @@ export class Utility { // ---- NOTE-FOR-REFERENCE ---- splittedStrings.push(input.substring(index, commaIndex)); // ---- NOTE-FOR-REFERENCE ---- index = commaIndex + 1; // ---- NOTE-FOR-REFERENCE ---- } + // ---- NOTE-FOR-REFERENCE ---- splittedStrings.push(input.substring(index)); // ---- NOTE-FOR-REFERENCE ---- return splittedStrings; } public static splitStringWithColumnDelimitersFilteredByQuotingDelimiters( @@ -1994,8 +2052,20 @@ export class Utility { return filenameWithoutExtension; } - public static JSONstringify(input: any): string { - return JSON.stringify(input, null, 4); + public static stringifyArray( + anArray: T[], + delimiter: string = ","): string { + return "[" + anArray.join(delimiter) + "]"; + } + public static jsonStringifyInLine( + value: any): string { + return Utility.jsonStringify(value, null, ""); + } + public static jsonStringify( + value: any, + replacer: Array | null = null, + space: string | number = 4): string { + return JSON.stringify(value, replacer, space); } public static split(input: string, delimiter: string): string[] { @@ -2005,6 +2075,27 @@ export class Utility { return input.split(delimiter); } + public static normalizeArray(array: T[], indexKey: keyof T): { [key: string]: T } { + const normalizedObject: { [key: string]: T } = {}; + // tslint:disable-next-line: prefer-for-of + for (let i = 0; i < array.length; i++) { + const key: any = array[i][indexKey]; + normalizedObject[key] = array[i]; + } + return normalizedObject; + } + + public static sparseArrayPairToMap(indexArray: number[], valueArray: number[]): Map { + const sparseIndexedMap: Map = new Map(); + // tslint:disable-next-line: prefer-for-of + for (let i = 0; i < indexArray.length; i++) { + const key: number = indexArray[i]; + const value: number = valueArray[i]; + sparseIndexedMap.set(key, value); + } + return sparseIndexedMap; + } + protected static rngBurninIterations: number = 16384; protected static rngBurninDone: boolean = false; protected static xorshift128plusState0: number = 1; @@ -2020,7 +2111,7 @@ export class Utility { protected static xorshift128plusState1BigInt: bigint = BigInt(2); protected static xorshift128plusCycleBigInt: bigint = BigInt("0xffffffffffffffff"); // ---- NOTE: 2^64 - 1 === 18446744073709551615 - protected static xorshift128plusCycleBigIntFloat: number = + protected static xorshift128plusCycleFloatBigInt: number = Number(Utility.xorshift128plusCycleBigInt); } diff --git a/packages/dispatcher/test/data/DataUtility.test.ts b/packages/dispatcher/test/data/DataUtility.test.ts index 714731148..f9e2d3b45 100644 --- a/packages/dispatcher/test/data/DataUtility.test.ts +++ b/packages/dispatcher/test/data/DataUtility.test.ts @@ -75,7 +75,7 @@ describe("Test Suite - data/DataUtility", () => { const luQnaJsonStructure: string = luData.getLuQnaJsonStructure(); Utility.debuggingLog( - `luQnaJsonStructure-MicrosoftFaqForWindows=${Utility.JSONstringify(luQnaJsonStructure)}`); + `luQnaJsonStructure-MicrosoftFaqForWindows=${Utility.jsonStringify(luQnaJsonStructure)}`); }); }); it("Test.0102 LoadData", function() { @@ -90,7 +90,7 @@ describe("Test Suite - data/DataUtility", () => { const luQnaJsonStructure: string = luData.getLuQnaJsonStructure(); Utility.debuggingLog( - `luQnaJsonStructure-MicrosoftFaqForOffice=${Utility.JSONstringify(luQnaJsonStructure)}`); + `luQnaJsonStructure-MicrosoftFaqForOffice=${Utility.jsonStringify(luQnaJsonStructure)}`); }); }); }); diff --git a/packages/dispatcher/test/mathematics/confusion_matrix/ConfusionMatrix.test.ts b/packages/dispatcher/test/mathematics/confusion_matrix/ConfusionMatrix.test.ts index dc9b8df5a..85aa7fd45 100644 --- a/packages/dispatcher/test/mathematics/confusion_matrix/ConfusionMatrix.test.ts +++ b/packages/dispatcher/test/mathematics/confusion_matrix/ConfusionMatrix.test.ts @@ -5,8 +5,8 @@ import assert = require("assert"); +import { IConfusionMatrix } from "../../../src/mathematics/confusion_matrix/IConfusionMatrix"; import { ConfusionMatrix } from "../../../src/mathematics/confusion_matrix/ConfusionMatrix"; - import { BinaryConfusionMatrix } from "../../../src/mathematics/confusion_matrix/BinaryConfusionMatrix"; import { DictionaryMapUtility } from "../../../src/data_structure/DictionaryMapUtility"; @@ -17,24 +17,24 @@ import { UnitTestHelper } from "../../utility/Utility.test"; function getTestingConfusionMatrix(): ConfusionMatrix { const labels: string[] = ["label0", "label1", "label2"]; - const labelMap: { [id: string]: number; } = {}; + const labelMap: { [id: string]: number } = {}; labelMap.label0 = 0; labelMap.label1 = 1; labelMap.label2 = 2; const confusionMatrix: ConfusionMatrix = new ConfusionMatrix(labels, labelMap); - confusionMatrix.addInstance("label0", "label0"); - confusionMatrix.addInstance("label0", "label1"); - confusionMatrix.addInstance("label0", "label2"); - confusionMatrix.addInstance("label1", "label0"); - confusionMatrix.addInstance("label1", "label1"); - confusionMatrix.addInstance("label1", "label2"); - confusionMatrix.addInstance("label2", "label0"); - confusionMatrix.addInstance("label2", "label1"); - confusionMatrix.addInstance("label2", "label2"); + confusionMatrix.addInstanceByLabel("label0", "label0"); + confusionMatrix.addInstanceByLabel("label0", "label1"); + confusionMatrix.addInstanceByLabel("label0", "label2"); + confusionMatrix.addInstanceByLabel("label1", "label0"); + confusionMatrix.addInstanceByLabel("label1", "label1"); + confusionMatrix.addInstanceByLabel("label1", "label2"); + confusionMatrix.addInstanceByLabel("label2", "label0"); + confusionMatrix.addInstanceByLabel("label2", "label1"); + confusionMatrix.addInstanceByLabel("label2", "label2"); Utility.debuggingLog( "labels=" + confusionMatrix.getLabels()); Utility.debuggingLog( - Utility.JSONstringify(confusionMatrix.getLabelMap())); + Utility.jsonStringify(confusionMatrix.getLabelMap())); Utility.debuggingLog( "rows=" + confusionMatrix.getConfusionMatrixRows()); Utility.debuggingLog( @@ -103,7 +103,7 @@ describe("Test Suite - model/evaluation/confusion_matrix/confusion_matrix", () = Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const confusionMatrix: ConfusionMatrix = getTestingConfusionMatrix(); - const labelMap: { [id: string]: number; } = + const labelMap: { [id: string]: number } = confusionMatrix.getLabelMap(); assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(labelMap) === 3, `DictionaryMapUtility.getStringIdGenericValueDictionaryLength(labelMap)=` + @@ -171,7 +171,7 @@ describe("Test Suite - model/evaluation/confusion_matrix/confusion_matrix", () = Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const confusionMatrix: ConfusionMatrix = getTestingConfusionMatrix(); - confusionMatrix.addInstance( + confusionMatrix.addInstanceByLabel( "label0", "label0"); const confusionMatrixCells: number[][] = @@ -198,46 +198,70 @@ describe("Test Suite - model/evaluation/confusion_matrix/confusion_matrix", () = Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const confusionMatrix: ConfusionMatrix = getTestingConfusionMatrix(); - const confusionMatrixMetrics: [number, number, number] = + const confusionMatrixMetrics: { + "averagePrecisionRecallF1Accuracy": number, + "truePositives": number, + "total": number } = confusionMatrix.getMicroAverageMetrics(); - assert.ok(Utility.almostEqual(confusionMatrixMetrics[0], 0.3333333333333333), - `confusionMatrixMetrics[0]=${confusionMatrixMetrics[0]}`); - assert.ok(confusionMatrixMetrics[1] === 3, - `confusionMatrixMetrics[1]=${confusionMatrixMetrics[1]}`); - assert.ok(confusionMatrixMetrics[2] === 9, - `confusionMatrixMetrics[2]=${confusionMatrixMetrics[2]}`); + assert.ok(Utility.almostEqual( + confusionMatrixMetrics.averagePrecisionRecallF1Accuracy, 0.3333333333333333), + `confusionMatrixMetrics.averagePrecisionRecallF1Accuracy=${confusionMatrixMetrics.averagePrecisionRecallF1Accuracy}`); + assert.ok( + confusionMatrixMetrics.truePositives === 3, + `confusionMatrixMetrics.truePositives=${confusionMatrixMetrics.truePositives}`); + assert.ok( + confusionMatrixMetrics.total === 9, + `confusionMatrixMetrics.total=${confusionMatrixMetrics.total}`); }); it("Test.0700 getMacroAverageMetrics()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const confusionMatrix: ConfusionMatrix = getTestingConfusionMatrix(); - const confusionMatrixMetrics: [number, number, number, number] = + const confusionMatrixMetrics: { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } = confusionMatrix.getMacroAverageMetrics(); - assert.ok(Utility.almostEqual(confusionMatrixMetrics[0], 0.3333333333333333), - `confusionMatrixMetrics[0]=${confusionMatrixMetrics[0]}`); - assert.ok(Utility.almostEqual(confusionMatrixMetrics[1], 0.3333333333333333), - `confusionMatrixMetrics[1]=${confusionMatrixMetrics[1]}`); - assert.ok(Utility.almostEqual(confusionMatrixMetrics[2], 0.3333333333333333), - `confusionMatrixMetrics[2]=${confusionMatrixMetrics[2]}`); - assert.ok(confusionMatrixMetrics[3] === 9, - `confusionMatrixMetrics[3]=${confusionMatrixMetrics[3]}`); + assert.ok(Utility.almostEqual( + confusionMatrixMetrics.averagePrecision, 0.3333333333333333), + `confusionMatrixMetrics.averagePrecision=${confusionMatrixMetrics.averagePrecision}`); + assert.ok(Utility.almostEqual( + confusionMatrixMetrics.averageRecall, 0.3333333333333333), + `confusionMatrixMetrics.averageRecall=${confusionMatrixMetrics.averageRecall}`); + assert.ok(Utility.almostEqual( + confusionMatrixMetrics.averageF1Score, 0.3333333333333333), + `confusionMatrixMetrics.averageF1Score=${confusionMatrixMetrics.averageF1Score}`); + assert.ok( + confusionMatrixMetrics.total === 9, + `confusionMatrixMetrics.total=${confusionMatrixMetrics.total}`); }); it("Test.0800 getWeightedMacroAverageMetrics()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const confusionMatrix: ConfusionMatrix = getTestingConfusionMatrix(); - const confusionMatrixMetrics: [number, number, number, number] = + const confusionMatrixMetrics: { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "total": number } = confusionMatrix.getWeightedMacroAverageMetrics(); - assert.ok(Utility.almostEqual(confusionMatrixMetrics[0], 0.3333333333333333), - `confusionMatrixMetrics[0]=${confusionMatrixMetrics[0]}`); - assert.ok(Utility.almostEqual(confusionMatrixMetrics[1], 0.3333333333333333), - `confusionMatrixMetrics[1]=${confusionMatrixMetrics[1]}`); - assert.ok(Utility.almostEqual(confusionMatrixMetrics[2], 0.3333333333333333), - `confusionMatrixMetrics[2]=${confusionMatrixMetrics[2]}`); - assert.ok(confusionMatrixMetrics[3] === 9, - `confusionMatrixMetrics[3]=${confusionMatrixMetrics[3]}`); + assert.ok(Utility.almostEqual( + confusionMatrixMetrics.averagePrecision, 0.3333333333333333), + `confusionMatrixMetrics.averagePrecision=${ + confusionMatrixMetrics.averagePrecision}`); + assert.ok(Utility.almostEqual( + confusionMatrixMetrics.averageRecall, 0.3333333333333333), + `confusionMatrixMetrics.averageRecall=${ + confusionMatrixMetrics.averageRecall}`); + assert.ok(Utility.almostEqual( + confusionMatrixMetrics.averageF1Score, 0.3333333333333333), + `confusionMatrixMetrics.averageF1Score=${confusionMatrixMetrics.averageF1Score}`); + assert.ok( + confusionMatrixMetrics.total === 9, + `confusionMatrixMetrics.total=${confusionMatrixMetrics.total}`); }); it("Test.0900 validateLabelId()", function() { @@ -288,29 +312,31 @@ describe("Test Suite - model/evaluation/confusion_matrix/confusion_matrix", () = this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const confusionMatrix: ConfusionMatrix = getTestingConfusionMatrix(); const confusionMatrixMetricStructure: { - "confusionMatrix": ConfusionMatrix, - "labelBinaryConfusionMatrixDerivedMetricMap": { [id: string]: { [id: string]: number }; }, - "labelBinaryConfusionMatrixMetricMap": { [id: string]: BinaryConfusionMatrix; }, - "macroAverageMetrics": { "averagePrecision": number, - "averageRecall": number, - "averageF1Score": number, - "support": number }, - "microAverageMetrics": { "accuracy": number, - "truePositives": number, - "support": number }, - "weightedMacroAverageMetrics": { "weightedAveragePrecision": number, - "weightedAverageRecall": number, - "weightedAverageF1Score": number, - "support": number } } = - ConfusionMatrix.generateConfusionMatrixMetricStructure( - confusionMatrix); + "confusionMatrix": IConfusionMatrix, + "labelBinaryConfusionMatrixBasicMetricMap": { [id: string]: { [id: string]: number } }, + "labelBinaryConfusionMatrixMap": { [id: string]: BinaryConfusionMatrix }, + "macroAverageMetrics": { + "averagePrecision": number, + "averageRecall": number, + "averageF1Score": number, + "support": number }, + "microAverageMetrics": { + "accuracy": number, + "truePositives": number, + "support": number }, + "weightedMacroAverageMetrics": { + "weightedAveragePrecision": number, + "weightedAverageRecall": number, + "weightedAverageF1Score": number, + "support": number } } = + confusionMatrix.generateConfusionMatrixMetricStructure(); const confusionMatrixMetrics: [number, number, number, number] = [ confusionMatrixMetricStructure.macroAverageMetrics.averagePrecision, confusionMatrixMetricStructure.macroAverageMetrics.averageRecall, confusionMatrixMetricStructure.macroAverageMetrics.averageF1Score, confusionMatrixMetricStructure.macroAverageMetrics.support ]; Utility.debuggingLog( - `confusionMatrixMetricStructure=${Utility.JSONstringify(confusionMatrixMetricStructure)}`); + `confusionMatrixMetricStructure=${Utility.jsonStringify(confusionMatrixMetricStructure)}`); assert.ok(Utility.almostEqual(confusionMatrixMetrics[0], 0.3333333333333333), `confusionMatrixMetrics[0]=${confusionMatrixMetrics[0]}`); assert.ok(Utility.almostEqual(confusionMatrixMetrics[1], 0.3333333333333333), diff --git a/packages/dispatcher/test/mathematics/mathematics_helper/MathematicsHelper.test.ts b/packages/dispatcher/test/mathematics/mathematics_helper/MathematicsHelper.test.ts index 172aea51d..e353858c7 100644 --- a/packages/dispatcher/test/mathematics/mathematics_helper/MathematicsHelper.test.ts +++ b/packages/dispatcher/test/mathematics/mathematics_helper/MathematicsHelper.test.ts @@ -5,6 +5,7 @@ import assert = require("assert"); +import { IMathematicsHelper } from "../../../src/mathematics/mathematics_helper/IMathematicsHelper"; import { MathematicsHelper } from "../../../src/mathematics/mathematics_helper/MathematicsHelper"; import { Utility } from "../../../src/utility/Utility"; @@ -13,12 +14,14 @@ import { UnitTestHelper } from "../../utility/Utility.test"; const testArray: number[] = [0.4, 0.5, 0.3]; +const MathematicsHelperObject: IMathematicsHelper = MathematicsHelper.GetMathematicsHelperObject(); + describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/MathematicsHelper", () => { it("Test.0000 softmaxSingleFunction()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const softmax: number = - MathematicsHelper.softmaxSingleFunction(testArray, 2); + MathematicsHelperObject.softmaxSingleFunction(testArray, 2); assert.ok( Utility.almostEqual(softmax, 0.3006096053557273), `softmax=${softmax}`); @@ -27,7 +30,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const smoothArgmax: number = - MathematicsHelper.smoothArgmaxApproximationSingleFunction(testArray, 2); + MathematicsHelperObject.smoothArgmaxApproximationSingleFunction(testArray, 2); assert.ok( Utility.almostEqual(smoothArgmax, 0.3006096053557273), `smoothArgmax=${smoothArgmax}`); @@ -36,7 +39,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const softmaxes: number[] = - MathematicsHelper.softmaxFunction(testArray); + MathematicsHelperObject.softmaxFunction(testArray); assert.ok( Utility.almostEqual(softmaxes[0], 0.3322249935333473), `softmaxes=${softmaxes}`); @@ -51,7 +54,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const smoothArgmaxes: number[] = - MathematicsHelper.smoothArgmaxApproximationFunction(testArray); + MathematicsHelperObject.smoothArgmaxApproximationFunction(testArray); assert.ok( Utility.almostEqual(smoothArgmaxes[0], 0.3322249935333473), `smoothArgmaxes=${smoothArgmaxes}`); @@ -67,7 +70,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const logsumexpStrictConvex: number = - MathematicsHelper.logsumexpStrictConvexSingleFunction(testArray); + MathematicsHelperObject.logsumexpStrictConvexSingleFunction(testArray); assert.ok( Utility.almostEqual(logsumexpStrictConvex, 1.7030019824788412), `logsumexpStrictConvex=${logsumexpStrictConvex}`); @@ -76,7 +79,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const smoothMaxApproximationStrict: number = - MathematicsHelper.smoothMaxApproximationStrictConvexFunction(testArray); + MathematicsHelperObject.smoothMaxApproximationStrictConvexFunction(testArray); assert.ok( Utility.almostEqual(smoothMaxApproximationStrict, 1.7030019824788412), `smoothMaxApproximationStrict=${smoothMaxApproximationStrict}`); @@ -85,7 +88,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const logsumexp: number = - MathematicsHelper.logsumexpSingleFunction(testArray); + MathematicsHelperObject.logsumexpSingleFunction(testArray); assert.ok( Utility.almostEqual(logsumexp, 1.501942848229244), `logsumexp=${logsumexp}`); @@ -94,7 +97,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const smoothMaxApproximation: number = - MathematicsHelper.smoothMaxApproximationFunction(testArray); + MathematicsHelperObject.smoothMaxApproximationFunction(testArray); assert.ok( Utility.almostEqual(smoothMaxApproximation, 1.501942848229244), `smoothMaxApproximation=${smoothMaxApproximation}`); @@ -104,7 +107,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sigmoidLogisticGradient: number = - MathematicsHelper.sigmoidLogisticGradientFunction(testArray[1]); + MathematicsHelperObject.sigmoidLogisticGradientFunction(testArray[1]); assert.ok( Utility.almostEqual(sigmoidLogisticGradient, 0.2350037122015945), `sigmoidLogisticGradient=${sigmoidLogisticGradient}`); @@ -114,7 +117,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sigmoid: number = - MathematicsHelper.sigmoidLogisticFunction(testArray[1]); + MathematicsHelperObject.sigmoidLogisticFunction(testArray[1]); assert.ok( Utility.almostEqual(sigmoid, 0.6224593312018546), `sigmoid=${sigmoid}`); @@ -123,7 +126,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sigmoid: number = - MathematicsHelper.sigmoidHyperbolicTangentFunction(testArray[1]); + MathematicsHelperObject.sigmoidHyperbolicTangentFunction(testArray[1]); assert.ok( Utility.almostEqual(sigmoid, 0.4621171572600098), `sigmoid=${sigmoid}`); @@ -132,7 +135,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sigmoid: number = - MathematicsHelper.sigmoidArctangentFunction(testArray[1]); + MathematicsHelperObject.sigmoidArctangentFunction(testArray[1]); assert.ok( Utility.almostEqual(sigmoid, 0.4636476090008061), `sigmoid=${sigmoid}`); @@ -141,7 +144,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sigmoid: number = - MathematicsHelper.sigmoidGudermannianFunction(testArray[1]); + MathematicsHelperObject.sigmoidGudermannianFunction(testArray[1]); assert.ok( Utility.almostEqual(sigmoid, 0.48038107913372946), `sigmoid=${sigmoid}`); @@ -150,7 +153,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sigmoid: number = - MathematicsHelper.sigmoidGeneralizedLogisticFunction(testArray[1]); + MathematicsHelperObject.sigmoidGeneralizedLogisticFunction(testArray[1], 1); assert.ok( Utility.almostEqual(sigmoid, 0.6224593312018546), `sigmoid=${sigmoid}`); @@ -159,7 +162,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sigmoid: number = - MathematicsHelper.sigmoidAlgebraicFunction(testArray[1]); + MathematicsHelperObject.sigmoidAlgebraicFunction(testArray[1]); assert.ok( Utility.almostEqual(sigmoid, 0.4472135954999579), `sigmoid=${sigmoid}`); @@ -171,7 +174,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const weight: number = 1; const value: number = 0.5; const regularization: number = - MathematicsHelper.getL1Regularized(weight, value); + MathematicsHelperObject.getL1Regularized(weight, value); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -184,7 +187,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const weight: number = -1; const value: number = 0.5; const regularization: number = - MathematicsHelper.getL1Regularized(weight, value); + MathematicsHelperObject.getL1Regularized(weight, value); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -197,7 +200,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const weight: number = 0; const value: number = 0.5; const regularization: number = - MathematicsHelper.getL1Regularized(weight, value); + MathematicsHelperObject.getL1Regularized(weight, value); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -210,7 +213,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const weight: number = 2; const value: number = 0.5; const regularization: number = - MathematicsHelper.getL2Regularized(weight, value); + MathematicsHelperObject.getL2Regularized(weight, value); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -224,7 +227,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueL1: number = 0.5; const valueL2: number = 0.5; const regularization: number = - MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); + MathematicsHelperObject.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -238,7 +241,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueL1: number = 0.5; const valueL2: number = 0.5; const regularization: number = - MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); + MathematicsHelperObject.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -252,7 +255,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueL1: number = 0.5; const valueL2: number = 0.5; const regularization: number = - MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); + MathematicsHelperObject.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -266,7 +269,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueL1: number = 0.5; const valueL2: number = 0.5; const regularization: number = - MathematicsHelper.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); + MathematicsHelperObject.getL1l2RegularizedWeightOptimizedSparse(weight, valueL1, valueL2); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -280,7 +283,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueL1: number = 0.5; const valueL2: number = 0.5; const regularization: number = - MathematicsHelper.getL1l2RegularizedWeightOptimizedDense(weight, valueL1, valueL2); + MathematicsHelperObject.getL1l2RegularizedWeightOptimizedDense(weight, valueL1, valueL2); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -294,7 +297,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueL1: number = 0.5; const valueL2: number = 0.5; const regularization: number = - MathematicsHelper.getL1l2RegularizedWeightOptimizedDense(weight, valueL1, valueL2); + MathematicsHelperObject.getL1l2RegularizedWeightOptimizedDense(weight, valueL1, valueL2); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -308,7 +311,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueL1: number = 0.5; const valueL2: number = 0.5; const regularization: number = - MathematicsHelper.getL1l2RegularizedWeightOptimizedDense(weight, valueL1, valueL2); + MathematicsHelperObject.getL1l2RegularizedWeightOptimizedDense(weight, valueL1, valueL2); Utility.debuggingLog( `regularization=${regularization}`); assert.ok( @@ -329,7 +332,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmax: number[][] = - MathematicsHelper.softmaxLogLossGradientUpdate( + MathematicsHelperObject.softmaxLogLossGradientUpdate( groundTruthPositiveLabelIndexes, featureVectorSparseIndexArrays, matrixWeightDenseArrays, @@ -366,7 +369,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmax: number[][] = - MathematicsHelper.softmaxLogLossGradientUpdate( + MathematicsHelperObject.softmaxLogLossGradientUpdate( groundTruthPositiveLabelIndexes, featureVectorSparseIndexArrays, matrixWeightDenseArrays, @@ -392,7 +395,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmax: number[][] = - MathematicsHelper.softmaxLogLossGradientUpdate( + MathematicsHelperObject.softmaxLogLossGradientUpdate( groundTruthPositiveLabelIndexes, featureVectorSparseIndexArrays, matrixWeightDenseArrays, @@ -418,7 +421,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmax: number[][] = - MathematicsHelper.softmaxLogLossGradientUpdate( + MathematicsHelperObject.softmaxLogLossGradientUpdate( groundTruthPositiveLabelIndexes, featureVectorSparseIndexArrays, matrixWeightDenseArrays, @@ -444,7 +447,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmax: number[][] = - MathematicsHelper.softmaxLogLossGradientUpdate( + MathematicsHelperObject.softmaxLogLossGradientUpdate( groundTruthPositiveLabelIndexes, featureVectorSparseIndexArrays, matrixWeightDenseArrays, @@ -463,7 +466,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const probabilityVector: number[] = testArray; const groundTruthPositiveLabelIndex: number = 1; const logLossValue: number = - MathematicsHelper.logLoss(probabilityVector, groundTruthPositiveLabelIndex); + MathematicsHelperObject.logLoss(probabilityVector, groundTruthPositiveLabelIndex); assert.ok( Utility.almostEqual(logLossValue, 0.6931471805599453), `logLossValue=${logLossValue}`); @@ -474,7 +477,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const probabilityVector: number[] = testArray; const labelArray: number[] = testArray; const logLossGenericValue: number = - MathematicsHelper.logLossGeneric(probabilityVector, labelArray); + MathematicsHelperObject.logLossGeneric(probabilityVector, labelArray); assert.ok( Utility.almostEqual(logLossGenericValue, 1.0742817243274154), `logLossGenericValue=${logLossGenericValue}`); @@ -486,7 +489,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const softmaxVectors: number[][] = [testArray, testArray]; const groundTruthPositiveLabelIndexes: number[] = [1, 1]; const softmaxLogLossValue: number = - MathematicsHelper.softmaxLogLoss(softmaxVectors, groundTruthPositiveLabelIndexes); + MathematicsHelperObject.softmaxLogLoss(softmaxVectors, groundTruthPositiveLabelIndexes); assert.ok( Utility.almostEqual(softmaxLogLossValue, 0.6931471805599453), `softmaxLogLossValue=${softmaxLogLossValue}`); @@ -497,7 +500,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const softmaxVectors: number[][] = [testArray, testArray]; const labelVectors: number[][] = [testArray, testArray]; const softmaxLogLossGenericValue: number = - MathematicsHelper.softmaxLogLossGeneric(softmaxVectors, labelVectors); + MathematicsHelperObject.softmaxLogLossGeneric(softmaxVectors, labelVectors); assert.ok( Utility.almostEqual(softmaxLogLossGenericValue, 1.0742817243274154), `softmaxLogLossGenericValue=${softmaxLogLossGenericValue}`); @@ -529,7 +532,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexesValues( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexesValues( featureVectorSparseIndexArrays, featureVectorSparseValueArrays, matrixWeightDenseArrays, @@ -560,7 +563,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexesValues( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexesValues( featureVectorSparseIndexArrays, featureVectorSparseValueArrays, matrixWeightDenseArrays, @@ -580,7 +583,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexesValues( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexesValues( featureVectorSparseIndexArrays, featureVectorSparseValueArrays, matrixWeightDenseArrays, @@ -600,7 +603,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexesValues( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexesValues( featureVectorSparseIndexArrays, featureVectorSparseValueArrays, matrixWeightDenseArrays, @@ -635,7 +638,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexesValues( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexesValues( featureVectorSparseIndexArrays, featureVectorSparseValueArrays, matrixWeightDenseArrays, @@ -665,7 +668,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 0; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexesValues( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexesValues( featureVectorSparseIndexArrays, featureVectorSparseValueArrays, matrixWeightDenseArrays, @@ -710,7 +713,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -739,7 +742,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -756,7 +759,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -785,7 +788,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -818,7 +821,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -846,7 +849,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 0; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxSparseIndexes( + MathematicsHelperObject.matrixVectorProductSoftmaxSparseIndexes( featureVectorSparseIndexArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -890,7 +893,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxDenseValues( + MathematicsHelperObject.matrixVectorProductSoftmaxDenseValues( vectorDenseValueArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -919,7 +922,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxDenseValues( + MathematicsHelperObject.matrixVectorProductSoftmaxDenseValues( vectorDenseValueArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -936,7 +939,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxDenseValues( + MathematicsHelperObject.matrixVectorProductSoftmaxDenseValues( vectorDenseValueArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -965,7 +968,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexEnd: number = 2; assert.throws(() => { const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxDenseValues( + MathematicsHelperObject.matrixVectorProductSoftmaxDenseValues( vectorDenseValueArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -998,7 +1001,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 2; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxDenseValues( + MathematicsHelperObject.matrixVectorProductSoftmaxDenseValues( vectorDenseValueArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -1026,7 +1029,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const featureVectorIndexBegin: number = 0; const featureVectorIndexEnd: number = 0; const softmaxVectors: number[][] = - MathematicsHelper.matrixVectorProductSoftmaxDenseValues( + MathematicsHelperObject.matrixVectorProductSoftmaxDenseValues( vectorDenseValueArrays, matrixWeightDenseArrays, biasVectorDenseValueArray, @@ -1064,7 +1067,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const matrixWeightDenseArrays: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const biasVectorDenseValueArray: number[] = [0.5, 0.5]; const matrixVectorProduct: number[] = - MathematicsHelper.matrixVectorProductSparseIndexesValues( + MathematicsHelperObject.matrixVectorProductSparseIndexesValues( featureVectorSparseIndexArray, featureVectorSparseValueArray, matrixWeightDenseArrays, @@ -1093,7 +1096,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const matrixWeightDenseArrays: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const biasVectorDenseValueArray: number[] = [0.5, 0.5]; const matrixVectorProduct: number[] = - MathematicsHelper.matrixVectorProductSparseIndexes( + MathematicsHelperObject.matrixVectorProductSparseIndexes( featureVectorSparseIndexArray, matrixWeightDenseArrays, biasVectorDenseValueArray); @@ -1121,7 +1124,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const matrixWeightDenseArrays: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const biasVectorDenseValueArray: number[] = [0.5, 0.5]; const matrixVectorProduct: number[] = - MathematicsHelper.matrixVectorProductDenseValues( + MathematicsHelperObject.matrixVectorProductDenseValues( vectorDenseValueArray, matrixWeightDenseArrays, biasVectorDenseValueArray); @@ -1150,7 +1153,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const weights: number[] = [0.1, 0.2]; const weightBias: number = 0.5; const dotProduct: number = - MathematicsHelper.dotProductSparseIndexesValues( + MathematicsHelperObject.dotProductSparseIndexesValues( indexArray, valueArray, weights, @@ -1175,7 +1178,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const weights: number[] = [0.1, 0.2]; const weightBias: number = 0.5; const dotProduct: number = - MathematicsHelper.dotProductSparseIndexes( + MathematicsHelperObject.dotProductSparseIndexes( indexArray, weights, weightBias); @@ -1199,7 +1202,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const weights: number[] = [0.1, 0.2]; const weightBias: number = 0.5; const dotProduct: number = - MathematicsHelper.dotProductDenseValues( + MathematicsHelperObject.dotProductDenseValues( valueArray, weights, weightBias); @@ -1242,7 +1245,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[][] = - MathematicsHelper.matrixDenseL1l2RegularizedSparseTo( + MathematicsHelperObject.matrixDenseL1l2RegularizedSparseTo( valueArray, l1Regularization, l2Regularization); @@ -1293,7 +1296,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[][] = - MathematicsHelper.matrixDenseL1l2RegularizedDenseTo( + MathematicsHelperObject.matrixDenseL1l2RegularizedDenseTo( valueArray, l1Regularization, l2Regularization); @@ -1343,7 +1346,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[] = - MathematicsHelper.vectorDenseL1l2RegularizedSparseTo( + MathematicsHelperObject.vectorDenseL1l2RegularizedSparseTo( valueArray, l1Regularization, l2Regularization); @@ -1386,7 +1389,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[] = - MathematicsHelper.vectorDenseL1l2RegularizedDenseTo( + MathematicsHelperObject.vectorDenseL1l2RegularizedDenseTo( valueArray, l1Regularization, l2Regularization); @@ -1413,7 +1416,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 0.01; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseAddConstantTo( + MathematicsHelperObject.matrixDenseAddConstantTo( valueArray, constant); assert.ok( @@ -1444,7 +1447,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 0.01; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseMultiplyConstantTo( + MathematicsHelperObject.matrixDenseMultiplyConstantTo( valueArray, constant); assert.ok( @@ -1475,7 +1478,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 0.01; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseSubtractConstantFrom( + MathematicsHelperObject.matrixDenseSubtractConstantFrom( valueArray, constant); assert.ok( @@ -1506,7 +1509,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 0.01; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseDivideConstantFrom( + MathematicsHelperObject.matrixDenseDivideConstantFrom( valueArray, constant); assert.ok( @@ -1537,7 +1540,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema `; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseAddTo( + MathematicsHelperObject.matrixDenseAddTo( valueArray, valueArray); assert.ok( @@ -1567,7 +1570,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema `; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseMultiplyTo( + MathematicsHelperObject.matrixDenseMultiplyTo( valueArray, valueArray); assert.ok( @@ -1597,7 +1600,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema `; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseSubtractFrom( + MathematicsHelperObject.matrixDenseSubtractFrom( valueArray, valueArray); assert.ok( @@ -1627,7 +1630,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema `; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseDivideFrom( + MathematicsHelperObject.matrixDenseDivideFrom( valueArray, valueArray); assert.ok( @@ -1660,7 +1663,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 2; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseAddScaledTo( + MathematicsHelperObject.matrixDenseAddScaledTo( valueArray, valueArray, constant); @@ -1693,7 +1696,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 2; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseMultiplyScaledTo( + MathematicsHelperObject.matrixDenseMultiplyScaledTo( valueArray, valueArray, constant); @@ -1726,7 +1729,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 2; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseSubtractScaledFrom( + MathematicsHelperObject.matrixDenseSubtractScaledFrom( valueArray, valueArray, constant); @@ -1759,7 +1762,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const constant: number = 2; const valueArrayResult: number[][] = - MathematicsHelper.matrixDenseDivideScaledFrom( + MathematicsHelperObject.matrixDenseDivideScaledFrom( valueArray, valueArray, constant); @@ -1790,7 +1793,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseAddConstantTo( + MathematicsHelperObject.vectorDenseAddConstantTo( valueArray, constant); assert.ok( @@ -1819,7 +1822,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseMultiplyConstantTo( + MathematicsHelperObject.vectorDenseMultiplyConstantTo( valueArray, constant); assert.ok( @@ -1848,7 +1851,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseSubtractConstantFrom( + MathematicsHelperObject.vectorDenseSubtractConstantFrom( valueArray, constant); assert.ok( @@ -1877,7 +1880,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseDivideConstantFrom( + MathematicsHelperObject.vectorDenseDivideConstantFrom( valueArray, constant); assert.ok( @@ -1906,7 +1909,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseAddTo( + MathematicsHelperObject.vectorDenseAddTo( valueArray, valueArray); assert.ok( @@ -1934,7 +1937,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseMultiplyTo( + MathematicsHelperObject.vectorDenseMultiplyTo( valueArray, valueArray); assert.ok( @@ -1962,7 +1965,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseSubtractFrom( + MathematicsHelperObject.vectorDenseSubtractFrom( valueArray, valueArray); assert.ok( @@ -1990,7 +1993,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const constant: number = 0.01; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseDivideFrom( + MathematicsHelperObject.vectorDenseDivideFrom( valueArray, valueArray); assert.ok( @@ -2020,7 +2023,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 0.01; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseAddScaledTo( + MathematicsHelperObject.vectorDenseAddScaledTo( valueArray, valueArray, constant); @@ -2050,7 +2053,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 0.01; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseMultiplyScaledTo( + MathematicsHelperObject.vectorDenseMultiplyScaledTo( valueArray, valueArray, constant); @@ -2080,7 +2083,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 0.01; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseSubtractScaledFrom( + MathematicsHelperObject.vectorDenseSubtractScaledFrom( valueArray, valueArray, constant); @@ -2110,7 +2113,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 0.01; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorDenseDivideScaledFrom( + MathematicsHelperObject.vectorDenseDivideScaledFrom( valueArray, valueArray, constant); @@ -2133,7 +2136,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewLikeWithRandomCells( + MathematicsHelperObject.matrixNewLikeWithRandomCells( valueArray); assert.ok( ((valueArrayResult[0][0] >= 0) && (valueArrayResult[0][0] <= 1)), @@ -2154,7 +2157,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewLikeWithRandomCellsScaled( + MathematicsHelperObject.matrixNewLikeWithRandomCellsScaled( valueArray, scale); assert.ok( @@ -2175,7 +2178,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewLikeWithZeroCells( + MathematicsHelperObject.matrixNewLikeWithZeroCells( valueArray); assert.ok( ((valueArrayResult[0][0] === 0)), @@ -2196,7 +2199,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 2; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewLikeWithConstantCells( + MathematicsHelperObject.matrixNewLikeWithConstantCells( valueArray, constant); assert.ok( @@ -2218,7 +2221,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewLikeWithScaledCells( + MathematicsHelperObject.matrixNewLikeWithScaledCells( valueArray, scale); assert.ok( @@ -2268,7 +2271,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[][] = - MathematicsHelper.matrixNewLikeWithL1l2RegularizedSparseCells( + MathematicsHelperObject.matrixNewLikeWithL1l2RegularizedSparseCells( valueArray, l1Regularization, l2Regularization); @@ -2319,7 +2322,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[][] = - MathematicsHelper.matrixNewLikeWithL1l2RegularizedDenseCells( + MathematicsHelperObject.matrixNewLikeWithL1l2RegularizedDenseCells( valueArray, l1Regularization, l2Regularization); @@ -2342,7 +2345,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewLikeWithRandomElements( + MathematicsHelperObject.vectorNewLikeWithRandomElements( valueArray); assert.ok( ((valueArrayResult[0] >= 0) && (valueArrayResult[0] <= 1)), @@ -2363,7 +2366,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewLikeWithRandomElementsScaled( + MathematicsHelperObject.vectorNewLikeWithRandomElementsScaled( valueArray, scale); assert.ok( @@ -2384,7 +2387,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewLikeWithZeroElements( + MathematicsHelperObject.vectorNewLikeWithZeroElements( valueArray); assert.ok( ((valueArrayResult[0] === 0)), @@ -2405,7 +2408,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 2; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewLikeWithConstantElements( + MathematicsHelperObject.vectorNewLikeWithConstantElements( valueArray, constant); assert.ok( @@ -2427,7 +2430,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewLikeWithScaledElements( + MathematicsHelperObject.vectorNewLikeWithScaledElements( valueArray, scale); assert.ok( @@ -2475,7 +2478,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[] = - MathematicsHelper.vectorNewLikeWithL1l2RegularizedSparseElements( + MathematicsHelperObject.vectorNewLikeWithL1l2RegularizedSparseElements( valueArray, l1Regularization, l2Regularization); @@ -2518,7 +2521,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[] = - MathematicsHelper.vectorNewLikeWithL1l2RegularizedDenseElements( + MathematicsHelperObject.vectorNewLikeWithL1l2RegularizedDenseElements( valueArray, l1Regularization, l2Regularization); @@ -2535,7 +2538,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewWithRandomCells( + MathematicsHelperObject.matrixNewWithRandomCells( valueArray.length, valueArray[0].length); assert.ok( @@ -2557,7 +2560,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewWithRandomCellsScaled( + MathematicsHelperObject.matrixNewWithRandomCellsScaled( valueArray.length, valueArray[0].length, scale); @@ -2579,7 +2582,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewWithZeroCells( + MathematicsHelperObject.matrixNewWithZeroCells( valueArray.length, valueArray[0].length); assert.ok( @@ -2601,7 +2604,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 2; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewWithConstantCells( + MathematicsHelperObject.matrixNewWithConstantCells( valueArray.length, valueArray[0].length, constant); @@ -2624,7 +2627,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[][] = [[0.1, 0.2], [0.3, 0.4]]; const valueArrayResult: number[][] = - MathematicsHelper.matrixNewWithScaledCells( + MathematicsHelperObject.matrixNewWithScaledCells( valueArray, scale); assert.ok( @@ -2674,7 +2677,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[][] = - MathematicsHelper.matrixNewWithL1l2RegularizedSparseCells( + MathematicsHelperObject.matrixNewWithL1l2RegularizedSparseCells( valueArray, l1Regularization, l2Regularization); @@ -2725,7 +2728,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[][] = - MathematicsHelper.matrixNewWithL1l2RegularizedDenseCells( + MathematicsHelperObject.matrixNewWithL1l2RegularizedDenseCells( valueArray, l1Regularization, l2Regularization); @@ -2748,7 +2751,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewWithRandomElements( + MathematicsHelperObject.vectorNewWithRandomElements( valueArray.length); assert.ok( ((valueArrayResult[0] >= 0) && (valueArrayResult[0] <= 1)), @@ -2769,7 +2772,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewWithRandomElementsScaled( + MathematicsHelperObject.vectorNewWithRandomElementsScaled( valueArray.length, scale); assert.ok( @@ -2790,7 +2793,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewWithZeroElements( + MathematicsHelperObject.vectorNewWithZeroElements( valueArray.length); assert.ok( ((valueArrayResult[0] === 0)), @@ -2811,7 +2814,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const constant: number = 2; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewWithConstantElements( + MathematicsHelperObject.vectorNewWithConstantElements( valueArray.length, constant); assert.ok( @@ -2833,7 +2836,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const scale: number = 2; const valueArray: number[] = [0.1, 0.2, 0.3, 0.4]; const valueArrayResult: number[] = - MathematicsHelper.vectorNewWithScaledElements( + MathematicsHelperObject.vectorNewWithScaledElements( valueArray, scale); assert.ok( @@ -2881,7 +2884,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[] = - MathematicsHelper.vectorNewWithL1l2RegularizedSparseElements( + MathematicsHelperObject.vectorNewWithL1l2RegularizedSparseElements( valueArray, l1Regularization, l2Regularization); @@ -2924,7 +2927,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const l1Regularization: number = 0.01; const l2Regularization: number = 0.01; const valueArrayRegularized: number[] = - MathematicsHelper.vectorNewWithL1l2RegularizedDenseElements( + MathematicsHelperObject.vectorNewWithL1l2RegularizedDenseElements( valueArray, l1Regularization, l2Regularization); @@ -2941,7 +2944,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: Int32Array = new Int32Array([1, 4, 5, 3, 5, 3, 2]); const maxResult: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntryOnArray(valueArray); + MathematicsHelperObject.getIndexOnFirstMaxEntryOnArray(valueArray); assert.ok( maxResult.indexMax === 2, `maxResult.indexMax=${maxResult.indexMax}`); @@ -2954,7 +2957,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: Int32Array = new Int32Array([1, 4, 5, 3, 5, 3, 2]); const maxResult: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnLastMaxEntryOnArray(valueArray); + MathematicsHelperObject.getIndexOnLastMaxEntryOnArray(valueArray); assert.ok( maxResult.indexMax === 4, `maxResult.indexMax=${maxResult.indexMax}`); @@ -2967,7 +2970,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [1, 4, 5, 3, 5, 3, 2]; const maxResult: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnFirstMaxEntry(valueArray); + MathematicsHelperObject.getIndexOnFirstMaxEntry(valueArray); assert.ok( maxResult.indexMax === 2, `maxResult.indexMax=${maxResult.indexMax}`); @@ -2980,7 +2983,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [1, 4, 5, 3, 5, 3, 2]; const maxResult: { "indexMax": number, "max": number } = - MathematicsHelper.getIndexOnLastMaxEntry(valueArray); + MathematicsHelperObject.getIndexOnLastMaxEntry(valueArray); assert.ok( maxResult.indexMax === 4, `maxResult.indexMax=${maxResult.indexMax}`); @@ -2994,7 +2997,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: Int32Array = new Int32Array([6, 4, 5, 3, 5, 3, 6]); const minResult: { "indexMin": number, "min": number } = - MathematicsHelper.getIndexOnFirstMinEntryOnArray(valueArray); + MathematicsHelperObject.getIndexOnFirstMinEntryOnArray(valueArray); assert.ok( minResult.indexMin === 3, `minResult.indexMin=${minResult.indexMin}`); @@ -3007,7 +3010,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: Int32Array = new Int32Array([6, 4, 5, 3, 5, 3, 6]); const minResult: { "indexMin": number, "min": number } = - MathematicsHelper.getIndexOnLastMinEntryOnArray(valueArray); + MathematicsHelperObject.getIndexOnLastMinEntryOnArray(valueArray); assert.ok( minResult.indexMin === 5, `minResult.indexMin=${minResult.indexMin}`); @@ -3020,7 +3023,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [6, 4, 5, 3, 5, 3, 6]; const minResult: { "indexMin": number, "min": number } = - MathematicsHelper.getIndexOnFirstMinEntry(valueArray); + MathematicsHelperObject.getIndexOnFirstMinEntry(valueArray); assert.ok( minResult.indexMin === 3, `minResult.indexMin=${minResult.indexMin}`); @@ -3033,7 +3036,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const valueArray: number[] = [6, 4, 5, 3, 5, 3, 6]; const minResult: { "indexMin": number, "min": number } = - MathematicsHelper.getIndexOnLastMinEntry(valueArray); + MathematicsHelperObject.getIndexOnLastMinEntry(valueArray); assert.ok( minResult.indexMin === 5, `minResult.indexMin=${minResult.indexMin}`); @@ -3048,7 +3051,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const numerator: number = 0; const denominator: number = 0; const result: number = - MathematicsHelper.safeDivide(numerator, denominator); + MathematicsHelperObject.safeDivide(numerator, denominator); assert.ok( Utility.almostEqual(result, 0), `result=${result}`); @@ -3059,7 +3062,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const numerator: number = 0; const denominator: number = 1; const result: number = - MathematicsHelper.safeDivide(numerator, denominator); + MathematicsHelperObject.safeDivide(numerator, denominator); assert.ok( Utility.almostEqual(result, 0), `result=${result}`); @@ -3070,7 +3073,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema const numerator: number = 1; const denominator: number = 1; const result: number = - MathematicsHelper.safeDivide(numerator, denominator); + MathematicsHelperObject.safeDivide(numerator, denominator); assert.ok( Utility.almostEqual(result, 1), `result=${result}`); @@ -3080,7 +3083,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const value: number = 0; const result: number = - MathematicsHelper.safeLog(value); + MathematicsHelperObject.safeLog(value); assert.ok( Utility.almostEqual(result, Number.MIN_VALUE), `result=${result}`); @@ -3090,7 +3093,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const value: number = -1; const result: number = - MathematicsHelper.safeLog(value); + MathematicsHelperObject.safeLog(value); assert.ok( Number.isNaN(result), `result=${result}`); @@ -3100,7 +3103,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const value: number = 1; const result: number = - MathematicsHelper.safeLog(value); + MathematicsHelperObject.safeLog(value); assert.ok( result === 0, `result=${result}`); @@ -3111,7 +3114,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const value: number = 0; const result: number = - MathematicsHelper.clipValue(value); + MathematicsHelperObject.clipValue(value); assert.ok( Utility.almostEqual(result, MathematicsHelper.epsilon), `result=${result}`); @@ -3121,7 +3124,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const value: number = 1; const result: number = - MathematicsHelper.clipValue(value); + MathematicsHelperObject.clipValue(value); assert.ok( Utility.almostEqual(result, MathematicsHelper.epsilonUp), `result=${result}`); @@ -3131,7 +3134,7 @@ describe("Test Suite - mathematics/mathematics_helper/mathematics_helper/Mathema this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const value: number = 0.5; const result: number = - MathematicsHelper.clipValue(value); + MathematicsHelperObject.clipValue(value); assert.ok( Utility.almostEqual(result, value), `result=${result}`); diff --git a/packages/dispatcher/test/mathematics/sampler/ReservoirSampler.test.ts b/packages/dispatcher/test/mathematics/sampler/ReservoirSampler.test.ts index 6d5d71ff8..d0c700aad 100644 --- a/packages/dispatcher/test/mathematics/sampler/ReservoirSampler.test.ts +++ b/packages/dispatcher/test/mathematics/sampler/ReservoirSampler.test.ts @@ -381,7 +381,7 @@ describe("Test Suite - mathematics/sampler/ReservoirSampler", () => { this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sampler: ReservoirSampler = new ReservoirSampler(); sampler.addInstance("label0", "label0 - utterance 0"); - const labelMap: { [id: string]: number; } = sampler.getLabelMap(); + const labelMap: { [id: string]: number } = sampler.getLabelMap(); Utility.debuggingLog( `labelMap=${labelMap}`); }); @@ -390,7 +390,7 @@ describe("Test Suite - mathematics/sampler/ReservoirSampler", () => { this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sampler: ReservoirSampler = new ReservoirSampler(); sampler.addInstance("label0", "label0 - utterance 0"); - const instances: { [id: string]: string[]; } = sampler.getInstances(); + const instances: { [id: string]: string[] } = sampler.getInstances(); Utility.debuggingLog( `instances=${instances}`); }); diff --git a/packages/dispatcher/test/model/language_understanding/featurizer/NgramSubwordFeaturizer.test.ts b/packages/dispatcher/test/model/language_understanding/featurizer/NgramSubwordFeaturizer.test.ts index b965d66db..05696e3ef 100644 --- a/packages/dispatcher/test/model/language_understanding/featurizer/NgramSubwordFeaturizer.test.ts +++ b/packages/dispatcher/test/model/language_understanding/featurizer/NgramSubwordFeaturizer.test.ts @@ -94,7 +94,7 @@ describe("Test Suite - model/language_understanding/featurizer/ngram_subword_fea const intentsUtterancesWeights: { "intents": string[], "utterances": string[], "weights": number[] } = featurizer.getIntentsUtterancesWeights(); Utility.debuggingLog( - `intentsUtterancesWeights=${Utility.JSONstringify(intentsUtterancesWeights)}`); + `intentsUtterancesWeights=${Utility.jsonStringify(intentsUtterancesWeights)}`); }); it("Test.0200 getLabels()", function() { @@ -116,7 +116,7 @@ describe("Test Suite - model/language_understanding/featurizer/ngram_subword_fea this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const featurizer: NgramSubwordFeaturizer = featurizerColumnarContentEmail; - const labelMap: { [id: string]: number; } = + const labelMap: { [id: string]: number } = featurizer.getLabelMap(); const numberLabels: number = DictionaryMapUtility.getStringIdGenericValueDictionaryLength(labelMap); @@ -144,7 +144,7 @@ describe("Test Suite - model/language_understanding/featurizer/ngram_subword_fea this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const featurizer: NgramSubwordFeaturizer = featurizerColumnarContentEmail; - const featureMap: { [id: string]: number; } = + const featureMap: { [id: string]: number } = featurizer.getFeatureMap(); const numberFeatures: number = DictionaryMapUtility.getStringIdGenericValueDictionaryLength(featureMap); diff --git a/packages/dispatcher/test/utility/utility.test.ts b/packages/dispatcher/test/utility/utility.test.ts index a25470d66..fa90ce09a 100644 --- a/packages/dispatcher/test/utility/utility.test.ts +++ b/packages/dispatcher/test/utility/utility.test.ts @@ -217,7 +217,7 @@ describe("Test Suite - utility/Utility", () => { stringNumberMap.set("a", 1); const mapInObject: any = Utility.stringMapToObject(stringNumberMap); const mapInJsonString: string = - Utility.JSONstringify(mapInObject); + Utility.jsonStringify(mapInObject); Utility.debuggingLog( `mapInJsonString=${mapInJsonString}`); }); @@ -228,7 +228,7 @@ describe("Test Suite - utility/Utility", () => { stringNumberMap.set("a", 1); const mapInObject: any = Utility.stringMapToObject(stringNumberMap); const mapInJsonString: string = - Utility.JSONstringify(mapInObject); + Utility.jsonStringify(mapInObject); Utility.debuggingLog( `mapInJsonString=${mapInJsonString}`); const mapInObjectFromJsonString: Map = @@ -245,7 +245,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapSetInObject: any = Utility.stringMapSetToObject(stringNumberMapSet); const mapSetInJsonString: string = - Utility.JSONstringify(stringMapSetInObject); + Utility.jsonStringify(stringMapSetInObject); Utility.debuggingLog( `mapSetInJsonString=${mapSetInJsonString}`); }); @@ -257,7 +257,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapSetInObject: any = Utility.stringMapSetToObject(stringNumberMapSet); const mapSetInJsonString: string = - Utility.JSONstringify(stringMapSetInObject); + Utility.jsonStringify(stringMapSetInObject); Utility.debuggingLog( `mapSetInJsonString=${mapSetInJsonString}`); const stringNumberMapSetNew: Map> = @@ -268,7 +268,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapSetNewInObject: any = Utility.stringMapSetToObject(stringNumberMapSetNew); const mapSetNewInJsonString: string = - Utility.JSONstringify(stringMapSetNewInObject); + Utility.jsonStringify(stringMapSetNewInObject); Utility.debuggingLog( `mapSetNewInJsonString=${mapSetNewInJsonString}`); assert.ok((stringNumberMapSetNew.get("a") as Set).size === 1, @@ -286,7 +286,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapArrayInObject: any = Utility.stringMapArrayToObject(stringNumberMapArray); const mapArrayInJsonString: string = - Utility.JSONstringify(stringMapArrayInObject); + Utility.jsonStringify(stringMapArrayInObject); Utility.debuggingLog( `mapArrayInJsonString=${mapArrayInJsonString}`); }); @@ -298,7 +298,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapArrayInObject: any = Utility.stringMapArrayToObject(stringNumberMapArray); const mapArrayInJsonString: string = - Utility.JSONstringify(stringMapArrayInObject); + Utility.jsonStringify(stringMapArrayInObject); Utility.debuggingLog( `mapArrayInJsonString=${mapArrayInJsonString}`); const stringNumberMapArrayNew: Map = @@ -309,7 +309,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapArrayNewInObject: any = Utility.stringMapArrayToObject(stringNumberMapArrayNew); const mapArrayNewInJsonString: string = - Utility.JSONstringify(stringMapArrayNewInObject); + Utility.jsonStringify(stringMapArrayNewInObject); Utility.debuggingLog( `mapArrayNewInJsonString=${mapArrayNewInJsonString}`); assert.ok((stringNumberMapArrayNew.get("a") as number[]).length === 1, @@ -329,7 +329,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapSetInJson: string = Utility.stringMapSetToJson(stringNumberMapSet); const mapSetInJsonString: string = - Utility.JSONstringify(stringMapSetInJson); + Utility.jsonStringify(stringMapSetInJson); Utility.debuggingLog( `mapSetInJsonString=${mapSetInJsonString}`); }); @@ -341,7 +341,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapSetInJson: string = Utility.stringMapSetToJson(stringNumberMapSet); const mapSetInJsonString: string = - Utility.JSONstringify(stringMapSetInJson); + Utility.jsonStringify(stringMapSetInJson); Utility.debuggingLog( `mapSetInJsonString=${mapSetInJsonString}`); const stringNumberMapSetNew: Map> = @@ -352,7 +352,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapSetNewInJson: string = Utility.stringMapSetToJson(stringNumberMapSetNew); const mapSetNewInJsonString: string = - Utility.JSONstringify(stringMapSetNewInJson); + Utility.jsonStringify(stringMapSetNewInJson); Utility.debuggingLog( `mapSetNewInJsonString=${mapSetNewInJsonString}`); assert.ok((stringNumberMapSetNew.get("a") as Set).size === 1, @@ -370,7 +370,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapArrayInJson: string = Utility.stringMapArrayToJson(stringNumberMapArray); const mapArrayInJsonString: string = - Utility.JSONstringify(stringMapArrayInJson); + Utility.jsonStringify(stringMapArrayInJson); Utility.debuggingLog( `mapArrayInJsonString=${mapArrayInJsonString}`); }); @@ -382,7 +382,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapArrayInJson: string = Utility.stringMapArrayToJson(stringNumberMapArray); const mapArrayInJsonString: string = - Utility.JSONstringify(stringMapArrayInJson); + Utility.jsonStringify(stringMapArrayInJson); Utility.debuggingLog( `mapArrayInJsonString=${mapArrayInJsonString}`); const stringNumberMapArrayNew: Map = @@ -393,7 +393,7 @@ describe("Test Suite - utility/Utility", () => { const stringMapArrayNewInJson: string = Utility.stringMapArrayToJson(stringNumberMapArrayNew); const mapArrayNewInJsonString: string = - Utility.JSONstringify(stringMapArrayNewInJson); + Utility.jsonStringify(stringMapArrayNewInJson); Utility.debuggingLog( `mapArrayNewInJsonString=${mapArrayNewInJsonString}`); assert.ok((stringNumberMapArrayNew.get("a") as number[]).length === 1, @@ -449,114 +449,114 @@ describe("Test Suite - utility/Utility", () => { `i=${i}, r=${r}, randomArray[i]=${randomArray[i]}`); } }); - it("Test.0504 getXorshift128plusState0BigInt()", function() { + it("Test.0504 getXorshift128plusState0WithBigIntStates()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); - const xorshift128plusState0BigInt: bigint = Utility.getXorshift128plusState0BigInt(); + const xorshift128plusState0BigInt: bigint = Utility.getXorshift128plusState0WithBigIntStates(); Utility.debuggingLog( `xorshift128plusState0BigInt=${xorshift128plusState0BigInt}`); }); - it("Test.0505 getXorshift128plusState1BigInt()", function() { + it("Test.0505 getXorshift128plusState1WithBigIntStates()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); - const xorshift128plusState1BigInt: bigint = Utility.getXorshift128plusState1BigInt(); + const xorshift128plusState1BigInt: bigint = Utility.getXorshift128plusState1WithBigIntStates(); Utility.debuggingLog( `xorshift128plusState1BigInt=${xorshift128plusState1BigInt}`); }); - it("Test.0506 getXorshift128plusCycleBigInt()", function() { + it("Test.0506 getXorshift128plusCycleWithBigIntStates()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); - const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleBigInt(); + const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleWithBigIntStates(); Utility.debuggingLog( `xorshift128plusCycleBigInt=${xorshift128plusCycleBigInt}`); }); - it("Test.0507 getXorshift128plusCycleBigIntFloat()", function() { + it("Test.0507 getXorshift128plusCycleFloatWithBigIntStates()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); - const xorshift128plusCycleBigIntFloat: number = Utility.getXorshift128plusCycleBigIntFloat(); + const xorshift128plusCycleBigIntFloat: number = Utility.getXorshift128plusCycleFloatWithBigIntStates(); Utility.debuggingLog( `xorshift128plusCycleBigIntFloat=${xorshift128plusCycleBigIntFloat}`); }); - it("Test.0508 rngSeedXorshift128plusBigIntWithNumber()", function() { + it("Test.0508 rngSeedXorshift128plusWithBigIntStatesWithNumberArguments()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); - Utility.rngSeedXorshift128plusBigIntWithNumber(3, 4); + Utility.rngSeedXorshift128plusWithBigIntStatesWithNumberArguments(3, 4); for (let i = 0; i < 16; i++) { - const r: bigint = Utility.rngNextXorshift128plusBigInt(); + const r: bigint = Utility.rngNextXorshift128plusWithBigIntStates(); Utility.debuggingLog( `i=${i}, r=${r}`); } }); - it("Test.0509 rngSeedXorshift128plusBigInt()", function() { + it("Test.0509 getXorshift128plusCycleWithBigIntStates()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const bigint3: bigint = BigInt(3); const bigint4: bigint = BigInt(4); - const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleBigInt(); - Utility.rngSeedXorshift128plusBigInt( + const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleWithBigIntStates(); + Utility.rngSeedXorshift128plusWithBigIntStates( bigint3, bigint4, xorshift128plusCycleBigInt); for (let i = 0; i < 16; i++) { - const r: bigint = Utility.rngNextXorshift128plusBigInt(); + const r: bigint = Utility.rngNextXorshift128plusWithBigIntStates(); Utility.debuggingLog( `i=${i}, r=${r}`); } }); - it("Test.0510 rngNextXorshift128plusBigInt()", function() { + it("Test.0510 rngNextXorshift128plusWithBigIntStates()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const bigint3: bigint = BigInt(3); const bigint4: bigint = BigInt(4); - const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleBigInt(); - Utility.rngSeedXorshift128plusBigInt( + const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleWithBigIntStates(); + Utility.rngSeedXorshift128plusWithBigIntStates( bigint3, bigint4, xorshift128plusCycleBigInt); const randomArray: Array = []; for (let i = 0; i < 16; i++) { - const r: bigint = Utility.rngNextXorshift128plusBigInt(); + const r: bigint = Utility.rngNextXorshift128plusWithBigIntStates(); randomArray.push(r); Utility.debuggingLog( `i=${i}, r=${r}`); } - Utility.rngSeedXorshift128plusBigInt( + Utility.rngSeedXorshift128plusWithBigIntStates( bigint3, bigint4, xorshift128plusCycleBigInt); for (let i = 0; i < 16; i++) { - const r: bigint = Utility.rngNextXorshift128plusBigInt(); + const r: bigint = Utility.rngNextXorshift128plusWithBigIntStates(); Utility.debuggingLog( `i=${i}, r=${r}`); assert.ok(r === randomArray[i], `i=${i}, r=${r}, randomArray[i]=${randomArray[i]}`); } }); - it("Test.0511 rngNextXorshift128plusBigIntFloat()", function() { + it("Test.0511 rngNextXorshift128plusFloatWithBigIntStates()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const bigint3: bigint = BigInt(3); const bigint4: bigint = BigInt(4); - const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleBigInt(); - Utility.rngSeedXorshift128plusBigInt( + const xorshift128plusCycleBigInt: bigint = Utility.getXorshift128plusCycleWithBigIntStates(); + Utility.rngSeedXorshift128plusWithBigIntStates( bigint3, bigint4, xorshift128plusCycleBigInt); const randomArray: number[] = []; for (let i = 0; i < 16; i++) { - const r: number = Utility.rngNextXorshift128plusBigIntFloat(); + const r: number = Utility.rngNextXorshift128plusFloatWithBigIntStates(); assert.ok(((r >= 0) && (r < 1)), `r=${r}`); randomArray.push(r); Utility.debuggingLog( `i=${i}, r=${r}`); } - Utility.rngSeedXorshift128plusBigInt( + Utility.rngSeedXorshift128plusWithBigIntStates( bigint3, bigint4, xorshift128plusCycleBigInt); for (let i = 0; i < 16; i++) { - const r: number = Utility.rngNextXorshift128plusBigIntFloat(); + const r: number = Utility.rngNextXorshift128plusFloatWithBigIntStates(); assert.ok(((r >= 0) && (r < 1)), `r=${r}`); Utility.debuggingLog( @@ -565,23 +565,23 @@ describe("Test Suite - utility/Utility", () => { `i=${i}, r=${r}, randomArray[i]=${randomArray[i]}`); } }); - it("Test.0512 rngNextXorshift128plusBigIntFloatUniformTest()", function() { + it("Test.0512 rngNextXorshift128plusFloatWithBigIntStatesUniformTest()", function() { Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const sampleSize: number = 100000; const sampleArray: number[] = []; for (let i = 0; i < sampleSize; i++) { - const r: number = Utility.rngNextXorshift128plusBigIntFloat(); + const r: number = Utility.rngNextXorshift128plusFloatWithBigIntStates(); assert.ok(((r >= 0) && (r < 1)), `r=${r}`); sampleArray.push(r); } const sampleArraySum: number = - sampleArray.reduce((accumulant, entry) => accumulant += entry, 0); + sampleArray.reduce((accumulant: number, entry: number) => accumulant += entry, 0); const sampleArrayMean: number = sampleArraySum / sampleSize; const sampleArraySecondMomentSum: number = - sampleArray.reduce((accumulant, entry) => accumulant += (entry * entry), 0); + sampleArray.reduce((accumulant: number, entry: number) => accumulant += (entry * entry), 0); const sampleArraySecondMoment: number = sampleArraySecondMomentSum / sampleSize; const theoreticMean: number = 0.5; @@ -643,13 +643,13 @@ describe("Test Suite - utility/Utility", () => { this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const array: number[] = [1, 2, 3, 4, 5]; const arrayInJsonString: string = - Utility.JSONstringify(array); + Utility.jsonStringify(array); Utility.debuggingLog( `arrayInJsonString=${arrayInJsonString}`); const arrayShuffled: number[] = Utility.shuffle(array); const arrayShuffledInJsonString: string = - Utility.JSONstringify(arrayShuffled); + Utility.jsonStringify(arrayShuffled); Utility.debuggingLog( `arrayShuffledInJsonString=${arrayShuffledInJsonString}`); assert.ok(array === arrayShuffled, @@ -660,23 +660,23 @@ describe("Test Suite - utility/Utility", () => { this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const arrayOriginal: number[] = [1, 2, 3, 4, 5]; const arrayOriginalInJsonString: string = - Utility.JSONstringify(arrayOriginal); + Utility.jsonStringify(arrayOriginal); Utility.debuggingLog( `arrayOriginalInJsonString=${arrayOriginalInJsonString}`); - Utility.rngSeedXorshift128plusBigIntWithNumber(3, 4); + Utility.rngSeedXorshift128plusWithBigIntStatesWithNumberArguments(3, 4); const arrayShuffled: number[] = Object.assign([], arrayOriginal); Utility.shuffle(arrayShuffled); const arrayShuffledInJsonString: string = - Utility.JSONstringify(arrayShuffled); + Utility.jsonStringify(arrayShuffled); Utility.debuggingLog( `arrayShuffledInJsonString=${arrayShuffledInJsonString}`); - Utility.rngSeedXorshift128plusBigIntWithNumber(3, 4); + Utility.rngSeedXorshift128plusWithBigIntStatesWithNumberArguments(3, 4); const arrayShuffledNew: number[] = Object.assign([], arrayOriginal); Utility.shuffle(arrayShuffledNew); const arrayShuffledNewInJsonString: string = - Utility.JSONstringify(arrayShuffledNew); + Utility.jsonStringify(arrayShuffledNew); Utility.debuggingLog( `arrayShuffledNewInJsonString=${arrayShuffledNewInJsonString}`); assert.ok(arrayOriginal.length === arrayShuffled.length, @@ -694,13 +694,13 @@ describe("Test Suite - utility/Utility", () => { this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const stringArray: string[] = ["b", "a", "c", "d"]; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); const stringMap: IDictionaryStringIdGenericValue = DictionaryMapUtility.buildStringIdNumberValueDictionaryFromUniqueStringArray(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringMapInJsonString=${stringMapInJsonString}`); }); @@ -709,7 +709,7 @@ describe("Test Suite - utility/Utility", () => { this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); const stringArray: string[] = ["b", "a", "b", "c", "a", "a", "b", "d"]; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); const stringArrayMap: { "stringArray": string[], "stringMap": IDictionaryStringIdGenericValue } = @@ -719,11 +719,11 @@ describe("Test Suite - utility/Utility", () => { const stringMappedMap: IDictionaryStringIdGenericValue = stringArrayMap.stringMap; const stringMappedArrayInJsonString: string = - Utility.JSONstringify(stringMappedArray); + Utility.jsonStringify(stringMappedArray); Utility.debuggingLog( `stringMappedArrayInJsonString=${stringMappedArrayInJsonString}`); const stringMappedMapInJsonString: string = - Utility.JSONstringify(stringMappedMap); + Utility.jsonStringify(stringMappedMap); Utility.debuggingLog( `stringMappedMapInJsonString=${stringMappedMapInJsonString}`); }); @@ -734,7 +734,7 @@ describe("Test Suite - utility/Utility", () => { [["b", "a", "b", "c", "a", "a", "b", "d"], ["b", "a", "b", "c", "a", "a", "b", "d"]]; const stringArraysInJsonString: string = - Utility.JSONstringify(stringArrays); + Utility.jsonStringify(stringArrays); Utility.debuggingLog( `stringArraysInJsonString=${stringArraysInJsonString}`); const stringArrayMap: { "stringArray": string[], "stringMap": IDictionaryStringIdGenericValue } = @@ -744,11 +744,11 @@ describe("Test Suite - utility/Utility", () => { const stringMappedMap: IDictionaryStringIdGenericValue = stringArrayMap.stringMap; const stringMappedArrayInJsonString: string = - Utility.JSONstringify(stringMappedArray); + Utility.jsonStringify(stringMappedArray); Utility.debuggingLog( `stringMappedArrayInJsonString=${stringMappedArrayInJsonString}`); const stringMappedMapInJsonString: string = - Utility.JSONstringify(stringMappedMap); + Utility.jsonStringify(stringMappedMap); Utility.debuggingLog( `stringMappedMapInJsonString=${stringMappedMapInJsonString}`); }); @@ -759,9 +759,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray0: string[] = ["b", "a", "c", "d"]; const stringArray1: string[] = ["b", "a", "c", "d"]; const stringArray0InJsonString: string = - Utility.JSONstringify(stringArray0); + Utility.jsonStringify(stringArray0); const stringArray1InJsonString: string = - Utility.JSONstringify(stringArray1); + Utility.jsonStringify(stringArray1); Utility.debuggingLog( `stringArray0InJsonString=${stringArray0InJsonString}`); Utility.debuggingLog( @@ -778,9 +778,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray0: string[] = []; const stringArray1: string[] = []; const stringArray0InJsonString: string = - Utility.JSONstringify(stringArray0); + Utility.jsonStringify(stringArray0); const stringArray1InJsonString: string = - Utility.JSONstringify(stringArray1); + Utility.jsonStringify(stringArray1); Utility.debuggingLog( `stringArray0InJsonString=${stringArray0InJsonString}`); Utility.debuggingLog( @@ -797,9 +797,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray0: string[] = []; const stringArray1: string[] = ["b", "a", "c", "d"]; const stringArray0InJsonString: string = - Utility.JSONstringify(stringArray0); + Utility.jsonStringify(stringArray0); const stringArray1InJsonString: string = - Utility.JSONstringify(stringArray1); + Utility.jsonStringify(stringArray1); Utility.debuggingLog( `stringArray0InJsonString=${stringArray0InJsonString}`); Utility.debuggingLog( @@ -816,9 +816,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray0: string[] = ["b", "a", "c", "d"]; const stringArray1: string[] = []; const stringArray0InJsonString: string = - Utility.JSONstringify(stringArray0); + Utility.jsonStringify(stringArray0); const stringArray1InJsonString: string = - Utility.JSONstringify(stringArray1); + Utility.jsonStringify(stringArray1); Utility.debuggingLog( `stringArray0InJsonString=${stringArray0InJsonString}`); Utility.debuggingLog( @@ -835,9 +835,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray0: string[] = []; const stringArray1: string[] = ["b", "f", "c", "d"]; const stringArray0InJsonString: string = - Utility.JSONstringify(stringArray0); + Utility.jsonStringify(stringArray0); const stringArray1InJsonString: string = - Utility.JSONstringify(stringArray1); + Utility.jsonStringify(stringArray1); Utility.debuggingLog( `stringArray0InJsonString=${stringArray0InJsonString}`); Utility.debuggingLog( @@ -851,9 +851,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray0: string[] = ["b", "a", "c", "d"]; const stringArray1: string[] = []; const stringArray0InJsonString: string = - Utility.JSONstringify(stringArray0); + Utility.jsonStringify(stringArray0); const stringArray1InJsonString: string = - Utility.JSONstringify(stringArray1); + Utility.jsonStringify(stringArray1); Utility.debuggingLog( `stringArray0InJsonString=${stringArray0InJsonString}`); Utility.debuggingLog( @@ -867,9 +867,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray0: string[] = ["b", "a", "c", "d"]; const stringArray1: string[] = ["b", "f", "c", "d"]; const stringArray0InJsonString: string = - Utility.JSONstringify(stringArray0); + Utility.jsonStringify(stringArray0); const stringArray1InJsonString: string = - Utility.JSONstringify(stringArray1); + Utility.jsonStringify(stringArray1); Utility.debuggingLog( `stringArray0InJsonString=${stringArray0InJsonString}`); Utility.debuggingLog( @@ -1007,9 +1007,9 @@ describe("Test Suite - utility/Utility", () => { const stringMap0: IDictionaryStringIdGenericValue = { a: 1, b: 2, c: 3 }; const stringMap1: IDictionaryStringIdGenericValue = { a: 1, b: 2, c: 3 }; const stringMap0InJsonString: string = - Utility.JSONstringify(stringMap0); + Utility.jsonStringify(stringMap0); const stringMap1InJsonString: string = - Utility.JSONstringify(stringMap1); + Utility.jsonStringify(stringMap1); Utility.debuggingLog( `stringMap0InJsonString=${stringMap0InJsonString}`); Utility.debuggingLog( @@ -1026,9 +1026,9 @@ describe("Test Suite - utility/Utility", () => { const stringMap0: IDictionaryStringIdGenericValue = {}; const stringMap1: IDictionaryStringIdGenericValue = {}; const stringMap0InJsonString: string = - Utility.JSONstringify(stringMap0); + Utility.jsonStringify(stringMap0); const stringMap1InJsonString: string = - Utility.JSONstringify(stringMap1); + Utility.jsonStringify(stringMap1); Utility.debuggingLog( `stringMap0InJsonString=${stringMap0InJsonString}`); Utility.debuggingLog( @@ -1045,9 +1045,9 @@ describe("Test Suite - utility/Utility", () => { const stringMap0: IDictionaryStringIdGenericValue = {}; const stringMap1: IDictionaryStringIdGenericValue = { a: 1, b: 2, c: 3 }; const stringMap0InJsonString: string = - Utility.JSONstringify(stringMap0); + Utility.jsonStringify(stringMap0); const stringMap1InJsonString: string = - Utility.JSONstringify(stringMap1); + Utility.jsonStringify(stringMap1); Utility.debuggingLog( `stringMap0InJsonString=${stringMap0InJsonString}`); Utility.debuggingLog( @@ -1064,9 +1064,9 @@ describe("Test Suite - utility/Utility", () => { const stringMap0: IDictionaryStringIdGenericValue = { a: 1, b: 2, c: 3 }; const stringMap1: IDictionaryStringIdGenericValue = {}; const stringMap0InJsonString: string = - Utility.JSONstringify(stringMap0); + Utility.jsonStringify(stringMap0); const stringMap1InJsonString: string = - Utility.JSONstringify(stringMap1); + Utility.jsonStringify(stringMap1); Utility.debuggingLog( `stringMap0InJsonString=${stringMap0InJsonString}`); Utility.debuggingLog( @@ -1083,9 +1083,9 @@ describe("Test Suite - utility/Utility", () => { const stringMap0: IDictionaryStringIdGenericValue = {}; const stringMap1: IDictionaryStringIdGenericValue = { a: 1, b: 2, c: 3 }; const stringMap0InJsonString: string = - Utility.JSONstringify(stringMap0); + Utility.jsonStringify(stringMap0); const stringMap1InJsonString: string = - Utility.JSONstringify(stringMap1); + Utility.jsonStringify(stringMap1); Utility.debuggingLog( `stringMap0InJsonString=${stringMap0InJsonString}`); Utility.debuggingLog( @@ -1099,9 +1099,9 @@ describe("Test Suite - utility/Utility", () => { const stringMap0: IDictionaryStringIdGenericValue = { a: 1, b: 2, c: 3 }; const stringMap1: IDictionaryStringIdGenericValue = {}; const stringMap0InJsonString: string = - Utility.JSONstringify(stringMap0); + Utility.jsonStringify(stringMap0); const stringMap1InJsonString: string = - Utility.JSONstringify(stringMap1); + Utility.jsonStringify(stringMap1); Utility.debuggingLog( `stringMap0InJsonString=${stringMap0InJsonString}`); Utility.debuggingLog( @@ -1115,9 +1115,9 @@ describe("Test Suite - utility/Utility", () => { const stringMap0: IDictionaryStringIdGenericValue = { a: 1, b: 2, c: 3 }; const stringMap1: IDictionaryStringIdGenericValue = { a: 2, b: 2, c: 3 }; const stringMap0InJsonString: string = - Utility.JSONstringify(stringMap0); + Utility.jsonStringify(stringMap0); const stringMap1InJsonString: string = - Utility.JSONstringify(stringMap1); + Utility.jsonStringify(stringMap1); Utility.debuggingLog( `stringMap0InJsonString=${stringMap0InJsonString}`); Utility.debuggingLog( @@ -1132,9 +1132,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b", "c"]; const stringMap: IDictionaryStringIdGenericValue = {a: 0, b: 1, c: 2}; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1151,9 +1151,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = []; const stringMap: IDictionaryStringIdGenericValue = {a: 0, b: 1, c: 2}; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1170,9 +1170,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b", "c"]; const stringMap: IDictionaryStringIdGenericValue = {}; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1189,9 +1189,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b"]; const stringMap: IDictionaryStringIdGenericValue = {a: 0, b: 1, c: 2}; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1208,9 +1208,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b", "d"]; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1227,9 +1227,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b", "c"]; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: -1, c: 2 }; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1246,9 +1246,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = []; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1265,9 +1265,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b", "c"]; const stringMap: IDictionaryStringIdGenericValue = {}; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1284,9 +1284,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b"]; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1303,9 +1303,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b", "d"]; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1322,9 +1322,9 @@ describe("Test Suite - utility/Utility", () => { const stringArray: string[] = ["a", "b", "c"]; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: -1, c: 2 }; const stringArrayInJsonString: string = - Utility.JSONstringify(stringArray); + Utility.jsonStringify(stringArray); const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `stringArrayInJsonString=${stringArrayInJsonString}`); Utility.debuggingLog( @@ -1341,7 +1341,7 @@ describe("Test Suite - utility/Utility", () => { const keyId: number = 1; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `keyId=${keyId}`); Utility.debuggingLog( @@ -1358,7 +1358,7 @@ describe("Test Suite - utility/Utility", () => { const keyId: number = -1; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `keyId=${keyId}`); Utility.debuggingLog( @@ -1375,7 +1375,7 @@ describe("Test Suite - utility/Utility", () => { const keyId: number = 3; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `keyId=${keyId}`); Utility.debuggingLog( @@ -1392,7 +1392,7 @@ describe("Test Suite - utility/Utility", () => { const keyId: number = -1; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `keyId=${keyId}`); Utility.debuggingLog( @@ -1408,7 +1408,7 @@ describe("Test Suite - utility/Utility", () => { const keyId: number = 3; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `keyId=${keyId}`); Utility.debuggingLog( @@ -1424,7 +1424,7 @@ describe("Test Suite - utility/Utility", () => { const key: string = "b"; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `key=${key}`); Utility.debuggingLog( @@ -1441,7 +1441,7 @@ describe("Test Suite - utility/Utility", () => { const key: string = "d"; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `key=${key}`); Utility.debuggingLog( @@ -1458,7 +1458,7 @@ describe("Test Suite - utility/Utility", () => { const key: string = "d"; const stringMap: IDictionaryStringIdGenericValue = { a: 0, b: 1, c: 2 }; const stringMapInJsonString: string = - Utility.JSONstringify(stringMap); + Utility.jsonStringify(stringMap); Utility.debuggingLog( `key=${key}`); Utility.debuggingLog( @@ -1640,7 +1640,7 @@ describe("Test Suite - utility/Utility", () => { const lineArray: string[] = Utility.stringToLineArray(fileContent); const fileContentReCombined: string = - lineArray.reduce((accumulant, entry) => accumulant += (entry + "\n"), ""); + lineArray.reduce((accumulant: string, entry: string) => accumulant += (entry + "\n"), ""); assert.ok(lineArray.length === 603, `lineArray.length=${lineArray.length}`); assert.ok(fileContentReCombined.length === 25291, // ---- NOTE ---- Windows file length: 25892, @@ -1659,7 +1659,7 @@ describe("Test Suite - utility/Utility", () => { const lineArray: string[] = Utility.stringToLineArray(fileContent); const fileContentReCombined: string = - lineArray.reduce((accumulant, entry) => accumulant += (entry + "\n"), ""); + lineArray.reduce((accumulant: string, entry: string) => accumulant += (entry + "\n"), ""); assert.ok(lineArray.length === 603, `lineArray.length=${lineArray.length}`); assert.ok(fileContentReCombined.length === 25291, // ---- NOTE ---- Windows file length: 25892, @@ -2239,4 +2239,36 @@ describe("Test Suite - utility/Utility", () => { const anArray: string[] = Utility.iterableIteratorToArray(anIterableIterator); assert.ok(anArray.length === 3, `anArray.length=${anArray.length}`); }); + + it("Test.2300 splitByPunctuation()", function() { + Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); + this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); + const input: string = "Hello, World"; + const output: string[] = Utility.splitByPunctuation(input); + Utility.debuggingLog( + `output=${output}`); + assert.ok(output.length === 3); + assert.ok(output[0] === "Hello"); + assert.ok(output[1] === ","); + assert.ok(output[2] === "World"); + }); + + it("Test.2400 splitStringWithCommaDelimitersFilteredByDoubleQuotes()", function() { + Utility.toPrintDebuggingLogToConsole = UnitTestHelper.getDefaultUnitTestDebuggingLogFlag(); + this.timeout(UnitTestHelper.getDefaultUnitTestTimeout()); + const input: string = "1,\"Hello, World\",Adam"; + const output: string[] = Utility.splitStringWithCommaDelimitersFilteredByDoubleQuotes(input); + Utility.debuggingLog( + `output=${output}`); + Utility.debuggingLog( + `output[0]=${output[0]}`); + Utility.debuggingLog( + `output[1]=${output[1]}`); + Utility.debuggingLog( + `output[2]=${output[2]}`); + assert.ok(output.length === 3); + assert.ok(output[0] === "1"); + assert.ok(output[1] === "\"Hello, World\""); + assert.ok(output[2] === "Adam"); + }); });