DGtal  0.9.4beta
math/polynomial2-derivative.cpp

computes the first and second derivatives of the given polynomial P (in two variables).

See also
Computing partial derivatives and Input and output for multivariate polynomials
* $ ./examples/math/polynomial2-derivative "(x-3)^2 + (y-2)^2 - 4"
* P(X_0,X_1)        = ((9 + -4 X_1 + 1 X_1^2) + -6 X_0 + 1 X_0^2)
* dP/dX_0(X_0,X_1)  = (-6 + 2 X_0)
* dP/dX_1(X_0,X_1)  = (-4 + 2 X_1)
* d/dX_1 dP/dX_0(X_0,X_1) = 0
* d/dX_0 dP/dX_1(X_0,X_1) = 0
* d/dX_0 dP/dX_0(X_0,X_1) = 2
* d/dX_1 dP/dX_1(X_0,X_1) = 2
* 
#include <iostream>
#include <string>
#include <sstream>
#include "DGtal/math/MPolynomial.h"
#include "DGtal/io/readers/MPolynomialReader.h"
using namespace DGtal;
void usage( int, char** argv )
{
std::cerr << "Usage: " << argv[ 0 ] << " <P(x,y)>" << std::endl;
std::cerr << "\t - computes the first and second derivatives of the given polynomial P (in two variables)." << std::endl;
}
int main( int argc, char** argv )
{
if ( argc < 2 )
{
usage( argc, argv );
return 1;
}
typedef double Ring;
typedef MPolynomial<2, Ring> MyPolynomial;
std::string polynomialString( argv[ 1 ] );
std::istringstream polynomialIStream( polynomialString );
MyPolynomial P;
polynomialIStream >> P;
MyPolynomial P1_0 = derivative<0>( P );
MyPolynomial P2_0 = derivative<0>( P1_0 );
MyPolynomial P0_1 = derivative<1>( P );
MyPolynomial P0_2 = derivative<1>( P0_1 );
MyPolynomial P1_1 = derivative<1>( P1_0 );
MyPolynomial P1_1b= derivative<0>( P0_1 );
std::cout << "P(X_0,X_1) = " << P << std::endl;
std::cout << "dP/dX_0(X_0,X_1) = " << P1_0 << std::endl;
std::cout << "dP/dX_1(X_0,X_1) = " << P0_1 << std::endl;
std::cout << "d/dX_1 dP/dX_0(X_0,X_1) = " << P1_1 << std::endl;
std::cout << "d/dX_0 dP/dX_1(X_0,X_1) = " << P1_1b << std::endl;
std::cout << "d/dX_0 dP/dX_0(X_0,X_1) = " << P2_0 << std::endl;
std::cout << "d/dX_1 dP/dX_1(X_0,X_1) = " << P0_2 << std::endl;
return 0;
}