2d-algebra 中文文档教程
2D Algebra Typescript Module
一个用于以编程方式建立用于数值分析的大型方程组的库。
Technologies
Project is created with:
- Typescript version: 3.6.2
- Node version: 12.10.0
- No external dependencies
Setup
要使用此库
npm install 2d-algebra
yarn add 2d-algebra
然后在您的代码中,您可以导入并使用 expression(...)
函数来流畅地构建表达式。
import expression from "2d-algebra";
const m = 3; // slope
const b = 4; // point
const x = Symbol("x");
const y = Symbol(); // naming your symbols is optional
const line = expression(m).times(x).plus(b).eq(y);
const solution = new Map([
[x, 7483],
[y, 22453],
]);
const err = line.eval(solution);
// err === 0
const dxLine = line.derivative(x);
const xSlope = dxLine.eval(solution);
// xSlope === 0
const dyLine = line.derivative(y);
const ySlope = dyLine.eval(solution);
// ySlope === 0
const dx2Line = dxLine.derivative(x);
const xCup = dx2Line.eval(solution);
// xCup > 0
const dy2Line = dyLine.derivative(y);
const yCup = dx2Line.eval(solution);
// yCup > 0
// https://en.wikipedia.org/wiki/Second_partial_derivative_test
const dxdyLine = dxLine.derivative(y);
const hessianDet = dx2Line.times(dy2Line).minus(dxdyLine.squared());
const xySaddle = hessianDet.eval(solution);
// xySaddle === 0
API
创建一个新的 Expression
很容易,只需从第一个 symbol
或 number
开始。
const one = expression(1).eval(new Map());
从那里您可以使用以下方法来增加复杂性。 所有方法都不会更改现有的 Expression,而是返回一个新的 Expression(也称为不可变)。 b
参数必须是 symbol
、number
或 Expression
。
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() |
abs() | replaces the top term with the absolution value |
表达式完成后,您可以使用以下方法
Method | Description |
---|---|
eval(Map | fully evaluate the expression. throw error if not all of the symbols are defined. |
apply(Map | substitute one or more variables with different term 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. |
Why no parentheses? (
or )
此时您可能遇到了一个表达式,您只想将下一个 times
或 squared
应用于仅一部分之前发生的事情。 例如,单位(半径为 1)圆可能会错误地将其定义为:
const r = 1;
const x = Symbol();
const y = Symbol();
// EXAMPLE OF HOW TO DO IT WRONG
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 + y)^2 - r)^2)^2
会产生 ((x^2 + y)^2 - r)^2)^2
。 当我期望 (x^2 + y^2 - r^2)^2
时。 请注意,在错误的表达式中,squared()
的每个应用程序如何应用于到该点定义的整个表达式。 为了解决这个问题,我将引入 push(b)
方法,该方法启动一个新的迷你表达式,与目前定义的内容分开。 当使用 push
时,plus()
、minus()
、times()
、< code>divide() 和 eq()
可用于使两个迷你表达式再次合并为一个。
更正后的代码现在如下所示:
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
Contributing
提交对项目的更改
- fork and clone the git repository
- make changes to the tests and source.
- If making changes to the
Expression
class make sure matching changes are made toExpressionStack
. - Changes to simplification logic can be quite tricky with all the symbiotic recursion.
- run
yarn test
. if they fail goto step 2 - push changes to your fork
- submit pull request
Other ussful commands
yarn compile
: compile the typescript code to POJSyarn test
: run unit tests once.yarn watch
: continuously run unit tests.
2D Algebra Typescript Module
A library for programatically building up large systems of equations for numerical analysis.
Technologies
Project is created with:
- Typescript version: 3.6.2
- Node version: 12.10.0
- No external dependencies
Setup
To use this library
npm install 2d-algebra
yarn add 2d-algebra
Then in your code you can import and use the expression(...)
function to fluently build expressions.
import expression from "2d-algebra";
const m = 3; // slope
const b = 4; // point
const x = Symbol("x");
const y = Symbol(); // naming your symbols is optional
const line = expression(m).times(x).plus(b).eq(y);
const solution = new Map([
[x, 7483],
[y, 22453],
]);
const err = line.eval(solution);
// err === 0
const dxLine = line.derivative(x);
const xSlope = dxLine.eval(solution);
// xSlope === 0
const dyLine = line.derivative(y);
const ySlope = dyLine.eval(solution);
// ySlope === 0
const dx2Line = dxLine.derivative(x);
const xCup = dx2Line.eval(solution);
// xCup > 0
const dy2Line = dyLine.derivative(y);
const yCup = dx2Line.eval(solution);
// yCup > 0
// https://en.wikipedia.org/wiki/Second_partial_derivative_test
const dxdyLine = dxLine.derivative(y);
const hessianDet = dx2Line.times(dy2Line).minus(dxdyLine.squared());
const xySaddle = hessianDet.eval(solution);
// xySaddle === 0
API
Creating a new Expression
is a easy as starting it off with the first symbol
or number
.
const one = expression(1).eval(new Map());
From there you can use the following methods to additional complexity. All methods do not change the existing Expression but return a new Expression (AKA immutable). The b
argument must be either a symbol
, number
or Expression
.
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() |
abs() | replaces the top term with the absolution value |
Once the expression is complete you can use the following methods
Method | Description |
---|---|
eval(Map | fully evaluate the expression. throw error if not all of the symbols are defined. |
apply(Map | substitute one or more variables with different term 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. |
Why no parentheses? (
or )
At this point you've probably run into an expression where you only want to apply the next times
or squared
to only part of what comes before. For example the unit (of radius 1) circle one might mistakenly define it as:
const r = 1;
const x = Symbol();
const y = Symbol();
// EXAMPLE OF HOW TO DO IT WRONG
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 + y)^2 - r)^2)^2
Would produce ((x^2 + y)^2 - r)^2)^2
. When I would have expected (x^2 + y^2 - r^2)^2
. Notice how in the wrong expression each application of the squared()
applied to the whole of expression defined up to that point. To fix this I'll introduce the push(b)
method that starts a new mini expression separate from what has been defined so far. When push
is used new zero argument versions of plus()
, minus()
, times()
, divide()
, and eq()
are available to cause the two mini expressions to be merged into one again.
The corrected code now looks like:
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
Contributing
To submit changes to the project
- fork and clone the git repository
- make changes to the tests and source.
- If making changes to the
Expression
class make sure matching changes are made toExpressionStack
. - Changes to simplification logic can be quite tricky with all the symbiotic recursion.
- run
yarn test
. if they fail goto step 2 - push changes to your fork
- submit pull request
Other ussful commands
yarn compile
: compile the typescript code to POJSyarn test
: run unit tests once.yarn watch
: continuously run unit tests.