我为我的一门com sci课程设计了一个多项式类,我遇到了一个关于积分方法的问题,有人能帮我吗?
/** The polynomial class includes the methods: evaluate , add, multiply,
* Differentiate , integrate and square root.
*/
public class polynomial {
private int degree;
private double[] coefficients;
// a constructor that creates a polynomial of
我想用midpoint_rule函数计算0到2pi范围内sin^2(x)的积分。代码可以使用此参数print(midpoint_rule(np.sin,0,2*np.pi,1000))计算sin(x),但当我将np.sin更改为此np.sin**2时,我在底部得到错误。我应该怎么做才能解决这个问题? def midpoint_rule(f,a,b,num_intervals):
"""
integrate function f using the midpoint rule
Args:
f: f
乘以x和2是行不通的。Scala版本为2.13.8。
import Integral.Implicits._
object hello {
def f[T:Integral](x: T): T = x * 2
val x: Int = 1
f(x)
}
type mismatch;
found : Int(2)
required: T
def f[T:Integral](x: T): T = x * 2
我的代码如下:
calcstep ::Integral a => a -> a
calcstep n = calcstep2 n 0
calcstep2 :: Integral (Integral a, Integral b) => a -> b -> a
calcstep2 1 k = k
calcstep2 n k | odd n = calcstep2 (n/2) (k+1)
| otherwise = calcstep2 (n*3+1) (k+1)
错误如下:
非类型变量参数
在约束中:积分(积分a,积分b)
(使用-XF
我想编写一个自定义函数来将表达式(python或lambda函数)与特定的容差进行数值集成。我知道使用scipy.integrate.quad可以简单地更改epsabs,但我想自己使用numpy编写函数。
从我知道这个函数:
def integrate(f, a, b, N):
x = np.linspace(a+(b-a)/(2*N), b-(b-a)/(2*N), N)
fx = f(x)
area = np.sum(fx)*(b-a)/N
return area
给出了N段的数值积分。如何编写另一个函数或扩展此函数以接受tol输入并增加N,直到两次后续计算
这真是太愚蠢了,但我试着编写了我的第一个Scala程序,发现:
def remainder[T](a:T, b:T):T = {
a%b
}
给我:
scala> :l d.scala
Loading d.scala...
<console>:8: error: value % is not a member of type parameter T
a%b
^
那么,我如何告诉Scala T是数字,并定义了数学操作?我不认为这样简单的事情需要了解Scala的所有黑暗角落。一定很简单吧!嗯,我尝试了一些组合,如“T:数字”,
所以,我让这个程序返回16的平方根是4.0;然而,它在后面加上了1.0。当我尝试输入28时,它返回5.0 1.12而不是2(7) (我不知道如何输入平方根符号),如果我输入197,它将返回14.0 1.00510204082而不是(197)。在解决如何生成正确输出方面的任何帮助都将不胜感激,以下是代码:
from math import modf
from math import sqrt
#Establish input
A = int(raw_input("Enter the number under the radical:"))
#Input taken, setup
我们刚刚开始在大学学习Haskell,在我的一个家庭作业问题中,我们必须为这种类型的签名定义一个函数:
f1 :: Integral a => a
老实说,我仍然在尝试理解关于Haskell的一些基本知识,但我知道使用Integral,您可以使用mod和div,再加上您可以使用Num、+、-和*完成的操作。
这样我就可以写
f1 :: Integral a=> a -> a
f1 k = k `mod` k
而且会成功的。但是有了
f1 :: Integral a => a
似乎没有产出。
我怎么能用这个来写一些在解释时不会出错的东西呢?
我太困了,所以我写了以下代码(修改后只是为了显示混乱):
fac s = take 10 [s, s `mod` 1 ..]
maxFactor x = if (s == [])
then x
else head <-- this should be 'head x' instead of just 'head'
where s = fac x
但是,这个加载到ghci (并编译)中是很好的。当我执行maxFactor 1时,它会抱怨(当然):
<interactive>:0:
我需要一个棱镜来将Integral a => a转换成(Integral b, Bounded b) => b,确保a实际上符合b的类型。我现在的定义(下面)要求使用ScopedTypeVariables,而且非常冗长。
我想知道是否有一种更好的方法(理想情况下已经定义好了,而且我错过了)来检查一个数字是否适合于一个Bounded类型,还是一个用于安全转换的函数,可以用来构建棱镜。
目前的定义如下:
boundedInt :: forall a b. (Integral a, Integral b, Bounded b) => Prism' a b
boundedIn
我在找一个文件中至少与一个字符匹配的所有代码。因此,如果我写下'a‘,我想知道所有包含'a’的代码名。如果我写了“anaconda”,并且这段代码存在,并且至少有一个寄存器,函数应该能找到它。
Private Function VerificaProducto(ByVal Codigo, ByVal Familia, ByVal Proveedor) As String
Dim Horno As Database
Dim Panes As Recordset
Set Horno = CurrentDb
If Proveedor = "
我试图在MATLAB的integral R2012b上使用MATLAB的R2012b函数。
有一个明显的例子
% create an anonymous function
fun = @(x) exp(-x.^2).*log(x).^2;
% integrate
q = integral(fun,0,Inf)
但是当我这样做的时候,我就得到了;
Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
我曾在网上的随机地点看到