Math_Derivative
[ class tree: Math_Derivative ] [ index: Math_Derivative ] [ all elements ]

Class: Math_Derivative

Source Location: /Math_Derivative-1.0.0RC1/Derivative.php

Class Overview


This class allows you to calculate the derivative of a mathematical expression.


Author(s):

Version:

  • Release: @package_version@

Copyright:

  • 1997-2005 The PHP Group

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 58]
This class allows you to calculate the derivative of a mathematical expression.

Notice that expressions that are passed to it won't always be valid php syntax : "a^b" means "a raised to the power of b" for Math_Derivative and not the usual bitwise XOR.

That's is important to know, especially if you plan to evaluate the expression returned using php's own eval() e.g eval($myobject->getDerivative(...));

Math_Derivative::getDerivative() will also work on literal expressions.

Some functions are already implemented, so you can use them in the input: sin(), cos(), tan(), ln(), log(), e(). Use Math_Derivative::registerFunction() to define your own functions.

A cache management is already bundled to save intermediary results of the whole object, it provides a significant speed boost, especially when used with repetitive expressions. It is also allowed to save the cache and reuse it in another instance of the class. Its use is optional though.



[ Top ]


Class Variables

$_cache = array()

[line 84]

The array that contains already calculated intermediary expressions
  • Access: protected

Type:   array


[ Top ]

$_d =  'x'

[line 68]

The variable on which the expression depends

Type:   string


[ Top ]

$_operatorsPrecedences = array('+' => 1, '-' => 1, '*' => 2, '/' => 3, '^' => 4)

[line 76]

The operators' precedences table. It's not ment to change.
  • Access: protected

Type:   array


[ Top ]

$_registeredFunctions = array('sin'  => 'cos(arg)*d(arg)',
                                      'cos'  => '-sin(arg)*d(arg)',
                                      'tan'  => '1/cos(arg)^2*d(arg)',
                                      'ln'   => 'd(arg)/(arg)',
                                      'log'  => 'd(arg)/(arg)',
                                      'e'    => 'd(arg)*e(arg)',
                                      'sqrt' => 'd((arg)^(1/2))',
                                      'acos' => '-1/((1-(arg)^2)^(1/2))',
                                      'asin' => '1/((1-(arg)^2)^(1/2))',
                                      'atan' => '1/(1+(arg)^2)',
                                      'sinh' => 'cosh(arg)*d(arg)',
                                      'cosh' => 'sinh(arg)*d(arg)',
                                      'tanh' => '(sech(arg)^2)*d(arg)',
                                      'coth' => '-(csch(arg)^2)*d(arg)',
                                      'sech' => '-sech(arg)*tanh(arg)*d(arg)',
                                      'csch' => '-csch(arg)*coth(arg)*d(arg)',
                                      )

[line 101]

Contains derivative forms of functions that could appear in expressions
  • Access: protected

Type:   array


[ Top ]

$_useCache =  true

[line 92]

Whether to use or not the cache
  • Access: protected

Type:   boolean


[ Top ]



Method Detail

cleanExpression   [line 255]

string cleanExpression( string $expression)

Cleans the expression to make the parser's job easier.
  • Return: cleaned expression
  • Access: protected

Parameters:

string   $expression   —  expression you want to clean

[ Top ]

getCache   [line 208]

array getCache( )

Returns the cache
  • Return: the current cache
  • Access: public

[ Top ]

getDerivative   [line 229]

string getDerivative( string $expression, string $d, [int $level = 1])

Calculates the derivative of $expression with respect to $d, taken $level times
  • Return: the derivative
  • Access: public

Parameters:

string   $expression   —  expression of which you want to get the derivative
string   $d   —  variable on which the expression depends
int   $level   —  level of the derivative you want.

[ Top ]

getDerivativeCallback   [line 682]

string getDerivativeCallback( array $match)

Callback used in preg_replace_callback as a recursive way to derivate nested d()'s
  • Return: expression with the rule applied
  • Access: protected

Parameters:

array   $match   —  parts of the expression

[ Top ]

parse   [line 308]

string parse( string $expression)

Parses the expression and recursively calculates its derivative
  • Return: the derivative
  • Access: protected

Parameters:

string   $expression   —  expressionyou want to parse

[ Top ]

registerFunction   [line 662]

bool registerFunction( string $name, string $derivative)

Registers a function to be used in the input

e.g. $object->registerFunction('test', 'd(arg)*arg') arg := the argument of the function d() := derivative

  • Return: true
  • Access: public

Parameters:

string   $name   —  name of the function
string   $derivative   —  derivative form of it

[ Top ]

reliesOndx   [line 423]

boolean reliesOndx( string $expression)

Checks whether the expression relies on d?
  • Return: whether $expression relies on d?
  • Access: protected

Parameters:

string   $expression   —  expression you want to check

[ Top ]

resetCache   [line 186]

boolean resetCache( [array $cache = array()])

Restores/resets the cache.
  • Return: Whether the old cache was accepted
  • Access: public

Parameters:

array   $cache   —  Old cache to restore.

[ Top ]

ruleAddition   [line 444]

string ruleAddition( array $parts)

Apply the rule of additions

scheme : (a+b-c)' = a' + b' - c'

  • Return: expression with the rule applied
  • Access: protected

Parameters:

array   $parts   —  parts of the expression

[ Top ]

ruleDivision   [line 567]

string ruleDivision( array $parts)

Apply the rule of divisions

scheme : (a/b/c)' = ((a/b)'/c)' (a/b)' = (a'*b - b'*a) / b*b

  • Return: expression with the rule applied
  • Access: protected

Parameters:

array   $parts   —  parts of the expression

[ Top ]

ruleMultiplication   [line 490]

string ruleMultiplication( array $parts)

Apply the rule of multiplications

scheme : (a*b*c)' = a'*b*c + b'*a*c + c'*a*b

  • Return: expression with the rule applied
  • Access: protected

Parameters:

array   $parts   —  parts of the expression

[ Top ]

rulePower   [line 601]

string rulePower( array $parts)

Apply the rule of powers

scheme : (a^b^c) = ((a^b)^c) (a^b)' => 1) b doesn't rely on dx -> a' * b * a^(b-1) 2) a and b rely on dx -> a^b * ((a'*b)/a + b'*log(a))

  • Return: expression with the rule applied
  • Access: protected

Parameters:

array   $parts   —  parts of the expression

[ Top ]

ruleTerm   [line 703]

string ruleTerm( string $part)

Checks is the term contains a function or if its the variable itself
  • Return: expression with the rule applied
  • Access: protected

Parameters:

string   $part   —  expression

[ Top ]

setVariableName   [line 136]

boolean setVariableName( [string $value = 'x'])

Defines the variable on which the expression depends.

Only alphabetical characters are allowed. It haven't to be a single char.


Parameters:

string   $value   —  the string to quote

[ Top ]

useCache   [line 165]

boolean useCache( boolean $flag)

Sets if Math_Derivative have to use the caching system.
  • Return: true
  • Access: public

Parameters:

boolean   $flag   — 

[ Top ]


Documentation generated on Mon, 11 Mar 2019 14:56:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.