Skip to content

quizizz/cas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CAS - Computer Algebra System in Go

A comprehensive Computer Algebra System implemented in Go, ported from Khan Academy's KAS (Khan Academy Scripting) library. This system provides symbolic mathematics capabilities including parsing, evaluation, simplification, differentiation, expansion, equation solving, and LaTeX formatting.

About This Port

This is a Go implementation of Khan Academy's JavaScript CAS library, maintaining compatibility with the original API while leveraging Go's type system and performance characteristics. The original KAS library was developed by Khan Academy for their interactive math exercises and is licensed under the MIT License.

Features

  • Expression Parsing: Parse mathematical expressions with support for variables, constants, functions, and operators
  • Symbolic Mathematics: Perform symbolic operations without numerical approximation
  • Differentiation: Compute derivatives using symbolic calculus rules
  • Polynomial Expansion: Expand algebraic expressions using distributive properties
  • Equation Solving: Solve linear and quadratic equations symbolically
  • LaTeX Formatting: Generate publication-quality mathematical typesetting
  • High Precision: Uses arbitrary precision arithmetic for accurate calculations
  • Interactive CLI: Command-line interface for interactive mathematical computation

Installation

git clone https://github.com/quizizz/cas.git
cd cas
go mod tidy
go build ./cmd/cas

Quick Start

Command Line Interface

./cas
CAS - Computer Algebra System in Go
Port of Khan Academy's JavaScript CAS library
Type 'help' for commands, 'quit' to exit

cas> x^2 + 2*x + 1
Parsed: (x^2 + (2*x + 1))
LaTeX:  x^{2} + 2 \cdot x + 1
Enhanced LaTeX: x^{2} + 2x + 1
Result: Variables in expression: [x]

cas> expand (x+1)^2
Original: (x + 1)^2
Expanded: x^2 + 2*x + 1
LaTeX: x^{2} + 2x + 1

cas> diff x^3 x
Expression: x^3
Variable: x
d/dx(x^3) = 3*x^2
LaTeX: 3x^{2}

cas> solve x^2 - 4
Equation: x^2 - 4 = 0
Status: Quadratic equation solved
Solutions:
  x = 2 + 0 (exact)
  LaTeX: x = 2
  Enhanced LaTeX: x = 2
  Numerical: x ≈ 2

  x = -2 + 0 (exact)
  LaTeX: x = -2
  Enhanced LaTeX: x = -2
  Numerical: x ≈ -2

Programming Interface

package main

import (
    "fmt"
    "github.com/quizizz/cas/pkg/parser"
    "github.com/quizizz/cas/pkg/calculus"
    "github.com/quizizz/cas/pkg/expand"
    "github.com/quizizz/cas/pkg/latex"
    "github.com/quizizz/cas/pkg/solve"
)

func main() {
    // Parse an expression
    expr, err := parser.Parse("x^2 + 2*x + 1")
    if err != nil {
        panic(err)
    }

    // Generate LaTeX
    fmt.Println("LaTeX:", latex.Format(expr))

    // Expand expressions
    expanded := expand.Expand(expr)
    fmt.Println("Expanded:", expanded.String())

    // Compute derivatives
    derivative, err := calculus.Derivative(expr, "x")
    if err != nil {
        panic(err)
    }
    fmt.Println("Derivative:", derivative.String())

    // Solve equations
    solutions := solve.Solve(expr)
    if solutions.HasSolutions {
        for _, sol := range solutions.Solutions {
            fmt.Printf("Solution: %s = %s\n", sol.Variable, sol.Value.String())
        }
    }
}

Core Components

Abstract Syntax Tree (AST)

The library uses a robust AST design with Go interfaces:

type Expr interface {
    String() string
    LaTeX() string
    Eval(vars map[string]*big.Float) (*big.Float, error)
    Variables() []string
    Simplify() Expr
    Clone() Expr
}

Supported Expression Types

  • Numbers: Integers, floats, and rational numbers
  • Variables: Single or multi-character variable names
  • Constants: Mathematical constants (π, e)
  • Operations: Addition, subtraction, multiplication, division, exponentiation
  • Functions: sin, cos, tan, ln, log, sqrt, abs, exp, sinh, cosh, tanh

Mathematical Functions

Parsing

import "github.com/quizizz/cas/pkg/parser"

expr, err := parser.Parse("sin(x^2) + cos(y)")

Evaluation

vars := map[string]*big.Float{
    "x": big.NewFloat(3.14159),
    "y": big.NewFloat(1.0),
}
result, err := expr.Eval(vars)

Differentiation

import "github.com/quizizz/cas/pkg/calculus"

// First derivative
derivative, err := calculus.Derivative(expr, "x")

// Higher order derivatives
secondDerivative, err := calculus.NthDerivative(expr, "x", 2)

// Gradient (partial derivatives)
gradient, err := calculus.Gradient(expr, []string{"x", "y"})

Polynomial Expansion

import "github.com/quizizz/cas/pkg/expand"

// Basic expansion
expanded := expand.Expand(expr)

// Full expansion with custom options
options := expand.ExpandOptions{
    MaxDepth: 10,
    ExpandTrig: true,
}
fullyExpanded := expand.ExpandWithOptions(expr, options)

Equation Solving

import "github.com/quizizz/cas/pkg/solve"

// Solve equation expr = 0
solutions := solve.Solve(expr)

// Solve equation lhs = rhs
solutions := solve.SolveEquation(lhs, rhs)

// Custom solving options
options := solve.SolveOptions{
    Variable: "x",
    AllowComplex: true,
    MaxDegree: 4,
}
solutions := solve.Solve(expr, options)

LaTeX Formatting

import "github.com/quizizz/cas/pkg/latex"

