N-dimensional mathematical functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jacek.strzelczyk@gmail.com

    N-dimensional mathematical functions

    Hello,

    I'm looking for a C library that provides the notation of n-
    dimensional mathematical functions. Or is there any other way to
    decode that kind of functions in C language?

    Thanks in advance,
    Jacek
  • user923005

    #2
    Re: N-dimensional mathematical functions

    On Feb 18, 7:38 am, jacek.strzelc.. .@gmail.com wrote:
    Hello,
    >
    I'm looking for a C library that provides the notation of n-
    dimensional mathematical functions. Or is there any other way to
    decode that kind of functions in C language?
    In C, we just code a vector as an array.
    Sample C vector code for differential equations is found here for
    cvode.tar.gz:

    Comment

    • jacek.strzelczyk@gmail.com

      #3
      Re: N-dimensional mathematical functions

      On 19 Lut, 00:11, user923005 <dcor...@connx. comwrote:
      On Feb 18, 7:38 am, jacek.strzelc.. .@gmail.com wrote:
      >
      Hello,
      >
      I'm looking for a C library that provides the notation of n-
      dimensional mathematical functions. Or is there any other way to
      decode that kind of functions in C language?
      >
      In C, we just code a vector as an array.
      Sample C vector code for differential equations is found here for
      cvode.tar.gz:http://www.netlib.org/ode/
      But vector is not enough to note all kinds of functions. For example
      f(x)=1/x is not in polynomial form, so you can't describe it by simple
      vector. Is there any other, more sophisticated way?

      Comment

      • Chris Dollin

        #4
        Re: N-dimensional mathematical functions

        jacek.strzelczy k@gmail.com wrote:
        But vector is not enough to note all kinds of functions. For example
        f(x)=1/x is not in polynomial form,
        -1 is a perfectly good exponent. I presume I'm missing something.

        --
        "Well begun is half done." - Proverb

        Hewlett-Packard Limited Cain Road, Bracknell, registered no:
        registered office: Berks RG12 1HN 690597 England

        Comment

        • Ben Bacarisse

          #5
          Re: N-dimensional mathematical functions

          jacek.strzelczy k@gmail.com writes:
          On 19 Lut, 00:11, user923005 <dcor...@connx. comwrote:
          >On Feb 18, 7:38 am, jacek.strzelc.. .@gmail.com wrote:
          >>
          Hello,
          >>
          I'm looking for a C library that provides the notation of n-
          dimensional mathematical functions. Or is there any other way to
          decode that kind of functions in C language?
          >>
          >In C, we just code a vector as an array.
          >Sample C vector code for differential equations is found here for
          >cvode.tar.gz :http://www.netlib.org/ode/
          >
          But vector is not enough to note all kinds of functions. For example
          f(x)=1/x is not in polynomial form, so you can't describe it by simple
          vector. Is there any other, more sophisticated way?
          comp.programmin g may be better. I, for one, was not replying simply
          because all the questions I had about what you need are not C
          questions. Anyway, to get further it would be better to know an
          example of the kind of thing you need the library to do, together with
          some idea of the range ("I need to work for all piece-wise continuous
          functions of n variables"). The example makes it concrete, the range
          says what can and can not be ignored. "Decode" and "n-dimensional"
          are very broad (and to me, at least, the dimension of a function is a
          very specific thing that crops up only in fractal geometry).

          --
          Ben.

          Comment

          • jacek.strzelczyk@gmail.com

            #6
            Re: N-dimensional mathematical functions

            The magic phrase is "function pointer."
            >
            double func(double x1, double x2)
            {
            return 19.739208802178 717237668981999 752
            / (3 * (x1+x2)*(x1+x2) *(x1+x2))
            + x1 * x2;
            }
            >
            result_type minimize(variou s_params,
            double (*fptr)(double, double),
            more_params)
            {
            ...
            z = fptr(x, y);
            ...
            }
            >
            ...
            result = minimize(params , func, other_params);
            >
            --
            Eric.Sos...@sun .com
            Ok, this is some solution. But in this case, the function has to be
            directly programmed by programmer. What if user would type the
            function and then the input would be parsed by some parser. How can
            the function can be coded then?

            Comment

            • Eric Sosman

              #7
              Re: N-dimensional mathematical functions

              jacek.strzelczy k@gmail.com wrote:
              > The magic phrase is "function pointer."
              >>
              > double func(double x1, double x2)
              > {
              > return 19.739208802178 717237668981999 752
              > / (3 * (x1+x2)*(x1+x2) *(x1+x2))
              > + x1 * x2;
              > }
              >>
              > result_type minimize(variou s_params,
              > double (*fptr)(double, double),
              > more_params)
              > {
              > ...
              > z = fptr(x, y);
              > ...
              > }
              >>
              > ...
              > result = minimize(params , func, other_params);
              >
              Ok, this is some solution. But in this case, the function has to be
              directly programmed by programmer. What if user would type the
              function and then the input would be parsed by some parser. How can
              the function can be coded then?
              Parse the user's input, "compile" it into an interpreted
              "instructio n set" of your own devising, and run your own
              interpreter on it each time you need a new value. There's
              nothing particularly C-specific about this stuff.

              --
              Eric.Sosman@sun .com

              Comment

              • jacek.strzelczyk@gmail.com

                #8
                Re: N-dimensional mathematical functions

                On 19 Lut, 18:48, Eric Sosman <Eric.Sos...@su n.comwrote:
                jacek.strzelc.. .@gmail.com wrote:
                The magic phrase is "function pointer."
                >
                double func(double x1, double x2)
                {
                return 19.739208802178 717237668981999 752
                / (3 * (x1+x2)*(x1+x2) *(x1+x2))
                + x1 * x2;
                }
                >
                result_type minimize(variou s_params,
                double (*fptr)(double, double),
                more_params)
                {
                ...
                z = fptr(x, y);
                ...
                }
                >
                ...
                result = minimize(params , func, other_params);
                >
                Ok, this is some solution. But in this case, the function has to be
                directly programmed by programmer. What if user would type the
                function and then the input would be parsed by some parser. How can
                the function can be coded then?
                >
                Parse the user's input, "compile" it into an interpreted
                "instructio n set" of your own devising, and run your own
                interpreter on it each time you need a new value. There's
                nothing particularly C-specific about this stuff.
                >
                --
                Eric.Sos...@sun .com
                Eric, your second solution seems to be quite complicated. It's not my
                aim to create my own interpreter. I was thinking about some easy way.
                But I think there is no other option despite your first advice -
                function pointer.
                Keith, I think this is what you've been also saying - that using C
                function directly would be the best solution.
                Ok, but maybe there is already a parser and interpreter in C to
                evaluate multi dimensional mathematical functions? You've mentioned
                that I could create it by myself, but isn't there something like this
                already?

                Comment

                • Ben Bacarisse

                  #9
                  Re: N-dimensional mathematical functions

                  jacek.strzelczy k@gmail.com writes:
                  On 19 Lut, 18:48, Eric Sosman <Eric.Sos...@su n.comwrote:
                  >jacek.strzelc. ..@gmail.com wrote:
                  > The magic phrase is "function pointer."
                  >>
                  > double func(double x1, double x2)
                  > {
                  > return 19.739208802178 717237668981999 752
                  > / (3 * (x1+x2)*(x1+x2) *(x1+x2))
                  > + x1 * x2;
                  > }
                  >>
                  > result_type minimize(variou s_params,
                  > double (*fptr)(double, double),
                  > more_params)
                  > {
                  > ...
                  > z = fptr(x, y);
                  > ...
                  > }
                  >>
                  > ...
                  > result = minimize(params , func, other_params);
                  >>
                  Ok, this is some solution. But in this case, the function has to be
                  directly programmed by programmer. What if user would type the
                  function and then the input would be parsed by some parser. How can
                  the function can be coded then?
                  >>
                  > Parse the user's input, "compile" it into an interpreted
                  >"instructio n set" of your own devising, and run your own
                  >interpreter on it each time you need a new value. There's
                  >nothing particularly C-specific about this stuff.
                  >>
                  >--
                  >Eric.Sos...@su n.com
                  Please don't quote sigs (the -- bit).
                  Eric, your second solution seems to be quite complicated.
                  There is a middle way where you don't have to parse the input (if you
                  don't want to). You use postfix notation and encode that in an array
                  and interpret it with a little stack machine.

                  Using $1, $2 for the parameters, your example is:

                  19.73920 $1 $2 + $1 $2 + $1 $2 + * * 3 * / $1 $2 *

                  (simpler if you allow a "dup" operation). In C you could have

                  enum instruction_typ e {
                  Number,
                  Parameter,
                  Operation
                  };

                  enum operation {
                  Add,
                  Multiply,
                  Divide,
                  /* and do on ... */
                  };

                  struct instruction {
                  enum instruction_typ e type;
                  union instruction_dat a {
                  double number;
                  int parameter;
                  enum operation operation;
                  } data;
                  };

                  It is not too hard to read input and store it in an array of these
                  instructions and the execution is simple enough. Of course, it may
                  not be fast enough for some numerical work.

                  --
                  Ben.

                  Comment

                  • dj3vande@csclub.uwaterloo.ca.invalid

                    #10
                    Re: N-dimensional mathematical functions

                    In article <35589316-8fcd-41c1-a295-304571e0083a@u6 9g2000hse.googl egroups.com>,
                    <jacek.strzelcz yk@gmail.comwro te:
                    >Keith, I think this is what you've been also saying - that using C
                    >function directly would be the best solution.
                    You would still have to write code to compile it to a temporary
                    executable and run that executable. This may or may not be easier than
                    writing a parser yourself, and in any case it will tie your program
                    down to a particular system, while an expression parser and interpreter
                    can be done portably.
                    >Ok, but maybe there is already a parser and interpreter in C to
                    >evaluate multi dimensional mathematical functions? You've mentioned
                    >that I could create it by myself, but isn't there something like this
                    >already?
                    There isn't (that I know of) a pre-packaged one.
                    If you don't mind forcing the user to enter the function in something
                    other than standard mathematical notation, section 4.3 of K&R2
                    describes a fairly simple RPN calculator; adapting this to your needs
                    would probably be a worthwhile exercise and would have as its end
                    result something you can use.
                    If you need standard mathematical notation and don't mind picking up
                    new tools, there are code generators like yacc that will do most of the
                    hard work of generating the parser for you. (You give the code
                    generator a description of what your input looks like and the code you
                    want it to run when it parses each sub-expression, and it will generate
                    C code to parse the input and run your code at appropriate times.)
                    comp.programmin g or comp.unix.progr ammer (or maybe comp.compilers)
                    would be good places to ask about that.


                    dave

                    --
                    Dave Vandervies dj3vande at eskimo dot com
                    Well, MS helpfully define a macro ERROR_SUCCESS in winerror.h.
                    Which, many would say, perfectly describes Windows...
                    --Mark McIntyre in comp.lang.c

                    Comment

                    • Eric Sosman

                      #11
                      Re: N-dimensional mathematical functions

                      jacek.strzelczy k@gmail.com wrote:
                      On 19 Lut, 18:48, Eric Sosman <Eric.Sos...@su n.comwrote:
                      >[...]
                      > Parse the user's input, "compile" it into an interpreted
                      >"instructio n set" of your own devising, and run your own
                      >interpreter on it each time you need a new value. There's
                      >nothing particularly C-specific about this stuff.
                      >
                      Eric, your second solution seems to be quite complicated.
                      Not very. I've written such a thing in Java and it runs
                      to only 1036 lines, of which 304 are comments. Java isn't C,
                      but I imagine a C version would be of similar size.
                      Ok, but maybe there is already a parser and interpreter in C to
                      evaluate multi dimensional mathematical functions? You've mentioned
                      that I could create it by myself, but isn't there something like this
                      already?
                      There's no such thing built into the C language or library.
                      If you search for a term like "expression evaluator" you might
                      find that somebody else has written something that you could use.
                      (By the way, I still don't understand why you seem so fixated on
                      the "multi dimensional" aspect; the same issues arise for user-
                      entered expressions in one variable, or even in no variables!)

                      --
                      Eric.Sosman@sun .com

                      Comment

                      • jacek.strzelczyk@gmail.com

                        #12
                        Re: N-dimensional mathematical functions

                        Thanks for the comments. I'll read them through tomorrow, because
                        today it's already too late for me to understand everything and write
                        something clever. ;) Then I'll probably have more questions.

                        Comment

                        • jacek.strzelczyk@gmail.com

                          #13
                          Re: N-dimensional mathematical functions

                          Ben, dj3va: Thanks for ideas. I have no question, so I'll try to read
                          more and implement your solutions.

                          Eric:
                          Ok, maybe it's not so complicated, but if there already is something
                          like that, why create it one more time? ;) And yes, I think you've
                          called it properly - "expression evaluator', I'll try to look for it.
                          I'm not fixated on the "multi dimensional" aspect, that's just my
                          perspective of this problem. ;)

                          user923005:
                          Hmmm... right, I'll take that into consideration. Thanks.

                          Comment

                          • Keith Thompson

                            #14
                            Re: N-dimensional mathematical functions

                            jacek.strzelczy k@gmail.com writes:
                            [...]
                            Eric:
                            Ok, maybe it's not so complicated, but if there already is something
                            like that, why create it one more time? ;) And yes, I think you've
                            called it properly - "expression evaluator', I'll try to look for it.
                            I'm not fixated on the "multi dimensional" aspect, that's just my
                            perspective of this problem. ;)
                            [...]

                            I'm still not 100% clear on what you mean by "multi dimensional". Are
                            you merely referring to functions that take multiple arguments, or is
                            there more to it?

                            --
                            Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            Working...