Defining functions after compilation

Discussion in 'C++' started by dsv, Mar 16, 2010.

  1. dsv

    dsv Guest

    I am writing a code that, among other things, iteratively solves a set
    of non-linear algebraic equations. I am using Newton's method.


    The difficulty arises because f1, f2, ..., fN are known at the time of
    compilation. In fact, the number of equations N is also unknown. They
    are provided by the user during run-time either via an input file, or
    using the keyboard. Therefore I cannot hard-code them into the source

    All I know about the functions are that they are a weighted sum of
    known functions. i.e. for i=1,2,...,N,


    where the functional forms of g1, g2, ... are known.

    How do I pass f1, f2, .., fN to the Newton's iteration subroutine? Is
    it possible at all, and what is the syntax?

    I guess this is a general programming question, but since I am using C+
    + in this case, I am posting this question here.

    dsv, Mar 16, 2010
    1. Advertisements

  2. dsv

    dsv Guest

    Ouch.. I meant to write :

    The difficulty arises because f1, f2, ..., fN are NOT known at the
    time of compilation.

    dsv, Mar 16, 2010
    1. Advertisements

  3. * dsv:
    I wonder how the functions g_i can be known, since each g_i takes N arguments
    and N is unknown?

    But anyways, to do this in C++ simply change your point of view a little.

    Instead of handling C++-defined functions that compute, handle objects that has
    member functions that compute. Such an object would carry a1, a2 etc., perhaps
    in a std::vector, and perhaps have a an 'at' member function (or simply
    operator(), but if you're new to C++ I suggest starting with an ordinarily named
    member function like 'at') that evaluates the represented function for any given
    value of -- a vector of x-values.

    Cheers & hth.,

    - Alf

    PS: There are numeruous optimizations possible, especially when things are known
    at compile time. Plus, you may just search for some library that does what you
    want. But it's not difficult to do yourself.
    Alf P. Steinbach, Mar 16, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.