Skip to content

Commit

Permalink
Normalization of columns unions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed May 15, 2024
1 parent 8bbabf7 commit c14c7e7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 37 deletions.
6 changes: 5 additions & 1 deletion src/lib/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import {
sortedNumbers,
weighArray,
weighMatrix,
} from './operations';
import {
normalizeArray,
normalizeMatrixColumns,
} from './operations';
normalizeMatrixColumnsUnion,
} from './normalization';
import {
createVector,
toVector,
Expand Down Expand Up @@ -66,6 +69,7 @@ export {
weighMatrix,
normalizeArray,
normalizeMatrixColumns,
normalizeMatrixColumnsUnion,
round,
roundWithStep,
};
68 changes: 68 additions & 0 deletions src/lib/math/normalization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { fullCopyObject } from '../utils/functions';
import { isEqual } from './helpers';

export function normalizeArray(input: number[], inplace: boolean = false): number[] {
const result = inplace ? input : fullCopyObject(input);

if (result.length === 0) {
return result;
}

const min = Math.min(...result);
const max = Math.max(...result);

if (isEqual(min, max)) {
return isEqual(min, 0) ? result.map(() => 0) : result.map(() => 1);
}

return result.map((x) => (x - min) / (max - min));
}

export function normalizeMatrixColumns(input: number[][], inplace: boolean = false): number[][] {
const result = inplace ? input : fullCopyObject(input);

if (result.length === 0) {
return result;
}

for (let i = 0; i < result[0].length; i++) {
const columnNormalized = normalizeArray(result.map((row) => row[i]));
for (let j = 0; j < result.length; j++) {
result[j][i] = columnNormalized[j];
}
}

return result;
}

function getBoundsOfMatrixColumnsUnion(matrix: number[][], columns: number[]): [number, number] {
if (columns.length === 0 || matrix.length === 0) {
return [0, 0];
}

let min = Infinity;
let max = -Infinity;

for (const column of columns) {
for (let i = 0; i < matrix.length; i++) {
min = Math.min(min, matrix[i][column]);
max = Math.max(max, matrix[i][column]);
}
}

return [min, max];
}

export function normalizeMatrixColumnsUnion(matrix: number[][], columns: number[], inplace: boolean = false): number[][] {
const result = inplace ? matrix : fullCopyObject(matrix);

const [min, max] = getBoundsOfMatrixColumnsUnion(result, columns);

for (const column of columns) {
for (let i = 0; i < result.length; i++) {
result[i][column] = (result[i][column] - min) / (max - min);
}
}

return result;
}
36 changes: 0 additions & 36 deletions src/lib/math/operations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { createEmptyMatrix, createEmptyTensor } from './factories';
import { isEqual } from "@/lib/math/helpers";
import { fullCopyObject } from "@/lib/utils/functions";

export function arrayUnaryOperation<T>(
input: Array<T>,
Expand Down Expand Up @@ -129,37 +127,3 @@ export function weighMatrix(
): number[][] {
return input.map((item) => weighArray((rowModifier ?? ((row) => row))(item), weight));
}

export function normalizeArray(input: number[]): number[] {
const result = fullCopyObject(input);

if (result.length === 0) {
return result;
}

const min = Math.min(...result);
const max = Math.max(...result);

if (isEqual(min, max)) {
return isEqual(min, 0) ? result.map(() => 0) : result.map(() => 1);
}

return result.map((x) => (x - min) / (max - min));
}

export function normalizeMatrixColumns(input: number[][]): number[][] {
const result = fullCopyObject(input);

if (result.length === 0) {
return result;
}

for (let i = 0; i < result[0].length; i++) {
const columnNormalized = normalizeArray(result.map((row) => row[i]));
for (let j = 0; j < result.length; j++) {
result[j][i] = columnNormalized[j];
}
}

return result;
}

0 comments on commit c14c7e7

Please sign in to comment.