Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
OT: translate ^ to pow()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 46 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gib Bogle  
View profile  
 More options Nov 13 2011, 9:23 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Mon, 14 Nov 2011 15:23:44 +1300
Local: Sun, Nov 13 2011 9:23 pm
Subject: OT: translate ^ to pow()
I know this is off-topic here, but posters here have good general
programming knowledge and may be able to help.  I have some long
expressions, generated by computer algebra software, in which many terms
contain powers, expressed as x^2, c^4 etc.  I need to use these
expressions in a C program.  If I tried to do the conversion manually it
would take an age and I'd be bound to make mistakes.  To give an idea of
the scale of this task, the equations are in three text files, each
about 700 Kb, with about 5000 lines of about 130 characters.  (I didn't
create these, it was done for me by one Robert Lewis using his Fermat
program).

I wonder if there exists a program for converting this kind of standard
maths format into C code.  As an illustration, here are the first two of
130 lines of the expression for 'a':

a :=  4*bx^2*cy^2*dz^2 - 8*ax*bx*cy^2*dz^2 + 4*ax^2*cy^2*dz^2 -
4*c2^2*cy^2*dz^2 - 8*bx*by*cx*cy*dz^2 + 8*ax*by*cx*cy*dz^2 +
8*ay*bx*cx*cy*dz^2
- 8*ax*ay*cx*cy*dz^2 + 8*ax*bx*by*cy*dz^2 - 8*ax^2*by*cy*dz^2 +
8*c2^2*by*cy*dz^2 - 8*c1*c2*by*cy*dz^2 - 8*ay*bx^2*cy*dz^2


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gib Bogle  
View profile  
 More options Nov 13 2011, 9:29 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Mon, 14 Nov 2011 15:29:05 +1300
Local: Sun, Nov 13 2011 9:29 pm
Subject: Re: OT: translate ^ to pow()
On 14/11/2011 3:23 p.m., Gib Bogle wrote:

Hah!  This didn't explain well, because the mail reader (Mozilla
Thunderbird, anyway) translates the powers, replacing '^ 2' by
superscripted 2.  In case it still isn't clear, I have thousands of '^'
to deal with.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "translate ^ to pow()" by Rafik Zurob
Rafik Zurob  
View profile  
 More options Nov 13 2011, 9:51 pm
Newsgroups: comp.lang.fortran
From: "Rafik Zurob" <nos...@hotmail.com>
Date: Sun, 13 Nov 2011 21:51:38 -0500
Local: Sun, Nov 13 2011 9:51 pm
Subject: Re: translate ^ to pow()
You can use a regular expression to convert.  For example, I used the
following sed command:

sed "s#\([a-zA-Z_(][a-zA-Z_() ,0-9]*\)\^\([0-9][0-9]*\)# pow(\1, \2) #g"
input.txt

and got:

a :=  4* pow(bx, 2) * pow(cy, 2) * pow(dz, 2)  - 8*ax*bx* pow(cy, 2) *
pow(dz, 2)  + 4* pow(ax, 2) * pow(cy, 2) * pow(dz, 2)  -
4* pow(c2, 2) * pow(cy, 2) * pow(dz, 2)  - 8*bx*by*cx*cy* pow(dz, 2)  +
8*ax*by*cx*cy* pow(dz, 2)  +
8*ay*bx*cx*cy* pow(dz, 2)
- 8*ax*ay*cx*cy* pow(dz, 2)  + 8*ax*bx*by*cy* pow(dz, 2)  - 8* pow(ax, 2)
*by*cy* pow(dz, 2)  +
8* pow(c2, 2) *by*cy* pow(dz, 2)  - 8*c1*c2*by*cy* pow(dz, 2)  - 8*ay*
pow(bx, 2) *cy* pow(dz, 2)

You might have to adjust the regular expression further if the input can be
more complicated (like ^ of ^, or ^ split over multiple lines).

If you're on Windows, you can get sed as part of Cygwin.  Many text editors
(e.g. textpad) also provide regular expression support.

Regards

