Question: I already tried every version of this script found here and there, but it's still not working.
I have a controller doing certain actions after saving an order in the back-end. One of this action is to produce automatically an invoice on certain conditions. Usual redirect sends me back to the order view: I'd like to go to the invoice view instead.

```
$url = Mage::helper('adminhtml')->getUrl('sales_order_invoice/view', array('invoice_id' => $invoice->getId(), 'order_id' => $order->getId()));
Mage::app()->getResponse()->setRedirect($url)->sendResponse();

```

This code is not working: I'm redirected to the order view as usual. The observer is trigger on `sales_order_save_after`.
How can i redirect properly?

Answer: 
[mlpack](http://www.mlpack.org/index.html) appears to have Lagrange multipliers, see [the docs](http://www.mlpack.org/doxygen.php?doc=classmlpack_1_1optimization_1_1AugLagrangianFunction.html).
These allow you to solve equations with constraints.
you can use [NLopt](http://ab-initio.mit.edu/wiki/index.php/NLopt) library for optimization and construct your objective function as a computation of squered innovations (probably using COBYLA algorithm).

You can use also QuantLib's [General Linear Leaast Squeres](http://quantlib.org/reference/class_quant_lib_1_1_general_linear_least_squares.html) class for this, usage is as simple as:

```
LinearRegression *lr = new LinearRegression( x, y, 1.0);
cout << lr->coefficients();

```

[QuantLib LinearRegression](https://github.com/lballabio/quantlib/blob/master/QuantLib/ql/math/linearleastsquaresregression.hpp)