传递数学函数作为函数参数

huangapple go评论76阅读模式
英文:

Passing mathematical function as function argument

问题

我是初级水平的Java程序员。

我想要一个接受数学函数(例如sin、cos、exp())作为参数并对其进行积分的函数。

请注意,我想传递函数本身,而不是函数的值。

这个函数的工作方式是接受一个数学函数作为参数,一个初始值,一个初始导数,以及要积分到的点。该函数返回曲线下的面积。

我甚至不知道这是否可能,如果可能的话,请帮助我。

谢谢。

英文:

I am beginner level java programmer.

I want a function which takes a mathematical function (i.e. sin, cos, exp() ) as argument and integrate it.

Note that I want to pass the function, not the value of function.

The function is such that it receives a mathematical function as argument, an initial value, initial derivative and the point upto which it integrates. The function returns the area under the curve.

I don't even know if this is possible, if this is possible please help me.

Thank you.

答案1

得分: 1

尝试使用方法引用:

List<Double> list = getRandomDoubles();
list.stream().map(Math::sqrt) // 映射为平方根
             .map(Math::log) // 映射为数字的对数
             .forEach(System.out::println); // 打印每个数字
英文:

Try using method references:

List&lt;Double&gt; list=getRandomDoubles();
list.stream().map(Math::sqrt) //Map to the square root
             .map(Math::log)//Map to the logarithm of the number
             .forEach(System.out::println);//Print each number

答案2

得分: 0

java.util.function 包提供了一种将函数存储为引用的方法。

只要您的函数属于相同的类型,您就可以将它们用作参数。例如,sincos 都属于 double -&gt; double 类型,在 Java 中它们是 DoubleFunction&lt;Double&gt;,更好的选择是 DoubleUnaryOperator(如果要使用原始数据类型)或者 Function&lt;Double, Double&gt;

这意味着您可以声明如下的函数:

public void foo(DoubleUnaryOperator f) {
    for (int i = 0; i &lt; 10; i++)
       System.out.println(f.apply((double) i));
}

此函数接受另一个函数并调用它十次,使用从 0 .. 9 的数字。

您可以使用此功能创建更复杂的东西,例如还可以传递适当的供应商等等。

以这种方式传递函数可以通过lambda 表达式方法引用 来完成。两者是相等的:

foo(Math::sin);
foo(d -&gt; Math.sin(d));

假设您想创建一个方法,该方法接受描述图形的函数、初始值、结束值和步长(delta),并且想要计算给定范围内图形下的面积。

那么该函数将具有以下签名和骨架实现:

public double area(DoubleUnaryOperator graph, double start, double end, double delta) {
    DoubleStream stream = DoubleStream
        .iterate(start, d -&gt; d + delta)
        .limit((long)((end-start)/delta);
        // stream 现在包含在 start 和 end 之间的所有值
        // 它们之间的差值为 delta
        // 现在对其进行映射和收集
}
英文:

The java.util.function package offers a way to store functions as references.

As long as your functions are of the same type, you can use them as arguments. sin and cos for example are both of the type double -&gt; double, in terms of Java they are a DoubleFunction&lt;Double&gt; or better yet DoubleUnaryOperator (if you want to use primitives) or Function&lt;Double, Double&gt;.

This means you can declare functions like these:

public void foo(DoubleUnaryOperator f) {
    for (int i = 0; i &lt; 10; i++)
       System.out.println(f.apply((double) i));
}

This function takes another functions and calls it ten times, with numbers from 0 .. 9.

You can create far more complex things with this, e.g. by also passing an appropriate supplier and much more.

Passing functions in this way is done either via lambda expressions or method references. Both are equal:

foo(Math::sin);
foo(d -&gt; Math.sin(d));

Lets say you want to create a method that takes a function that describes a graph, an initial value, and end value, and a step size (delta), and want to calculate the area under the graph in the given bounds.

Then the function would have this signature and skeleton implementation:

public double area(DoubleUnaryOperator graph, double start, double end, double delta) {
    DoubleStream stream = DoubleStream
        .iterate(start, d -&gt; d + delta)
        .limit((long)((end-start)/delta);
        // stream now consists of all values between start and end
        // with a difference of delta between them
        // now map it &amp; collect it
}

答案3

得分: -2

以下是翻译好的内容:

Java 是一种面向对象编程语言(OOP),不接受在参数中使用 function

为了具有这样的行为,在 Java 8 中,它们添加了使用 Lambda 表达式的可能性,以模拟在代码中使用函数的可能性,但实际上,您始终在传递对象...

由于您说自己是初学者,我建议您先阅读一些材料:

英文:

Java is a OOP (Object Oriented Programming Language), and does not accept a function in a parameter.

To have such a behavior, since Java 8, they added the possibility of using Lambda expressions, to simulate the possibility of using function in the code, but in reality, you are always conveying object...

As you are a beginner like you said, I'd recommend some readings first:

huangapple
  • 本文由 发表于 2020年7月26日 15:27:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定