Skip to content

Commit

Permalink
fix etchlonForm
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanKojiCheslin committed Jun 3, 2018
1 parent 34d47e0 commit d0d3481
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
26 changes: 9 additions & 17 deletions changeToEtchlonForm.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import {largestAbsoluteMovedToTopOfColumn} from './largestAbsoluteMovedToTopOfColumn'
import {zeroAllRowsUnderThePivot} from './zeroAllRowsUnderThePivot'
import {scalePivotToOne} from './scalePivotToOne'
import {pivotValueIsOne} from './pivotValueIsOne'
import {largestAbsoluteMovedToTopOfColumn} from './largestAbsoluteMovedToTopOfColumn';
import {zeroAllRowsUnderThePivot} from './zeroAllRowsUnderThePivot';
import {scalePivotToOne} from './scalePivotToOne';
import {findNextPivot} from './findNextPivot';

export function changeToEtchlonForm(system){
let newSystem = { ...system };
console.log("pivotValueIsOne");
console.log(pivotValueIsOne);
const valueIsOne = pivotValueIsOne(newSystem);
if ( ! valueIsOne) {
newSystem = scalePivotToOne(newSystem);
}
if (newSystem.s.length - 1 == newSystem.pivot.row) {
return newSystem
const lastSystem = scalePivotToOne(newSystem);
return lastSystem;
}else {
const systemLargestTop = largestAbsoluteMovedToTopOfColumn(newSystem);
const zeroedUnderPivot = zeroAllRowsUnderThePivot(systemLargestTop);
const { pivot, ...zeroedUnderPivotPostIteration } = zeroedUnderPivot;
zeroedUnderPivotPostIteration.pivot = {
row : zeroedUnderPivot.pivot.row + 1,
column : zeroedUnderPivot.pivot.column + 1
};
return changeToEtchlonForm(zeroedUnderPivotPostIteration);
const scaled = scalePivotToOne(zeroedUnderPivot);
const nextSystem = findNextPivot(scaled);
return changeToEtchlonForm(nextSystem);
}
}
8 changes: 8 additions & 0 deletions findNextPivot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function findNextPivot(system) {
const { pivot, ...nextSystem } = system;
nextSystem.pivot = {
row : pivot.row + 1,
column : pivot.column + 1
};
return nextSystem;
}
7 changes: 5 additions & 2 deletions scalePivotToOne.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {getRowScaledBy} from './getRowScaledBy'
import {pivotValueIsOne} from './pivotValueIsOne'

export function scalePivotToOne(system){
// a = ((a/b) * b)
let newSystem = { ...system};
const pivotValue = Number(newSystem.s[system.pivot.row][newSystem.pivot.column]);
const pivotIsOne = pivotValueIsOne(newSystem);
if ( ! pivotIsOne ) {
const deleteCount = 1;
const rowNumber = newSystem.pivot.row;
const pivotValue = Number(newSystem.s[system.pivot.row][newSystem.pivot.column]);
if (pivotValue != 1) {
const scale = 1 / pivotValue;
const rowPivotScaledToOne = getRowScaledBy(newSystem.s[rowNumber], scale);
newSystem.s.splice(rowNumber, deleteCount, rowPivotScaledToOne);
Expand Down
97 changes: 50 additions & 47 deletions spec/spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import {changeToEtchlonForm} from '../changeToEtchlonForm';
import {findPivotColumn} from '../findPivotColumn';
import {initializePivot} from '../initializePivot';
import {largestAbsoluteMovedToTopOfColumn} from '../largestAbsoluteMovedToTopOfColumn';
import {zeroAllRowsUnderThePivot} from '../zeroAllRowsUnderThePivot';
import {changeToEtchlonForm} from '../changeToEtchlonForm';
import {pivotValueIsOne} from '../pivotValueIsOne';
import {zeroAllRowsUnderThePivot} from '../zeroAllRowsUnderThePivot';
import sinon from 'sinon';

//change order of imports / tests to match file sturcture of package
//findPivotColumn change to findFirstPivotColumn
// largestAbsoluteMovedToTopOfColumn change to moveLargestToTopOfPivotColumn

describe('changeToEtchlonForm', () => {
it('return expected value', () => {
let inputSystem = {
s : [
[3,3,3],
[4,0,2]
],
pivot : {
row : 0,
column : 0
}
}
const expectedOutput = {
s: [
[ 1, 0, 0.5 ],
[ 0, 1, 0.5 ]
],
pivot: {
row: 1,
column: 1
}
}
let outputSystem = changeToEtchlonForm(inputSystem);
console.log("outputSystem");
console.log(outputSystem);
expect(outputSystem).toEqual(expectedOutput);
});
});

describe('findPivotColumn', () => {

Expand Down Expand Up @@ -129,6 +159,23 @@ describe("largestAbsoluteMovedToTopOfColumn", () => {
});
});

describe('pivotValueIsOne', () => {
it('return expected value', () => {
let inputSystem = {
s : [
[3,3,3],
[4,0,2]
],
pivot : {
row : 0,
column : 0
}
};
let output = pivotValueIsOne(inputSystem);
expect(output).toEqual(false);
});
});

describe("zeroAllRowsUnderThePivot", () => {
it('returns correct value', () => {
let inputSystem = {
Expand All @@ -152,47 +199,3 @@ describe("zeroAllRowsUnderThePivot", () => {
);
});
});

describe('changeToEtchlonForm', () => {
it('return expected value', () => {
let inputSystem = {
s : [
[3,3,3],
[4,0,2]
],
pivot : {
row : 0,
column : 0
}
}
const expectedOutput = {
s: [
[ 4, 0, 2 ],
[ 0, 3, 1.5 ]
],
pivot: {
row: 1,
column: 1
}
}
let outputSystem = changeToEtchlonForm(inputSystem);
expect(outputSystem).toEqual(expectedOutput);
});
});

describe('pivotValueIsOne', () => {
it('return expected value', () => {
let inputSystem = {
s : [
[3,3,3],
[4,0,2]
],
pivot : {
row : 0,
column : 0
}
};
let output = pivotValueIsOne(inputSystem);
expect(output).toEqual(false);
});
});

0 comments on commit d0d3481

Please sign in to comment.