Sha256: 0bb39dd17d7e21794c8b723b6ef860a76378586316c9af5335517a4e88761895

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'symath/value'
require 'symath/definition/operator'
require 'symath/definition/function'

module SyMath
  class Definition::D < Definition::Operator
    def initialize()
      super(:d)
    end

    def description()
      return 'd(f) - differential of f with respect to its input variables'
    end
    
    def validate_args(e)
      # Arguments 1, 2, ... are supposed to be variables to differentiate
      # over.
      e.args[1..-1].each do |v|
        if !v.is_a?(SyMath::Definition::Variable)
          raise "Expected variable, got #{v.class.name}"
        end

        if v.is_d?
          raise "Var is not allowed to be differential, got #{v}"
        end
      end
    end

    def compose_with_simplify(name, args)
      exp = args[0]
      vars = args[1..-1]

      if exp.is_a?(SyMath::Definition::Variable) and
        exp.type.is_scalar? and
        vars.length == 0
        return exp.to_d
      end

      return
    end

    def evaluate_call(c)
      e = c.args[0]

      # If argument is a function, differentiate on all function
      # arguments.
      if e.is_a?(SyMath::Definition::Function)
        vars = e.args
      else
        # Find first free variable in expression.
        vars = [(e.variables)[0].to_m]
        e = lmd(e, *vars)
      end

      return lmd(e.d(vars), *vars)
    end

    def latex_format()
      return '\mathrm{d}(%s)'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
symath-0.1.1 lib/symath/definition/d.rb
symath-0.1.0 lib/symath/definition/d.rb