# Vector arithmetic or Map problem

(Peter Vanderwaart) #1

I’m having trouble with this sequence:

``````  var y_diff = new Func<Vector<double>, Vector<double>>(p => f0(p) - y);.
var g0 = new Func<Vector<double>, Vector<double>>(p => (x.Map(z1 => 2.0 * y_diff(p) )));
``````

On the x.Map, I’m getting “The type Vector must be a non-nullable value type in order to use it as a parameter.”

Hey, all I’m trying to do is multiply a vector by a scalar (and eventually get the sum of the elements).

Edit: Also, I could do with an explanation of forceMapZero which also arose while trying various fixes. I would have expected it to be an attribute of Zeros, but it doesn’t appear.

#2

What are you trying to do?Can you provide MCVE?

(Peter Vanderwaart) #3

I realized (in the night) that the Map function as I wrote it was trying to multiply a vector times each element of x, resulting in I don’t know what kind of result structure. The corrected version is below. It creates a Gradient function for a minimization problem.

``````            // create gradient = vector of partial differential equations evaluate at point p.
// function y_diff creates a vector of difference between actual y and modeled y.  Simplifies following statements.
var y_diff = new Func<Vector<double>, Vector<double>>(p => f0(p) - y);
var g0 = new Func<Vector<double>, double>((p) => (2.0 * y_diff(p)).Sum());                                  // partial differential for first term.
var g1 = new Func<Vector<double>, double>((p) => ((2.0 * y_diff(p)) * x.Map(z => Math.Exp(p[2] * z))));     // partial differential for second term
var g2 = new Func<Vector<double>, double>((p) => ((2.0 * y_diff(p)) * x.Map(z => p[1] * z * Math.Exp(p[2] * z))));   // partial differential for third term
var g = new Func<Vector<double>, Vector<double>>((p) => new DenseVector(new[] { g0(p), g1(p), g2(p) }));   // gradient vector
``````

As far as I can tell, it’s all working properly.