# HG changeset patch
# User Jaroslav Hajek
# Date 1211783291 -7200
# Node ID b282f098f4a83f9027ead3acf0bce422e9ec6932
# Parent 936478aed051aafca8914e36afb89acf004d3807
make fsolve pass arguments in a Matlab-compatible way
diff --git a/src/ChangeLog b/src/ChangeLog
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -11,6 +11,14 @@
Simplify to trivial wrappers.
(int_vector_value): Avoid conversions if integer type, query N-d array
value, simplify.
+
+2008-05-26 Jaroslav Hajek
+
+ * DLD-FUNCTIONS/fsolve.cc (fsolve_user_function,
+ fsolve_user_jacobian): Reshape argument to original dims before
+ passing.
+ (Ffsolve): Save original dimensions of the starting guess and reshape
+ on return. Fix tests.
2008-05-21 David Bateman
diff --git a/src/DLD-FUNCTIONS/fsolve.cc b/src/DLD-FUNCTIONS/fsolve.cc
--- a/src/DLD-FUNCTIONS/fsolve.cc
+++ b/src/DLD-FUNCTIONS/fsolve.cc
@@ -31,6 +31,7 @@
#include
#include
+#include "dNDArray.h"
#include "NLEqn.h"
#include "defun-dld.h"
@@ -51,6 +52,9 @@
// Global pointer for optional user defined jacobian function.
static octave_function *fsolve_jac;
+
+// original dimensions of X0
+static dim_vector x_dims;
// Have we warned about imaginary values returned from user function?
static bool warned_fcn_imaginary = false;
@@ -110,9 +114,7 @@
if (n > 1)
{
- Matrix m (n, 1);
- for (octave_idx_type i = 0; i < n; i++)
- m (i, 0) = x (i);
+ NDArray m (ArrayN (x, x_dims));
octave_value vars (m);
args(0) = vars;
}
@@ -135,7 +137,7 @@
warned_fcn_imaginary = true;
}
- retval = ColumnVector (tmp(0).vector_value ());
+ retval = ColumnVector (tmp(0).vector_value (false, true));
if (error_state || retval.length () <= 0)
gripe_user_supplied_eval ("fsolve");
@@ -161,9 +163,7 @@
if (n > 1)
{
- Matrix m (n, 1);
- for (octave_idx_type i = 0; i < n; i++)
- m(i,0) = x(i);
+ NDArray m (ArrayN (x, x_dims));
octave_value vars (m);
args(0) = vars;
}
@@ -399,7 +399,9 @@
if (error_state || ! fsolve_fcn)
FSOLVE_ABORT ();
- ColumnVector x (args(1).vector_value ());
+ NDArray xa = args(1).array_value ();
+ x_dims = xa.dims ();
+ ColumnVector x (xa);
if (error_state)
FSOLVE_ABORT1 ("expecting vector as second argument");
@@ -429,7 +431,7 @@
{
retval(2) = static_cast (hybrd_info_to_fsolve_info (info));
retval(1) = nleqn.function_value ();
- retval(0) = soln;
+ retval(0) = NDArray (ArrayN (soln.reshape (x_dims)));
if (! nleqn.solution_ok () && nargout < 2)
{
@@ -460,7 +462,7 @@
%! 2.395931;
%! 2.005014 ];
%! tol = 1.0e-5;
-%! [x, fval, info] = fsolve ("f", [ 0.5, 2.0, 2.5 ]);
+%! [x, fval, info] = fsolve ("f", [ 0.5; 2.0; 2.5 ]);
%! info_bad = (info != 1);
%! solution_bad = sum (abs (x - x_opt) > tol);
%! value_bad = sum (abs (fval) > tol);
@@ -492,10 +494,7 @@
%! retval(3) = x^4 - 4*y^2 + 6*z - 8*w - 20;
%! retval(4) = x^2 + 2*y^3 + z - w - 4;
%!test
-%! x_opt = [ -0.767297326653401;
-%! 0.590671081117440;
-%! 1.47190018629642;
-%! -1.52719341133957 ];
+%! x_opt = [ -0.767297326653401, 0.590671081117440, 1.47190018629642, -1.52719341133957 ];
%! tol = 1.0e-5;
%! [x, fval, info] = fsolve ("f", [-1, 1, 2, -1]);
%! info_bad = (info != 1);