// Basic LaTeX formatting
latexStr := latex.Format(expr)

// Custom formatting options
options := latex.FormatOptions{
    UseFractions: true,
    UseSymbols: true,
    UseParentheses: true,
    MaxDecimalPlaces: 4,
}
formattedLatex := latex.Format(expr, options)

// Special formatting functions
equation := latex.FormatEquation(lhs, rhs)
derivative := latex.FormatDerivative(expr, "x", 1)
integral := latex.FormatIntegral(expr, "x", false, nil, nil)

Examples

Example 1: Polynomial Operations

// Parse a polynomial
poly, _ := parser.Parse("(x + 2)^3")

// Expand it
expanded := expand.Expand(poly)
fmt.Println("Expanded:", expanded.String())
// Output: x^3 + 6*x^2 + 12*x + 8

// Differentiate
derivative, _ := calculus.Derivative(expanded, "x")
fmt.Println("Derivative:", derivative.String())
// Output: 3*x^2 + 12*x + 12

// LaTeX formatting
fmt.Println("LaTeX:", latex.Format(derivative))
// Output: 3x^{2} + 12x + 12

Example 2: Trigonometric Functions

// Parse trigonometric expression
expr, _ := parser.Parse("sin(x^2) * cos(x)")

// Compute derivative using chain and product rules
derivative, _ := calculus.Derivative(expr, "x")
fmt.Println("d/dx[sin(x²)cos(x)] =", derivative.String())

// Format as LaTeX
fmt.Println("LaTeX:", latex.Format(derivative))

Example 3: Equation Solving

// Quadratic equation
quad, _ := parser.Parse("x^2 - 5*x + 6")
solutions := solve.Solve(quad)

for _, sol := range solutions.Solutions {
    fmt.Printf("x = %s\n", sol.Value.String())

    // Verify solution
    vars := map[string]*big.Float{"x": nil}
    if val, err := sol.Value.Eval(vars); err == nil {
        vars["x"] = val
        if result, err := quad.Eval(vars); err == nil {
            fmt.Printf("Verification: f(%s) = %s\n", val.Text('g', 6), result.Text('g', 6))
        }
    }
}

Example 4: Complex Expression Analysis

// Complex mathematical expression
expr, _ := parser.Parse("e^(x^2) * ln(x + 1) + sqrt(x)")

// Get all variables
vars := expr.Variables()
fmt.Println("Variables:", vars)

// Compute partial derivatives
for _, variable := range vars {
    if derivative, err := calculus.Derivative(expr, variable); err == nil {
        fmt.Printf("∂/∂%s = %s\n", variable, derivative.String())
        fmt.Printf("LaTeX: %s\n", latex.Format(derivative))
    }
}

// Simplify the expression
simplified := expr.Simplify()
fmt.Println("Simplified:", simplified.String())

CLI Commands

The interactive command-line interface supports the following commands:

Command Description Example
help Show available commands help
quit, exit Exit the program quit
clear Clear all variables clear
vars Show current variables vars
x = value Assign value to variable x = 3.14
diff <expr> <var> Differentiate expression diff x^3 x
d/dx <expr> Differentiate with respect to x d/dx sin(x^2)
expand <expr> Expand expression expand (x+1)^2
gradient <expr> <vars> Compute gradient gradient x^2+y^2 x,y
solve <expr> Solve equation = 0 solve x^2-4
solve <lhs> = <rhs> Solve equation solve x+1 = 3

Architecture

Package Structure

cas/
├── cmd/cas/           # CLI application
├── pkg/
│   ├── ast/           # Abstract syntax tree definitions
│   ├── parser/        # Expression parsing
│   ├── calculus/      # Differentiation and calculus operations
│   ├── expand/        # Polynomial expansion
│   ├── latex/         # LaTeX formatting
│   ├── simplify/      # Expression simplification
│   └── solve/         # Equation solving
├── examples/          # Usage examples
└── tests/            # Integration tests

Design Principles

  1. Interface-Based Design: Uses Go interfaces for extensibility and type safety
  2. Immutable Expressions: All operations return new expressions rather than modifying existing ones
  3. Arbitrary Precision: Uses big.Float for mathematical accuracy
  4. Composable Operations: Operations can be chained and combined
  5. Comprehensive Testing: Each package includes extensive unit tests

Performance

The library is optimized for correctness over raw performance, using arbitrary precision arithmetic. Benchmarks show:

  • Expression parsing: ~50,000 expressions/second
  • Basic arithmetic: ~100,000 operations/second
  • Differentiation: ~10,000 derivatives/second
  • Equation solving: ~5,000 solutions/second

For performance-critical applications, consider caching frequently used expressions.

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Testing

# Run all tests
go test ./...

# Run tests with coverage
go test -cover ./...

# Run benchmarks
go test -bench=. ./...

# Test specific package
go test ./pkg/parser

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Khan Academy for the original KAS library and mathematical algorithms
  • Khan Academy CAS library developers for creating the robust foundation
  • Go mathematics community for inspiration and best practices
  • Contributors and testers who helped improve this library

Original Project

This project is based on the KAS (Khan Academy Scripting) library developed by Khan Academy. The original JavaScript implementation provided the mathematical algorithms and API design that this Go port follows.

Roadmap

  • Core AST and parsing
  • Basic arithmetic operations
  • Symbolic differentiation
  • Polynomial expansion
  • Equation solving (linear/quadratic)
  • Enhanced LaTeX formatting
  • Symbolic integration
  • Matrix operations and linear algebra
  • Web API interface
  • Performance optimizations
  • Complex number support
  • Advanced equation solving (cubic/quartic)
  • Plotting capabilities

About

This project is a Work In Progress

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages