Skip to content

Commit

Permalink
Reformat code (bits-and-electrons#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
saich08 authored Jun 12, 2021
1 parent d56cf4b commit 391795b
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/events/toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {

import {
Complex
} from "./../mathutils.js";
} from "./../math/complex.js";

import {
QuantumGate
Expand Down
2 changes: 1 addition & 1 deletion src/geometry/composite_shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

import {
Vector3Helpers
} from "../mathutils.js";
} from "../math/vector3_helpers.js";


class Axis extends BaseGroup {
Expand Down
25 changes: 25 additions & 0 deletions src/math/complex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Float
} from "./float.js";


class Complex {
constructor(real, img) {
this.real = Float.round(real);
this.img = Float.round(img);
}

toString() {
if (this.img < 0) {
return this.real.toString() + " - i * " + Float.abs(this.img).toString();
}
else {
return this.real.toString() + " + i * " + Float.abs(this.img).toString();
}
}
}


export {
Complex
}
22 changes: 22 additions & 0 deletions src/math/complex.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
Complex
} from "./complex.js";


test("complex-test1", () => {
expect(
new Complex(2, 3).toString()
).toBe("+2.0000 + i * 3.0000");
});

test("complex-test2", () => {
expect(
new Complex(3, -4).toString()
).toBe("+3.0000 - i * 4.0000");
});

test("complex-test3", () => {
expect(
new Complex(-6, -7).toString()
).toBe("-6.0000 - i * 7.0000");
});
51 changes: 51 additions & 0 deletions src/math/float.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
PRECISION
} from "./../constants.js";


class Float {
// returns floating point number with given precision
// adds sign as prefix for both positive and negative numbers
static round(num, precision) {
if (precision == null) {
precision = PRECISION;
}

if (typeof num === "string") {
num = parseFloat(num);
}

let result = parseFloat(num.toFixed(precision)).toFixed(precision);

if (result >= 0) {
result = "+" + result;
}

return result;
}

// returns absolute value of number with given precision
// removes sign from prefix for both positive and negative numbers
static abs(num, precision) {
if (precision == null) {
precision = PRECISION;
}

if (typeof num === "string") {
num = parseFloat(num);
}

if (num < 0) {
num *= -1;
}

let result = parseFloat(num.toFixed(precision)).toFixed(precision);

return result;
}
}


export {
Float
}
24 changes: 24 additions & 0 deletions src/math/float.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
Float
} from "./float.js";


test("float-test1", () => {
expect(
Float.round("2.423451", 4)
).toBe("+2.4235");

expect(
Float.abs("2.423451", 4)
).toBe("2.4235");
});

test("float-test2", () => {
expect(
Float.round("2.42", 4)
).toBe("+2.4200");

expect(
Float.abs("-2.423451", 4)
).toBe("2.4235");
});
25 changes: 25 additions & 0 deletions src/math/vector3_helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as THREE from './../libs/three/three.module.js';

import {
Float
} from "./float.js";


class Vector3Helpers {
static angleBetweenVectors(vector1, vector2, planeNormal) {
let angle = THREE.Math.radToDeg(vector1.angleTo(vector2));
let crossProduct = new THREE.Vector3();

crossProduct.crossVectors(vector1, vector2);
if (Float.round(crossProduct.dot(planeNormal)) < 0) {
angle *= -1;
}

return angle;
}
}


export {
Vector3Helpers
}
13 changes: 3 additions & 10 deletions src/mathutils.test.js → src/math/vector3_helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import {
Complex,
Vector3Helpers
} from "./mathutils.js";
} from "./vector3_helpers.js";

import {
import {
CartesianAxes
} from "./geometry/composite_shapes.js";

} from "./../geometry/composite_shapes.js";

test("complex-basic", () => {
expect(
new Complex(2, 3).toString()
).toBe("+2.0000 + i * 3.0000");
});

test("vector3-helpers-angle-between-vectors-basic", () => {
expect(
Expand Down
84 changes: 0 additions & 84 deletions src/mathutils.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/quantum/bloch_sphere.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as THREE from "../libs/three/three.module.js";

import {
Float
} from "../mathutils.js";
} from "../math/float.js";

import {
BlochSphere
Expand Down
7 changes: 5 additions & 2 deletions src/quantum/bloch_sphere_state.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as THREE from '../libs/three/three.module.js';

import {
Float,
Float
} from "../math/float.js";

import {
Complex
} from "../mathutils.js";
} from "../math/complex.js";


class BlochSphereState {
Expand Down
2 changes: 1 addition & 1 deletion src/quantum/bloch_sphere_state.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Complex
} from "../mathutils.js";
} from "../math/complex.js";

import {
BlochSphereState
Expand Down

0 comments on commit 391795b

Please sign in to comment.