Git Product home page Git Product logo

kas's Introduction

KAS

A lightweight JavaScript CAS for comparing expressions and equations. It is used throughout Khan Academy's interactive exercises.

What can it do?

It can parse plain text math, LaTeX, or a mix of both:

var expr = KAS.parse("3x \\frac{42}{42} sin^2y").expr;
expr.print();
// "3*x*42/42*sin(y)^(2)"

It can evaluate expressions:

var expr = KAS.parse("(x^2+y^2)^.5").expr;
expr.eval({x: 3, y: 4});
// 5

It can compare expressions and equations:

var expr1 = KAS.parse("(1-x)(-1-6x)").expr;
var expr2 = KAS.parse("(6x+1)(x-1)").expr;
KAS.compare(expr1, expr2).equal;
// true

var eq1 = KAS.parse("2w+50/w=25").expr;
var eq2 = KAS.parse("w(12.5-w)=25").expr;
KAS.compare(eq1, eq2).equal;
// true

It can perform basic transforms that always simplify an expression:

var expr = KAS.parse("1+1+x+x+x+y").expr;
expr.collect().print();
// "2+3*x+y"

var expr = KAS.parse("b^(2*y*log_b x)").expr;
expr.collect().print();
// "x^(2*y)"

It can perform non-simplifying transforms on an expression:

var expr = KAS.parse("ab(c+d)e^f").expr;
expr.print();
// "a*b*(c+d)*e^(f)"
expr.expand().print();
// "a*b*e^(f)*c+a*b*e^(f)*d"
expr.expand().factor().print();
// "a*b*e^(f)*(c+d)"

It can combine the above abilities to perform powerful simplification:

var expr = KAS.parse("((nx^5)^5)/(n^-2x^2)^-3").expr;
expr.print();
// "(n*x^(5))^(5)*(n^(-2)*x^(2))^(-1*-3)"
expr.simplify().print();
// "n^(-1)*x^(31)"

var expr = KAS.parse("(15np-25mp)/(15p^2-5p)+(20mp+10p^2)/(15p^2-5p)").expr;
expr.print();
// "(15*n*p+-25*m*p)*(15*p^(2)+-5*p)^(-1)+(20*m*p+10*p^(2))*(15*p^(2)+-5*p)^(-1)"
expr.simplify().print();
// "(-1+3*p)^(-1)*(3*n+-1*m+2*p)"

How to build the library

npm install
npm run build

How to build the parser

First, make any changes in src/parser-generator.js

npm install
npm run build:parser

License

MIT License

kas's People

Contributors

aasmundeldhuset avatar alopatin avatar ariabuckles avatar briangenisio avatar csilvers avatar dierat avatar drosile avatar itsjohncs avatar jeresig avatar jlfwong avatar justinj avatar mauk81 avatar nixterrimus avatar nsfmc avatar samiskin avatar sophiebits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kas's Issues

compute a reverse expression

Dear @jeresig , @sophiebits , @nixterrimus , @aag , @adinardi

I am looking to compute the reverse on an expression.

Use case :

$ is the output value for example of a sensor and the manufacturer gives us the expr1 as formula to convert the $ to a display value for the user

But when the user wants to interact with the sensor, he needs to translate the display value to an expr2 to be sure the sensor could interpret the value.

That's my example :

The user write the expr1 and the library compute the reverse of the expr1 to expr2
expr1 = 1 + log($, 2)
expr2 = 2^($ - 1)

expr1 = sin($ / 7.43) where $ is of type INTEGER
expr2 = int(7.43 * asin($))

expr1 = $ - log($ / (e^(-$) * $^sin(1 / $)), 10)
expr2 = exercise left to the reader

Thanks in advance if you could help us to find a solution of our issue.

Best Regards
Youssef

Simplify hanging.

KAS hangs when I run the following:

const kasHang = 'x - ( ( - 2 ^ ( - 1 ) * y + z - 2 ^ ( - 1 ) * x * y + 2 * x * z + x ^ 2 * z ) * ( 1 + x ) ^ ( - 2 ) + 2 ^ ( - 1 ) * y - z ) / ( - ( - 2 ^ ( - 1 ) * y + z - 2 ^ ( - 1 ) * x * y + 2 * x * z + x ^ 2 * z ) * ( 1 + x ) ^ ( - 2 ) + z )';
const dd = KAS.parse(kasHang).expr;
const hung = dd.simplify().print(); 

It gets stuck in some infinite loop with stack traces like simplify -> factor -> simplify -> expand.

I haven't tried to hunt down the simplest case in which it hangs.

Simplifying Logarithms of Fractions

"KAS.parse("log(.1)").expr.simplify().print()" outputs "log_(10) (0.1)". Any chance of getting it to output "-1"?

Simplify correctly handles "-log(1/.1)", so this should be as simple as checking which of log(x) and -log(1/x) is simpler, and returning that.

Similarly for "KAS.parse("log_(9) (3)").expr.simplify().print()" which outputs "log_(9) (3)" but should output "1/2". Just check which of log_(a)(b) and 1/(log_(b)(a)) is simpler and return that, once you've handled log_(3)(9) (which also doesn't simplify).

Inconsistent evaluation of some expressions

I am having difficulty with consistency is evaluating expressions.

For example:

var input =  KAS.parse("(3x+7)/(x+4)").expr;
var output = KAS.parse("(-3x-7)/(-x-4)").expr;
input.compare(output)

Should evaluate to true every time, but it only does so 90% of the time.

The following simulation run in any browser will show the problem:

var countTrue = 0; 
var countFalse = 0; 
for(var i = 0; i < 1000; i++) { 
    if(KAS.parse("(3x+7)/(x+4)").expr.compare(KAS.parse("(-3x-7)/(-x-4)").expr)) 
       countTrue++; 
    else 
       countFalse++;
} 
console.log('Count true: '+countTrue); 
console.log('Count false: '+countFalse);

Any help would be greatly appreciated.

Simplify fails on "0+0"

KAS.parse("0+0").expr.simplify() yields Rational {n: NaN, d: NaN}.

I'm using this in a case where the expression being simplified can involve variables (but defaults to "0+0"), so switching to .eval() alone is not enough to work around the problem.

.simplify() and .tex() not working together with fraction

Combining .simplify() and .tex() seems to drop a coefficient in the numerator:

KAS.parse("3*x/2").expr.simplify().tex()
"\frac{x}{2}"

But, using either .simplify() or .tex() on their own works correctly:

KAS.parse("3x/2").expr.simplify().print()
"3/2
x"
KAS.parse("3*x/2").expr.tex()
"\frac{3x}{2}"

To get started on figuring out what's happening, it seems that KAS.parse("3/2x").expr and KAS.parse("3/2x").expr.simplify() are identical, except KAS.parse("3/2x").expr.terms[0].hints.fraction is true while KAS.parse("3/2x").expr.simplify().terms[0].hints.fraction is false.

Doesn't recognize 3|x|

Doesn't recognize 3|x| as Mul(Int(3),Abs(Var("x"))). It recognizes 3*|x|, but not 3|x|.

NPM Package

First of all...
AWESOME PROJECT
I had been looking for this kind of stuff for a long time. I absolutely love the simplicity of KaTeX and was very impressed when I found this project. Really impressive.

It came to my attention however, that this library is not listed on npm. Is that intentional?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.