FuncLibhttp://funclib.codeplex.com/project/feeds/rssC# automatic differentiation and numerical optimization library, transparent use with operator overloading, unlimited order of differentiation and unlimited number of variables, very flexible support for matrix algebra, on-the-fly function compilation to IL code for very fast evaluation. Various numerical optimization methods are supported.New Post: Compile() throws error for a function of the product of variableshttps://funclib.codeplex.com/discussions/663535<div style="line-height: normal;">Hi,<br />
<br />
First of all: what a great piece of work this library is. I am very impressed by the elegance and its usefulness. <br />
<br />
Here is an issue I stumbled upon. If I try to compile a function that is a simple product of two variables, the compiler throws an error in the function GenerateCode()(System.Collections.Generic.KeyNotFoundException).<br />
<br />
This also happens in Optimizers.Sample3(), where g1 is the product of 4 variables:<br />
<pre><code>Function g1 = x1 * x2 * x3 * x4;</code></pre>
The error occurs when I uncomment the compile section (was it commented because it gave an error?):<br />
<pre><code>CompiledFunction[] c = Compiler.Compile(new Function[] { f, g1, g2 }, new Variable[] { x1, x2, x3, x4 }, 2);</code></pre>
It also doesn't work with the simplest example of two variables:<br />
<pre><code> Variable x = new Variable();
Variable y = new Variable();
Function f0 = x * y;
f0 = Compiler.Compile(f, new Variable[] { x, y }, 2);</code></pre>
=> error thrown<br />
<br />
Interestingly enough it DOES work when I add a dummy Function to the equation:<br />
<pre><code> Function f0 = Function.Abs(1) * x * y;
f0 = Compiler.Compile(f0, new Variable[] { x, y }, 2);</code></pre>
Which is good enough for a workaround I guess.<br />
<br />
I hope this is useful information for the author or anyone who comes across the same thing.<br />
<br />
cheers<br />
</div>absvm09Wed, 02 Aug 2017 06:23:38 GMTNew Post: Compile() throws error for a function of the product of variables 20170802062338ANew Post: using an array of variables and adding a constraint on their sumhttps://funclib.codeplex.com/discussions/661916<div style="line-height: normal;">Hello,<br />
<br />
I can't immediately see how I could use an array of variables and adding a constraint on their sum.<br />
I also would like to express constraints on weighted sums of variables, where weights are functions.<br />
In summary, I would like to express constraints on expressions like<br />
<br />
Sum x(i)<br />
<br />
Sum x(i) Weight(y(i))<br />
<br />
where x and y are Variables.<br />
<br />
Of course the size of the array(s) would only be known at run time and it could be large (10-100).<br />
<br />
Could you suggest me how to proceed?<br />
Ideally I would like to use Linq, but other ways would ok.<br />
<br />
Thanks,<br />
<br />
Michel<br />
</div>lalbatrosTue, 14 Mar 2017 09:28:27 GMTNew Post: using an array of variables and adding a constraint on their sum 20170314092827ANew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">Hi,
<br />
I am desperately trying to make FuncLib work on VS 2015 but I get the same errors as Chagui and I can't find a how to make it work.
<br />
<br />
I downloaded FuncLib 0.4 from the website, then found a more recent version on <a href="http://www.java2s.com/Open-Source/CSharp_Free_Code/Numerical_Library/Download_FuncLib.htm" rel="nofollow">http://www.java2s.com/Open-Source/CSharp_Free_Code/Numerical_Library/Download_FuncLib.htm</a>.
<br />
<br />
I always get the following error message: Unable to load DLL 'Ipopt39': The specified module could not be found.
<br />
<br />
Could you please help me?
<br />
<br />
Thanks you<br />
</div>osiris59Fri, 16 Dec 2016 07:27:50 GMTNew Post: Error with dll 20161216072750AReviewed: FuncLib 0.4 (Aug 02, 2016)https://funclib.codeplex.com/releases/view/80519#ReviewBy-CriticalCauchyRated 5 Stars (out of 5) - The L-BFGS-B algorithm works perfectly. CriticalCauchyTue, 02 Aug 2016 16:09:22 GMTReviewed: FuncLib 0.4 (Aug 02, 2016) 20160802040922PNew Post: Least squares residual function for curve fitting to X-Y-pointshttps://funclib.codeplex.com/discussions/656659<div style="line-height: normal;">Hi Mr. Bakkedal,<br />
<br />
first, I want to thank you for your great work!<br />
<br />
I'm currently testing your implementation of the BFGS-B algorithm for nonlinear curve fitting with box constraints. I want to fit the function f to data points, for example:<br />
<pre><code> double[] dataX = { 1.0, 2.0, 3.0, 4.10 };
double[] dataY = { 2.0, 2.2, 2.5, 5.43 };
Variable a = new Variable("a");
Variable n = new Variable("n");
//Variable x = new Variable("x");
//Function f = a * Function.Pow(x, n);
Function residualfunction = 0;
for (int i = 0; i < dataX.Length; i++)
{
residualfunction += Function.Sqr(dataY[i] - a * Function.Pow(dataX[i], n));
}
VariableConstrainedOptimizer o = new BfgsOptimizer();
o.Variables.Add(a, n);
o.ObjectiveFunction = residualfunction;
o.VariableConstraints.Add(0.0 <= n, n <= 1.0);
IOptimizerResult or = o.Run(a | 0.2, n | 0.8);</code></pre>
It works, but it feels as if there's a better solution to do this. My problem is that i have multiple functions. I'd like to have a comprehensive function for the calculation of the residual function that is called with the different functions (for the purpose of OOP). Therefore I must do something like that: <br />
<strong>f[ x | dataX[i] ] << x is substituted by the x-value but a and n should stay variables.</strong><br />
<pre><code> Function f = a * Function.Pow(x, n);
Function residualfunction = 0;
for (int i = 0; i < dataX.Length; i++)
{
residualfunction += Function.Sqr(dataY[i] - f[ x | dataX[i] ]);
}</code></pre>
Is this possible?<br />
Sorry if this is a noob question. I'm quite new to numerical computation.<br />
Regards,<br />
Stevy<br />
</div>CriticalCauchySun, 24 Jul 2016 23:06:46 GMTNew Post: Least squares residual function for curve fitting to X-Y-points 20160724110646PNew Post: External Derivative and Function calculationhttp://funclib.codeplex.com/discussions/646384<div style="line-height: normal;">Hello,
<br />
<br />
Is it possible to know what is the best way one can implement their own equation (constraint) as following...
<br />
<br />
F(x1, x2, x3, x4.......) = 0
<br />
<br />
F is very complex and I want user to supply functional values (residuals) and derivatives (with respect to each variables).
<br />
<br />
I would like to know what methods do I need to intercept (e.g eval_grad_f, eval_g, eval_jac_g, eval_h). Is it possible to get an example?
<br />
<br />
Thanks.
<br />
Dhaval. <br />
</div>dhavaldaveMon, 19 Oct 2015 18:25:51 GMTNew Post: External Derivative and Function calculation 20151019062551PNew Post: Boolean Variableshttp://funclib.codeplex.com/discussions/644165<div style="line-height: normal;">That is unfortunately not possible. The kind of optimization implemented in this library is smooth function optimization. What you're asking for is discrete (or integer) optimization. That's a completely different game, requiring completely different mathematics and numerical techniques. This is also the reason that object support for that kind of stuff is not implemented (e.g. Boolean data types).<br />
</div>bakkedalSun, 30 Aug 2015 17:32:17 GMTNew Post: Boolean Variables 20150830053217PNew Post: Boolean Variableshttp://funclib.codeplex.com/discussions/644165<div style="line-height: normal;">Hello, is there any way of setting the variables to be booleans or integers?
<br />
I understand that you can constraint them to be in the range [0,1], but they can still get a decimal value, which is not what i want.<br />
</div>ChaguiTue, 25 Aug 2015 00:00:42 GMTNew Post: Boolean Variables 20150825120042ANew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">Ok, thank you. I'll see what i can do about it.
<br />
I suggest you consider submitting your project to GitHub. Its more friendly when it comes to collaboration.
<br />
Also, i really suggest you submit the proyect to nuget.<br />
</div>ChaguiMon, 24 Aug 2015 00:45:26 GMTNew Post: Error with dll 20150824124526ANew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">Good. This is an open source project. Feel free to add any feature you like and submit them back.
<br />
<br />
The Ipopt DLLs are either 32 bit or 64 bit, unfortunately, and cannot be automatically chosen by the Visual Basic build process without dirty hacks. Maybe I'll change the references to 64 bit DLLs as that platform seem to be dominating now.<br />
</div>bakkedalSun, 23 Aug 2015 16:29:16 GMTNew Post: Error with dll 20150823042916PNew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">Got it! i was building with the "any CPU" target platform.
<br />
And the library needs especifically the "x86" target.
<br />
Question. How to make it work with any cpu ?<br />
</div>ChaguiSun, 23 Aug 2015 16:11:23 GMTNew Post: Error with dll 20150823041123PNew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">Also the version i get from the big Download button in the main page is ancient.<br />
</div>ChaguiSun, 23 Aug 2015 16:01:39 GMTNew Post: Error with dll 20150823040139PNew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">Well there are some issues in the issue tracker than have not been answered and they are from 2013.
<br />
There is very little documentation regarding installation. There isnt a nuget package.
<br />
I have everything in 32 bits.
<br />
There are 3 projects: FuncLib, FuncLibConsole and FuncLibIpopt
<br />
Where do i get the dll from and whats the difference?
<br />
First I tried adding the reference to FuncLib.dll in FuncLib folder.
<br />
The i runned this code to test:<br />
<pre><code>// Variables. The string names are added for convenience when printing out the optimal point with Evaluation.ToString().
Variable x = new Variable("x");
Variable y = new Variable("y");
// Rosenbrock optimization test function.
Function f = Function.Sqr(1.0 - x) + 100.0 * Function.Sqr(y - Function.Sqr(x));
// Use the BFGS optimizer.
Optimizer o = new BfgsOptimizer();
// Specify variables and the objective function.
o.Variables.Add(x, y);
o.ObjectiveFunction = f;
// Start the optimizer from a random point.
Random r = new Random(1);
OptimizerResult or = (OptimizerResult) o.Run(x | r.NextDouble(), y | r.NextDouble());
Console.WriteLine(or.OptimalValue);</code></pre>
I get the following exception:
<br />
An unhandled exception of type 'System.BadImageFormatException' occurred in Program.exe
<br />
Additional information: Could not load file or assembly 'FuncLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
<br />
<br />
Then i tried dlls in FuncLibIpopt: i add funcLib.dll, funcLibIpopt.dll, and i cant add ipopt39.dll nor IpOptFSS39.dll because the IDE says they are not valid assemblys
<br />
I get same exception.<br />
</div>ChaguiSun, 23 Aug 2015 15:43:35 GMTNew Post: Error with dll 20150823034335PNew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">The project is not abandoned, but is in a more-or-less finished state (and the fundamental mathematics is also more-or-less stable last time I checked), thus the lack of commits recently.
<br />
<br />
It's difficult to tell what your problem is with no information whatsoever. Maybe you're using the 32 bit DLLs but compiling 64 bit code? In that case the DLLs should be replaced.<br />
</div>bakkedalSun, 23 Aug 2015 10:02:05 GMTNew Post: Error with dll 20150823100205ANew Post: Error with dllhttp://funclib.codeplex.com/discussions/644051<div style="line-height: normal;">Hi, i cant seem to make this library work in my project, im using visual studio 2013.
<br />
I dont think i'll get any help though, this project seems abandoned :/<br />
</div>ChaguiSat, 22 Aug 2015 22:19:42 GMTNew Post: Error with dll 20150822101942PNew Post: Derivative of sin(x)/xhttp://funclib.codeplex.com/discussions/568926<div style="line-height: normal;">Suppose I want to define function SinX = sin(x)/x which is continuous and has a continuous derivative at x=0. I am trying to avoid divisions by 0 by specifying that: if x < 1e-3 set SinX, otherwise set SinX to sin(x)/x. This is what I have for now:<br />
<pre><code> private class SinXFunction : Function
{
private Function f;
public SinXFunction(Function f)
{
this.f = f;
}
protected override double ComputeValue(IEvaluator evaluation)
{
double temp = f.Value(evaluation);
if (Math.Abs(temp) < 1e-3)
return 1;
return Math.Sin(temp) / temp;
}
protected override Function ComputeDerivative(Variable variable)
{
//What do I need to write here to avoid divisions by 0???
CosFunction cos = new CosFunction(f);
return (cos - this) / f * f.Derivative(variable);//this gives NaN if f=0!
}
public override Expression Compile(CodeGenerator generator)
{
return generator.GetReference(f) + "<1e-3?1:(Math.Cos(" + generator.GetReference(f) + ")-Math.Sin(" + generator.GetReference(f) + ")/" + generator.GetReference(f) + ")/" + generator.GetReference(f);
}
}</code></pre>
Is there any way to define function ComputeDerivative so we avoid divisions by 0? Even if we define a new class (say SinXDerivativeFunction) the derivative of this new class will again involve divisions by 0.
<br />
<br />
<br />
Thanks a lot!
<br />
Andrew<br />
</div>pandrew1Fri, 03 Oct 2014 00:53:27 GMTNew Post: Derivative of sin(x)/x 20141003125327ANew Post: Very good libraryhttp://funclib.codeplex.com/discussions/568707<div style="line-height: normal;">Indeed, I was using FuncLib 0.4 (Jan 14, 2012, Beta). The latest version does not have this issue.<br />
<br />
One improvement that in my case speeds up the evaluation of the compiled functions is to add a base class<br />
<pre><code> public class GeneratedFunctionBase
{
public virtual void Value(double[] x, double[] y)
{
}
}</code></pre>
and to inherit GeneratedFunction from it<br />
<pre><code> code.AppendLine("public class GeneratedFunction : GeneratedFunctionBase");
code.AppendLine("public override void Value(double[] x, double[] y)");
</code></pre>
Thanks again for the good work. <br />
<br />
Andrew<br />
</div>pandrew1Wed, 01 Oct 2014 13:42:11 GMTNew Post: Very good library 20141001014211PNew Post: Very good libraryhttp://funclib.codeplex.com/discussions/568707<div style="line-height: normal;">I can't reproduce this. I works fine on my machine. Have you tried with the lastest version from the source control? You can download it as a zip-file.
<br />
<br />
Morten<br />
</div>bakkedalWed, 01 Oct 2014 11:27:14 GMTNew Post: Very good library 20141001112714ANew Post: Bughttps://funclib.codeplex.com/discussions/568707<div style="line-height: normal;">First thank you for the great work. There seems to be a bug when I run Sample2
<br />
<br />
The only change that I am making is to simplify the function as follows:
<br />
<br />
Function f = x * y;
<br />
//Function f = Function.Exp(x * y) + x + 0.5;
<br />
//f *= Function.Cos(f);
<br />
//f *= Function.Cos(f);
<br />
//f *= Function.Cos(f);
<br />
//f *= Function.Cos(f);
<br />
//f = f.Derivative(x);
<br />
<br />
The error comes when I execute this line:
<br />
CompiledFunction g2 = Compiler.Compile(f, new Variable[] { x, y }, 1); /<em> 2 </em>/
<br />
<br />
The error is: the 'references' dictionary does not contain the key:
<br />
code.AppendLine("y[" + i + "]=" + references[functions[i]].GenerateCode(this) + ";");
<br />
<br />
Is there a simple fix?
<br />
<br />
Thanks,
<br />
Andrew<br />
</div>pandrew1Tue, 30 Sep 2014 21:12:36 GMTNew Post: Bug 20140930091236PNew Post: Determining the variables underlying a functionhttp://funclib.codeplex.com/discussions/549217<div style="line-height: normal;">It's unfortunately not possible. The library is currently designed so that Function objects are as minimal as possible to make it easy to added new functions. I have been considering to add a Dependencies property or something, though, and will maybe do it in the future. I was considering to do some sorting before function evaluation using such a property, but I couldn't make it efficient. So I decided against adding this property.
<br />
<br />
Morten<br />
</div>bakkedalFri, 27 Jun 2014 08:32:07 GMTNew Post: Determining the variables underlying a function 20140627083207A