我想在c#中调用cminpack_dll.dll中的lmdif1方法,但遇到了一些奇怪的错误。
传递给lmdif1的第二个和第三个参数是整数,在我的测试中,按顺序的值分别为12和9。现在当它们在C代码中时,原来是12的值现在变成了9,而原来是9的值在308000和912000之间变化。不知道为什么。
首先,我想知道我使用的签名是否有效。
C#签名:
[DllImport("cminpack_dll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int lmdif1(IntPtr fcn, int m, int n, double[] x, double[] fVec, double tol, int[] iwa, double[] wa, int lwa);
C语言的签名:
int __cminpack_func__(lmdif1)(__cminpack_decl_fcn_mn__ void *p, int m, int n, real *x,
real *fvec, real tol, int *iwa,
real *wa, int lwa)
我称之为:
//All of the variables passed in match the types in C# signature above.
var info =lmdif1(
functionPointer,
pointsetLength,
initialGuessLength,
initialGuess,
fVec,
tol,
iwa,
wa,
lwa);
这是我第一次处理PInvoke,我的C语言水平不太好,以前从未真正接触过它,所以任何帮助都将不胜感激。我怀疑我可能需要进行Marshal操作,但我已经尝试将int作为I4和U4进行Marshal操作,但仍然出现相同的问题。
非常感谢您的帮助。
编辑: 以下是描述C函数的注释,如果有帮助:
/* ********** */
/* subroutine lmdif1 */
/* the purpose of lmdif1 is to minimize the sum of the squares of */
/* m nonlinear functions in n variables by a modification of the */
/* levenberg-marquardt algorithm. this is done by using the more */
/* general least-squares solver lmdif. the user must provide a */
/* subroutine which calculates the functions. the jacobian is */
/* then calculated by a forward-difference approximation. */
/* the subroutine statement is */
/* subroutine lmdif1(fcn,m,n,x,fvec,tol,info,iwa,wa,lwa) */
/* where */
/* fcn is the name of the user-supplied subroutine which */
/* calculates the functions. fcn must be declared */
/* in an external statement in the user calling */
/* program, and should be written as follows. */
/* subroutine fcn(m,n,x,fvec,iflag) */
/* integer m,n,iflag */
/* double precision x(n),fvec(m) */
/* ---------- */
/* calculate the functions at x and */
/* return this vector in fvec. */
/* ---------- */
/* return */
/* end */
/* the value of iflag should not be changed by fcn unless */
/* the user wants to terminate execution of lmdif1. */
/* in this case set iflag to a negative integer. */
/* m is a positive integer input variable set to the number */
/* of functions. */
/* n is a positive integer input variable set to the number */
/* of variables. n must not exceed m. */
/* x is an array of length n. on input x must contain */
/* an initial estimate of the solution vector. on output x */
/* contains the final estimate of the solution vector. */
/* fvec is an output array of length m which contains */
/* the functions evaluated at the output x. */
/* tol is a nonnegative input variable. termination occurs */
/* when the algorithm estimates either that the relative */
/* error in the sum of squares is at most tol or that */
/* the relative error between x and the solution is at */
/* most tol. */
/* info is an integer output variable. if the user has */
/* terminated execution, info is set to the (negative) */
/* value of iflag. see description of fcn. otherwise, */
/* info is set as follows. */
/* info = 0 improper input parameters. */
/* info = 1 algorithm estimates that the relative error */
/* in the sum of squares is at most tol. */
/* info = 2 algorithm estimates that the relative error */
/* between x and the solution is at most tol. */
/* info = 3 conditions for info = 1 and info = 2 both hold. */
/* info = 4 fvec is orthogonal to the columns of the */
/* jacobian to machine precision. */
/* info = 5 number of calls to fcn has reached or */
/* exceeded 200*(n+1). */
/* info = 6 tol is too small. no further reduction in */
/* the sum of squares is possible. */
/* info = 7 tol is too small. no further improvement in */
/* the approximate solution x is possible. */
/* iwa is an integer work array of length n. */
/* wa is a work array of length lwa. */
/* lwa is a positive integer input variable not less than */
/* m*n+5*n+m. */
/* subprograms called */
/* user-supplied ...... fcn */
/* minpack-supplied ... lmdif */
/* argonne national laboratory. minpack project. march 1980. */
/* burton s. garbow, kenneth e. hillstrom, jorge j. more */
/* ********** */
/* check the input parameters for errors. */
cminpack_func_nn fcn_nn
,它是另一个IntPtr
。 - GSergint
的指针,而不是函数指针(我也不是C语言专业人士)。我不知道它应该指向什么,可能文档中有答案。 - GSerg