Rafik
Visit the Fortran Cafe at http://ibm.com/rational/cafe

"Gib Bogle" <g.bo...@auckland.ac.nz> wrote in message

news:j9pu3i$nq3$1@speranza.aioe.org...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gib Bogle  
View profile  
 More options Nov 13 2011, 10:34 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Mon, 14 Nov 2011 16:34:46 +1300
Local: Sun, Nov 13 2011 10:34 pm
Subject: Re: translate ^ to pow()
On 14/11/2011 3:51 p.m., Rafik Zurob wrote:

That's perfect!  One question: executing that sed command works fine,
but when I put the script into a text file and call it power.txt, this
fails:

 >sed -e power.txt input.txt
sed: -e expression #1, char 2: Extra characters after command

If I remove the quotes it fails with the same error message.  This
indeed on Windows, using sed from the UnixUtils collection of utilities.
  How should I put the command in a script file?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rafik Zurob  
View profile  
 More options Nov 13 2011, 10:44 pm
Newsgroups: comp.lang.fortran
From: "Rafik Zurob" <nos...@hotmail.com>
Date: Sun, 13 Nov 2011 22:44:33 -0500
Local: Sun, Nov 13 2011 10:44 pm
Subject: Re: translate ^ to pow()

>  How should I put the command in a script file?

You need the -f option, not the -e option.  e.g.

sed -f power.txt input.txt

where power.txt contains the regular expression without the enclosing
quotes.

Regards

Rafik


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gib Bogle  
View profile  
 More options Nov 13 2011, 10:45 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Mon, 14 Nov 2011 16:45:04 +1300
Local: Sun, Nov 13 2011 10:45 pm
Subject: Re: translate ^ to pow()
OK, I see, I need

 >sed -f power.txt input.txt

Thanks very much!

Gib


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Louis Krupp  
View profile  
 More options Nov 13 2011, 10:49 pm
Newsgroups: comp.lang.fortran
From: Louis Krupp <lkr...@nospam.indra.com.invalid>
Date: Sun, 13 Nov 2011 20:49:12 -0700
Local: Sun, Nov 13 2011 10:49 pm
Subject: Re: translate ^ to pow()
On Sun, 13 Nov 2011 21:51:38 -0500, "Rafik Zurob" <nos...@hotmail.com>
wrote:

It's amazing what you can do with sed.

If there are nested expressions, that regular expression might not
balance parentheses.  It's probably better to assume there aren't  --
the example given looks like a kind of canonical form with no
parentheses -- and try something slightly simpler (and then check for
leftover carets).

Or the OP could replace the caret by '**' and convert the expressions
to Fortran, keeping in mind that if an expression contains something
like 'a^b^c', commutativity might be an issue.

Louis


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gib Bogle  
View profile  
 More options Nov 13 2011, 11:44 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Mon, 14 Nov 2011 17:44:28 +1300
Local: Sun, Nov 13 2011 11:44 pm
Subject: Re: translate ^ to pow()
On 14/11/2011 4:49 p.m., Louis Krupp wrote:

> It's amazing what you can do with sed.

I'm impressed!

> If there are nested expressions, that regular expression might not
> balance parentheses.  It's probably better to assume there aren't  --
> the example given looks like a kind of canonical form with no
> parentheses -- and try something slightly simpler (and then check for
> leftover carets).

> Or the OP could replace the caret by '**' and convert the expressions
> to Fortran, keeping in mind that if an expression contains something
> like 'a^b^c', commutativity might be an issue.

As it turns out the format is very simple, and this works a treat.  The
problem now is going to be compiling it for the target host, which is an
embedded processor system (on a tiny board that fits in your hand, which
comes with a C compiler).  I've tested one of the three files.  It
compiles with MSVC (after a bit of a wait on a fast Win 7 box),
producing a 4.5 Mb object file.  gcc however just crashes silently.  I
fear that the size of the executable might be an issue for the tiny
computer.

