Home > metametameta

metametameta

Metametameta is a project mainly written in Ruby, it's free.

Just some hacky stuff. Making arrays respond to ordinal phrases, classes for Polynomial Terms and Expressions which can return their own derivatives and calculate zeroes of functions using newton's method, some metaprogramming examples maybe

This is just a bunch of random hacky stuff. Fork, add, change, whatever. You can send pull requests if you want. I will try to get to them all. :-)

ord.rb

The ord.rb file adds a methods to Fixnum and String to turn numbers into phrases and phrases into numbers. This is then used in Array with method_missing to make Array's respond to ordinal phrases; e.g. my_array.three_hundred_forty_third would return my_array[343]. No, I don't know why anyone would use this either, but it was sort of fun to make.

poly.rb

The poly.rb file defines the classes PolynomialTerm and PolynomialExpression (a polynomial expression is made up of several PolynomialTerm objects) which can do things like return their own derivatives, and calculate the zeros (if any) of the function they represent using Newton's method.

Also added FunctionalTerm and FunctionalExpression classes; a functional term can take an arbitrary function, not just the :** function which PolynomialTerm uses. So you can:

FunctionalTerm.new(:coefficient => 3, :function => :*, :argument => 2) # to emulate PolynomialTerm, giving 3x**2 FunctionalTerm.new(:function => :log) # ruby's log is the natural log, or ln. FunctionalTerm.new(:function => :log, :argument => 2) # if log gets an argument, that become the base. FunctionalTerm.new(:function => :sin) FunctionalTerm.new(:function => :log, :argument => Math::E) # same as if no argument were given; e is the base of ln.

Note: you can also provide a proc instead of a symbol for the function parameter, but then FunctionalTerm will not know how to calculate the derivative... UNLESS you also pass the derivative as an argument. This is a little less magical, as you have to figure out the derivative yourself and provide it as an argument, but it gives you a little more freedom. The :derivative you provide can be a FunctionalTerm object, or just a proc; eg:

FunctionalTerm.new(:function => lambda{|n| 1.0/n }, :derivative => lambda {|n| -1*n-2 }) FunctionalTerm.new(:function => lambda{|n} 1.0/n }, :derivative => FunctionalTerm.new(:coefficient => -1, :function => :, :argument => -2)) FunctionalTerm.new(:function => lambda{|n| Math::En }, :derivative => lambda{|n| Math::En }) # aha! FunctionalTerm.new(:function => lambda{|n| 3*a }, :derivative => lambda{|n| 3.log 3**n })

So, you could use this for some basic calculus, but it's still pretty basic. It assumes each term is simple (ie, one function), so it doesn't know the chain rule. You could give it a more complex function, but you would need to provide a lambda for the derivative yourself.

If you really need to do calculus programmatically, you should probably use Mathematica or something. This is solely to explore some meta-programming concepts. :-)

errata

If you fork and require new gems, please add them to the Gemfile for bundler.

There are no tests yet! Maybe you could add some.

Previous:Triskel