Socket
Socket
Sign inDemoInstall

2d-algebra

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.1 to 3.0.0

30

CHANGELOG.md

@@ -9,38 +9,64 @@ # Changelog

## [Unreleased]
https://github.com/abersnaze/2d-algebra/compare/v1.0.2...HEAD
### Added
- _nothing_
### Changed
- _nothing_
### Removed
- _nothing_
## [1.0.2] - 2020-01-01
https://github.com/abersnaze/2d-algebra/compare/compare/v1.0.1...v1.0.2
### Added
- _nothing_
### Changed
- Major upgraded the dev dependencies.
### Removed
- _nothing_
## [1.0.1] - 2019-12-01
https://github.com/abersnaze/2d-algebra/compare/compare/v1.0.0...v1.0.1
### Added
- This CHANGELOG file.
### Changed
- Updates to README file.
- Upgraded the dependencies.
### Removed
- _nothing_
## [1.0.0] - 2019-12-01
https://github.com/abersnaze/2d-algebra/compare/releases/tag/v1.0.0
### Added
- The rest of the owl
### Changed
- _nothing_
### Removed
- _nothing_

@@ -15,4 +15,7 @@ import { ExpressionStack } from "./ExpressionStack";

sin(): Expression;
sin(b: Term): ExpressionStack<Expression>;
cos(): Expression;
cos(b: Term): ExpressionStack<Expression>;
tan(): Expression;
tan(b: Term): ExpressionStack<Expression>;
eq(b: Term): Expression;

@@ -19,0 +22,0 @@ push(b: Term): ExpressionStack<Expression>;

15

lib/Expression.js