The situation is quite amusing.  It would take too long to go into the
details, but what I have here is the analytical solution to 7 nonlinear
equations.  I thought it would be nice and fast to have a closed form
solver, although I've already implemented an approach (in Fortran) using
Newton-Raphson.  The ifort executable for this is 650 Kb.  Surprisingly
the numerical approach yields code that is about an order of magnitude
smaller than the analytical approach yields.  The reason, of course, is
that the (canonical?) code that the computer algebra program spits out
is extremely verbose.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
glen herrmannsfeldt  
View profile  
 More options Nov 13 2011, 11:49 pm
Newsgroups: comp.lang.fortran
From: glen herrmannsfeldt <g...@ugcs.caltech.edu>
Date: Mon, 14 Nov 2011 04:49:28 +0000 (UTC)
Local: Sun, Nov 13 2011 11:49 pm
Subject: Re: translate ^ to pow()

Gib Bogle <g.bo...@auckland.ac.nz> wrote:

(snip)

> As it turns out the format is very simple, and this works a treat.  The
> problem now is going to be compiling it for the target host, which is an
> embedded processor system (on a tiny board that fits in your hand, which
> comes with a C compiler).  I've tested one of the three files.  It
> compiles with MSVC (after a bit of a wait on a fast Win 7 box),
> producing a 4.5 Mb object file.  gcc however just crashes silently.  I
> fear that the size of the executable might be an issue for the tiny
> computer.

It might be smaller to compile to an intermediate form and then
interpret that.  If you encode just the needed operations and
operands, it should take just a few bits each.

-- glen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
glen herrmannsfeldt  
View profile  
 More options Nov 13 2011, 11:53 pm
Newsgroups: comp.lang.fortran
From: glen herrmannsfeldt <g...@ugcs.caltech.edu>
Date: Mon, 14 Nov 2011 04:53:45 +0000 (UTC)
Local: Sun, Nov 13 2011 11:53 pm
Subject: Re: translate ^ to pow()

Louis Krupp <lkr...@nospam.indra.com.invalid> wrote:
> On Sun, 13 Nov 2011 21:51:38 -0500, "Rafik Zurob" <nos...@hotmail.com>
> wrote:
>>You can use a regular expression to convert.  For example, I used the
>>following sed command:
>>sed "s#\([a-zA-Z_(][a-zA-Z_() ,0-9]*\)\^\([0-9][0-9]*\)# pow(\1, \2) #g"
>>input.txt

(snip)

> It's amazing what you can do with sed.
> If there are nested expressions, that regular expression might not
> balance parentheses.  It's probably better to assume there aren't  --
> the example given looks like a kind of canonical form with no
> parentheses -- and try something slightly simpler (and then check for
> leftover carets).

I think you can write separate sed commands for different levels
of nested parentheses.  

Otherwise, it shouldn't be so hard to do with flex and bison,
to write a real parser for it.  

-- glen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven G. Kargl  
View profile  
 More options Nov 14 2011, 12:34 am
Newsgroups: comp.lang.fortran
From: "Steven G. Kargl" <s...@REMOVEtroutmask.apl.washington.edu>
Date: Mon, 14 Nov 2011 05:34:50 +0000 (UTC)
Local: Mon, Nov 14 2011 12:34 am
Subject: Re: translate ^ to pow()

On Mon, 14 Nov 2011 17:44:28 +1300, Gib Bogle wrote:
> On 14/11/2011 4:49 p.m., Louis Krupp wrote:

>> Or the OP could replace the caret by '**' and convert the expressions
>> to Fortran, keeping in mind that if an expression contains something
>> like 'a^b^c', commutativity might be an issue.

> As it turns out the format is very simple, and this works a treat.  The
> problem now is going to be compiling it for the target host, which is an
> embedded processor system (on a tiny board that fits in your hand, which
> comes with a C compiler).  I've tested one of the three files.

The real problem (once you can get it to compile) is the inefficiency in
calling pow().  It would be much better to change the ^ to **, and compile
this with a Fortran compiler. If you can't use Fortran and must use
C, then convert x^2 to x*x etc.

Do you have with numerical round-off issues?  With an
expression like

