private double CallFunc(Func <Object> f, string[] ids, double[] vals, string pos) { for (int i = 0; i < ids.Length; i++) { func.SetLocal(ids[i], vals[i]); } Object r = f(); if (r.GetType() == typeof(Object[])) { // For lists, use the magnitude as value to optimize try { return((double)VectorMath.Mag(r, pos)); } catch (Exception) { throw new Exception(pos + "solver cannot optimize a function that returns a list of lists"); } } else { double res = Math.Abs((double)r); return(res); } }