Click here to Skip to main content
15,902,026 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
C++
for(i=0;i<500;i++)
{
    x=valuex[i];
    y=valuey[i];

    A[i][0]=x+y;
    A[i][1]=x*x+3*y;
    A[i][2]=x+2*y*x;
    ……
    A[i][99]=x/2+20*y;
}

In the above code,there are 100 terms:A[i][0]~A[i][99], In fact, I want to use the first n terms of A[i][j],and "n" is the input number by user, how can I realize this efficiently,Thank you for your help.
Best wishes.
Posted
Updated 25-Jun-12 19:45pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Jun-12 23:04pm    
What is your criteria for "effectiveness" and what's the problem? What have you done so far?
Besides, I don't know what is the general algorithm to build an expression for right side of assignment to A[i][N] for any N (hidden under '......'). Are you going to write all 100 terms manually, 100 lines of code? Would look very discouraging... :-)
--SA
Angela2012 26-Jun-12 2:32am    
Thank you for your help, it's difficult for me to express myself clearly in English, and I learned c++ for only serval months. some problems are too difficult for me. The 100 terms,which stands for zernike polynomials can be generated by writing a function with a input "n"(first n terms ),
http://research.opt.indiana.edu/Library/HVO/ZernikeStringR.m
but the calculation speed will be slow(verified using matlab :0.1s vs 10s): at least three for loops and many if...else...will be used to generate a single item. so I decide to write all 100 terms manually,
because speed is a prerequisite.
Sergey Alexandrovich Kryukov 25-Jun-12 23:30pm    
Besides, A[i][0]~A[i][100] would be 101 terms, not 100.

Here is one big problem with your request is: you are trying to get a solution from experts, but only for a part of the problem, not explaining the rest of it and the final goal. A person like myself will consider it as a waste of time, because I would seriously suspect the rest of it is already wrong. Nobody wants to give help which won't be used.

--SA
Angela2012 26-Jun-12 2:55am    
Thank you for your advice, It's difficult to explain the whole problem in several sentences, any of your help will be appreciate.
Sandeep Mewara 26-Jun-12 2:40am    
It just looks like a Homework question for now. Can you share your thoughts and efforts around it?

Nice question, although I don't see what the real application is. When I understood your question correctly, you want to compute only the first n terms of
A[i][0]=x+y;
A[i][1]=x*x+3*y;
A[i][2]=x+2*y*x;
……
A[i][99]=x/2+20*y;


thus n being the limit for j. Correct?

There are (at least) two principal approaches of how you can go about that:

(1) Interspersing if statements

while (1)
{
    if (0 >= n) break;
    A[i][0]=x+y;

    if (1 >= n) break;
    A[i][1]=x*x+3*y;

    if (2 >= n) break;
    A[i][2]=x+2*y*x;
     ……

    if (99 >= n) break;
    A[i][99]=x/2+20*y;
    break;
}


That is not elegant, but probably faster than approach number 2:

(2) Placing the arithmetic terms in an array of functions and then looping over that array.

typedef double PolyFunc (double x, double y);

double f0 (double x, double y)
    {return x + y;}

double f1 (double x, double y)
    {return x*x + 3*y;}

...

static PolyFunc* sFunctions[100] = {&f0, &f1, ...};

void ComputeArray (int n)
{
    ...
    for (int j = 0; j < n; ++j)
        A[i][j] = sFunctions[j] (x, y);
}


Hope that helps.
 
Share this answer
 
Comments
stib_markc 26-Jun-12 6:46am    
5!
nv3 26-Jun-12 7:23am    
Thanks!
There are many questions about your question...
but...

C#
switch (n)
{
case 99: A[i][99]=x/2+20*y;
...
case 2:   A[i][2]=x+2*y*x;
case 1:   A[i][1]=x*x+3*y;
case 0:   A[i][0]=x+y;
}


will be very fast. Using switch let the compilers make some optimization over the if variant.
A bit confusing bot probably faster:

C#
switch (99-n)
{
case 0:   A[i][99]=x/2+20*y;
...
case 97:   A[i][2]=x+2*y*x;
case 98:   A[i][1]=x*x+3*y;
case 99:   A[i][0]=x+y;

}
 
Share this answer
 
v2
Comments
nv3 26-Jun-12 9:53am    
Very nice idea! 5.
Angela2012 26-Jun-12 22:42pm    
can you explain in detail why "swith(99-n){}" is faster than "switch(n){}",thank you very much.
qPCR4vir 27-Jun-12 7:24am    
Actually I don’t know. That is the compiler job. I´m intending to give a tip to the compiler. It will code “switch(n)“ into something like jmp DWORD PTR $LN318@main[edi*4], (edi is our n) that is, a lookup table. (See: http://stackoverflow.com/q/2596320/1458030). I “feel” that with (99-n) this table is simpler or more efficient implemented. But a very gut compiler will figure out by self more than I can. In that case both variant will be equal fast.
qPCR4vir 27-Jun-12 7:28am    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900