a :=  4*bx^2*cy^2*dz^2 - 8*ax*bx*cy^2*dz^2 + 4*ax^2*cy^2*dz^2
   - 4*c2^2*cy^2*dz^2 - 8*bx*by*cx*cy*dz^2 + 8*ax*by*cx*cy*dz^2
   + 8*ay*bx*cx*cy*dz^2 - 8*ax*ay*cx*cy*dz^2 + 8*ax*bx*by*cy*dz^2
   - 8*ax^2*by*cy*dz^2 + 8*c2^2*by*cy*dz^2 - 8*c1*c2*by*cy*dz^2
   - 8*ay*bx^2*cy*dz^2

subtracting nearly equal large numbers typically yields erroneous
rsults.

--
steve


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
glen herrmannsfeldt  
View profile  
 More options Nov 14 2011, 2:44 am
Newsgroups: comp.lang.fortran
From: glen herrmannsfeldt <g...@ugcs.caltech.edu>
Date: Mon, 14 Nov 2011 07:44:35 +0000 (UTC)
Local: Mon, Nov 14 2011 2:44 am
Subject: Re: translate ^ to pow()
Steven G. Kargl <s...@removetroutmask.apl.washington.edu> wrote:

(snip of converting complicated expressions to use pow())

> The real problem (once you can get it to compile) is the inefficiency in
> calling pow().  It would be much better to change the ^ to **, and compile
> this with a Fortran compiler. If you can't use Fortran and must use
> C, then convert x^2 to x*x etc.

Personally, I would like to see a separate function, such as ipow(),
for integer powers.  However, there is no requirement that the
implementation actually use a function call, and it can optimize
the case of integer powers, especially integer constants.

As a quick test with gcc:

#include <stdio.h>
#include <math.h>

int main() {
   double x,y;
   while(scanf("%lf",&x)>0) printf("%f\n",pow(x,2));

}

gcc does figure it out and generate a multiply instruction
without any function call.

I didn't check what it does with in integer variable.

> Do you have with numerical round-off issues?  With an expression like
> a :=  4*bx^2*cy^2*dz^2 - 8*ax*bx*cy^2*dz^2 + 4*ax^2*cy^2*dz^2
>   - 4*c2^2*cy^2*dz^2 - 8*bx*by*cx*cy*dz^2 + 8*ax*by*cx*cy*dz^2
>   + 8*ay*bx*cx*cy*dz^2 - 8*ax*ay*cx*cy*dz^2 + 8*ax*bx*by*cy*dz^2
>   - 8*ax^2*by*cy*dz^2 + 8*c2^2*by*cy*dz^2 - 8*c1*c2*by*cy*dz^2
>   - 8*ay*bx^2*cy*dz^2
> subtracting nearly equal large numbers typically yields erroneous
> rsults.

That certainly could cause problems.  Factoring, if it could be
factored, might help.

-- glen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
glen herrmannsfeldt  
View profile  
 More options Nov 14 2011, 2:59 am
Newsgroups: comp.lang.fortran
From: glen herrmannsfeldt <g...@ugcs.caltech.edu>
Date: Mon, 14 Nov 2011 07:59:02 +0000 (UTC)
Local: Mon, Nov 14 2011 2:59 am
Subject: Re: translate ^ to pow()

glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> Steven G. Kargl <s...@removetroutmask.apl.washington.edu> wrote:
> (snip of converting complicated expressions to use pow())
>> The real problem (once you can get it to compile) is the
>> inefficiency in calling pow().  

(snip, then I wrote)

> Personally, I would like to see a separate function, such as ipow(),
> for integer powers.  However, there is no requirement that the
> implementation actually use a function call, and it can optimize
> the case of integer powers, especially integer constants.
> As a quick test with gcc:

(snip)

Further tests show that for anything other than a constant 2,
even a constant 3, gcc calls the actual pow() function.

I suppose the case of a constant 2 is the largest fraction,
but it wouldn't be hard to special case other constants, or
even integer variables (which are converted according to
the prototype in scope, but the compiler still knows that
they are integer at compile time).

