• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Apply function names as parameters in other functions

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of my project is to apply function names as parameters in other functions. For that purpose we consider a function F which takes three real-valued arguments and returns a three-dimensional vector [p, q, r]. So, F is a mapping from R3 into R3, that is [p, q, r] = F(x, y, z).
All what i need is a place to start on it.....I'll appreciate it if anyone helps me with any tip,site,idea that i can searh to solve my problem
Thanks.
Below are the complete description of my project:
--------------------------------------------------
This Programming Project represents 15% of your overal grade. The purpose of this project is to apply function names as parameters in other functions. For that purpose we consider a function F which takes three real-valued arguments and returns a three-dimensional vector [p, q, r]. So, F is a mapping from R3 into R3, that is [p, q, r] = F(x, y, z).

The function F can be represented obviously by three separate functions, f, g, h, each of which takes three real-valued arguments and returns a real value. So, the vector equation [p, q, r] = F(x, y, z) can be written as three separate equations,

p = f(x,y,z)
q = g(x,y,z)
r = h(x,y,z)
Now, the function F may or may not have a fixed point by which we mean a vector [a, b, c] such that [a, b, c] = F(a, b, c).

A possible attempt at finding such a fixed point is to start with some initial values, x = x0 , y = y0 , and z = z0 , and repeat the following computations

x = f(x,y,z);
y = g(x,y,z);
z = h(x,y,z);
If the functions are nice and the initial values are well-chosen then this iteration will eventually converge to a three-dimensional fixed point, [a, b, c], that remains unchanged under the mapping F, i.e.


[a, b, c] = [ f(a, b, c), g(a, b, c), h(a, b, c)]
Now, this iteration should be performed by a method called fixer which is actually a higher-order function that takes six parameters namely, the three function names f, g, and h, and three real values for the initial values of x, y, and z.

For an example, apply the fixer method to the following fuctions,

double f1(double x, double y, double z)
{return ((7-2*y*y)/5+x)/2; }

double g1(double x, double y, double z)
{return (((10-(2*x+3)*x*x)/3)+2*z -x)/3; }

double h1(double x, double y, double z)
{return (x+y)/2; }

using the initial values, x = 0, y = 1, z = 2. Limit the number of iterations to 200. Also, your fixer should print out the current values of x, y, and z at every step of the iteration so that you may see the progress of the computation.

Then you should also use the exact same fixer method for solving simultaneous linear equations of the form

a1,1 x + a1,2 y + a1,3 z = b1

a2,1 x + a2,2 y + a2,3 z = b2

a3,1 x + a3,2 y + a3,3 z = b3

where the coefficients, a1,1 , a2,2 , and a3,3 are nonzero, so that the equations can be brought to the explicit form

x = (b1 - a1,2 y - a1,3 z) / a1,1 , etc.

Your program should read the coefficient matrix A and the right-hand side vector B from an input file where the input data are represented as floating point numbers. Your program, of course, should also include the definitions of the three linear functions obtained from the above linear equations. Write this program in Java where you cannot pass function names directly as parameters to other functions, but you can make use of interfaces or abstract functions.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Write this program in Java where you cannot pass function names directly as parameters to other functions, but you can make use of interfaces or abstract functions.



