Improved Horner class, added unit test

This commit is contained in:
Filip Znachor 2022-10-04 21:32:42 +02:00
parent dd5c896cb4
commit d45f041551
7 changed files with 3372 additions and 8 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

19
__tests__/horner.ts Normal file
View file

@ -0,0 +1,19 @@
import { Horner } from "../horner";
let h = new Horner([2, 17, -11, -153, 29, 304, 52, -96]);
test('Horner.roots', () => {
expect(Horner.roots(h)[0]).toEqual([-1, -1, 2, 2, -3, -8]);
});
test('Horner.functional_value(1)', () => {
expect(h.functional_value(1)).toEqual([2, 19, 8, -145, -116, 188, 240, 144]);
});
test('Horner.functional_value(-3)', () => {
expect(h.functional_value(-3)).toEqual([2, 11, -44, -21, 92, 28, -32, 0]);
});
test('Horner.candidates', () => {
expect(h.candidates()).toEqual([1, -1, 2, -2, 3, -3, 4, -4, 6, -6, 8, -8, 12, -12, 16, -16, 24, -24, 32, -32, 48, -48, 96, -96]);
});

6
babel.config.js Normal file
View file

@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript'
]
};

View file

@ -1,4 +1,4 @@
class Horner {
export class Horner {
constants: number[];
@ -7,13 +7,13 @@ class Horner {
}
candidates() {
// Not effective solution
let max = Math.abs(this.constants[0]) + Math.abs(this.constants[this.constants.length - 1]);
let max = Math.abs(this.constants[this.constants.length - 1]);
let min = 1;
let candidates: number[] = [];
for(let i = min; i<=max; i++) {
candidates.push(i);
candidates.push(-i);
if(max % i == 0) {
candidates.push(i); candidates.push(-i);
}
}
return candidates;
}
@ -42,7 +42,6 @@ class Horner {
let [new_polynom, root] = r;
roots.push(root);
new_polynom.pop();
if(new_polynom.length < 3) return [roots, new_polynom];
horner = new Horner(new_polynom);
}
}

View file

@ -1,7 +1,7 @@
type RawMatrix = number[][];
export type RawMatrix = number[][];
class Matrix {
export class Matrix {
rows: number;
cols: number;

9
package.json Normal file
View file

@ -0,0 +1,9 @@
{
"devDependencies": {
"@babel/core": "^7.19.3",
"@babel/preset-env": "^7.19.3",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^29.1.1",
"jest": "^29.1.2"
}
}

3330
pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load diff