-- glen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gib Bogle  
View profile  
 More options Nov 14 2011, 3:47 am
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Mon, 14 Nov 2011 21:47:14 +1300
Local: Mon, Nov 14 2011 3:47 am
Subject: Re: translate ^ to pow()
On 14/11/2011 6:34 p.m., Steven G. Kargl wrote:

It's highly likely that Fortran will not be available on the tiny computer.

Maybe some kind soul will show me the sed script that turns x^2 into
x*x, y^3 into y*y*y, and z^4 into z*z*z*z.  I've never worked with
regular expressions, and I have a feeling that my brain is too ossified
to start now.

Another option is to replace pow() by ipow(), then in the function use
*.  Would that make a difference?

The easiest way to help the compiler swallow this big mouthful would be
to cut each expression (actually just the three big ones - 700, 1400 and
3100 lines) up into many pieces.  I presume it's the length of the
expressions that is creating the problem.  It wouldn't be much effort to
create pieces no more than 100 lines long.

As far as roundoff is concerned, at this stage I don't have a clue about
this.  I suspect it will not be an issue.

If all else fails, the fall-back is to pass the info to another computer
via a serial link, use a Fortran program to get the solution (either
analytical or Newton-Raphson, which works well), then pass it back.  The
hitch here is that the computation has to be done within a 10 ms
time-slice, together with various other things, and therefore we'd
prefer to keep communications to a minimum.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Louis Krupp  
View profile  
 More options Nov 14 2011, 4:50 am
Newsgroups: comp.lang.fortran
From: Louis Krupp <lkr...@nospam.indra.com.invalid>
Date: Mon, 14 Nov 2011 02:50:01 -0700
Local: Mon, Nov 14 2011 4:50 am
Subject: Re: translate ^ to pow()
On Mon, 14 Nov 2011 21:47:14 +1300, Gib Bogle <g.bo...@auckland.ac.nz>
wrote:

<snip>

>Maybe some kind soul will show me the sed script that turns x^2 into
>x*x, y^3 into y*y*y, and z^4 into z*z*z*z.  I've never worked with
>regular expressions, and I have a feeling that my brain is too ossified
>to start now.

<snip>

I would put the following into the file:

s#\([a-zA-Z_][a-zA-Z_0-9]*\)^2#\1*\1#g
s#\([a-zA-Z_][a-zA-Z_0-9]*\)^3#\1*\1*\1#g
s#\([a-zA-Z_][a-zA-Z_0-9]*\)^4#\1*\1*\1*\1#g

etc., for as many powers as you need, and call it good.

If you're not going to balance parentheses, I would keep it simple and
leave them out entirely.  I would search the file for carets after
running sed, and if you find any, then you know you have something
else to deal with.

Expanding on Glen's suggestion of an intermediate form, you might be
able to put your variables (c1, c2, ax, bx, cx, ay, cy, dy, etc) in an
array and have a parallel arrary with exponents.  I.e., if

v[1] = c1
v[2] = c2
v[3] = ax
v[4] = bx

and you want to evaluate 3*c2*ax^2, then set

e[0] = 3
e[2] = 1
e[3] = 2

and all other members of e[] to 0.

To evaluate things like c1* bx^3 - 3*c3^ax, you could have a
two-dimensional array and add a level of nesting.

Louis


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gib Bogle  
View profile  
 More options Nov 14 2011, 5:48 am
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Mon, 14 Nov 2011 23:48:01 +1300
Local: Mon, Nov 14 2011 5:48 am
Subject: Re: translate ^ to pow()
On 14/11/2011 10:50 p.m., Louis Krupp wrote:

Thanks Louis.  I've just realized there is another rather simple option
that will speed things up a bit.  I could just globally replace ^ with
_, then at the start of the function define all the powers like this:

ax_2 = ax*ax;
ax_3 = ax*ax*ax;
ax_4 = ax*ax*ax*ax;

etc.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ron Shepard  
View profile  
 More options Nov 14 2011, 7:55 am