@@ -30,9 +30,18 @@ "use strict";

}
sin() {
sin(b) {
if (b !== undefined) {
return this.push(b).sin();
}
return new Expression((0, index_1.sin)(this.a));
}
cos() {
cos(b) {
if (b !== undefined) {
return this.push(b).cos();
}
return new Expression((0, index_1.cos)(this.a));
}
tan() {
tan(b) {
if (b !== undefined) {
return this.push(b).tan();
}
return new Expression((0, index_1.tan)(this.a));

@@ -39,0 +48,0 @@ }

@@ -21,6 +21,9 @@ import { Expression } from "./Expression";

sin(): ExpressionStack<N>;
sin(b: Term): ExpressionStack<ExpressionStack<N>>;
cos(): ExpressionStack<N>;
cos(b: Term): ExpressionStack<ExpressionStack<N>>;
tan(): ExpressionStack<N>;
tan(b: Term): ExpressionStack<ExpressionStack<N>>;
push(b: Term): ExpressionStack<ExpressionStack<N>>;
}
export {};

@@ -6,2 +6,3 @@ "use strict";

const index_1 = require("./node/index");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function stack(op, parent, b, c) {

@@ -18,2 +19,3 @@ if (c !== undefined) {

}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
class ExpressionStack {

@@ -48,9 +50,18 @@ constructor(parent, b) {

}
sin() {
sin(b) {
if (b !== undefined) {
return this.push(b).sin();
}
return new ExpressionStack(this.parent, (0, index_1.sin)(this.b));
}
cos() {
cos(b) {
if (b !== undefined) {
return this.push(b).cos();
}
return new ExpressionStack(this.parent, (0, index_1.cos)(this.b));
}
tan() {
tan(b) {
if (b !== undefined) {
return this.push(b).tan();
}
return new ExpressionStack(this.parent, (0, index_1.tan)(this.b));

@@ -57,0 +68,0 @@ }

@@ -6,3 +6,3 @@ "use strict";

constructor() {
this.precedence = ['^', '*', '+'];
this.precedence = ["^", "*", "+"];
}

@@ -9,0 +9,0 @@ binary(indent, op, a, b) {

@@ -6,5 +6,11 @@ "use strict";

binary(indent, op, a, b) {
return op +
"\n" + indent + "├ " + a.toString(indent + "│ ", this) +
"\n" + indent + "└ " + b.toString(indent + " ", this);
return (op +
"\n" +
indent +
"├ " +
a.toString(indent + "│ ", this) +
"\n" +
indent +
"└ " +
b.toString(indent + " ", this));
}

@@ -11,0 +17,0 @@ func(indent, func, arg) {

import { Expression } from "./Expression";
import { Identifier } from "./node";
export default function expression(a: number | Identifier): Expression;
import { Term } from "./node";
export declare function expression(a: Term): Expression;
export * from "./Expression";
export * from "./ExpressionStack";
export { Term } from "./node";

@@ -13,2 +13,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.expression = void 0;
const Expression_1 = require("./Expression");

@@ -19,5 +20,5 @@ const node_1 = require("./node");

}
exports.default = expression;
exports.expression = expression;
__exportStar(require("./Expression"), exports);
__exportStar(require("./ExpressionStack"), exports);
//# sourceMappingURL=index.js.map

@@ -12,3 +12,3 @@ import { Assignments, Substitutions } from "../Expression";

derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]> | undefined;
degree(): [INode, number][] | undefined;
coefficient(): [number, INode];

@@ -15,0 +15,0 @@ exponent(): [number, INode];

@@ -1,4 +0,2 @@

import { Assignments, Substitutions } from "../Expression";
import { InlineFormat } from "../format/InlineFormat";
import { Identifier, INode } from "./index";
import { INode } from "./index";
export declare class Constant implements INode {

@@ -8,10 +6,10 @@ readonly n: number;

op(): string;
eval(assign: Assignments): number;
apply(subs: Substitutions): INode;
derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]>;
eval(): number;
apply(): INode;
derivative(): INode;
degree(): [INode, number][];
coefficient(): [number, INode];
exponent(): [number, INode];
toString(indent?: string, fmt?: InlineFormat): string;
toString(): string;
equals(that: INode): boolean;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Constant = void 0;
const InlineFormat_1 = require("../format/InlineFormat");
const index_1 = require("./index");

@@ -13,9 +12,9 @@ class Constant {

}
eval(assign) {
eval() {
return this.n;
}
apply(subs) {
apply() {
return this;
}
derivative(withRespectTo) {
derivative() {
return (0, index_1.value)(0);

@@ -32,3 +31,3 @@ }

}
toString(indent = "", fmt = new InlineFormat_1.InlineFormat()) {
toString() {
return this.n.toString();

@@ -35,0 +34,0 @@ }

@@ -11,3 +11,3 @@ import { Assignments, Substitutions } from "../Expression";

derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]>;
degree(): [INode, number][];
coefficient(): [number, INode];

@@ -14,0 +14,0 @@ exponent(): [number, INode];

@@ -23,3 +23,6 @@ "use strict";

degree() {
return [[this, 1], [index_1.degreeSum, 1]];
return [
[this, 1],
[index_1.degreeSum, 1],
];
}

@@ -26,0 +29,0 @@ coefficient() {

@@ -14,3 +14,3 @@ import { Assignments, Expression, Substitutions } from "../Expression";

derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]> | undefined;
degree(): [INode, number][] | undefined;
coefficient(): [number, INode];

@@ -17,0 +17,0 @@ exponent(): [number, INode];

@@ -110,8 +110,8 @@ "use strict";

}
if (cmp === 0) {
const [aExp, aTerm] = a.exponent();
const [bExp] = b.a.exponent();
if (a instanceof Constant_1.Constant) {
return mult(value(a.n * b.a.n), b.b);
}
if (a instanceof Constant_1.Constant && b.a instanceof Constant_1.Constant) {
return mult(value(a.n * b.a.n), b.b);
}
const [aExp, aTerm] = a.exponent();
const [bExp, bTerm] = b.a.exponent();
if (aTerm.equals(bTerm)) {
return mult(pow(aTerm, aExp + bExp), b.b);

@@ -125,8 +125,8 @@ }

}
if (cmp === 0) {
const [aExp, aTerm] = a.exponent();
const [bExp] = b.exponent();
if (a instanceof Constant_1.Constant) {
return value(a.n * b.n);
}
if (a instanceof Constant_1.Constant && b instanceof Constant_1.Constant) {
return value(a.n * b.n);
}
const [aExp, aTerm] = a.exponent();
const [bExp, bTerm] = b.exponent();
if (aTerm.equals(bTerm)) {
return pow(aTerm, aExp + bExp);

@@ -175,3 +175,3 @@ }

if (a instanceof Power_1.Power) {
return pow(a.a, Math.pow(a.b, b));
return pow(a.a, a.b * b);
}

@@ -236,4 +236,4 @@ return new Power_1.Power(a, b);

}
const aTotal = aDegrees.find(([aExp, aDegree]) => aExp.equals(exports.degreeSum))[1];
const bTotal = bDegrees.find(([bExp, bDegree]) => bExp.equals(exports.degreeSum))[1];
const aTotal = aDegrees.find(([aExp]) => aExp.equals(exports.degreeSum))[1];
const bTotal = bDegrees.find(([bExp]) => bExp.equals(exports.degreeSum))[1];
// Math.abs allow x and 1/x to sort to the same place to be canceled out

@@ -244,4 +244,4 @@ if (Math.abs(aTotal) !== Math.abs(bTotal)) {

// create a set of expresions the hard way.
const exps = aDegrees.map(([aExp, aDegree]) => aExp);
bDegrees.forEach(([bExp, bDegree]) => {
const exps = aDegrees.map(([aExp]) => aExp);
bDegrees.forEach(([bExp]) => {
if (exps.findIndex((exp) => exp.equals(bExp)) === -1) {

@@ -251,6 +251,6 @@ exps.push(bExp);

});
exps.sort((a, b) => a.toString().localeCompare(b.toString()));
exps.sort((aExp, bExp) => aExp.toString().localeCompare(bExp.toString()));
for (const exp of exps) {
const aExpDegree = aDegrees.find(([aExp, aDegree]) => exp.equals(aExp));
const bExpDegree = bDegrees.find(([bExp, bDegree]) => exp.equals(bExp));
const aExpDegree = aDegrees.find(([aExp]) => exp.equals(aExp));
const bExpDegree = bDegrees.find(([bExp]) => exp.equals(bExp));
if (aExpDegree === undefined)

@@ -263,3 +263,2 @@ return 0 - 1;

}
;
return 0;

@@ -266,0 +265,0 @@ }

@@ -12,3 +12,3 @@ import { Assignments, Substitutions } from "../Expression";

derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]> | undefined;
degree(): [INode, number][] | undefined;
coefficient(): [number, INode];

@@ -15,0 +15,0 @@ exponent(): [number, INode];

@@ -35,3 +35,3 @@ "use strict";

bDegrees.forEach(([bExp, bDegree]) => {
const index = degrees.findIndex(([exp, degree]) => exp.equals(bExp));
const index = degrees.findIndex(([exp]) => exp.equals(bExp));
if (index === -1) {

@@ -41,3 +41,3 @@ degrees.push([bExp, bDegree]);

else {
degrees[index][1] + bDegree;
degrees[index][1] += bDegree;
}

@@ -44,0 +44,0 @@ });

@@ -12,3 +12,3 @@ import { Assignments, Substitutions } from "../Expression";

derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]> | undefined;
degree(): [INode, number][] | undefined;
coefficient(): [number, INode];

@@ -15,0 +15,0 @@ exponent(): [number, INode];

@@ -31,3 +31,3 @@ "use strict";

}
return degrees.map(([exp, degree]) => [exp, degree * this.b]);
return degrees.map(([exp, degree]) => [exp, Math.abs(degree * this.b)]);
}

@@ -34,0 +34,0 @@ coefficient() {

@@ -11,3 +11,3 @@ import { Assignments, Substitutions } from "../Expression";

derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]>;
degree(): [INode, number][];
coefficient(): [number, INode];

@@ -14,0 +14,0 @@ exponent(): [number, INode];

@@ -23,3 +23,6 @@ "use strict";

degree() {
return [[this, 1], [index_1.degreeSum, 1]];
return [
[this, 1],
[index_1.degreeSum, 1],
];
}

@@ -26,0 +29,0 @@ coefficient() {

import { Assignments, Substitutions } from "../Expression";
import { InlineFormat } from "../format/InlineFormat";
import { Identifier, INode } from "./index";

@@ -13,7 +12,7 @@ export declare class Variable implements INode {

derivative(withRespectTo: Identifier): INode;
degree(): Array<[INode, number]>;
degree(): [INode, number][];
coefficient(): [number, INode];
exponent(): [number, INode];
toString(indent?: string, fmt?: InlineFormat): string;
toString(): string;
equals(that: INode): boolean;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Variable = void 0;
const InlineFormat_1 = require("../format/InlineFormat");
const index_1 = require("./index");

@@ -9,4 +8,5 @@ class Variable {

this.a = a;
if (typeof a === 'symbol')
this.description = this.a.description || ("x" + Variable.idSequence++);
if (typeof a === "symbol")
this.description =
this.a["description"] || "x" + Variable.idSequence++;
else

@@ -33,6 +33,9 @@ this.description = a;

derivative(withRespectTo) {
return (withRespectTo === this.a) ? (0, index_1.value)(1) : (0, index_1.value)(0);
return withRespectTo === this.a ? (0, index_1.value)(1) : (0, index_1.value)(0);
}
degree() {
return [[this, 1], [index_1.degreeSum, 1]];
return [
[this, 1],
[index_1.degreeSum, 1],
];
}

@@ -45,3 +48,3 @@ coefficient() {

}
toString(indent = "", fmt = new InlineFormat_1.InlineFormat()) {
toString() {
return this.description;

@@ -48,0 +51,0 @@ }

{
"name": "2d-algebra",
"version": "2.0.1",
"version": "3.0.0",
"description": "Library for building expressions and computing derivatives",

@@ -14,3 +14,5 @@ "repository": "github:abersnaze/2d-algebra",

"watch": "yarn test -w",
"compile": "tsc"
"compile": "tsc",
"lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
"format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
},

@@ -28,8 +30,12 @@ "keywords": [

"@types/mocha": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"chai": "^4.2.0",
"eslint": "^8.6.0",
"mocha": "^9",
"nyc": "^15.1.0",
"prettier": "^2.5.1",
"ts-node": "^10.4.0",
"typescript": "^4.0.3"
"typescript": "^4.5.4"
}
}

@@ -9,8 +9,11 @@ # 2D Algebra Typescript Module

## Technologies
Project is created with:
* Typescript version: 3.6.2
* Node version: 12.10.0
* No external dependencies
- Typescript version: 3.6.2
- Node version: 12.10.0
- No external dependencies
## Setup
To use this library

@@ -32,5 +35,8 @@

const solution = new Map([[x, 7483], [y, 22453]]);
const solution = new Map([
[x, 7483],
[y, 22453],
]);
const err = line.eval(solution)
const err = line.eval(solution);
// err === 0

@@ -54,4 +60,5 @@

// https://en.wikipedia.org/wiki/Second_partial_derivative_test
const dxdyLine = dxLine.derivative(y);
const hessianDet = dx2Line.times(dy2Line).minus(dxdyLine.squared());
const hessianDet = dx2Line.times(dy2Line).minus(dxdyLine.squared());
const xySaddle = hessianDet.eval(solution);

@@ -71,23 +78,23 @@ // xySaddle === 0

| Method | Description |
|--------------|-----------------------------------------------|
| plus(b) | add the top term to `b` and simplifies |
| minus(b) | equivalent to `plus(-b)` |
| times(b) | multiplies the top term with b and simplifies |
| dividedBy(b) | equivalent to `times(b^-1)` |
| toThe(n) | raises the top term by the `number` n. |
| squared() | equivalent to `toThe(2)` |
| sin() | replaces the top term with the sin(a) |
| cos() | replaces the top term with the cos(a) |
| tan() | equivalent to `sin(a).dividedBy(cos(a))` |
| eq(b) | equivalent to `minus(b).squared()` |
| Method | Description |
| ------------ | ---------------------------------------------------- |
| plus(b) | add the top term to `b` and simplifies |
| minus(b) | equivalent to `plus(-b)` |
| times(b) | multiplies the top term with b and simplifies |
| dividedBy(b) | equivalent to `push(b).toThe(-1).times()` |
| toThe(n) | raises the top term by the `number` n. |
| squared() | equivalent to `toThe(2)` |
| sin() | replaces the top term with the sine |
| cos() | replaces the top term with the cossine |
| tan() | equivalent to `this.sin().push(this).cos().divide()` |
| eq(b) | equivalent to `minus(b).squared()` |
Once the expression is complete you can use the following methods
| Method | Description |
|---------------------------|-----------------------------------------------|
| eval(Map<symbol, number>) | fully evaluate the expression. throw error if not all of the symbols are defined. |
| apply(Map<symbol, Expression>) | substitute one or more variables with expressions and return the new expression. |
| derivative(symbol) | compute the partial derivative with respect to one symbol. |
| toString() | makes a ASCII art tree diagram of the expression tree. |
| Method | Description |
| ------------------------------ | --------------------------------------------------------------------------------- |
| eval(Map<symbol, number>) | fully evaluate the expression. throw error if not all of the symbols are defined. |
| apply(Map<symbol, Expression>) | substitute one or more variables with expressions and return the new expression. |
| derivative(symbol) | compute the partial derivative with respect to one symbol. |
| toString() | makes a ASCII art tree diagram of the expression tree. |

@@ -105,6 +112,6 @@ ### Why no parentheses? `(` or `)`

const circle = expression(x)
.squared() // x^2
.plus(y) // x^2 + y
.squared() // (x^2 + y)^2
.eq(r) // (x^2 + y)^2 - r)^2
.squared() // x^2
.plus(y) // x^2 + y
.squared() // (x^2 + y)^2
.eq(r) // (x^2 + y)^2 - r)^2
.squared(); // ((x^2 + y)^2 - r)^2)^2

@@ -119,9 +126,9 @@ ```

const circle = expression(x)
.squared() // x^2
.push(y) // x^2 | y <---- y here is separate from x^2
.squared() // x^2 | y^2 <---- now that y is squared on its own
.plus() // x^2 + y^2 <---- merge y^2 by adding it to x^2
.push(r) // x^2 + y^2 | r
.squared() // x^2 + y^2 | r^2
.eq(); // (x^2 + y^2 - r^2)^2
.squared() // x^2
.push(y) // x^2 | y <---- y here is separate from x^2
.squared() // x^2 | y^2 <---- now that y is squared on its own
.plus() // x^2 + y^2 <---- merge y^2 by adding it to x^2
.push(r) // x^2 + y^2 | r
.squared() // x^2 + y^2 | r^2
.eq(); // (x^2 + y^2 - r^2)^2
```

@@ -135,4 +142,4 @@

2. make changes to the tests and source.
* If making changes to the `Expression` class make sure matching changes are made to `ExpressionStack`.
* Changes to simplification logic can be quite tricky with all the symbiotic recursion.
- If making changes to the `Expression` class make sure matching changes are made to `ExpressionStack`.
- Changes to simplification logic can be quite tricky with all the symbiotic recursion.
3. run `yarn test`. if they fail goto step 2

@@ -144,7 +151,8 @@ 4. push changes to your fork

* `yarn compile`: compile the typescript code to POJS
* `yarn test`: run unit tests once.
* `yarn watch`: continuously run unit tests.
- `yarn compile`: compile the typescript code to POJS
- `yarn test`: run unit tests once.
- `yarn watch`: continuously run unit tests.
<!-- Markdown link & img dfn's -->
[npm-image]: https://img.shields.io/npm/v/2d-algebra.svg?style=flat-square

@@ -151,0 +159,0 @@ [npm-url]: https://npmjs.org/package/2d-algebra

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc