Git Product home page Git Product logo

symbolic's Introduction

Symbolic math for ruby.

Installation

gem install symbolic

Introduction

This gem can help you

  • if you want to get a simplified form of a big equation

  • if you want to speed up similar calculations

  • if you need an abstraction layer for math

Symbolic doesn’t have any external dependencies.

Tutorial

First, you need to create a symbolic variable.

x = var

# you can set a name of variable (useful if you print equations)
angle = var :name => 'θ'

# or starting value
pi = var :value => 3.14

# or bind its value to a proc
y = var { x ** 2 }

# you can set a value for already created variable
x.value = 3

Now, you can do any math operations with it.

f = 2*x + 1
puts f # => 2*x+1

To get value of symbolic expression you just call value:

f.value # => 7

You can accomplish the same thing with subs:

f.subs(x,3) # => 7

Or make a more complicated substitution:

f.subs(x,x**2) # => 2*x**2+1

If symbolic expression contains variables without value then it returns nil.

z = var
(z+1).value # => nil

All symbolic expression are automatically simplified when created:

0 * x           # => 0
2 + x + 1       # => x + 3
-(x-y) + 2*x    # => x + y
(x**2)**3 / x   # => x**5
# etc. (more examples can be found in symbolic_spec.rb)

If you need to use a function from Math module with symbolic variable, use Symbolic::Math module.

cos = Symbolic::Math.cos(x)
x.value = 0
cos.value # => 1.0

You can get a list of variables from symbolic expression:

(x+y+1).variables # => [x, y]

So you can get a list of variables without value:

(x+y+1).variables.select {|var| var.value.nil? }

You can get information about the number of different operations used in a symbolic expression:

f = (2*x-y+2)*x-2**(x*y)
f.operations # => {"+"=>1, "-"=>2, "*"=>3, "/"=>0, "**"=>1, "-@"=>0}

You can also take derivitives and do taylor expansions:

Symbolic::Math.cos(x**2).diff(x)
# => -2*(sin(x**2))*x
Symbolic::Math.cos(x).taylor(x,0,3)
# => -0.5*x**2+1.0

TODO

  • a lot of refactoring (code is pretty messy at this stage)

  • plotting capabilities

  • integrals

  • thorough documentation

Author

brainopia (ravwar at gmail.com).

I am ready to help with any questions related to Symbolic. I welcome any contribution.

symbolic's People

Contributors

eregon avatar lnmaurer avatar

Stargazers

 avatar

Watchers

 avatar

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.