Newsgroups: comp.lang.fortran
From: Ron Shepard <ron-shep...@NOSPAM.comcast.net>
Date: Mon, 14 Nov 2011 06:55:17 -0600
Local: Mon, Nov 14 2011 7:55 am
Subject: Re: translate ^ to pow()
In article <j9qrl2$e...@speranza.aioe.org>,
 Gib Bogle <g.bo...@auckland.ac.nz> wrote:

> Thanks Louis.  I've just realized there is another rather simple option
> that will speed things up a bit.  I could just globally replace ^ with
> _, then at the start of the function define all the powers like this:

> ax_2 = ax*ax;
> ax_3 = ax*ax*ax;
> ax_4 = ax*ax*ax*ax;

ax_3 = ax_2 * ax
ax_4 = ax_3 * ax

and so on.  Also, you might try to run your expression through a
symbolic algebra program to look for common subexpressions and other
such simplifications.  This may both shorten the code and make the
execution faster.

$.02 -Ron Shepard


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "OT: translate ^ to pow()" by mecej4
mecej4  
View profile  
 More options Nov 14 2011, 8:35 am
Newsgroups: comp.lang.fortran
From: mecej4 <mec...@NOSPAM.operamail.com>
Date: Mon, 14 Nov 2011 07:35:25 -0600
Local: Mon, Nov 14 2011 8:35 am
Subject: Re: OT: translate ^ to pow()
On 11/13/2011 8:23 PM, Gib Bogle wrote:

All those power evaluations can be expensive.

If you (or a colleague) has access to Maple, you can do the following (I
take a polynomial in x as an example):

      a := x^6+3*x^4+x^3+5*x^2+11*x+15;
      with(codegen): C(horner(a,x),optimized);

and you would see the output (C source, ready to be pasted in):

      t1 = x*x;
      t10 = 15.0+(11.0+(5.0+(1.0+(3.0+t1)*x)*x)*x)*x;

-- mecej4


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Prince  
View profile  
 More options Nov 14 2011, 11:50 am
Newsgroups: comp.lang.fortran
From: Tim Prince <tpri...@computer.org>
Date: Mon, 14 Nov 2011 11:50:49 -0500
Local: Mon, Nov 14 2011 11:50 am
Subject: Re: OT: translate ^ to pow()
On 11/14/2011 8:35 AM, mecej4 wrote:

These associations are permitted by Fortran (although not by C, why is C
being discussed here?) and many compilers will go part way.  As a
consequence, each compiler may produce slightly different numerical
results; those variations may be reduced by writing the associations
into the source.

--
Tim Prince


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mecej4  
View profile  
 More options Nov 14 2011, 1:33 pm
Newsgroups: comp.lang.fortran
From: mecej4 <mec...@NOSPAM.operamail.com>
Date: Mon, 14 Nov 2011 12:33:17 -0600
Local: Mon, Nov 14 2011 1:33 pm
Subject: Re: OT: translate ^ to pow()
On 11/14/2011 10:50 AM, Tim Prince wrote:

> ... why is C being discussed here?

This thread is in C.L.F. not because of Fortran association; the
original post said, "posters here have good general programming
knowledge and may be able to help" and "I need to use these expressions
in a C program."

-- mecej4


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "translate ^ to pow()" by Gib Bogle
Gib Bogle  
View profile  
 More options Nov 14 2011, 3:17 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Tue, 15 Nov 2011 09:17:35 +1300
Local: Mon, Nov 14 2011 3:17 pm
Subject: Re: translate ^ to pow()
On 15/11/2011 1:55 a.m., Ron Shepard wrote:

The idea of simplifying the expression systematically had occurred to me
(as to mecej4 as well).  I don't have ready access to Maple (but
possibly could locate someone who has it).  Do you know of any free
program with the required capability?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "OT: translate ^ to pow()" by Robert Miles
Robert Miles  
View profile  
 More options Nov 14 2011, 3:44 pm
