// ---------------------------------------------------------------------------
// - Newton.hpp                                                              -
// - afnix:mth module - newton based zero solver class definitions           -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2012 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_NEWTON_HPP
#define  AFNIX_NEWTON_HPP

#ifndef  AFNIX_RFI_HPP
#include "Rfi.hpp"
#endif

#ifndef  AFNIX_RNI_HPP
#include "Rni.hpp"
#endif

namespace afnix {

  /// The Newton class is a class that implements various newton based 
  /// zero solver for non linear system. With single real variable function,
  /// the Newton algorithm requires the function and it derivate. With multi
  /// variable function, a vector is used to represent the set of variables
  /// and the jacobian the set of derivatives in the function space. The
  /// newton method is an iterative local method, meaning that it works well
  /// when the initial point is closed to the solution. Obviously, things
  /// become interesting when the derivate is null.
  /// @author amaury darsch

  class Newton : public Object {
  private:
    /// the automatic verification flag
    bool d_avf;
    /// the maximum number of iteration
    long d_mni;

  public:
    /// create a default linear solver
    Newton (void);

    /// create a linear solver by verification flag
    /// @param avf the automatic verification flag
    Newton (const bool avf);

    /// create a linear solver by verification flag and iteration number
    /// @param avf the automatic verification flag
    /// @param mni the maximum iteration number
    Newton (const bool avf, const long mni);

    /// copy construct this solver
    /// @param that the object to copy
    Newton (const Newton& that);

    /// assign a solver to this one
    /// @param that the object to assign
    Newton& operator = (const Newton& that);

    /// @return the class name
    String repr (void) const;

    /// @return a clone of this object
    Object* clone (void) const;

    /// find a function root with the newton method
    /// @param fo the function object
    /// @param xi the initial point
    t_real solve (const Rfi& fo, const t_real xi);

    /// find a system root with the newton method
    /// @param so the system object
    /// @param xi the initial point
    Rvi* solve (const Rni& so, const Rvi& xi);

  public:
    /// create a new object in a generic way
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);

    /// @return true if the given quark is defined
    bool isquark (const long quark, const bool hflg) const;

    /// apply this object with a set of arguments and a quark
    /// @param robj  the current runnable
    /// @param nset  the current nameset    
    /// @param quark the quark to apply these arguments
    /// @param argv  the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, const long quark,
                   Vector* argv);
  };
}

#endif
