Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to square a function but it looks like I can't multiply func<>...
I have a function f, and i would like to create a g function which will give f² : g=f²


With the code I tried, i got an error telling me I can't apply the multiplier * to a function f * f

Anybody has a solution ?
Thanks in advance.

What I have tried:

C#
double cste = 10;
public Func<double, double> f
{
	get { return F => cste  * F; }
}

private Func<double, double> g
{
	get 
	{
		// ERROR BELOW :
		Func<Func<double, double>, Func<double, double>> G = f => f * f;
		return G;
	}
}
Posted
Updated 8-May-22 10:41am
v3
Comments
0x01AA 8-May-22 12:20pm    
*lol* over complicate a simple thing (and no longer see the forest for the trees). Since when a Square function needs two arguments?
Richard MacCutchan 8-May-22 12:30pm    
You cannot "square a function", only a number.
0x01AA 8-May-22 12:33pm    
I think theoretically it is possible, but it is questionable :-)
Richard MacCutchan 8-May-22 12:36pm    
Well that all depends on what the OP actually means.
0x01AA 8-May-22 12:39pm    
Yep. And for me it looks suspicious because a square function does not need two parameters.
Curious where this ends.

In Math, functions are pretty much multipliable,
If you have f(x) = 2x, and g(x) = x+5,
then f.g(x) = 2x * (x+5)

To achieve that, you can use a function that takes 2 functions as arguments and multiplies them mathematically:

public static class FunctionsHelper
{
        private static Func<double, double> MultiplyTwoFunctions(Func<double, double> f1 , Func<double,double> f2)
        {
            Func<double, double> result = (double x) => f1(x) * f2(x);
            return result;
        }
        private static Func<double, double> GetSquareOfFunction(Func<double, double> f)
        {
            Func<double, double> result = (double x) => Math.Pow(f(x) ,2);
            return result;
        }

}
double cste = 10.0;
Func<double,double> F = (double x) => x * cste;
Func<double,double> SquaredFunction = FunctionsHelper.GetSquareOfFunction(F);

doube x = SquaredFunction(5.00); // x = 2500
 
Share this answer
 
Comments
Aseel Pharmacy 10-May-22 11:32am    
Thanks, that helped me.
N.Enguehard 28-May-22 18:44pm    
Wow that's exactly what i wanted.
Thanks a lot for your help ! :D
A function is not a number, you can't do math with it any more than you can with a phone number. Or at least, if you force a system to do maths with a non-numeric value, the result you get are of no use to anyone.

Go back to the documentation on tuples, and read it carefully: Tuple types - C# reference | Microsoft Docs[^]
Then decide exactly what you expect a "square" to do, and implement that - at the moment I'm not sure you did more than skim the subject and guess what to do with it!
 
Share this answer
 
Comments
0x01AA 8-May-22 12:47pm    
A square function will most probably not return a tuple ;)
N.Enguehard 8-May-22 13:24pm    
ReplyModify the comment. Delete the comment.
Ok, thanks for the feedback... i was expecting this answer :(
I will change to another way to get what I want.

something like :
ublic Func < double, double > f(int pow)
{
return F => Math.Pow(cste * F, pow);
}

Thanks everyone for your answers
:) CodeProject is a very good community ^^

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