Newsgroups: comp.lang.fortran
From: Robert Miles <mile...@Usenet-News.net>
Date: Mon, 14 Nov 2011 14:44:09 -0600
Local: Mon, Nov 14 2011 3:44 pm
Subject: Re: OT: translate ^ to pow()
On 11/13/2011 8:23 PM, Gib Bogle wrote:

I haven't seen such a program, but that might be because I haven't
been looking for one.

If you decide to write such a program yourself, some suggestions
for these lines:

1.  Replace the ":=" with "=".

2.  Add a semicolon at the end of the last line.

3.  For each of the groups after the "=" with no spaces included,
add parentheses around that group.  For example, "4*bx^2*cy^2*dz^2"
becomes "(4*bx^2*cy^2*dz^2)".

4.  For each of the powers, create a new variable for that power of
that variable.  For example,

x2 = x * x;
y2 = y * y;
z2 = z * z;

Or, if you prefer, the equivalent using pow().

Depending on your C compiler's level of optimization, this may
speed up the code by avoiding calculating those powers over and
over.

5.  Wherever two variables are adjacent, insert a "*" between
them.  For example, "bx^2" becomes " b*x^2".

6.  Using some text editing program, replace such powers with the
corresponding new variable; or just have your program do that.
For example, replace "x^2" with "x2", "y^2" with "y2", and "z^2"
with "z2".

7.  Note that I haven't mentioned writing the first few lines,
including declarations of all the variables, or the last few.
Those should be short enough to do them by hand.

If that's not adequate, show us more about what fails.

Is there any particular reason for not converting it into
Fortran instead?

Robert Miles


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "translate ^ to pow()" by Robert Miles
Robert Miles  
View profile  
 More options Nov 14 2011, 5:08 pm
Newsgroups: comp.lang.fortran
From: Robert Miles <mile...@Usenet-News.net>
Date: Mon, 14 Nov 2011 16:08:49 -0600
Local: Mon, Nov 14 2011 5:08 pm
Subject: Re: translate ^ to pow()
On 11/14/2011 2:17 PM, Gib Bogle wrote:

Maple is available free.  Do you have a suitable computer to run it on?

http://download.cnet.com/Maple/3000-2053_4-43766.html

Robert Miles


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gib Bogle  
View profile  
 More options Nov 14 2011, 5:50 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Tue, 15 Nov 2011 11:50:50 +1300
Local: Mon, Nov 14 2011 5:50 pm
Subject: Re: translate ^ to pow()
On 15/11/2011 11:08 a.m., Robert Miles wrote:

> Maple is available free. Do you have a suitable computer to run it on?

> http://download.cnet.com/Maple/3000-2053_4-43766.html

> Robert Miles

That is an upgrade to Maple.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "OT: translate ^ to pow()" by Gib Bogle
Gib Bogle  
View profile  
 More options Nov 14 2011, 6:04 pm
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.ac.nz>
Date: Tue, 15 Nov 2011 12:04:37 +1300
Local: Mon, Nov 14 2011 6:04 pm
Subject: Re: OT: translate ^ to pow()
On 15/11/2011 2:35 a.m., mecej4 wrote:

I managed to locate an old Matlab CD, and I've installed it on a Win32
machine.  So I now do have Maple capability, albeit a few years old.
Meanwhile Bob Lewis has pointed out something that should have been
obvious but I missed it.  All the variables appearing in the expressions
are constant parameters except for the c1, c2 and c3.  To be more
precise, they are not known now, but will be determined in advance each
time the device is deployed, in an initial setup stage.  What this means
is that if I could represent the expressions as polynomials in c1, c2
and c3, with coefficients given as functions of ax, ay, az etc, it would
be a simple matter to precompute the coefficients in the setup stage,
then pass these values to the program that uses the expressions, which
would now be very small and fast.

My new question, then, is can I use Matlab symbolics (i.e. Maple) to
extract the coefficients of an expression thought of as a polynomial in
c1, c2 and c3?  BTW this old version of Matlab has the ability to
generate C code from a symbolic expression.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 46   Newer >
« Back to Discussions « Newer topic     Older topic »