Announcement

Collapse
No announcement yet.

Script to replace "a^b" in an expression with "pow(a,b)"

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to replace "a^b" in an expression with "pow(a,b)"

    I have a few beastly mathematical expressions that I need to use in C++, but I got it from Maple. Maple uses "^" as the exponentiation operator, incompatible with C (which uses pow().

    So, I need a script to go from a^b to pow(a,b).

    For instance, here's one of them:

    1/12/d/k/tan(b)/a*((6*sqrt(3)*r*sqrt(-k^4*a^4-a^4*d^4*tan(b)^4*k^2-2*a^4*d^2*tan(b)^2*k^3+4*k^3*a^3*r*tan(b)^2-4*k^2*a^2*r^2*tan(b)^4-2*a^4*tan(b)^4*d^2*k^2+4*a^3*tan(b)^4*k^2*r-2*a^4*tan(b)^2*k^3-a^4*tan(b)^4*k^2+4*a^3*d^2*tan(b)^4*k^2*r-16*r*k^4*d*tan(b)^2*a^2+k^3*a^4+a^4*tan(b)^6+3*a^4 *tan(b)^6*d^2+a^4*d^6*tan(b)^6-8*a*r^3*tan(b)^6+3*a^4*tan(b)^2*k^2+12*a^2*tan(b)^ 6*r^2-6*a^3*tan(b)^6*r+3*a^4*tan(b)^6*d^4-12*a^3*d^2*tan(b)^4*k*r+3*a^4*d^2*tan(b)^2*k^2-12*a^3*tan(b)^6*d^2*r+6*a^4*tan(b)^4*d^2*k+12*a^2* d^2*tan(b)^6*r^2+27*r^2*tan(b)^4*d^2*k^2+3*a^4*d^4 *tan(b)^4*k-6*a^3*d^4*tan(b)^6*r+12*k*a^2*r^2*tan(b)^4-6*k^2*a^3*r*tan(b)^2-12*a^3*tan(b)^4*k*r+18*r*d*k^2*tan(b)^4*a^2+18*r*d ^3*k^2*tan(b)^4*a^2+18*r*d*k^3*tan(b)^2*a^2-36*r^2*d*k^2*tan(b)^4*a+3*a^4*tan(b)^4*k)*d*k*tan( b)^2+k^3*a^4+a^4*tan(b)^6+3*a^4*tan(b)^6*d^2+a^4*d ^6*tan(b)^6-8*a*r^3*tan(b)^6+3*a^4*tan(b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a^4*tan(b)^2*k^2+12*a^2*tan(b)^ 6*r^2-6*a^3*tan(b)^6*r+3*a^4*tan(b)^6*d^4-12*a^3*d^2*tan(b)^4*k*r+3*a^4*d^2*tan(b)^2*k^2-12*a^3*tan(b)^6*d^2*r+6*a^4*tan(b)^4*d^2*k+12*a^2* d^2*tan(b)^6*r^2+54*r^2*tan(b)^4*d^2*k^2+3*a^4*d^4 *tan(b)^4*k-6*a^3*d^4*tan(b)^6*r+12*k*a^2*r^2*tan(b)^4-6*k^2*a^3*r*tan(b)^2-12*a^3*tan(b)^4*k*r+18*r*d*k^2*tan(b)^4*a^2+18*r*d ^3*k^2*tan(b)^4*a^2+18*r*d*k^3*tan(b)^2*a^2-36*r^2*d*k^2*tan(b)^4*a+3*a^4*tan(b)^4*k)*a^2)^(1/3));

    I tried doing a find and replace shtick over and over again, but I definitely missed some things.

    Any ideas?
    "Beware of the man who works hard to learn something, learns it, and finds himself no wiser than before. He is full of murderous resentment of people who are ignorant without having come by their ignorance the hard way. "
    -Bokonon

  • #2
    What editor are you using? Emacs?
    I came upon a barroom full of bad Salon pictures in which men with hats on the backs of their heads were wolfing food from a counter. It was the institution of the "free lunch" I had struck. You paid for a drink and got as much as you wanted to eat. For something less than a rupee a day a man can feed himself sumptuously in San Francisco, even though he be a bankrupt. Remember this if ever you are stranded in these parts. ~ Rudyard Kipling, 1891

    Comment


    • #3
      MS Visual Studio.

      But I can use another editor if needed of course...

      I should probably just go back and rewrite the program in Fortran which doesn't have this stupid restriction...
      "Beware of the man who works hard to learn something, learns it, and finds himself no wiser than before. He is full of murderous resentment of people who are ignorant without having come by their ignorance the hard way. "
      -Bokonon

      Comment


      • #4
        Probably be easier to turn ^ into an operator in C++. Forget how that's done, though.

        Or maybe you can only overload operators, not create new ones. I forget offhand, and my C++ blackbook is at work.

        Edit: Nevermind, you can't define new operators, you can only overload existing ones. Piece of crap language...
        <p style="font-size:1024px">HTML is disabled in signatures </p>

        Comment


        • #5
          Problem is, the ^ operator is already defined on numbers and such in C++ - it's the bitwise xor.

          Comment


          • #6
            The problem with overloading ^ is the order in which C evaluates * and / in relation to it.

            So 2*k^2 will end up being evaluated as (2*k)^2.
            "Beware of the man who works hard to learn something, learns it, and finds himself no wiser than before. He is full of murderous resentment of people who are ignorant without having come by their ignorance the hard way. "
            -Bokonon

            Comment


            • #7
              s/+\w\^+\w/pow($1,$2)/g;

              in a perl script should do the trick
              (if this is not the exact line, it comes real close)

              edit: added + since your expressions may be more than 1 character long.
              “It is no use trying to 'see through' first principles. If you see through everything, then everything is transparent. But a wholly transparent world is an invisible world. To 'see through' all things is the same as not to see.”

              ― C.S. Lewis, The Abolition of Man

              Comment


              • #8
                Thanks, I'll try that out.
                "Beware of the man who works hard to learn something, learns it, and finds himself no wiser than before. He is full of murderous resentment of people who are ignorant without having come by their ignorance the hard way. "
                -Bokonon

                Comment


                • #9
                  That doesn't even remotely cover it, pchang, I'm afraid... See the last use of the ^ in the one Ramo provided, for instance. It's got complex nested expressions. The one you suggested wouldn't even work with "tan(b)^4".

                  And, um, the one you suggested doesn't work. It should be:
                  s/(\w+)\^(\w+)/pow($1,$2)/g;

                  This one works a bit better, it'll also take functions (e.g. "tan(b)") or parenthesized expressions, but it won't work with nested parentheses:
                  s/(\w+(?:\([^()]*\))?)\^(\w*(?:\(.*?\))?)/pow($1,$2)/g;

                  That reduces the number of ^ from 794 to 4 in the one in your first post though, so you can reasonably finish it off manually.
                  Civilization II: maps, guides, links, scenarios, patches and utilities (+ Civ2Tech and CivEngineer)

                  Comment


                  • #10
                    I bow to the superior intellect
                    “It is no use trying to 'see through' first principles. If you see through everything, then everything is transparent. But a wholly transparent world is an invisible world. To 'see through' all things is the same as not to see.”

                    ― C.S. Lewis, The Abolition of Man

                    Comment


                    • #11
                      WTF is that thing anyway?

                      Comment


                      • #12
                        Originally posted by pchang
                        I bow to the superior intellect


                        You beat me to mentioning regular expressions though. I didn't mean to be too harsh.

                        Originally posted by Kuciwalker
                        WTF is that thing anyway?
                        I hope you're not talking about that regular expression. I'm wondering what the hell Ramo's monster expressions are.
                        Civilization II: maps, guides, links, scenarios, patches and utilities (+ Civ2Tech and CivEngineer)

                        Comment


                        • #13
                          I'm talking about Ramo's monster.

                          Comment


                          • #14
                            s/(\w+(?:\([^()]*\))?)\^(\w*(?:\(.*?\))?)/pow($1,$2)/g;
                            And to think, that was going be the name of my next DL...
                            Monkey!!!

                            Comment


                            • #15
                              Why don't you just write a C program to do what you need, to replace a^b with pow(a,b) should be too hard of a program to make.

                              Comment

                              Working...
                              X