That's about the only thing I understood! You can't pass function names (well, you can with reflection but let's not go there) so you have to pass something that implements the functions.

Have you studied Java interfaces at all yet? An interface describes a function in terms of name, return type and arguments but doesn't provide any code for the function. You have to "implement" the interface to do that.

Say we have an interface IFunctionF that describes funtion f

Now if you make a class that implements IFunctionF the compiler will require you to write the method:

That's one part of the puzzle. The next is fixer. What does it look like? Maybe ...

When fixer gets an argument of type IFunctionF it knows that the argument is an object that implements the method f. fixer can call funkf.f(x, y, z).

The last part of the puzzle is to call fixer you'll have to create and pass along an object that implements IFunctionF like MyFunctionF above.

That's a blazingly fast course on interfaces with some things you can actually use. (In fact I may get spanked by the gang for doing a little too much of your homework, but I was too lazy to make up abstract examples.) Did it make any sense?

Try some very small steps - maybe just create the interfaces, or sketch an English (not java) description of what you think fixer() will look like - and post some code. We'll help you take the next step.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Stan is talking about is known as a "functor" or a function which is an object. Take a look at the Jakarta Commons Functor library if you want something that's pre-built.
 
carl varola
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all thank you all for the help.
Here are my java classes that i got,my beggest problem is i do not know exactly what i need to get and is not how to implement it in Java,i believe if i do understand what the logic i can maybe develop a solution,so may question now "If you take a look at my method called [fixer] below and if you can help me what do i need to get(to output) there it will be great"
Thanks again
-----------------------------------------------------
public class F1 extends P2Abstract
{
public double fgh(double x, double y, double z)
{
return ((7-2*y*y)/5+x)/2;
}
}
--------------------------------------------------------
public class G1 extends P2Abstract
{
public double fgh(double x, double y, double z)
{
return (((10-(2*x+3)*x*x)/3)+2*z -x)/3;
}
}
--------------------------------------------------------
public class H1 extends P2Abstract
{
public double fgh(double x, double y, double z)
{
return (x+y)/2;
}
}
--------------------------------------------------------
public class P2
{
public static void fixer( int x, int y, int z, P2Abstract f1, P2Abstract g1,P2Abstract h1)
{
double result1 =f1.fgh(x,y,z);
double result2 =g1.fgh(x,y,z);
double result3 =h1.fgh(x,y,z);

//what do i need to get here?
}
public static void main(String arg[])
{
P2Abstract f1 = new F1();
P2Abstract g1 = new G1();
P2Abstract h1 = new H1();
fixer(0,1,2,f1,g1,h1);
}
}
------------------------------------------------
public abstract class P2Abstract
{
public abstract double fgh(double x, double y, double z) ;
}
 
carl varola
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the output that i must get,so i hope that will help somehow to figure out what logic we should have in the fixer method.
Thanks
---------------------------------------------------------
Starting fixpoint iteration
(n=1) --> x = 0.0 y = 1.0 z = 2.0
(n=2) --> x = 0.5 y = 2.1666666666666665 z = 1.3333333333333333
(n=3) --> x = 0.011111111111111238 y = 1.996254839201341 z = 1.0036829751562262
(n=4) --> x = -0.09145112105139877 y = 1.8080989954630695 z = 0.8583239372058353
(n=5) --> x = 4.300439953883739E-4 y = 1.6831836595862522 z = 0.8418068517908203
(n=6) --> x = 0.13359357561806054 y = 1.6213055666019578 z = 0.8774495711100092
(n=7) --> x = 0.2410704397501312 y = 1.5932357378003923 z = 0.9171530887752617
(n=8) --> x = 0.3128551966341935 y = 1.5788304782933045 z = 0.945842837463749
(n=9) --> x = 0.3578864624795238 y = 1.5694968149017559 z = 0.9636916386906398
(n=10) --> x = 0.38627918084241064
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain in English what you are trying to accomplish with the fixer() method? Honestly I have no idea what should go there yet as I don't know what it should do. If you can explain it in words, we can help you from there.

Layne
 
carl varola
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do have the same question and that is what i'm looking for.......I'm not really looking for any implementation............i like just to understand what do we need to have in the fixer method............what type of logic should be there..............what should be written there to get the output that i've included in my previous post.
Please if anyone can take a look at the first post where i've post the requirements for this problem that may solve our problem.....i kept reading and reading the requiremements and cannot figure out what do i need to accomplish with the fixer method to get the above output.
Thanks a bunch
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was a music major so the point of the math is beyond me, but the instructions talk about iterating - repeating the operation - until the results don't change any more. So you'd repeat:

unil (x,y,z) == (x',y',z') or 200 times.
 
carl varola
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the implementation for the fixer method...........i'm not really sure if it right or wrong...........am i on the right track?
----------------------------------------------------------
public static void fixer( int x, int y, int z, P2Abstract f1, P2Abstract g1,P2Abstract h1)
{
double result1 =f1.fgh(x,y,z);
double result2 =g1.fgh(x,y,z);
double result3 =h1.fgh(x,y,z);

double result1A =f1.fgh(result1,result2,result3);
double result2A =g1.fgh(result1,result2,result3);
double result3A =h1.fgh(result1,result2,result3);

for(int i=1;i<=200;i=i+1)
{
if((result1==result1A)&&(result2==result2A)&&(result3==result3A))
{
System.out.println("result1A= "+result1A);
System.out.println("result2A= "+result2A);
System.out.println("result3A= "+result3A);
}
else
{
result1=result1A;
result2=result2A;
result3=result3A;
result1A =f1.fgh(result1,result2,result3);
result2A =g1.fgh(result1,result2,result3);
result3A =h1.fgh(result1,result2,result3);
System.out.println("( "+i+" )"+" result1A= "+result1A+" result2A= "+result2A+" result3A= "+result3A);
}
}
}
----------------------------------------------------------------------

This is the output which is so wierd,because it starts with iteration number 52 and i do not know why...............any idea plz?
9974569544452454
( 52 ) result1A= 0.48415335379672464 result2A= 1.511302667884595 result3A= 0.9
986966916772468
( 53 ) result1A= 0.4852695261073434 result2A= 1.5121701397901912 result3A= 0.9
977280108406599
( 54 ) result1A= 0.4853030567190544 result2A= 1.5106167878567618 result3A= 0.9
987198329487673
( 55 ) result1A= 0.48625891240859104 result2A= 1.5112507133925046 result3A= 0.
9979599222879081
( 56 ) result1A= 0.48635371245842474 result2A= 1.509965552761015 result3A= 0.9
987548129005478
( 57 ) result1A= 0.4871776621242369 result2A= 1.5104181988264653 result3A= 0.9
981596326097198
( 58 ) result1A= 0.48731620399288167 result2A= 1.5093492302151452 result3A= 0.
9987979304753511
( 59 ) result1A= 0.4880310822462305 result2A= 1.5096616513248156 result3A= 0.9
983327171040135
( 60 ) result1A= 0.4881998808269614 result2A= 1.508767453893179 result3A= 0.99
8846366785523
( 61 ) result1A= 0.4888240944280195 result2A= 1.508971880422167 result3A= 0.99
84836673600702
( 62 ) result1A= 0.48901282003304763 result2A= 1.5082194091687453 result3A= 0.
9988979874250933
( 63 ) result1A= 0.48956125277786 result2A= 1.5083411242516052 result3A= 0.998
6161146008965
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At a quick read it looks like the right logic, except that it should maybe terminate when all three terms are equal. Try displaying the three results on every iteration instead of only when not equal and see if the first 51 iterations show anything interesting.
 
carl varola
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes now i got the output that starts with 1,please take a look at my out put below.
I'm really looking to get same output as my teacher output(posted below too),i do not know why we are getting different output here...........any ideas?
My teacher output for the first iteration (where n=1) is "(n=1) --> x = 0.0 y = 1.0 z = 2.0
"
Why we got something different,how can i get same output?
Also i have another question if i use while loop what is my condition should be?
Below is :
My latest fixer implementation
My output
My teacher output.
---------------------------------------------------------------------------------------------
My fixer codes
----------------------------------------------------------------------------------------------
public static void fixer( int x, int y, int z, P2Abstract f1, P2Abstract g1,P2Abstract h1)
{
double result1 =f1.fgh(x,y,z);
double result2 =g1.fgh(x,y,z);
double result3 =h1.fgh(x,y,z);

double result1A =f1.fgh(result1,result2,result3);
double result2A =g1.fgh(result1,result2,result3);
double result3A =h1.fgh(result1,result2,result3);

for(int n=1;n<=200;n=n+1)
{
if((Math.abs(result1A -result1)<0.00000000000000001)&&(Math.abs(result2A -result2)<0.00000000000000001)&&(Math.abs(result3A -result3)<0.00000000000000001))
{
System.out.println("result1A= "+result1A);
System.out.println("result2A= "+result2A);
System.out.println("result3A= "+result3A);
}
else
{
result1=result1A;
result2=result2A;
result3=result3A;
result1A =f1.fgh(result1,result2,result3);
result2A =g1.fgh(result1,result2,result3);
result3A =h1.fgh(result1,result2,result3);
System.out.println("(n="+n+")"+" x="+result1A+" y="+result2A+" z="+result3A);
}
}
}
--------------------------------------------------------------------------------------------------------
My output
------------------------------------------------------------------------------------------------------
(n=1) x=0.30524691358024675 y=2.1575319170477587 z=0.4608024691358024
(n=2) x=-0.07836533782583202 y=1.2791848736566658 z=1.2313894153140028
(n=3) x=0.33355454288868003 y=1.9562190699664586 z=0.6004097679154169
(n=4) x=0.1014186615042528 y=1.354866381620164 z=1.1448868064275692
(n=5) x=0.3835767483432232 y=1.836902365154198 z=0.7281425215622084
(n=6) x=0.21694631434979428 y=1.407095484902363 z=1.1102395567487107
(n=7) x=0.41248961644837395 y=1.7609977584202499 z=0.8120208996260786
(n=8) x=0.2860221871919581 y=1.4426494657576177 z=1.086743687434312
(n=9) x=0.42676359738583114 y=1.7077968096161893 z=0.8643358264747879
(n=10) x=0.3300678101058686 y=1.46709913018773 z=1.0672802035010103
(n=11) x=0.4345579334934155 y=1.6683027996284387 z=0.8985834701467994
(n=12) x=0.36063212049709037 y=1.4841312075602087 z=1.051430366560927
(n=13) x=0.4397869719977206 y=1.6380794287092282 z=0.9223816640286495
(n=14) x=0.38323264304679006 y=1.4960634066114549 z=1.0389332003534744
(n=15) x=0.44397517820302074 y=1.6145256490484332 z=0.9396480248291225
(n=16) x=0.4006489748144575 y=1.5043992586401957 z=1.029250413625727
(n=17) x=0.4476810615277946 y=1.595930303008765 z=0.9525241167273266
(n=18) x=0.41444182435156757 y=1.5101555036920216 z=1.0218056822682797
(n=19) x=0.45110698310952313 y=1.5810946454009989 z=0.9622986640217945
(n=20) x=0.42558143601161946 y=1.514042245374361 z=1.016100814255261
-------------------------------------------------------------------------------------------
My Teacher's output
-----------------------------------------------------------------------------------------------
Starting fixpoint iteration
(n=1) --> x = 0.0 y = 1.0 z = 2.0
(n=2) --> x = 0.5 y = 2.1666666666666665 z = 1.3333333333333333
(n=3) --> x = 0.011111111111111238 y = 1.996254839201341 z = 1.0036829751562262
(n=4) --> x = -0.09145112105139877 y = 1.8080989954630695 z = 0.8583239372058353
(n=5) --> x = 4.300439953883739E-4 y = 1.6831836595862522 z = 0.8418068517908203
(n=6) --> x = 0.13359357561806054 y = 1.6213055666019578 z = 0.8774495711100092
(n=7) --> x = 0.2410704397501312 y = 1.5932357378003923 z = 0.9171530887752617
(n=8) --> x = 0.3128551966341935 y = 1.5788304782933045 z = 0.945842837463749
(n=9) --> x = 0.3578864624795238 y = 1.5694968149017559 z = 0.9636916386906398
(n=10) --> x = 0.38627918084241064 y = 1.5622669777144358 z = 0.9742730792784232
(n=11) --> x = 0.40500396848980585 y = 1.5561864190106816 z = 0.9805951937502437
(n=12) --> x = 0.41815875010224524 y = 1.5509209932669688 z = 0.984539871684607
(n=13) --> x = 0.42800818957988246 y = 1.546314124994823 z = 0.9871611572873527
(n=14) --> x = 0.4357866201582403 y = 1.54226185479549 z = 0.9890242374768652
(n=15) --> x = 0.4421789843276751 y = 1.538681116200059 z = 0.9904300502638671
(n=16) --> x = 0.44758157669370563 y = 1.5355022654488326 z = 0.9915419210712692
(n=17) --> x = 0.45223734690715334 y = 1.5326668575060929 z = 0.992452102206623
(n=18) --> x = 0.4563051342340563 y = 1.5301261674225144 z = 0.9932156508282853
(n=19) --> x = 0.4598953494708057 y = 1.5278397214722272 z = 0.9938675354715165
(n=20) --> x = 0.4630888318337363 y = 1.5257738829054919 z = 0.9944313573696141
(n=21) --> x = 0.46594722756556783 y = 1.5239005960234582 z = 0.994923911794513
(n=22) --> x = 0.4685190084706537 y = 1.5221963321024994 z = 0.9953576702865765
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like your F, G, and H functions may not be returning the expected values even the first time through. I never trust my reading of operator precedence and always use enough parens or intermediate results to make sure there is nothing ambiguous about math expressions. I'd start debugging there to make sure they are returning what you expect.

I see you've added a fuzz factor of 0.00000000000000001 in your comparisons. That's a mighty small number. I'm not sure what your instructor expects, but after you get the basic calculations to line up, if you're still doing too many iterations you might try larger numbers to relax your precision a bit.
 
reply
    Bookmark Topic Watch Topic
  • New Topic