### Home Classes Methods

### In Files

- lib/Dobjects/Dvector_extras.rb

### Parent

Object

### Methods

- ::new
- #compute
- #compute_unsafe

### Included Modules

- Math

### Class/Module Index

Quicksearch

- Tioga
- Tioga::Arcs_and_Circles
- Tioga::ColorConstants
- Tioga::Colorbars
- Tioga::Colormaps
- Tioga::Creating_Paths
- Tioga::Executive
- Tioga::FigureConstants
- Tioga::FigureMaker
- Tioga::Figures_and_Plots
- Tioga::Flate
- Tioga::Images
- Tioga::Legends
- Tioga::MarkerConstants
- Tioga::Markers
- Tioga::Page_Frame_Bounds
- Tioga::Rectangles
- Tioga::Shading
- Tioga::Special_Paths
- Tioga::Strokes
- Tioga::TeX_Text
- Tioga::Titles_and_Labels
- Tioga::Transparency
- Tioga::Tutorial
- Tioga::Tutorial::Animation
- Tioga::Tutorial::CommandLine
- Tioga::Tutorial::DocStructure
- Tioga::Tutorial::Epilog
- Tioga::Tutorial::Figures
- Tioga::Tutorial::OtherStuff
- Tioga::Tutorial::OtherWaysToRunTioga
- Tioga::Tutorial::Plots
- Tioga::Tutorial::SampledData
- Tioga::Tutorial::SimpleFigure
- Tioga::Tutorial::TextForTeX
- Tioga::Tutorial::UsingRuby
- Tioga::Using_Paths
- Tioga::X_and_Y_Axes
- Dobjects
- Dobjects::Dtable
- Dobjects::Dvector
- Dobjects::Function
- Dobjects::MathEvaluator
- Flate
- TiogaDoc

No matching classes.

# Dobjects::MathEvaluator

MathEvaluator enables one to evaluate a simple mathematical expression such as "x + cos(x)", where the array x is given at each call to compute, or "x + y**z", or...

This class acts as a backend for Dvector.compute_formula, to make sure that the Math module is included, without the drawback of cluttering all Math functions in Dvector, which would admittedly be quite stupid.

### Public Class Methods

new(formula, argname, mods = []) click to toggle source

Creates an evaluator for a formula. `formula` is the formula. It is transformed into a block that takes `argname` as an argument -- `argname` can be whatever you want. `mods` are the modules you would like the formula to include. Math is included by default, but you can include other ones to make other kinds of functions available.

    MathEvaluator.new("col[0] + col[1]", "col")
    MathEvaluator.new("x*cos(y)", "x,y")

    # File lib/Dobjects/Dvector_extras.rb, line 24
    def initialize(formula, argname, mods = [])
      for mod in mods
        self.extend mod
      end
      @block = eval "proc { |#{argname}| #{formula} }"
    end

### Public Instance Methods

compute(*args) click to toggle source

This function does the actual evaluation with the blocks given.

    e = MathEvaluator.new("x*y", "x,y")
    e.compute(1,2)         -> 2

If an exception arises, NaN is returned. Note that compilation problems will be caught before ;-)...

    # File lib/Dobjects/Dvector_extras.rb, line 39
    def compute(*args)
      begin
        return compute_unsafe(*args)
      rescue
        return 0.0/0.0
      end
    end

compute_unsafe(*args) click to toggle source

This function does the actual evaluation with the blocks given.

    e = MathEvaluator.new("x*y", "x,y")
    e.compute(1,2)         -> 2

No care is taken to intercept exceptions.

    # File lib/Dobjects/Dvector_extras.rb, line 54
    def compute_unsafe(*args)
      return @block.call(*args)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.