Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ALGLIB License.txt
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/ALGLIB License.txt	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ALGLIB License.txt	(revision 7294)
@@ -1,3 +1,3 @@
-﻿ALGLIB® - numerical analysis library, 1999-2010.
+﻿ALGLIB® - numerical analysis library, 1999-2012.
 ALGLIB is registered trademark of the ALGLIB Project.
 
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ALGLIB-3.1.0.csproj
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/ALGLIB-3.1.0.csproj	(revision 6866)
+++ 	(revision )
@@ -1,154 +1,0 @@
-﻿<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <PropertyGroup>
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProductVersion>9.0.30729</ProductVersion>
-    <SchemaVersion>2.0</SchemaVersion>
-    <ProjectGuid>{FC841674-62A7-4055-BE91-E41944B6C606}</ProjectGuid>
-    <OutputType>Library</OutputType>
-    <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>ALGLIB</RootNamespace>
-    <AssemblyName>ALGLIB-3.1.0</AssemblyName>
-    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
-    <TargetFrameworkProfile></TargetFrameworkProfile>
-    <FileAlignment>512</FileAlignment>
-    <SignAssembly>true</SignAssembly>
-    <AssemblyOriginatorKeyFile>HeuristicLab.snk</AssemblyOriginatorKeyFile>
-    <FileUpgradeFlags>
-    </FileUpgradeFlags>
-    <OldToolsVersion>3.5</OldToolsVersion>
-    <UpgradeBackupLocation />
-    <PublishUrl>publish\</PublishUrl>
-    <Install>true</Install>
-    <InstallFrom>Disk</InstallFrom>
-    <UpdateEnabled>false</UpdateEnabled>
-    <UpdateMode>Foreground</UpdateMode>
-    <UpdateInterval>7</UpdateInterval>
-    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
-    <UpdatePeriodically>false</UpdatePeriodically>
-    <UpdateRequired>false</UpdateRequired>
-    <MapFileExtensions>true</MapFileExtensions>
-    <ApplicationRevision>0</ApplicationRevision>
-    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
-    <IsWebBootstrapper>false</IsWebBootstrapper>
-    <UseApplicationTrust>false</UseApplicationTrust>
-    <BootstrapperEnabled>true</BootstrapperEnabled>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
-    <DebugSymbols>true</DebugSymbols>
-    <DebugType>full</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>$(SolutionDir)\bin\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
-    <DebugType>pdbonly</DebugType>
-    <Optimize>true</Optimize>
-    <OutputPath>$(SolutionDir)\bin\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
-    <DebugSymbols>true</DebugSymbols>
-    <OutputPath>$(SolutionDir)\bin\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
-    <DebugType>full</DebugType>
-    <PlatformTarget>x86</PlatformTarget>
-    <ErrorReport>prompt</ErrorReport>
-    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
-    <OutputPath>$(SolutionDir)\bin\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
-    <Optimize>true</Optimize>
-    <DebugType>pdbonly</DebugType>
-    <PlatformTarget>x86</PlatformTarget>
-    <ErrorReport>prompt</ErrorReport>
-    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
-    <DebugSymbols>true</DebugSymbols>
-    <OutputPath>$(SolutionDir)\bin\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
-    <DebugType>full</DebugType>
-    <PlatformTarget>x64</PlatformTarget>
-    <ErrorReport>prompt</ErrorReport>
-    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
-    <OutputPath>$(SolutionDir)\bin\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
-    <Optimize>true</Optimize>
-    <DebugType>pdbonly</DebugType>
-    <PlatformTarget>x64</PlatformTarget>
-    <ErrorReport>prompt</ErrorReport>
-    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-  <ItemGroup>
-    <Reference Include="System" />
-    <Reference Include="System.Core">
-      <RequiredTargetFramework>3.5</RequiredTargetFramework>
-    </Reference>
-    <Reference Include="System.Xml.Linq">
-      <RequiredTargetFramework>3.5</RequiredTargetFramework>
-    </Reference>
-    <Reference Include="System.Data.DataSetExtensions">
-      <RequiredTargetFramework>3.5</RequiredTargetFramework>
-    </Reference>
-    <Reference Include="System.Data" />
-    <Reference Include="System.Xml" />
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Include="alglibinternal.cs" />
-    <Compile Include="alglibmisc.cs" />
-    <Compile Include="ap.cs" />
-    <Compile Include="dataanalysis.cs" />
-    <Compile Include="diffequations.cs" />
-    <Compile Include="fasttransforms.cs" />
-    <Compile Include="integration.cs" />
-    <Compile Include="interpolation.cs" />
-    <Compile Include="linalg.cs" />
-    <Compile Include="optimization.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="solvers.cs" />
-    <Compile Include="specialfunctions.cs" />
-    <Compile Include="statistics.cs" />
-  </ItemGroup>
-  <ItemGroup>
-    <Content Include="ALGLIB License.txt" />
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="HeuristicLab.snk" />
-  </ItemGroup>
-  <ItemGroup>
-    <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
-      <Visible>False</Visible>
-      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
-      <Install>false</Install>
-    </BootstrapperPackage>
-    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
-      <Visible>False</Visible>
-      <ProductName>.NET Framework 3.5 SP1</ProductName>
-      <Install>true</Install>
-    </BootstrapperPackage>
-    <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
-      <Visible>False</Visible>
-      <ProductName>Windows Installer 3.1</ProductName>
-      <Install>true</Install>
-    </BootstrapperPackage>
-  </ItemGroup>
-  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
-       Other similar extension points exist, see Microsoft.Common.targets.
-  <Target Name="BeforeBuild">
-  </Target>
-  <Target Name="AfterBuild">
-  </Target>
-  -->
-</Project>
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ALGLIB-3.4.0.csproj
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ALGLIB-3.4.0.csproj	(revision 7294)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ALGLIB-3.4.0.csproj	(revision 7294)
@@ -0,0 +1,155 @@
+﻿<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>9.0.30729</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{FC841674-62A7-4055-BE91-E41944B6C606}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>ALGLIB</RootNamespace>
+    <AssemblyName>ALGLIB-3.4.0</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <TargetFrameworkProfile>
+    </TargetFrameworkProfile>
+    <FileAlignment>512</FileAlignment>
+    <SignAssembly>true</SignAssembly>
+    <AssemblyOriginatorKeyFile>HeuristicLab.snk</AssemblyOriginatorKeyFile>
+    <FileUpgradeFlags>
+    </FileUpgradeFlags>
+    <OldToolsVersion>3.5</OldToolsVersion>
+    <UpgradeBackupLocation />
+    <PublishUrl>publish\</PublishUrl>
+    <Install>true</Install>
+    <InstallFrom>Disk</InstallFrom>
+    <UpdateEnabled>false</UpdateEnabled>
+    <UpdateMode>Foreground</UpdateMode>
+    <UpdateInterval>7</UpdateInterval>
+    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
+    <UpdatePeriodically>false</UpdatePeriodically>
+    <UpdateRequired>false</UpdateRequired>
+    <MapFileExtensions>true</MapFileExtensions>
+    <ApplicationRevision>0</ApplicationRevision>
+    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
+    <IsWebBootstrapper>false</IsWebBootstrapper>
+    <UseApplicationTrust>false</UseApplicationTrust>
+    <BootstrapperEnabled>true</BootstrapperEnabled>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>$(SolutionDir)\bin\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>$(SolutionDir)\bin\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <OutputPath>$(SolutionDir)\bin\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <DebugType>full</DebugType>
+    <PlatformTarget>x86</PlatformTarget>
+    <ErrorReport>prompt</ErrorReport>
+    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <OutputPath>$(SolutionDir)\bin\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <Optimize>true</Optimize>
+    <DebugType>pdbonly</DebugType>
+    <PlatformTarget>x86</PlatformTarget>
+    <ErrorReport>prompt</ErrorReport>
+    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
+    <DebugSymbols>true</DebugSymbols>
+    <OutputPath>$(SolutionDir)\bin\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <DebugType>full</DebugType>
+    <PlatformTarget>x64</PlatformTarget>
+    <ErrorReport>prompt</ErrorReport>
+    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
+    <OutputPath>$(SolutionDir)\bin\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <Optimize>true</Optimize>
+    <DebugType>pdbonly</DebugType>
+    <PlatformTarget>x64</PlatformTarget>
+    <ErrorReport>prompt</ErrorReport>
+    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Xml.Linq">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data.DataSetExtensions">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="alglibinternal.cs" />
+    <Compile Include="alglibmisc.cs" />
+    <Compile Include="ap.cs" />
+    <Compile Include="dataanalysis.cs" />
+    <Compile Include="diffequations.cs" />
+    <Compile Include="fasttransforms.cs" />
+    <Compile Include="integration.cs" />
+    <Compile Include="interpolation.cs" />
+    <Compile Include="linalg.cs" />
+    <Compile Include="optimization.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="solvers.cs" />
+    <Compile Include="specialfunctions.cs" />
+    <Compile Include="statistics.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Content Include="ALGLIB License.txt" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="HeuristicLab.snk" />
+  </ItemGroup>
+  <ItemGroup>
+    <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
+      <Visible>False</Visible>
+      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
+      <Install>false</Install>
+    </BootstrapperPackage>
+    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
+      <Visible>False</Visible>
+      <ProductName>.NET Framework 3.5 SP1</ProductName>
+      <Install>true</Install>
+    </BootstrapperPackage>
+    <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
+      <Visible>False</Visible>
+      <ProductName>Windows Installer 3.1</ProductName>
+      <Install>true</Install>
+    </BootstrapperPackage>
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/alglibinternal.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/alglibinternal.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/alglibinternal.cs	(revision 7294)
@@ -29,4 +29,1338 @@
 public partial class alglib
 {
+    public class scodes
+    {
+        public static int getrdfserializationcode()
+        {
+            int result = 0;
+
+            result = 1;
+            return result;
+        }
+
+
+        public static int getkdtreeserializationcode()
+        {
+            int result = 0;
+
+            result = 2;
+            return result;
+        }
+
+
+        public static int getmlpserializationcode()
+        {
+            int result = 0;
+
+            result = 3;
+            return result;
+        }
+
+
+    }
+    public class apserv
+    {
+        /*************************************************************************
+        Buffers for internal functions which need buffers:
+        * check for size of the buffer you want to use.
+        * if buffer is too small, resize it; leave unchanged, if it is larger than
+          needed.
+        * use it.
+
+        We can pass this structure to multiple functions;  after first run through
+        functions buffer sizes will be finally determined,  and  on  a next run no
+        allocation will be required.
+        *************************************************************************/
+        public class apbuffers
+        {
+            public int[] ia0;
+            public int[] ia1;
+            public int[] ia2;
+            public int[] ia3;
+            public double[] ra0;
+            public double[] ra1;
+            public double[] ra2;
+            public double[] ra3;
+            public apbuffers()
+            {
+                ia0 = new int[0];
+                ia1 = new int[0];
+                ia2 = new int[0];
+                ia3 = new int[0];
+                ra0 = new double[0];
+                ra1 = new double[0];
+                ra2 = new double[0];
+                ra3 = new double[0];
+            }
+        };
+
+
+
+
+        /*************************************************************************
+        This  function  generates  1-dimensional  general  interpolation task with
+        moderate Lipshitz constant (close to 1.0)
+
+        If N=1 then suborutine generates only one point at the middle of [A,B]
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void taskgenint1d(double a,
+            double b,
+            int n,
+            ref double[] x,
+            ref double[] y)
+        {
+            int i = 0;
+            double h = 0;
+
+            x = new double[0];
+            y = new double[0];
+
+            ap.assert(n>=1, "TaskGenInterpolationEqdist1D: N<1!");
+            x = new double[n];
+            y = new double[n];
+            if( n>1 )
+            {
+                x[0] = a;
+                y[0] = 2*math.randomreal()-1;
+                h = (b-a)/(n-1);
+                for(i=1; i<=n-1; i++)
+                {
+                    if( i!=n-1 )
+                    {
+                        x[i] = a+(i+0.2*(2*math.randomreal()-1))*h;
+                    }
+                    else
+                    {
+                        x[i] = b;
+                    }
+                    y[i] = y[i-1]+(2*math.randomreal()-1)*(x[i]-x[i-1]);
+                }
+            }
+            else
+            {
+                x[0] = 0.5*(a+b);
+                y[0] = 2*math.randomreal()-1;
+            }
+        }
+
+
+        /*************************************************************************
+        This function generates  1-dimensional equidistant interpolation task with
+        moderate Lipshitz constant (close to 1.0)
+
+        If N=1 then suborutine generates only one point at the middle of [A,B]
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void taskgenint1dequidist(double a,
+            double b,
+            int n,
+            ref double[] x,
+            ref double[] y)
+        {
+            int i = 0;
+            double h = 0;
+
+            x = new double[0];
+            y = new double[0];
+
+            ap.assert(n>=1, "TaskGenInterpolationEqdist1D: N<1!");
+            x = new double[n];
+            y = new double[n];
+            if( n>1 )
+            {
+                x[0] = a;
+                y[0] = 2*math.randomreal()-1;
+                h = (b-a)/(n-1);
+                for(i=1; i<=n-1; i++)
+                {
+                    x[i] = a+i*h;
+                    y[i] = y[i-1]+(2*math.randomreal()-1)*h;
+                }
+            }
+            else
+            {
+                x[0] = 0.5*(a+b);
+                y[0] = 2*math.randomreal()-1;
+            }
+        }
+
+
+        /*************************************************************************
+        This function generates  1-dimensional Chebyshev-1 interpolation task with
+        moderate Lipshitz constant (close to 1.0)
+
+        If N=1 then suborutine generates only one point at the middle of [A,B]
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void taskgenint1dcheb1(double a,
+            double b,
+            int n,
+            ref double[] x,
+            ref double[] y)
+        {
+            int i = 0;
+
+            x = new double[0];
+            y = new double[0];
+
+            ap.assert(n>=1, "TaskGenInterpolation1DCheb1: N<1!");
+            x = new double[n];
+            y = new double[n];
+            if( n>1 )
+            {
+                for(i=0; i<=n-1; i++)
+                {
+                    x[i] = 0.5*(b+a)+0.5*(b-a)*Math.Cos(Math.PI*(2*i+1)/(2*n));
+                    if( i==0 )
+                    {
+                        y[i] = 2*math.randomreal()-1;
+                    }
+                    else
+                    {
+                        y[i] = y[i-1]+(2*math.randomreal()-1)*(x[i]-x[i-1]);
+                    }
+                }
+            }
+            else
+            {
+                x[0] = 0.5*(a+b);
+                y[0] = 2*math.randomreal()-1;
+            }
+        }
+
+
+        /*************************************************************************
+        This function generates  1-dimensional Chebyshev-2 interpolation task with
+        moderate Lipshitz constant (close to 1.0)
+
+        If N=1 then suborutine generates only one point at the middle of [A,B]
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void taskgenint1dcheb2(double a,
+            double b,
+            int n,
+            ref double[] x,
+            ref double[] y)
+        {
+            int i = 0;
+
+            x = new double[0];
+            y = new double[0];
+
+            ap.assert(n>=1, "TaskGenInterpolation1DCheb2: N<1!");
+            x = new double[n];
+            y = new double[n];
+            if( n>1 )
+            {
+                for(i=0; i<=n-1; i++)
+                {
+                    x[i] = 0.5*(b+a)+0.5*(b-a)*Math.Cos(Math.PI*i/(n-1));
+                    if( i==0 )
+                    {
+                        y[i] = 2*math.randomreal()-1;
+                    }
+                    else
+                    {
+                        y[i] = y[i-1]+(2*math.randomreal()-1)*(x[i]-x[i-1]);
+                    }
+                }
+            }
+            else
+            {
+                x[0] = 0.5*(a+b);
+                y[0] = 2*math.randomreal()-1;
+            }
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from X[] are distinct. It does more
+        than just usual floating point comparison:
+        * first, it calculates max(X) and min(X)
+        * second, it maps X[] from [min,max] to [1,2]
+        * only at this stage actual comparison is done
+
+        The meaning of such check is to ensure that all values are "distinct enough"
+        and will not cause interpolation subroutine to fail.
+
+        NOTE:
+            X[] must be sorted by ascending (subroutine ASSERT's it)
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool aredistinct(double[] x,
+            int n)
+        {
+            bool result = new bool();
+            double a = 0;
+            double b = 0;
+            int i = 0;
+            bool nonsorted = new bool();
+
+            ap.assert(n>=1, "APSERVAreDistinct: internal error (N<1)");
+            if( n==1 )
+            {
+                
+                //
+                // everything is alright, it is up to caller to decide whether it
+                // can interpolate something with just one point
+                //
+                result = true;
+                return result;
+            }
+            a = x[0];
+            b = x[0];
+            nonsorted = false;
+            for(i=1; i<=n-1; i++)
+            {
+                a = Math.Min(a, x[i]);
+                b = Math.Max(b, x[i]);
+                nonsorted = nonsorted | (double)(x[i-1])>=(double)(x[i]);
+            }
+            ap.assert(!nonsorted, "APSERVAreDistinct: internal error (not sorted)");
+            for(i=1; i<=n-1; i++)
+            {
+                if( (double)((x[i]-a)/(b-a)+1)==(double)((x[i-1]-a)/(b-a)+1) )
+                {
+                    result = false;
+                    return result;
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        If Length(X)<N, resizes X
+
+          -- ALGLIB --
+             Copyright 20.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void bvectorsetlengthatleast(ref bool[] x,
+            int n)
+        {
+            if( ap.len(x)<n )
+            {
+                x = new bool[n];
+            }
+        }
+
+
+        /*************************************************************************
+        If Length(X)<N, resizes X
+
+          -- ALGLIB --
+             Copyright 20.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void ivectorsetlengthatleast(ref int[] x,
+            int n)
+        {
+            if( ap.len(x)<n )
+            {
+                x = new int[n];
+            }
+        }
+
+
+        /*************************************************************************
+        If Length(X)<N, resizes X
+
+          -- ALGLIB --
+             Copyright 20.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void rvectorsetlengthatleast(ref double[] x,
+            int n)
+        {
+            if( ap.len(x)<n )
+            {
+                x = new double[n];
+            }
+        }
+
+
+        /*************************************************************************
+        If Cols(X)<N or Rows(X)<M, resizes X
+
+          -- ALGLIB --
+             Copyright 20.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void rmatrixsetlengthatleast(ref double[,] x,
+            int m,
+            int n)
+        {
+            if( ap.rows(x)<m | ap.cols(x)<n )
+            {
+                x = new double[m, n];
+            }
+        }
+
+
+        /*************************************************************************
+        Resizes X and:
+        * preserves old contents of X
+        * fills new elements by zeros
+
+          -- ALGLIB --
+             Copyright 20.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void rmatrixresize(ref double[,] x,
+            int m,
+            int n)
+        {
+            double[,] oldx = new double[0,0];
+            int i = 0;
+            int j = 0;
+            int m2 = 0;
+            int n2 = 0;
+
+            m2 = ap.rows(x);
+            n2 = ap.cols(x);
+            ap.swap(ref x, ref oldx);
+            x = new double[m, n];
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    if( i<m2 & j<n2 )
+                    {
+                        x[i,j] = oldx[i,j];
+                    }
+                    else
+                    {
+                        x[i,j] = 0.0;
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from X[] are finite
+
+          -- ALGLIB --
+             Copyright 18.06.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool isfinitevector(double[] x,
+            int n)
+        {
+            bool result = new bool();
+            int i = 0;
+
+            ap.assert(n>=0, "APSERVIsFiniteVector: internal error (N<0)");
+            for(i=0; i<=n-1; i++)
+            {
+                if( !math.isfinite(x[i]) )
+                {
+                    result = false;
+                    return result;
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from X[] are finite
+
+          -- ALGLIB --
+             Copyright 18.06.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool isfinitecvector(complex[] z,
+            int n)
+        {
+            bool result = new bool();
+            int i = 0;
+
+            ap.assert(n>=0, "APSERVIsFiniteCVector: internal error (N<0)");
+            for(i=0; i<=n-1; i++)
+            {
+                if( !math.isfinite(z[i].x) | !math.isfinite(z[i].y) )
+                {
+                    result = false;
+                    return result;
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from X[0..M-1,0..N-1] are finite
+
+          -- ALGLIB --
+             Copyright 18.06.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool apservisfinitematrix(double[,] x,
+            int m,
+            int n)
+        {
+            bool result = new bool();
+            int i = 0;
+            int j = 0;
+
+            ap.assert(n>=0, "APSERVIsFiniteMatrix: internal error (N<0)");
+            ap.assert(m>=0, "APSERVIsFiniteMatrix: internal error (M<0)");
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    if( !math.isfinite(x[i,j]) )
+                    {
+                        result = false;
+                        return result;
+                    }
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from X[0..M-1,0..N-1] are finite
+
+          -- ALGLIB --
+             Copyright 18.06.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool apservisfinitecmatrix(complex[,] x,
+            int m,
+            int n)
+        {
+            bool result = new bool();
+            int i = 0;
+            int j = 0;
+
+            ap.assert(n>=0, "APSERVIsFiniteCMatrix: internal error (N<0)");
+            ap.assert(m>=0, "APSERVIsFiniteCMatrix: internal error (M<0)");
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    if( !math.isfinite(x[i,j].x) | !math.isfinite(x[i,j].y) )
+                    {
+                        result = false;
+                        return result;
+                    }
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from upper/lower triangle of
+        X[0..N-1,0..N-1] are finite
+
+          -- ALGLIB --
+             Copyright 18.06.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool isfinitertrmatrix(double[,] x,
+            int n,
+            bool isupper)
+        {
+            bool result = new bool();
+            int i = 0;
+            int j1 = 0;
+            int j2 = 0;
+            int j = 0;
+
+            ap.assert(n>=0, "APSERVIsFiniteRTRMatrix: internal error (N<0)");
+            for(i=0; i<=n-1; i++)
+            {
+                if( isupper )
+                {
+                    j1 = i;
+                    j2 = n-1;
+                }
+                else
+                {
+                    j1 = 0;
+                    j2 = i;
+                }
+                for(j=j1; j<=j2; j++)
+                {
+                    if( !math.isfinite(x[i,j]) )
+                    {
+                        result = false;
+                        return result;
+                    }
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from upper/lower triangle of
+        X[0..N-1,0..N-1] are finite
+
+          -- ALGLIB --
+             Copyright 18.06.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool apservisfinitectrmatrix(complex[,] x,
+            int n,
+            bool isupper)
+        {
+            bool result = new bool();
+            int i = 0;
+            int j1 = 0;
+            int j2 = 0;
+            int j = 0;
+
+            ap.assert(n>=0, "APSERVIsFiniteCTRMatrix: internal error (N<0)");
+            for(i=0; i<=n-1; i++)
+            {
+                if( isupper )
+                {
+                    j1 = i;
+                    j2 = n-1;
+                }
+                else
+                {
+                    j1 = 0;
+                    j2 = i;
+                }
+                for(j=j1; j<=j2; j++)
+                {
+                    if( !math.isfinite(x[i,j].x) | !math.isfinite(x[i,j].y) )
+                    {
+                        result = false;
+                        return result;
+                    }
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function checks that all values from X[0..M-1,0..N-1] are  finite  or
+        NaN's.
+
+          -- ALGLIB --
+             Copyright 18.06.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool apservisfiniteornanmatrix(double[,] x,
+            int m,
+            int n)
+        {
+            bool result = new bool();
+            int i = 0;
+            int j = 0;
+
+            ap.assert(n>=0, "APSERVIsFiniteOrNaNMatrix: internal error (N<0)");
+            ap.assert(m>=0, "APSERVIsFiniteOrNaNMatrix: internal error (M<0)");
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    if( !(math.isfinite(x[i,j]) | Double.IsNaN(x[i,j])) )
+                    {
+                        result = false;
+                        return result;
+                    }
+                }
+            }
+            result = true;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Safe sqrt(x^2+y^2)
+
+          -- ALGLIB --
+             Copyright by Bochkanov Sergey
+        *************************************************************************/
+        public static double safepythag2(double x,
+            double y)
+        {
+            double result = 0;
+            double w = 0;
+            double xabs = 0;
+            double yabs = 0;
+            double z = 0;
+
+            xabs = Math.Abs(x);
+            yabs = Math.Abs(y);
+            w = Math.Max(xabs, yabs);
+            z = Math.Min(xabs, yabs);
+            if( (double)(z)==(double)(0) )
+            {
+                result = w;
+            }
+            else
+            {
+                result = w*Math.Sqrt(1+math.sqr(z/w));
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Safe sqrt(x^2+y^2)
+
+          -- ALGLIB --
+             Copyright by Bochkanov Sergey
+        *************************************************************************/
+        public static double safepythag3(double x,
+            double y,
+            double z)
+        {
+            double result = 0;
+            double w = 0;
+
+            w = Math.Max(Math.Abs(x), Math.Max(Math.Abs(y), Math.Abs(z)));
+            if( (double)(w)==(double)(0) )
+            {
+                result = 0;
+                return result;
+            }
+            x = x/w;
+            y = y/w;
+            z = z/w;
+            result = w*Math.Sqrt(math.sqr(x)+math.sqr(y)+math.sqr(z));
+            return result;
+        }
+
+
+        /*************************************************************************
+        Safe division.
+
+        This function attempts to calculate R=X/Y without overflow.
+
+        It returns:
+        * +1, if abs(X/Y)>=MaxRealNumber or undefined - overflow-like situation
+              (no overlfow is generated, R is either NAN, PosINF, NegINF)
+        *  0, if MinRealNumber<abs(X/Y)<MaxRealNumber or X=0, Y<>0
+              (R contains result, may be zero)
+        * -1, if 0<abs(X/Y)<MinRealNumber - underflow-like situation
+              (R contains zero; it corresponds to underflow)
+
+        No overflow is generated in any case.
+
+          -- ALGLIB --
+             Copyright by Bochkanov Sergey
+        *************************************************************************/
+        public static int saferdiv(double x,
+            double y,
+            ref double r)
+        {
+            int result = 0;
+
+            r = 0;
+
+            
+            //
+            // Two special cases:
+            // * Y=0
+            // * X=0 and Y<>0
+            //
+            if( (double)(y)==(double)(0) )
+            {
+                result = 1;
+                if( (double)(x)==(double)(0) )
+                {
+                    r = Double.NaN;
+                }
+                if( (double)(x)>(double)(0) )
+                {
+                    r = Double.PositiveInfinity;
+                }
+                if( (double)(x)<(double)(0) )
+                {
+                    r = Double.NegativeInfinity;
+                }
+                return result;
+            }
+            if( (double)(x)==(double)(0) )
+            {
+                r = 0;
+                result = 0;
+                return result;
+            }
+            
+            //
+            // make Y>0
+            //
+            if( (double)(y)<(double)(0) )
+            {
+                x = -x;
+                y = -y;
+            }
+            
+            //
+            //
+            //
+            if( (double)(y)>=(double)(1) )
+            {
+                r = x/y;
+                if( (double)(Math.Abs(r))<=(double)(math.minrealnumber) )
+                {
+                    result = -1;
+                    r = 0;
+                }
+                else
+                {
+                    result = 0;
+                }
+            }
+            else
+            {
+                if( (double)(Math.Abs(x))>=(double)(math.maxrealnumber*y) )
+                {
+                    if( (double)(x)>(double)(0) )
+                    {
+                        r = Double.PositiveInfinity;
+                    }
+                    else
+                    {
+                        r = Double.NegativeInfinity;
+                    }
+                    result = 1;
+                }
+                else
+                {
+                    r = x/y;
+                    result = 0;
+                }
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function calculates "safe" min(X/Y,V) for positive finite X, Y, V.
+        No overflow is generated in any case.
+
+          -- ALGLIB --
+             Copyright by Bochkanov Sergey
+        *************************************************************************/
+        public static double safeminposrv(double x,
+            double y,
+            double v)
+        {
+            double result = 0;
+            double r = 0;
+
+            if( (double)(y)>=(double)(1) )
+            {
+                
+                //
+                // Y>=1, we can safely divide by Y
+                //
+                r = x/y;
+                result = v;
+                if( (double)(v)>(double)(r) )
+                {
+                    result = r;
+                }
+                else
+                {
+                    result = v;
+                }
+            }
+            else
+            {
+                
+                //
+                // Y<1, we can safely multiply by Y
+                //
+                if( (double)(x)<(double)(v*y) )
+                {
+                    result = x/y;
+                }
+                else
+                {
+                    result = v;
+                }
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function makes periodic mapping of X to [A,B].
+
+        It accepts X, A, B (A>B). It returns T which lies in  [A,B] and integer K,
+        such that X = T + K*(B-A).
+
+        NOTES:
+        * K is represented as real value, although actually it is integer
+        * T is guaranteed to be in [A,B]
+        * T replaces X
+
+          -- ALGLIB --
+             Copyright by Bochkanov Sergey
+        *************************************************************************/
+        public static void apperiodicmap(ref double x,
+            double a,
+            double b,
+            ref double k)
+        {
+            k = 0;
+
+            ap.assert((double)(a)<(double)(b), "APPeriodicMap: internal error!");
+            k = (int)Math.Floor((x-a)/(b-a));
+            x = x-k*(b-a);
+            while( (double)(x)<(double)(a) )
+            {
+                x = x+(b-a);
+                k = k-1;
+            }
+            while( (double)(x)>(double)(b) )
+            {
+                x = x-(b-a);
+                k = k+1;
+            }
+            x = Math.Max(x, a);
+            x = Math.Min(x, b);
+        }
+
+
+        /*************************************************************************
+        'bounds' value: maps X to [B1,B2]
+
+          -- ALGLIB --
+             Copyright 20.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double boundval(double x,
+            double b1,
+            double b2)
+        {
+            double result = 0;
+
+            if( (double)(x)<=(double)(b1) )
+            {
+                result = b1;
+                return result;
+            }
+            if( (double)(x)>=(double)(b2) )
+            {
+                result = b2;
+                return result;
+            }
+            result = x;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Allocation of serializer: complex value
+        *************************************************************************/
+        public static void alloccomplex(alglib.serializer s,
+            complex v)
+        {
+            s.alloc_entry();
+            s.alloc_entry();
+        }
+
+
+        /*************************************************************************
+        Serialization: complex value
+        *************************************************************************/
+        public static void serializecomplex(alglib.serializer s,
+            complex v)
+        {
+            s.serialize_double(v.x);
+            s.serialize_double(v.y);
+        }
+
+
+        /*************************************************************************
+        Unserialization: complex value
+        *************************************************************************/
+        public static complex unserializecomplex(alglib.serializer s)
+        {
+            complex result = 0;
+
+            result.x = s.unserialize_double();
+            result.y = s.unserialize_double();
+            return result;
+        }
+
+
+        /*************************************************************************
+        Allocation of serializer: real array
+        *************************************************************************/
+        public static void allocrealarray(alglib.serializer s,
+            double[] v,
+            int n)
+        {
+            int i = 0;
+
+            if( n<0 )
+            {
+                n = ap.len(v);
+            }
+            s.alloc_entry();
+            for(i=0; i<=n-1; i++)
+            {
+                s.alloc_entry();
+            }
+        }
+
+
+        /*************************************************************************
+        Serialization: complex value
+        *************************************************************************/
+        public static void serializerealarray(alglib.serializer s,
+            double[] v,
+            int n)
+        {
+            int i = 0;
+
+            if( n<0 )
+            {
+                n = ap.len(v);
+            }
+            s.serialize_int(n);
+            for(i=0; i<=n-1; i++)
+            {
+                s.serialize_double(v[i]);
+            }
+        }
+
+
+        /*************************************************************************
+        Unserialization: complex value
+        *************************************************************************/
+        public static void unserializerealarray(alglib.serializer s,
+            ref double[] v)
+        {
+            int n = 0;
+            int i = 0;
+            double t = 0;
+
+            v = new double[0];
+
+            n = s.unserialize_int();
+            if( n==0 )
+            {
+                return;
+            }
+            v = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                t = s.unserialize_double();
+                v[i] = t;
+            }
+        }
+
+
+        /*************************************************************************
+        Allocation of serializer: Integer array
+        *************************************************************************/
+        public static void allocintegerarray(alglib.serializer s,
+            int[] v,
+            int n)
+        {
+            int i = 0;
+
+            if( n<0 )
+            {
+                n = ap.len(v);
+            }
+            s.alloc_entry();
+            for(i=0; i<=n-1; i++)
+            {
+                s.alloc_entry();
+            }
+        }
+
+
+        /*************************************************************************
+        Serialization: Integer array
+        *************************************************************************/
+        public static void serializeintegerarray(alglib.serializer s,
+            int[] v,
+            int n)
+        {
+            int i = 0;
+
+            if( n<0 )
+            {
+                n = ap.len(v);
+            }
+            s.serialize_int(n);
+            for(i=0; i<=n-1; i++)
+            {
+                s.serialize_int(v[i]);
+            }
+        }
+
+
+        /*************************************************************************
+        Unserialization: complex value
+        *************************************************************************/
+        public static void unserializeintegerarray(alglib.serializer s,
+            ref int[] v)
+        {
+            int n = 0;
+            int i = 0;
+            int t = 0;
+
+            v = new int[0];
+
+            n = s.unserialize_int();
+            if( n==0 )
+            {
+                return;
+            }
+            v = new int[n];
+            for(i=0; i<=n-1; i++)
+            {
+                t = s.unserialize_int();
+                v[i] = t;
+            }
+        }
+
+
+        /*************************************************************************
+        Allocation of serializer: real matrix
+        *************************************************************************/
+        public static void allocrealmatrix(alglib.serializer s,
+            double[,] v,
+            int n0,
+            int n1)
+        {
+            int i = 0;
+            int j = 0;
+
+            if( n0<0 )
+            {
+                n0 = ap.rows(v);
+            }
+            if( n1<0 )
+            {
+                n1 = ap.cols(v);
+            }
+            s.alloc_entry();
+            s.alloc_entry();
+            for(i=0; i<=n0-1; i++)
+            {
+                for(j=0; j<=n1-1; j++)
+                {
+                    s.alloc_entry();
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Serialization: complex value
+        *************************************************************************/
+        public static void serializerealmatrix(alglib.serializer s,
+            double[,] v,
+            int n0,
+            int n1)
+        {
+            int i = 0;
+            int j = 0;
+
+            if( n0<0 )
+            {
+                n0 = ap.rows(v);
+            }
+            if( n1<0 )
+            {
+                n1 = ap.cols(v);
+            }
+            s.serialize_int(n0);
+            s.serialize_int(n1);
+            for(i=0; i<=n0-1; i++)
+            {
+                for(j=0; j<=n1-1; j++)
+                {
+                    s.serialize_double(v[i,j]);
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Unserialization: complex value
+        *************************************************************************/
+        public static void unserializerealmatrix(alglib.serializer s,
+            ref double[,] v)
+        {
+            int i = 0;
+            int j = 0;
+            int n0 = 0;
+            int n1 = 0;
+            double t = 0;
+
+            v = new double[0,0];
+
+            n0 = s.unserialize_int();
+            n1 = s.unserialize_int();
+            if( n0==0 | n1==0 )
+            {
+                return;
+            }
+            v = new double[n0, n1];
+            for(i=0; i<=n0-1; i++)
+            {
+                for(j=0; j<=n1-1; j++)
+                {
+                    t = s.unserialize_double();
+                    v[i,j] = t;
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Copy integer array
+        *************************************************************************/
+        public static void copyintegerarray(int[] src,
+            ref int[] dst)
+        {
+            int i = 0;
+
+            dst = new int[0];
+
+            if( ap.len(src)>0 )
+            {
+                dst = new int[ap.len(src)];
+                for(i=0; i<=ap.len(src)-1; i++)
+                {
+                    dst[i] = src[i];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Copy real array
+        *************************************************************************/
+        public static void copyrealarray(double[] src,
+            ref double[] dst)
+        {
+            int i = 0;
+
+            dst = new double[0];
+
+            if( ap.len(src)>0 )
+            {
+                dst = new double[ap.len(src)];
+                for(i=0; i<=ap.len(src)-1; i++)
+                {
+                    dst[i] = src[i];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Copy real matrix
+        *************************************************************************/
+        public static void copyrealmatrix(double[,] src,
+            ref double[,] dst)
+        {
+            int i = 0;
+            int j = 0;
+
+            dst = new double[0,0];
+
+            if( ap.rows(src)>0 & ap.cols(src)>0 )
+            {
+                dst = new double[ap.rows(src), ap.cols(src)];
+                for(i=0; i<=ap.rows(src)-1; i++)
+                {
+                    for(j=0; j<=ap.cols(src)-1; j++)
+                    {
+                        dst[i,j] = src[i,j];
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function searches integer array. Elements in this array are actually
+        records, each NRec elements wide. Each record has unique header - NHeader
+        integer values, which identify it. Records are lexicographically sorted by
+        header.
+
+        Records are identified by their index, not offset (offset = NRec*index).
+
+        This function searches A (records with indices [I0,I1)) for a record with
+        header B. It returns index of this record (not offset!), or -1 on failure.
+
+          -- ALGLIB --
+             Copyright 28.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static int recsearch(ref int[] a,
+            int nrec,
+            int nheader,
+            int i0,
+            int i1,
+            int[] b)
+        {
+            int result = 0;
+            int mididx = 0;
+            int cflag = 0;
+            int k = 0;
+            int offs = 0;
+
+            result = -1;
+            while( true )
+            {
+                if( i0>=i1 )
+                {
+                    break;
+                }
+                mididx = (i0+i1)/2;
+                offs = nrec*mididx;
+                cflag = 0;
+                for(k=0; k<=nheader-1; k++)
+                {
+                    if( a[offs+k]<b[k] )
+                    {
+                        cflag = -1;
+                        break;
+                    }
+                    if( a[offs+k]>b[k] )
+                    {
+                        cflag = 1;
+                        break;
+                    }
+                }
+                if( cflag==0 )
+                {
+                    result = mididx;
+                    return result;
+                }
+                if( cflag<0 )
+                {
+                    i0 = mididx+1;
+                }
+                else
+                {
+                    i1 = mididx;
+                }
+            }
+            return result;
+        }
+
+
+    }
     public class tsort
     {
@@ -65,9 +1399,32 @@
             ref int[] p2)
         {
+            apserv.apbuffers buf = new apserv.apbuffers();
+
+            p1 = new int[0];
+            p2 = new int[0];
+
+            tagsortbuf(ref a, n, ref p1, ref p2, buf);
+        }
+
+
+        /*************************************************************************
+        Buffered variant of TagSort, which accepts preallocated output arrays as
+        well as special structure for buffered allocations. If arrays are too
+        short, they are reallocated. If they are large enough, no memory
+        allocation is done.
+
+        It is intended to be used in the performance-critical parts of code, where
+        additional allocations can lead to severe performance degradation
+
+          -- ALGLIB --
+             Copyright 14.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void tagsortbuf(ref double[] a,
+            int n,
+            ref int[] p1,
+            ref int[] p2,
+            apserv.apbuffers buf)
+        {
             int i = 0;
-            int[] pv = new int[0];
-            int[] vp = new int[0];
-            double[] bufa = new double[0];
-            int[] bufb = new int[0];
             int lv = 0;
             int lp = 0;
@@ -75,7 +1432,4 @@
             int rp = 0;
 
-            p1 = new int[0];
-            p2 = new int[0];
-
             
             //
@@ -88,6 +1442,6 @@
             if( n==1 )
             {
-                p1 = new int[0+1];
-                p2 = new int[0+1];
+                apserv.ivectorsetlengthatleast(ref p1, 1);
+                apserv.ivectorsetlengthatleast(ref p2, 1);
                 p1[0] = 0;
                 p2[0] = 0;
@@ -98,5 +1452,5 @@
             // General case, N>1: prepare permutations table P1
             //
-            p1 = new int[n-1+1];
+            apserv.ivectorsetlengthatleast(ref p1, n);
             for(i=0; i<=n-1; i++)
             {
@@ -107,7 +1461,7 @@
             // General case, N>1: sort, update P1
             //
-            bufa = new double[n];
-            bufb = new int[n];
-            tagsortfasti(ref a, ref p1, ref bufa, ref bufb, n);
+            apserv.rvectorsetlengthatleast(ref buf.ra0, n);
+            apserv.ivectorsetlengthatleast(ref buf.ia0, n);
+            tagsortfasti(ref a, ref p1, ref buf.ra0, ref buf.ia0, n);
             
             //
@@ -115,6 +1469,6 @@
             //
             // To fill P2 we maintain two arrays:
-            // * PV, Position(Value). PV[i] contains position of I-th key at the moment
-            // * VP, Value(Position). VP[i] contains key which has position I at the moment
+            // * PV (Buf.IA0), Position(Value). PV[i] contains position of I-th key at the moment
+            // * VP (Buf.IA1), Value(Position). VP[i] contains key which has position I at the moment
             //
             // At each step we making permutation of two items:
@@ -123,11 +1477,11 @@
             // and updating PV[] and VP[] correspondingly.
             //
-            pv = new int[n-1+1];
-            vp = new int[n-1+1];
-            p2 = new int[n-1+1];
+            apserv.ivectorsetlengthatleast(ref buf.ia0, n);
+            apserv.ivectorsetlengthatleast(ref buf.ia1, n);
+            apserv.ivectorsetlengthatleast(ref p2, n);
             for(i=0; i<=n-1; i++)
             {
-                pv[i] = i;
-                vp[i] = i;
+                buf.ia0[i] = i;
+                buf.ia1[i] = i;
             }
             for(i=0; i<=n-1; i++)
@@ -138,7 +1492,7 @@
                 //
                 lp = i;
-                lv = vp[lp];
+                lv = buf.ia1[lp];
                 rv = p1[i];
-                rp = pv[rv];
+                rp = buf.ia0[rv];
                 
                 //
@@ -150,8 +1504,8 @@
                 // update PV and VP
                 //
-                vp[lp] = rv;
-                vp[rp] = lv;
-                pv[lv] = rp;
-                pv[rv] = lp;
+                buf.ia1[lp] = rv;
+                buf.ia1[rp] = lv;
+                buf.ia0[lv] = rp;
+                buf.ia0[rv] = lp;
             }
         }
@@ -1183,746 +2537,4 @@
             tagsortfastrec(ref a, ref bufa, i1, i1+cntless-1);
             tagsortfastrec(ref a, ref bufa, i1+cntless+cnteq, i2);
-        }
-
-
-    }
-    public class apserv
-    {
-        /*************************************************************************
-        Buffers for internal functions which need buffers:
-        * check for size of the buffer you want to use.
-        * if buffer is too small, resize it; leave unchanged, if it is larger than
-          needed.
-        * use it.
-
-        We can pass this structure to multiple functions;  after first run through
-        functions buffer sizes will be finally determined,  and  on  a next run no
-        allocation will be required.
-        *************************************************************************/
-        public class apbuffers
-        {
-            public int[] ia1;
-            public int[] ia2;
-            public double[] ra1;
-            public double[] ra2;
-            public apbuffers()
-            {
-                ia1 = new int[0];
-                ia2 = new int[0];
-                ra1 = new double[0];
-                ra2 = new double[0];
-            }
-        };
-
-
-
-
-        /*************************************************************************
-        This  function  generates  1-dimensional  general  interpolation task with
-        moderate Lipshitz constant (close to 1.0)
-
-        If N=1 then suborutine generates only one point at the middle of [A,B]
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void taskgenint1d(double a,
-            double b,
-            int n,
-            ref double[] x,
-            ref double[] y)
-        {
-            int i = 0;
-            double h = 0;
-
-            x = new double[0];
-            y = new double[0];
-
-            ap.assert(n>=1, "TaskGenInterpolationEqdist1D: N<1!");
-            x = new double[n];
-            y = new double[n];
-            if( n>1 )
-            {
-                x[0] = a;
-                y[0] = 2*math.randomreal()-1;
-                h = (b-a)/(n-1);
-                for(i=1; i<=n-1; i++)
-                {
-                    if( i!=n-1 )
-                    {
-                        x[i] = a+(i+0.2*(2*math.randomreal()-1))*h;
-                    }
-                    else
-                    {
-                        x[i] = b;
-                    }
-                    y[i] = y[i-1]+(2*math.randomreal()-1)*(x[i]-x[i-1]);
-                }
-            }
-            else
-            {
-                x[0] = 0.5*(a+b);
-                y[0] = 2*math.randomreal()-1;
-            }
-        }
-
-
-        /*************************************************************************
-        This function generates  1-dimensional equidistant interpolation task with
-        moderate Lipshitz constant (close to 1.0)
-
-        If N=1 then suborutine generates only one point at the middle of [A,B]
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void taskgenint1dequidist(double a,
-            double b,
-            int n,
-            ref double[] x,
-            ref double[] y)
-        {
-            int i = 0;
-            double h = 0;
-
-            x = new double[0];
-            y = new double[0];
-
-            ap.assert(n>=1, "TaskGenInterpolationEqdist1D: N<1!");
-            x = new double[n];
-            y = new double[n];
-            if( n>1 )
-            {
-                x[0] = a;
-                y[0] = 2*math.randomreal()-1;
-                h = (b-a)/(n-1);
-                for(i=1; i<=n-1; i++)
-                {
-                    x[i] = a+i*h;
-                    y[i] = y[i-1]+(2*math.randomreal()-1)*h;
-                }
-            }
-            else
-            {
-                x[0] = 0.5*(a+b);
-                y[0] = 2*math.randomreal()-1;
-            }
-        }
-
-
-        /*************************************************************************
-        This function generates  1-dimensional Chebyshev-1 interpolation task with
-        moderate Lipshitz constant (close to 1.0)
-
-        If N=1 then suborutine generates only one point at the middle of [A,B]
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void taskgenint1dcheb1(double a,
-            double b,
-            int n,
-            ref double[] x,
-            ref double[] y)
-        {
-            int i = 0;
-
-            x = new double[0];
-            y = new double[0];
-
-            ap.assert(n>=1, "TaskGenInterpolation1DCheb1: N<1!");
-            x = new double[n];
-            y = new double[n];
-            if( n>1 )
-            {
-                for(i=0; i<=n-1; i++)
-                {
-                    x[i] = 0.5*(b+a)+0.5*(b-a)*Math.Cos(Math.PI*(2*i+1)/(2*n));
-                    if( i==0 )
-                    {
-                        y[i] = 2*math.randomreal()-1;
-                    }
-                    else
-                    {
-                        y[i] = y[i-1]+(2*math.randomreal()-1)*(x[i]-x[i-1]);
-                    }
-                }
-            }
-            else
-            {
-                x[0] = 0.5*(a+b);
-                y[0] = 2*math.randomreal()-1;
-            }
-        }
-
-
-        /*************************************************************************
-        This function generates  1-dimensional Chebyshev-2 interpolation task with
-        moderate Lipshitz constant (close to 1.0)
-
-        If N=1 then suborutine generates only one point at the middle of [A,B]
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void taskgenint1dcheb2(double a,
-            double b,
-            int n,
-            ref double[] x,
-            ref double[] y)
-        {
-            int i = 0;
-
-            x = new double[0];
-            y = new double[0];
-
-            ap.assert(n>=1, "TaskGenInterpolation1DCheb2: N<1!");
-            x = new double[n];
-            y = new double[n];
-            if( n>1 )
-            {
-                for(i=0; i<=n-1; i++)
-                {
-                    x[i] = 0.5*(b+a)+0.5*(b-a)*Math.Cos(Math.PI*i/(n-1));
-                    if( i==0 )
-                    {
-                        y[i] = 2*math.randomreal()-1;
-                    }
-                    else
-                    {
-                        y[i] = y[i-1]+(2*math.randomreal()-1)*(x[i]-x[i-1]);
-                    }
-                }
-            }
-            else
-            {
-                x[0] = 0.5*(a+b);
-                y[0] = 2*math.randomreal()-1;
-            }
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from X[] are distinct. It does more
-        than just usual floating point comparison:
-        * first, it calculates max(X) and min(X)
-        * second, it maps X[] from [min,max] to [1,2]
-        * only at this stage actual comparison is done
-
-        The meaning of such check is to ensure that all values are "distinct enough"
-        and will not cause interpolation subroutine to fail.
-
-        NOTE:
-            X[] must be sorted by ascending (subroutine ASSERT's it)
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool aredistinct(double[] x,
-            int n)
-        {
-            bool result = new bool();
-            double a = 0;
-            double b = 0;
-            int i = 0;
-            bool nonsorted = new bool();
-
-            ap.assert(n>=1, "APSERVAreDistinct: internal error (N<1)");
-            if( n==1 )
-            {
-                
-                //
-                // everything is alright, it is up to caller to decide whether it
-                // can interpolate something with just one point
-                //
-                result = true;
-                return result;
-            }
-            a = x[0];
-            b = x[0];
-            nonsorted = false;
-            for(i=1; i<=n-1; i++)
-            {
-                a = Math.Min(a, x[i]);
-                b = Math.Max(b, x[i]);
-                nonsorted = nonsorted | (double)(x[i-1])>=(double)(x[i]);
-            }
-            ap.assert(!nonsorted, "APSERVAreDistinct: internal error (not sorted)");
-            for(i=1; i<=n-1; i++)
-            {
-                if( (double)((x[i]-a)/(b-a)+1)==(double)((x[i-1]-a)/(b-a)+1) )
-                {
-                    result = false;
-                    return result;
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from X[] are finite
-
-          -- ALGLIB --
-             Copyright 18.06.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool isfinitevector(double[] x,
-            int n)
-        {
-            bool result = new bool();
-            int i = 0;
-
-            ap.assert(n>=0, "APSERVIsFiniteVector: internal error (N<0)");
-            for(i=0; i<=n-1; i++)
-            {
-                if( !math.isfinite(x[i]) )
-                {
-                    result = false;
-                    return result;
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from X[] are finite
-
-          -- ALGLIB --
-             Copyright 18.06.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool isfinitecvector(complex[] z,
-            int n)
-        {
-            bool result = new bool();
-            int i = 0;
-
-            ap.assert(n>=0, "APSERVIsFiniteCVector: internal error (N<0)");
-            for(i=0; i<=n-1; i++)
-            {
-                if( !math.isfinite(z[i].x) | !math.isfinite(z[i].y) )
-                {
-                    result = false;
-                    return result;
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from X[0..M-1,0..N-1] are finite
-
-          -- ALGLIB --
-             Copyright 18.06.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool apservisfinitematrix(double[,] x,
-            int m,
-            int n)
-        {
-            bool result = new bool();
-            int i = 0;
-            int j = 0;
-
-            ap.assert(n>=0, "APSERVIsFiniteMatrix: internal error (N<0)");
-            ap.assert(m>=0, "APSERVIsFiniteMatrix: internal error (M<0)");
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    if( !math.isfinite(x[i,j]) )
-                    {
-                        result = false;
-                        return result;
-                    }
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from X[0..M-1,0..N-1] are finite
-
-          -- ALGLIB --
-             Copyright 18.06.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool apservisfinitecmatrix(complex[,] x,
-            int m,
-            int n)
-        {
-            bool result = new bool();
-            int i = 0;
-            int j = 0;
-
-            ap.assert(n>=0, "APSERVIsFiniteCMatrix: internal error (N<0)");
-            ap.assert(m>=0, "APSERVIsFiniteCMatrix: internal error (M<0)");
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    if( !math.isfinite(x[i,j].x) | !math.isfinite(x[i,j].y) )
-                    {
-                        result = false;
-                        return result;
-                    }
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from upper/lower triangle of
-        X[0..N-1,0..N-1] are finite
-
-          -- ALGLIB --
-             Copyright 18.06.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool isfinitertrmatrix(double[,] x,
-            int n,
-            bool isupper)
-        {
-            bool result = new bool();
-            int i = 0;
-            int j1 = 0;
-            int j2 = 0;
-            int j = 0;
-
-            ap.assert(n>=0, "APSERVIsFiniteRTRMatrix: internal error (N<0)");
-            for(i=0; i<=n-1; i++)
-            {
-                if( isupper )
-                {
-                    j1 = i;
-                    j2 = n-1;
-                }
-                else
-                {
-                    j1 = 0;
-                    j2 = i;
-                }
-                for(j=j1; j<=j2; j++)
-                {
-                    if( !math.isfinite(x[i,j]) )
-                    {
-                        result = false;
-                        return result;
-                    }
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from upper/lower triangle of
-        X[0..N-1,0..N-1] are finite
-
-          -- ALGLIB --
-             Copyright 18.06.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool apservisfinitectrmatrix(complex[,] x,
-            int n,
-            bool isupper)
-        {
-            bool result = new bool();
-            int i = 0;
-            int j1 = 0;
-            int j2 = 0;
-            int j = 0;
-
-            ap.assert(n>=0, "APSERVIsFiniteCTRMatrix: internal error (N<0)");
-            for(i=0; i<=n-1; i++)
-            {
-                if( isupper )
-                {
-                    j1 = i;
-                    j2 = n-1;
-                }
-                else
-                {
-                    j1 = 0;
-                    j2 = i;
-                }
-                for(j=j1; j<=j2; j++)
-                {
-                    if( !math.isfinite(x[i,j].x) | !math.isfinite(x[i,j].y) )
-                    {
-                        result = false;
-                        return result;
-                    }
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function checks that all values from X[0..M-1,0..N-1] are  finite  or
-        NaN's.
-
-          -- ALGLIB --
-             Copyright 18.06.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool apservisfiniteornanmatrix(double[,] x,
-            int m,
-            int n)
-        {
-            bool result = new bool();
-            int i = 0;
-            int j = 0;
-
-            ap.assert(n>=0, "APSERVIsFiniteOrNaNMatrix: internal error (N<0)");
-            ap.assert(m>=0, "APSERVIsFiniteOrNaNMatrix: internal error (M<0)");
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    if( !(math.isfinite(x[i,j]) | Double.IsNaN(x[i,j])) )
-                    {
-                        result = false;
-                        return result;
-                    }
-                }
-            }
-            result = true;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Safe sqrt(x^2+y^2)
-
-          -- ALGLIB --
-             Copyright by Bochkanov Sergey
-        *************************************************************************/
-        public static double safepythag2(double x,
-            double y)
-        {
-            double result = 0;
-            double w = 0;
-            double xabs = 0;
-            double yabs = 0;
-            double z = 0;
-
-            xabs = Math.Abs(x);
-            yabs = Math.Abs(y);
-            w = Math.Max(xabs, yabs);
-            z = Math.Min(xabs, yabs);
-            if( (double)(z)==(double)(0) )
-            {
-                result = w;
-            }
-            else
-            {
-                result = w*Math.Sqrt(1+math.sqr(z/w));
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Safe sqrt(x^2+y^2)
-
-          -- ALGLIB --
-             Copyright by Bochkanov Sergey
-        *************************************************************************/
-        public static double safepythag3(double x,
-            double y,
-            double z)
-        {
-            double result = 0;
-            double w = 0;
-
-            w = Math.Max(Math.Abs(x), Math.Max(Math.Abs(y), Math.Abs(z)));
-            if( (double)(w)==(double)(0) )
-            {
-                result = 0;
-                return result;
-            }
-            x = x/w;
-            y = y/w;
-            z = z/w;
-            result = w*Math.Sqrt(math.sqr(x)+math.sqr(y)+math.sqr(z));
-            return result;
-        }
-
-
-        /*************************************************************************
-        Safe division.
-
-        This function attempts to calculate R=X/Y without overflow.
-
-        It returns:
-        * +1, if abs(X/Y)>=MaxRealNumber or undefined - overflow-like situation
-              (no overlfow is generated, R is either NAN, PosINF, NegINF)
-        *  0, if MinRealNumber<abs(X/Y)<MaxRealNumber or X=0, Y<>0
-              (R contains result, may be zero)
-        * -1, if 0<abs(X/Y)<MinRealNumber - underflow-like situation
-              (R contains zero; it corresponds to underflow)
-
-        No overflow is generated in any case.
-
-          -- ALGLIB --
-             Copyright by Bochkanov Sergey
-        *************************************************************************/
-        public static int saferdiv(double x,
-            double y,
-            ref double r)
-        {
-            int result = 0;
-
-            r = 0;
-
-            
-            //
-            // Two special cases:
-            // * Y=0
-            // * X=0 and Y<>0
-            //
-            if( (double)(y)==(double)(0) )
-            {
-                result = 1;
-                if( (double)(x)==(double)(0) )
-                {
-                    r = Double.NaN;
-                }
-                if( (double)(x)>(double)(0) )
-                {
-                    r = Double.PositiveInfinity;
-                }
-                if( (double)(x)<(double)(0) )
-                {
-                    r = Double.NegativeInfinity;
-                }
-                return result;
-            }
-            if( (double)(x)==(double)(0) )
-            {
-                r = 0;
-                result = 0;
-                return result;
-            }
-            
-            //
-            // make Y>0
-            //
-            if( (double)(y)<(double)(0) )
-            {
-                x = -x;
-                y = -y;
-            }
-            
-            //
-            //
-            //
-            if( (double)(y)>=(double)(1) )
-            {
-                r = x/y;
-                if( (double)(Math.Abs(r))<=(double)(math.minrealnumber) )
-                {
-                    result = -1;
-                    r = 0;
-                }
-                else
-                {
-                    result = 0;
-                }
-            }
-            else
-            {
-                if( (double)(Math.Abs(x))>=(double)(math.maxrealnumber*y) )
-                {
-                    if( (double)(x)>(double)(0) )
-                    {
-                        r = Double.PositiveInfinity;
-                    }
-                    else
-                    {
-                        r = Double.NegativeInfinity;
-                    }
-                    result = 1;
-                }
-                else
-                {
-                    r = x/y;
-                    result = 0;
-                }
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        This function makes periodic mapping of X to [A,B].
-
-        It accepts X, A, B (A>B). It returns T which lies in  [A,B] and integer K,
-        such that X = T + K*(B-A).
-
-        NOTES:
-        * K is represented as real value, although actually it is integer
-        * T is guaranteed to be in [A,B]
-        * T replaces X
-
-          -- ALGLIB --
-             Copyright by Bochkanov Sergey
-        *************************************************************************/
-        public static void apperiodicmap(ref double x,
-            double a,
-            double b,
-            ref double k)
-        {
-            k = 0;
-
-            ap.assert((double)(a)<(double)(b), "APPeriodicMap: internal error!");
-            k = (int)Math.Floor((x-a)/(b-a));
-            x = x-k*(b-a);
-            while( (double)(x)<(double)(a) )
-            {
-                x = x+(b-a);
-                k = k-1;
-            }
-            while( (double)(x)>(double)(b) )
-            {
-                x = x-(b-a);
-                k = k+1;
-            }
-            x = Math.Max(x, a);
-            x = Math.Min(x, b);
-        }
-
-
-        /*************************************************************************
-        'bounds' value: maps X to [B1,B2]
-
-          -- ALGLIB --
-             Copyright 20.03.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double boundval(double x,
-            double b1,
-            double b2)
-        {
-            double result = 0;
-
-            if( (double)(x)<=(double)(b1) )
-            {
-                result = b1;
-                return result;
-            }
-            if( (double)(x)>=(double)(b2) )
-            {
-                result = b2;
-                return result;
-            }
-            result = x;
-            return result;
         }
 
@@ -4035,4 +4647,29 @@
     public class rotations
     {
+        /*************************************************************************
+        Application of a sequence of  elementary rotations to a matrix
+
+        The algorithm pre-multiplies the matrix by a sequence of rotation
+        transformations which is given by arrays C and S. Depending on the value
+        of the IsForward parameter either 1 and 2, 3 and 4 and so on (if IsForward=true)
+        rows are rotated, or the rows N and N-1, N-2 and N-3 and so on, are rotated.
+
+        Not the whole matrix but only a part of it is transformed (rows from M1 to
+        M2, columns from N1 to N2). Only the elements of this submatrix are changed.
+
+        Input parameters:
+            IsForward   -   the sequence of the rotation application.
+            M1,M2       -   the range of rows to be transformed.
+            N1, N2      -   the range of columns to be transformed.
+            C,S         -   transformation coefficients.
+                            Array whose index ranges within [1..M2-M1].
+            A           -   processed matrix.
+            WORK        -   working array whose index ranges within [N1..N2].
+
+        Output parameters:
+            A           -   transformed matrix.
+
+        Utility subroutine.
+        *************************************************************************/
         public static void applyrotationsfromtheleft(bool isforward,
             int m1,
@@ -4177,4 +4814,29 @@
 
 
+        /*************************************************************************
+        Application of a sequence of  elementary rotations to a matrix
+
+        The algorithm post-multiplies the matrix by a sequence of rotation
+        transformations which is given by arrays C and S. Depending on the value
+        of the IsForward parameter either 1 and 2, 3 and 4 and so on (if IsForward=true)
+        rows are rotated, or the rows N and N-1, N-2 and N-3 and so on are rotated.
+
+        Not the whole matrix but only a part of it is transformed (rows from M1
+        to M2, columns from N1 to N2). Only the elements of this submatrix are changed.
+
+        Input parameters:
+            IsForward   -   the sequence of the rotation application.
+            M1,M2       -   the range of rows to be transformed.
+            N1, N2      -   the range of columns to be transformed.
+            C,S         -   transformation coefficients.
+                            Array whose index ranges within [1..N2-N1].
+            A           -   processed matrix.
+            WORK        -   working array whose index ranges within [M1..M2].
+
+        Output parameters:
+            A           -   transformed matrix.
+
+        Utility subroutine.
+        *************************************************************************/
         public static void applyrotationsfromtheright(bool isforward,
             int m1,
@@ -4315,4 +4977,12 @@
 
 
+        /*************************************************************************
+        The subroutine generates the elementary rotation, so that:
+
+        [  CS  SN  ]  .  [ F ]  =  [ R ]
+        [ -SN  CS  ]     [ G ]     [ 0 ]
+
+        CS**2 + SN**2 = 1
+        *************************************************************************/
         public static void generaterotation(double f,
             double g,
@@ -4370,4 +5040,47 @@
     public class hsschur
     {
+        /*************************************************************************
+        Subroutine performing  the  Schur  decomposition  of  a  matrix  in  upper
+        Hessenberg form using the QR algorithm with multiple shifts.
+
+        The  source matrix  H  is  represented as  S'*H*S = T, where H - matrix in
+        upper Hessenberg form,  S - orthogonal matrix (Schur vectors),   T - upper
+        quasi-triangular matrix (with blocks of sizes  1x1  and  2x2  on  the main
+        diagonal).
+
+        Input parameters:
+            H   -   matrix to be decomposed.
+                    Array whose indexes range within [1..N, 1..N].
+            N   -   size of H, N>=0.
+
+
+        Output parameters:
+            H      contains the matrix T.
+                    Array whose indexes range within [1..N, 1..N].
+                    All elements below the blocks on the main diagonal are equal
+                    to 0.
+            S   -   contains Schur vectors.
+                    Array whose indexes range within [1..N, 1..N].
+
+        Note 1:
+            The block structure of matrix T could be easily recognized: since  all
+            the elements  below  the blocks are zeros, the elements a[i+1,i] which
+            are equal to 0 show the block border.
+
+        Note 2:
+            the algorithm  performance  depends  on  the  value  of  the  internal
+            parameter NS of InternalSchurDecomposition  subroutine  which  defines
+            the number of shifts in the QR algorithm (analog of  the  block  width
+            in block matrix algorithms in linear algebra). If you require  maximum
+            performance  on  your  machine,  it  is  recommended  to  adjust  this
+            parameter manually.
+
+        Result:
+            True, if the algorithm has converged and the parameters H and S contain
+                the result.
+            False, if the algorithm has not converged.
+
+        Algorithm implemented on the basis of subroutine DHSEQR (LAPACK 3.0 library).
+        *************************************************************************/
         public static bool upperhessenbergschurdecomposition(ref double[,] h,
             int n,
@@ -7715,5 +8428,4 @@
         public const double ftol = 0.001;
         public const double xtol = 100*math.machineepsilon;
-        public const double gtol = 0.3;
         public const int maxfev = 20;
         public const double stpmin = 1.0E-50;
@@ -7803,4 +8515,20 @@
         ALGORITHM  USUALLY STOPS  WHEN  ROUNDING ERRORS  PREVENT FURTHER PROGRESS.
         IN THIS CASE STP ONLY SATISFIES THE SUFFICIENT DECREASE CONDITION.
+
+
+        :::::::::::::IMPORTANT NOTES:::::::::::::
+
+        NOTE 1:
+
+        This routine  guarantees that it will stop at the last point where function
+        value was calculated. It won't make several additional function evaluations
+        after finding good point. So if you store function evaluations requested by
+        this routine, you can be sure that last one is the point where we've stopped.
+
+        NOTE 2:
+
+        when 0<StpMax<StpMin, algorithm will terminate with INFO=5 and Stp=0.0
+        :::::::::::::::::::::::::::::::::::::::::
+
 
         PARAMETERS DESCRIPRION
@@ -7871,4 +8599,5 @@
             ref double stp,
             double stpmax,
+            double gtol,
             ref int info,
             ref int nfev,
@@ -7926,4 +8655,10 @@
                     //     CHECK THE INPUT PARAMETERS FOR ERRORS.
                     //
+                    if( (double)(stpmax)<(double)(stpmin) & (double)(stpmax)>(double)(0) )
+                    {
+                        info = 5;
+                        stp = 0.0;
+                        return;
+                    }
                     if( ((((((n<=0 | (double)(stp)<=(double)(0)) | (double)(ftol)<(double)(0)) | (double)(gtol)<(double)(zero)) | (double)(xtol)<(double)(zero)) | (double)(stpmin)<(double)(zero)) | (double)(stpmax)<(double)(stpmin)) | maxfev<=0 )
                     {
@@ -8798,4 +9533,59 @@
                 {
                     stp = Math.Max(stx+0.66*(sty-stx), stp);
+                }
+            }
+        }
+
+
+    }
+    public class optserv
+    {
+        /*************************************************************************
+        This subroutine is used to prepare threshold value which will be used for
+        trimming of the target function (see comments on TrimFunction() for more
+        information).
+
+        This function accepts only one parameter: function value at the starting
+        point. It returns threshold which will be used for trimming.
+
+          -- ALGLIB --
+             Copyright 10.05.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void trimprepare(double f,
+            ref double threshold)
+        {
+            threshold = 0;
+
+            threshold = 10*(Math.Abs(f)+1);
+        }
+
+
+        /*************************************************************************
+        This subroutine is used to "trim" target function, i.e. to do following
+        transformation:
+
+                           { {F,G}          if F<Threshold
+            {F_tr, G_tr} = {
+                           { {Threshold, 0} if F>=Threshold
+                           
+        Such transformation allows us to  solve  problems  with  singularities  by
+        redefining function in such way that it becomes bounded from above.
+
+          -- ALGLIB --
+             Copyright 10.05.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void trimfunction(ref double f,
+            ref double[] g,
+            int n,
+            double threshold)
+        {
+            int i = 0;
+
+            if( (double)(f)>=(double)(threshold) )
+            {
+                f = threshold;
+                for(i=0; i<=n-1; i++)
+                {
+                    g[i] = 0.0;
                 }
             }
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/alglibmisc.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/alglibmisc.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/alglibmisc.cs	(revision 7294)
@@ -214,4 +214,49 @@
             _innerobj = obj;
         }
+    }
+
+
+    /*************************************************************************
+    This function serializes data structure to string.
+
+    Important properties of s_out:
+    * it contains alphanumeric characters, dots, underscores, minus signs
+    * these symbols are grouped into words, which are separated by spaces
+      and Windows-style (CR+LF) newlines
+    * although  serializer  uses  spaces and CR+LF as separators, you can 
+      replace any separator character by arbitrary combination of spaces,
+      tabs, Windows or Unix newlines. It allows flexible reformatting  of
+      the  string  in  case you want to include it into text or XML file. 
+      But you should not insert separators into the middle of the "words"
+      nor you should change case of letters.
+    * s_out can be freely moved between 32-bit and 64-bit systems, little
+      and big endian machines, and so on. You can serialize structure  on
+      32-bit machine and unserialize it on 64-bit one (or vice versa), or
+      serialize  it  on  SPARC  and  unserialize  on  x86.  You  can also 
+      serialize  it  in  C# version of ALGLIB and unserialize in C++ one, 
+      and vice versa.
+    *************************************************************************/
+    public static void kdtreeserialize(kdtree obj, out string s_out)
+    {
+        alglib.serializer s = new alglib.serializer();
+        s.alloc_start();
+        nearestneighbor.kdtreealloc(s, obj.innerobj);
+        s.sstart_str();
+        nearestneighbor.kdtreeserialize(s, obj.innerobj);
+        s.stop();
+        s_out = s.get_string();
+    }
+
+
+    /*************************************************************************
+    This function unserializes data structure from string.
+    *************************************************************************/
+    public static void kdtreeunserialize(string s_in, out kdtree obj)
+    {
+        alglib.serializer s = new alglib.serializer();
+        obj = new kdtree();
+        s.ustart_str(s_in);
+        nearestneighbor.kdtreeunserialize(s, obj.innerobj);
+        s.stop();
     }
 
@@ -961,12 +1006,8 @@
             public int ny;
             public int normtype;
-            public int distmatrixtype;
             public double[,] xy;
             public int[] tags;
             public double[] boxmin;
             public double[] boxmax;
-            public double[] curboxmin;
-            public double[] curboxmax;
-            public double curdist;
             public int[] nodes;
             public double[] splits;
@@ -980,4 +1021,7 @@
             public double[] r;
             public double[] buf;
+            public double[] curboxmin;
+            public double[] curboxmax;
+            public double curdist;
             public int debugcounter;
             public kdtree()
@@ -987,6 +1031,4 @@
                 boxmin = new double[0];
                 boxmax = new double[0];
-                curboxmin = new double[0];
-                curboxmax = new double[0];
                 nodes = new int[0];
                 splits = new double[0];
@@ -995,4 +1037,6 @@
                 r = new double[0];
                 buf = new double[0];
+                curboxmin = new double[0];
+                curboxmax = new double[0];
             }
         };
@@ -1002,4 +1046,5 @@
 
         public const int splitnodesize = 6;
+        public const int kdtreefirstversion = 0;
 
 
@@ -1133,11 +1178,10 @@
             kdt.ny = ny;
             kdt.normtype = normtype;
-            kdt.distmatrixtype = 0;
-            kdt.xy = new double[n, 2*nx+ny];
-            kdt.tags = new int[n];
-            kdt.idx = new int[n];
-            kdt.r = new double[n];
-            kdt.x = new double[nx];
-            kdt.buf = new double[Math.Max(n, nx)];
+            
+            //
+            // Allocate
+            //
+            kdtreeallocdatasetindependent(kdt, nx, ny);
+            kdtreeallocdatasetdependent(kdt, n, nx, ny);
             
             //
@@ -1161,8 +1205,4 @@
             // Determine bounding box
             //
-            kdt.boxmin = new double[nx];
-            kdt.boxmax = new double[nx];
-            kdt.curboxmin = new double[nx];
-            kdt.curboxmax = new double[nx];
             for(i_=0; i_<=nx-1;i_++)
             {
@@ -1753,4 +1793,106 @@
 
             kdtreequeryresultsdistances(kdt, ref r);
+        }
+
+
+        /*************************************************************************
+        Serializer: allocation
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void kdtreealloc(alglib.serializer s,
+            kdtree tree)
+        {
+            
+            //
+            // Header
+            //
+            s.alloc_entry();
+            s.alloc_entry();
+            
+            //
+            // Data
+            //
+            s.alloc_entry();
+            s.alloc_entry();
+            s.alloc_entry();
+            s.alloc_entry();
+            apserv.allocrealmatrix(s, tree.xy, -1, -1);
+            apserv.allocintegerarray(s, tree.tags, -1);
+            apserv.allocrealarray(s, tree.boxmin, -1);
+            apserv.allocrealarray(s, tree.boxmax, -1);
+            apserv.allocintegerarray(s, tree.nodes, -1);
+            apserv.allocrealarray(s, tree.splits, -1);
+        }
+
+
+        /*************************************************************************
+        Serializer: serialization
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void kdtreeserialize(alglib.serializer s,
+            kdtree tree)
+        {
+            
+            //
+            // Header
+            //
+            s.serialize_int(scodes.getkdtreeserializationcode());
+            s.serialize_int(kdtreefirstversion);
+            
+            //
+            // Data
+            //
+            s.serialize_int(tree.n);
+            s.serialize_int(tree.nx);
+            s.serialize_int(tree.ny);
+            s.serialize_int(tree.normtype);
+            apserv.serializerealmatrix(s, tree.xy, -1, -1);
+            apserv.serializeintegerarray(s, tree.tags, -1);
+            apserv.serializerealarray(s, tree.boxmin, -1);
+            apserv.serializerealarray(s, tree.boxmax, -1);
+            apserv.serializeintegerarray(s, tree.nodes, -1);
+            apserv.serializerealarray(s, tree.splits, -1);
+        }
+
+
+        /*************************************************************************
+        Serializer: unserialization
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void kdtreeunserialize(alglib.serializer s,
+            kdtree tree)
+        {
+            int i0 = 0;
+            int i1 = 0;
+
+            
+            //
+            // check correctness of header
+            //
+            i0 = s.unserialize_int();
+            ap.assert(i0==scodes.getkdtreeserializationcode(), "KDTreeUnserialize: stream header corrupted");
+            i1 = s.unserialize_int();
+            ap.assert(i1==kdtreefirstversion, "KDTreeUnserialize: stream header corrupted");
+            
+            //
+            // Unserialize data
+            //
+            tree.n = s.unserialize_int();
+            tree.nx = s.unserialize_int();
+            tree.ny = s.unserialize_int();
+            tree.normtype = s.unserialize_int();
+            apserv.unserializerealmatrix(s, ref tree.xy);
+            apserv.unserializeintegerarray(s, ref tree.tags);
+            apserv.unserializerealarray(s, ref tree.boxmin);
+            apserv.unserializerealarray(s, ref tree.boxmax);
+            apserv.unserializeintegerarray(s, ref tree.nodes);
+            apserv.unserializerealarray(s, ref tree.splits);
+            kdtreealloctemporaries(tree, tree.n, tree.nx, tree.ny);
         }
 
@@ -2405,4 +2547,75 @@
 
 
+        /*************************************************************************
+        This function allocates all dataset-independent array  fields  of  KDTree,
+        i.e.  such  array  fields  that  their dimensions do not depend on dataset
+        size.
+
+        This function do not sets KDT.NX or KDT.NY - it just allocates arrays
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        private static void kdtreeallocdatasetindependent(kdtree kdt,
+            int nx,
+            int ny)
+        {
+            kdt.x = new double[nx];
+            kdt.boxmin = new double[nx];
+            kdt.boxmax = new double[nx];
+            kdt.curboxmin = new double[nx];
+            kdt.curboxmax = new double[nx];
+        }
+
+
+        /*************************************************************************
+        This function allocates all dataset-dependent array fields of KDTree, i.e.
+        such array fields that their dimensions depend on dataset size.
+
+        This function do not sets KDT.N, KDT.NX or KDT.NY -
+        it just allocates arrays.
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        private static void kdtreeallocdatasetdependent(kdtree kdt,
+            int n,
+            int nx,
+            int ny)
+        {
+            kdt.xy = new double[n, 2*nx+ny];
+            kdt.tags = new int[n];
+            kdt.idx = new int[n];
+            kdt.r = new double[n];
+            kdt.x = new double[nx];
+            kdt.buf = new double[Math.Max(n, nx)];
+            kdt.nodes = new int[splitnodesize*2*n];
+            kdt.splits = new double[2*n];
+        }
+
+
+        /*************************************************************************
+        This function allocates temporaries.
+
+        This function do not sets KDT.N, KDT.NX or KDT.NY -
+        it just allocates arrays.
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        private static void kdtreealloctemporaries(kdtree kdt,
+            int n,
+            int nx,
+            int ny)
+        {
+            kdt.x = new double[nx];
+            kdt.idx = new int[n];
+            kdt.r = new double[n];
+            kdt.buf = new double[Math.Max(n, nx)];
+            kdt.curboxmin = new double[nx];
+            kdt.curboxmax = new double[nx];
+        }
+
+
     }
 }
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ap.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/ap.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ap.cs	(revision 7294)
@@ -3,5 +3,5 @@
 Copyright (c) 2003-2009 Sergey Bochkanov (ALGLIB project).
 
->>> LICENSE >>>
+>>> SOURCE LICENSE >>>
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -16,5 +16,4 @@
 A copy of the GNU General Public License is available at
 http://www.fsf.org/licensing/licenses
-
 >>> END OF LICENSE >>>
 *************************************************************************/
@@ -258,4 +257,18 @@
 
         /****************************************************************
+        prints formatted complex
+        ****************************************************************/
+        public static string format(complex a, int _dps)
+        {
+            int dps = Math.Abs(_dps);
+            string fmt = _dps>=0 ? "F" : "E";
+            string fmtx = String.Format("{{0:"+fmt+"{0}}}", dps);
+            string fmty = String.Format("{{0:"+fmt+"{0}}}", dps);
+            string result = String.Format(fmtx, a.x) + (a.y >= 0 ? "+" : "-") + String.Format(fmty, Math.Abs(a.y)) + "i";
+            result = result.Replace(',', '.');
+            return result;
+        }
+
+        /****************************************************************
         prints formatted array
         ****************************************************************/
@@ -287,7 +300,9 @@
         prints formatted array
         ****************************************************************/
-        public static string format(double[] a, int dps)
-        {
-            string fmt = String.Format("{{0:F{0}}}", dps);
+        public static string format(double[] a, int _dps)
+        {
+            int dps = Math.Abs(_dps);
+            string sfmt = _dps >= 0 ? "F" : "E";
+            string fmt = String.Format("{{0:" + sfmt + "{0}}}", dps);
             string[] result = new string[len(a)];
             int i;
@@ -303,8 +318,10 @@
         prints formatted array
         ****************************************************************/
-        public static string format(complex[] a, int dps)
-        {
-            string fmtx = String.Format("{{0:F{0}}}", dps);
-            string fmty = String.Format("{{0:F{0}}}", dps);
+        public static string format(complex[] a, int _dps)
+        {
+            int dps = Math.Abs(_dps);
+            string fmt = _dps >= 0 ? "F" : "E";
+            string fmtx = String.Format("{{0:"+fmt+"{0}}}", dps);
+            string fmty = String.Format("{{0:"+fmt+"{0}}}", dps);
             string[] result = new string[len(a)];
             int i;
@@ -586,3 +603,622 @@
 
     }
-}
+    
+    /********************************************************************
+    serializer object (should not be used directly)
+    ********************************************************************/
+    public class serializer
+    {
+        enum SMODE { DEFAULT, ALLOC, TO_STRING, FROM_STRING };
+        private const int SER_ENTRIES_PER_ROW = 5;
+        private const int SER_ENTRY_LENGTH    = 11;
+        
+        private SMODE mode;
+        private int entries_needed;
+        private int entries_saved;
+        private int bytes_asked;
+        private int bytes_written;
+        private int bytes_read;
+        private char[] out_str;
+        private char[] in_str;
+        
+        public serializer()
+        {
+            mode = SMODE.DEFAULT;
+            entries_needed = 0;
+            bytes_asked = 0;
+        }
+
+        public void alloc_start()
+        {
+            entries_needed = 0;
+            bytes_asked = 0;
+            mode = SMODE.ALLOC;
+        }
+
+        public void alloc_entry()
+        {
+            if( mode!=SMODE.ALLOC )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            entries_needed++;
+        }
+
+        private int get_alloc_size()
+        {
+            int rows, lastrowsize, result;
+            
+            // check and change mode
+            if( mode!=SMODE.ALLOC )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            
+            // if no entries needes (degenerate case)
+            if( entries_needed==0 )
+            {
+                bytes_asked = 1;
+                return bytes_asked;
+            }
+            
+            // non-degenerate case
+            rows = entries_needed/SER_ENTRIES_PER_ROW;
+            lastrowsize = SER_ENTRIES_PER_ROW;
+            if( entries_needed%SER_ENTRIES_PER_ROW!=0 )
+            {
+                lastrowsize = entries_needed%SER_ENTRIES_PER_ROW;
+                rows++;
+            }
+            
+            // calculate result size
+            result  = ((rows-1)*SER_ENTRIES_PER_ROW+lastrowsize)*SER_ENTRY_LENGTH;
+            result +=  (rows-1)*(SER_ENTRIES_PER_ROW-1)+(lastrowsize-1);
+            result += rows*2;
+            bytes_asked = result;
+            return result;
+        }
+
+        public void sstart_str()
+        {
+            int allocsize = get_alloc_size();
+            
+            // check and change mode
+            if( mode!=SMODE.ALLOC )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            mode = SMODE.TO_STRING;
+            
+            // other preparations
+            out_str = new char[allocsize];
+            entries_saved = 0;
+            bytes_written = 0;
+        }
+
+        public void ustart_str(string s)
+        {
+            // check and change mode
+            if( mode!=SMODE.DEFAULT )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            mode = SMODE.FROM_STRING;
+            
+            in_str = s.ToCharArray();
+            bytes_read = 0;
+        }
+
+        public void serialize_bool(bool v)
+        {
+            if( mode!=SMODE.TO_STRING )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            bool2str(v, out_str, ref bytes_written);
+            entries_saved++;
+            if( entries_saved%SER_ENTRIES_PER_ROW!=0 )
+            {
+                out_str[bytes_written] = ' ';
+                bytes_written++;
+            }
+            else
+            {
+                out_str[bytes_written+0] = '\r';
+                out_str[bytes_written+1] = '\n';
+                bytes_written+=2;
+            }            
+        }
+
+        public void serialize_int(int v)
+        {
+            if( mode!=SMODE.TO_STRING )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            int2str(v, out_str, ref bytes_written);
+            entries_saved++;
+            if( entries_saved%SER_ENTRIES_PER_ROW!=0 )
+            {
+                out_str[bytes_written] = ' ';
+                bytes_written++;
+            }
+            else
+            {
+                out_str[bytes_written+0] = '\r';
+                out_str[bytes_written+1] = '\n';
+                bytes_written+=2;
+            }
+        }
+
+        public void serialize_double(double v)
+        {
+            if( mode!=SMODE.TO_STRING )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            double2str(v, out_str, ref bytes_written);
+            entries_saved++;
+            if( entries_saved%SER_ENTRIES_PER_ROW!=0 )
+            {
+                out_str[bytes_written] = ' ';
+                bytes_written++;
+            }
+            else
+            {
+                out_str[bytes_written+0] = '\r';
+                out_str[bytes_written+1] = '\n';
+                bytes_written+=2;
+            }
+        }
+
+        public bool unserialize_bool()
+        {
+            if( mode!=SMODE.FROM_STRING )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            return str2bool(in_str, ref bytes_read);
+        }
+
+        public int unserialize_int()
+        {
+            if( mode!=SMODE.FROM_STRING )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            return str2int(in_str, ref bytes_read);
+        }
+
+        public double unserialize_double()
+        {
+            if( mode!=SMODE.FROM_STRING )
+                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
+            return str2double(in_str, ref bytes_read);
+        }
+
+        public void stop()
+        {
+        }
+
+        public string get_string()
+        {
+            return new string(out_str, 0, bytes_written);
+        }
+
+
+        /************************************************************************
+        This function converts six-bit value (from 0 to 63)  to  character  (only
+        digits, lowercase and uppercase letters, minus and underscore are used).
+
+        If v is negative or greater than 63, this function returns '?'.
+        ************************************************************************/
+        private static char[] _sixbits2char_tbl = new char[64]{ 
+                '0', '1', '2', '3', '4', '5', '6', '7',
+                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
+                'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
+                'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
+                'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 
+                'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
+                'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
+                'u', 'v', 'w', 'x', 'y', 'z', '-', '_' };
+        private static char sixbits2char(int v)
+        {
+            if( v<0 || v>63 )
+                return '?';
+            return _sixbits2char_tbl[v];
+        }
+        
+        /************************************************************************
+        This function converts character to six-bit value (from 0 to 63).
+
+        This function is inverse of ae_sixbits2char()
+        If c is not correct character, this function returns -1.
+        ************************************************************************/
+        private static int[] _char2sixbits_tbl = new int[128] {
+            -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, 62, -1, -1,
+             0,  1,  2,  3,  4,  5,  6,  7,
+             8,  9, -1, -1, -1, -1, -1, -1,
+            -1, 10, 11, 12, 13, 14, 15, 16,
+            17, 18, 19, 20, 21, 22, 23, 24,
+            25, 26, 27, 28, 29, 30, 31, 32,
+            33, 34, 35, -1, -1, -1, -1, 63,
+            -1, 36, 37, 38, 39, 40, 41, 42,
+            43, 44, 45, 46, 47, 48, 49, 50,
+            51, 52, 53, 54, 55, 56, 57, 58,
+            59, 60, 61, -1, -1, -1, -1, -1 };
+        private static int char2sixbits(char c)
+        {
+            return (c>=0 && c<127) ? _char2sixbits_tbl[c] : -1;
+        }
+        
+        /************************************************************************
+        This function converts three bytes (24 bits) to four six-bit values 
+        (24 bits again).
+
+        src         array
+        src_offs    offset of three-bytes chunk
+        dst         array for ints
+        dst_offs    offset of four-ints chunk
+        ************************************************************************/
+        private static void threebytes2foursixbits(byte[] src, int src_offs, int[] dst, int dst_offs)
+        {
+            dst[dst_offs+0] =  src[src_offs+0] & 0x3F;
+            dst[dst_offs+1] = (src[src_offs+0]>>6) | ((src[src_offs+1]&0x0F)<<2);
+            dst[dst_offs+2] = (src[src_offs+1]>>4) | ((src[src_offs+2]&0x03)<<4);
+            dst[dst_offs+3] =  src[src_offs+2]>>2;
+        }
+
+        /************************************************************************
+        This function converts four six-bit values (24 bits) to three bytes
+        (24 bits again).
+
+        src         pointer to four ints
+        src_offs    offset of the chunk
+        dst         pointer to three bytes
+        dst_offs    offset of the chunk
+        ************************************************************************/
+        private static void foursixbits2threebytes(int[] src, int src_offs, byte[] dst, int dst_offs)
+        {
+            dst[dst_offs+0] =      (byte)(src[src_offs+0] | ((src[src_offs+1]&0x03)<<6));
+            dst[dst_offs+1] = (byte)((src[src_offs+1]>>2) | ((src[src_offs+2]&0x0F)<<4));
+            dst[dst_offs+2] = (byte)((src[src_offs+2]>>4) |  (src[src_offs+3]<<2));
+        }
+
+        /************************************************************************
+        This function serializes boolean value into buffer
+
+        v           boolean value to be serialized
+        buf         buffer, at least 11 characters wide
+        offs        offset in the buffer
+        
+        after return from this function, offs points to the char's past the value
+        being read.
+        ************************************************************************/
+        private static void bool2str(bool v, char[] buf, ref int offs)
+        {
+            char c = v ? '1' : '0';
+            int i;
+            for(i=0; i<SER_ENTRY_LENGTH; i++)
+                buf[offs+i] = c;
+            offs += SER_ENTRY_LENGTH;
+        }
+
+        /************************************************************************
+        This function unserializes boolean value from buffer
+
+        buf         buffer which contains value; leading spaces/tabs/newlines are 
+                    ignored, traling spaces/tabs/newlines are treated as  end  of
+                    the boolean value.
+        offs        offset in the buffer
+        
+        after return from this function, offs points to the char's past the value
+        being read.
+
+        This function raises an error in case unexpected symbol is found
+        ************************************************************************/
+        private static bool str2bool(char[] buf, ref int offs)
+        {
+            bool was0, was1;
+            string emsg = "ALGLIB: unable to read boolean value from stream";
+            
+            was0 = false;
+            was1 = false;
+            while( buf[offs]==' ' || buf[offs]=='\t' || buf[offs]=='\n' || buf[offs]=='\r' )
+                offs++;
+            while( buf[offs]!=' ' && buf[offs]!='\t' && buf[offs]!='\n' && buf[offs]!='\r' && buf[offs]!=0 )
+            {
+                if( buf[offs]=='0' )
+                {
+                    was0 = true;
+                    offs++;
+                    continue;
+                }
+                if( buf[offs]=='1' )
+                {
+                    was1 = true;
+                    offs++;
+                    continue;
+                }
+                throw new alglib.alglibexception(emsg);
+            }
+            if( (!was0) && (!was1) )
+                throw new alglib.alglibexception(emsg);
+            if( was0 && was1 )
+                throw new alglib.alglibexception(emsg);
+            return was1 ? true : false;
+        }
+
+        /************************************************************************
+        This function serializes integer value into buffer
+
+        v           integer value to be serialized
+        buf         buffer, at least 11 characters wide 
+        offs        offset in the buffer
+        
+        after return from this function, offs points to the char's past the value
+        being read.
+
+        This function raises an error in case unexpected symbol is found
+        ************************************************************************/
+        private static void int2str(int v, char[] buf, ref int offs)
+        {
+            int i;
+            byte[] _bytes = System.BitConverter.GetBytes((int)v);
+            byte[]  bytes = new byte[9];
+            int[] sixbits = new int[12];
+            byte c;
+            
+            //
+            // copy v to array of bytes, sign extending it and 
+            // converting to little endian order. Additionally, 
+            // we set 9th byte to zero in order to simplify 
+            // conversion to six-bit representation
+            //
+            if( !System.BitConverter.IsLittleEndian )
+                System.Array.Reverse(_bytes);
+            c = v<0 ? (byte)0xFF : (byte)0x00;
+            for(i=0; i<sizeof(int); i++)
+                bytes[i] = _bytes[i];
+            for(i=sizeof(int); i<8; i++)
+                bytes[i] = c;
+            bytes[8] = 0;
+            
+            //
+            // convert to six-bit representation, output
+            //
+            // NOTE: last 12th element of sixbits is always zero, we do not output it
+            //
+            threebytes2foursixbits(bytes, 0, sixbits, 0);
+            threebytes2foursixbits(bytes, 3, sixbits, 4);
+            threebytes2foursixbits(bytes, 6, sixbits, 8);        
+            for(i=0; i<SER_ENTRY_LENGTH; i++)
+                buf[offs+i] = sixbits2char(sixbits[i]);
+            offs += SER_ENTRY_LENGTH;
+        }
+
+        /************************************************************************
+        This function unserializes integer value from string
+
+        buf         buffer which contains value; leading spaces/tabs/newlines are 
+                    ignored, traling spaces/tabs/newlines are treated as  end  of
+                    the integer value.
+        offs        offset in the buffer
+        
+        after return from this function, offs points to the char's past the value
+        being read.
+
+        This function raises an error in case unexpected symbol is found
+        ************************************************************************/
+        private static int str2int(char[] buf, ref int offs)
+        {
+            string emsg =       "ALGLIB: unable to read integer value from stream";
+            string emsg3264 =   "ALGLIB: unable to read integer value from stream (value does not fit into 32 bits)";
+            int[] sixbits = new int[12];
+            byte[] bytes = new byte[9];
+            byte[] _bytes = new byte[sizeof(int)];
+            int sixbitsread, i;
+            byte c;
+            
+            // 
+            // 1. skip leading spaces
+            // 2. read and decode six-bit digits
+            // 3. set trailing digits to zeros
+            // 4. convert to little endian 64-bit integer representation
+            // 5. check that we fit into int
+            // 6. convert to big endian representation, if needed
+            //
+            sixbitsread = 0;
+            while( buf[offs]==' ' || buf[offs]=='\t' || buf[offs]=='\n' || buf[offs]=='\r' )
+                offs++;
+            while( buf[offs]!=' ' && buf[offs]!='\t' && buf[offs]!='\n' && buf[offs]!='\r' && buf[offs]!=0 )
+            {
+                int d;
+                d = char2sixbits(buf[offs]);
+                if( d<0 || sixbitsread>=SER_ENTRY_LENGTH )
+                    throw new alglib.alglibexception(emsg);
+                sixbits[sixbitsread] = d;
+                sixbitsread++;
+                offs++;
+            }
+            if( sixbitsread==0 )
+                throw new alglib.alglibexception(emsg);
+            for(i=sixbitsread; i<12; i++)
+                sixbits[i] = 0;
+            foursixbits2threebytes(sixbits, 0, bytes, 0);
+            foursixbits2threebytes(sixbits, 4, bytes, 3);
+            foursixbits2threebytes(sixbits, 8, bytes, 6);
+            c = (bytes[sizeof(int)-1] & 0x80)!=0 ? (byte)0xFF : (byte)0x00;
+            for(i=sizeof(int); i<8; i++)
+                if( bytes[i]!=c )
+                    throw new alglib.alglibexception(emsg3264);
+            for(i=0; i<sizeof(int); i++)
+                _bytes[i] = bytes[i];        
+            if( !System.BitConverter.IsLittleEndian )
+                System.Array.Reverse(_bytes);
+            return System.BitConverter.ToInt32(_bytes,0);
+        }    
+        
+        
+        /************************************************************************
+        This function serializes double value into buffer
+
+        v           double value to be serialized
+        buf         buffer, at least 11 characters wide 
+        offs        offset in the buffer
+        
+        after return from this function, offs points to the char's past the value
+        being read.
+        ************************************************************************/
+        private static void double2str(double v, char[] buf, ref int offs)
+        {
+            int i;
+            int[] sixbits = new int[12];
+            byte[] bytes = new byte[9];
+
+            //
+            // handle special quantities
+            //
+            if( System.Double.IsNaN(v) )
+            {
+                buf[offs+0] = '.';
+                buf[offs+1] = 'n';
+                buf[offs+2] = 'a';
+                buf[offs+3] = 'n';
+                buf[offs+4] = '_';
+                buf[offs+5] = '_';
+                buf[offs+6] = '_';
+                buf[offs+7] = '_';
+                buf[offs+8] = '_';
+                buf[offs+9] = '_';
+                buf[offs+10] = '_';
+                offs += SER_ENTRY_LENGTH;
+                return;
+            }
+            if( System.Double.IsPositiveInfinity(v) )
+            {
+                buf[offs+0] = '.';
+                buf[offs+1] = 'p';
+                buf[offs+2] = 'o';
+                buf[offs+3] = 's';
+                buf[offs+4] = 'i';
+                buf[offs+5] = 'n';
+                buf[offs+6] = 'f';
+                buf[offs+7] = '_';
+                buf[offs+8] = '_';
+                buf[offs+9] = '_';
+                buf[offs+10] = '_';
+                offs += SER_ENTRY_LENGTH;
+                return;
+            }
+            if( System.Double.IsNegativeInfinity(v) )
+            {
+                buf[offs+0] = '.';
+                buf[offs+1] = 'n';
+                buf[offs+2] = 'e';
+                buf[offs+3] = 'g';
+                buf[offs+4] = 'i';
+                buf[offs+5] = 'n';
+                buf[offs+6] = 'f';
+                buf[offs+7] = '_';
+                buf[offs+8] = '_';
+                buf[offs+9] = '_';
+                buf[offs+10] = '_';
+                offs += SER_ENTRY_LENGTH;
+                return;
+            }
+            
+            //
+            // process general case:
+            // 1. copy v to array of chars
+            // 2. set 9th byte to zero in order to simplify conversion to six-bit representation
+            // 3. convert to little endian (if needed)
+            // 4. convert to six-bit representation
+            //    (last 12th element of sixbits is always zero, we do not output it)
+            //
+            byte[] _bytes = System.BitConverter.GetBytes((double)v);
+            if( !System.BitConverter.IsLittleEndian )
+                System.Array.Reverse(_bytes);
+            for(i=0; i<sizeof(double); i++)
+                bytes[i] = _bytes[i];
+            for(i=sizeof(double); i<9; i++)
+                bytes[i] = 0;
+            threebytes2foursixbits(bytes, 0, sixbits, 0);
+            threebytes2foursixbits(bytes, 3, sixbits, 4);
+            threebytes2foursixbits(bytes, 6, sixbits, 8);
+            for(i=0; i<SER_ENTRY_LENGTH; i++)
+                buf[offs+i] = sixbits2char(sixbits[i]);
+            offs += SER_ENTRY_LENGTH;
+        }
+
+        /************************************************************************
+        This function unserializes double value from string
+
+        buf         buffer which contains value; leading spaces/tabs/newlines are 
+                    ignored, traling spaces/tabs/newlines are treated as  end  of
+                    the double value.
+        offs        offset in the buffer
+        
+        after return from this function, offs points to the char's past the value
+        being read.
+
+        This function raises an error in case unexpected symbol is found
+        ************************************************************************/
+        private static double str2double(char[] buf, ref int offs)
+        {
+            string emsg = "ALGLIB: unable to read double value from stream";
+            int[] sixbits = new int[12];
+            byte[]  bytes = new byte[9];
+            byte[] _bytes = new byte[sizeof(double)];
+            int sixbitsread, i;
+            
+            
+            // 
+            // skip leading spaces
+            //
+            while( buf[offs]==' ' || buf[offs]=='\t' || buf[offs]=='\n' || buf[offs]=='\r' )
+                offs++;
+            
+              
+            //
+            // Handle special cases
+            //
+            if( buf[offs]=='.' )
+            {
+                string s = new string(buf, offs, SER_ENTRY_LENGTH);
+                if( s==".nan_______" )
+                {
+                    offs += SER_ENTRY_LENGTH;
+                    return System.Double.NaN;
+                }
+                if( s==".posinf____" )
+                {
+                    offs += SER_ENTRY_LENGTH;
+                    return System.Double.PositiveInfinity;
+                }
+                if( s==".neginf____" )
+                {
+                    offs += SER_ENTRY_LENGTH;
+                    return System.Double.NegativeInfinity;
+                }
+                throw new alglib.alglibexception(emsg);
+            }
+            
+            // 
+            // General case:
+            // 1. read and decode six-bit digits
+            // 2. check that all 11 digits were read
+            // 3. set last 12th digit to zero (needed for simplicity of conversion)
+            // 4. convert to 8 bytes
+            // 5. convert to big endian representation, if needed
+            //
+            sixbitsread = 0;
+            while( buf[offs]!=' ' && buf[offs]!='\t' && buf[offs]!='\n' && buf[offs]!='\r' && buf[offs]!=0 )
+            {
+                int d;
+                d = char2sixbits(buf[offs]);
+                if( d<0 || sixbitsread>=SER_ENTRY_LENGTH )
+                    throw new alglib.alglibexception(emsg);
+                sixbits[sixbitsread] = d;
+                sixbitsread++;
+                offs++;
+            }
+            if( sixbitsread!=SER_ENTRY_LENGTH )
+                throw new alglib.alglibexception(emsg);
+            sixbits[SER_ENTRY_LENGTH] = 0;
+            foursixbits2threebytes(sixbits, 0, bytes, 0);
+            foursixbits2threebytes(sixbits, 4, bytes, 3);
+            foursixbits2threebytes(sixbits, 8, bytes, 6);
+            for(i=0; i<sizeof(double); i++)
+                _bytes[i] = bytes[i];        
+            if( !System.BitConverter.IsLittleEndian )
+                System.Array.Reverse(_bytes);        
+            return System.BitConverter.ToDouble(_bytes,0);
+        }
+    }}
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/dataanalysis.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/dataanalysis.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/dataanalysis.cs	(revision 7294)
@@ -21,4 +21,83 @@
 using System;
 
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Optimal binary classification
+
+    Algorithms finds optimal (=with minimal cross-entropy) binary partition.
+    Internal subroutine.
+
+    INPUT PARAMETERS:
+        A       -   array[0..N-1], variable
+        C       -   array[0..N-1], class numbers (0 or 1).
+        N       -   array size
+
+    OUTPUT PARAMETERS:
+        Info    -   completetion code:
+                    * -3, all values of A[] are same (partition is impossible)
+                    * -2, one of C[] is incorrect (<0, >1)
+                    * -1, incorrect pararemets were passed (N<=0).
+                    *  1, OK
+        Threshold-  partiton boundary. Left part contains values which are
+                    strictly less than Threshold. Right part contains values
+                    which are greater than or equal to Threshold.
+        PAL, PBL-   probabilities P(0|v<Threshold) and P(1|v<Threshold)
+        PAR, PBR-   probabilities P(0|v>=Threshold) and P(1|v>=Threshold)
+        CVE     -   cross-validation estimate of cross-entropy
+
+      -- ALGLIB --
+         Copyright 22.05.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void dsoptimalsplit2(double[] a, int[] c, int n, out int info, out double threshold, out double pal, out double pbl, out double par, out double pbr, out double cve)
+    {
+        info = 0;
+        threshold = 0;
+        pal = 0;
+        pbl = 0;
+        par = 0;
+        pbr = 0;
+        cve = 0;
+        bdss.dsoptimalsplit2(a, c, n, ref info, ref threshold, ref pal, ref pbl, ref par, ref pbr, ref cve);
+        return;
+    }
+
+    /*************************************************************************
+    Optimal partition, internal subroutine. Fast version.
+
+    Accepts:
+        A       array[0..N-1]       array of attributes     array[0..N-1]
+        C       array[0..N-1]       array of class labels
+        TiesBuf array[0..N]         temporaries (ties)
+        CntBuf  array[0..2*NC-1]    temporaries (counts)
+        Alpha                       centering factor (0<=alpha<=1, recommended value - 0.05)
+        BufR    array[0..N-1]       temporaries
+        BufI    array[0..N-1]       temporaries
+
+    Output:
+        Info    error code (">0"=OK, "<0"=bad)
+        RMS     training set RMS error
+        CVRMS   leave-one-out RMS error
+
+    Note:
+        content of all arrays is changed by subroutine;
+        it doesn't allocate temporaries.
+
+      -- ALGLIB --
+         Copyright 11.12.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void dsoptimalsplit2fast(ref double[] a, ref int[] c, ref int[] tiesbuf, ref int[] cntbuf, ref double[] bufr, ref int[] bufi, int n, int nc, double alpha, out int info, out double threshold, out double rms, out double cvrms)
+    {
+        info = 0;
+        threshold = 0;
+        rms = 0;
+        cvrms = 0;
+        bdss.dsoptimalsplit2fast(ref a, ref c, ref tiesbuf, ref cntbuf, ref bufr, ref bufi, n, nc, alpha, ref info, ref threshold, ref rms, ref cvrms);
+        return;
+    }
+
+}
 public partial class alglib
 {
@@ -88,4 +167,49 @@
     }
 
+
+    /*************************************************************************
+    This function serializes data structure to string.
+
+    Important properties of s_out:
+    * it contains alphanumeric characters, dots, underscores, minus signs
+    * these symbols are grouped into words, which are separated by spaces
+      and Windows-style (CR+LF) newlines
+    * although  serializer  uses  spaces and CR+LF as separators, you can 
+      replace any separator character by arbitrary combination of spaces,
+      tabs, Windows or Unix newlines. It allows flexible reformatting  of
+      the  string  in  case you want to include it into text or XML file. 
+      But you should not insert separators into the middle of the "words"
+      nor you should change case of letters.
+    * s_out can be freely moved between 32-bit and 64-bit systems, little
+      and big endian machines, and so on. You can serialize structure  on
+      32-bit machine and unserialize it on 64-bit one (or vice versa), or
+      serialize  it  on  SPARC  and  unserialize  on  x86.  You  can also 
+      serialize  it  in  C# version of ALGLIB and unserialize in C++ one, 
+      and vice versa.
+    *************************************************************************/
+    public static void dfserialize(decisionforest obj, out string s_out)
+    {
+        alglib.serializer s = new alglib.serializer();
+        s.alloc_start();
+        dforest.dfalloc(s, obj.innerobj);
+        s.sstart_str();
+        dforest.dfserialize(s, obj.innerobj);
+        s.stop();
+        s_out = s.get_string();
+    }
+
+
+    /*************************************************************************
+    This function unserializes data structure from string.
+    *************************************************************************/
+    public static void dfunserialize(string s_in, out decisionforest obj)
+    {
+        alglib.serializer s = new alglib.serializer();
+        obj = new decisionforest();
+        s.ustart_str(s_in);
+        dforest.dfunserialize(s, obj.innerobj);
+        s.stop();
+    }
+
     /*************************************************************************
     This subroutine builds random decision forest.
@@ -131,4 +255,49 @@
 
     /*************************************************************************
+    This subroutine builds random decision forest.
+    This function gives ability to tune number of variables used when choosing
+    best split.
+
+    INPUT PARAMETERS:
+        XY          -   training set
+        NPoints     -   training set size, NPoints>=1
+        NVars       -   number of independent variables, NVars>=1
+        NClasses    -   task type:
+                        * NClasses=1 - regression task with one
+                                       dependent variable
+                        * NClasses>1 - classification task with
+                                       NClasses classes.
+        NTrees      -   number of trees in a forest, NTrees>=1.
+                        recommended values: 50-100.
+        NRndVars    -   number of variables used when choosing best split
+        R           -   percent of a training set used to build
+                        individual trees. 0<R<=1.
+                        recommended values: 0.1 <= R <= 0.66.
+
+    OUTPUT PARAMETERS:
+        Info        -   return code:
+                        * -2, if there is a point with class number
+                              outside of [0..NClasses-1].
+                        * -1, if incorrect parameters was passed
+                              (NPoints<1, NVars<1, NClasses<1, NTrees<1, R<=0
+                              or R>1).
+                        *  1, if task has been solved
+        DF          -   model built
+        Rep         -   training report, contains error on a training set
+                        and out-of-bag estimates of generalization error.
+
+      -- ALGLIB --
+         Copyright 19.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void dfbuildrandomdecisionforestx1(double[,] xy, int npoints, int nvars, int nclasses, int ntrees, int nrndvars, double r, out int info, out decisionforest df, out dfreport rep)
+    {
+        info = 0;
+        df = new decisionforest();
+        rep = new dfreport();
+        dforest.dfbuildrandomdecisionforestx1(xy, npoints, nvars, nclasses, ntrees, nrndvars, r, ref info, df.innerobj, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
     Procesing
 
@@ -291,83 +460,4 @@
 
     /*************************************************************************
-    Optimal binary classification
-
-    Algorithms finds optimal (=with minimal cross-entropy) binary partition.
-    Internal subroutine.
-
-    INPUT PARAMETERS:
-        A       -   array[0..N-1], variable
-        C       -   array[0..N-1], class numbers (0 or 1).
-        N       -   array size
-
-    OUTPUT PARAMETERS:
-        Info    -   completetion code:
-                    * -3, all values of A[] are same (partition is impossible)
-                    * -2, one of C[] is incorrect (<0, >1)
-                    * -1, incorrect pararemets were passed (N<=0).
-                    *  1, OK
-        Threshold-  partiton boundary. Left part contains values which are
-                    strictly less than Threshold. Right part contains values
-                    which are greater than or equal to Threshold.
-        PAL, PBL-   probabilities P(0|v<Threshold) and P(1|v<Threshold)
-        PAR, PBR-   probabilities P(0|v>=Threshold) and P(1|v>=Threshold)
-        CVE     -   cross-validation estimate of cross-entropy
-
-      -- ALGLIB --
-         Copyright 22.05.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void dsoptimalsplit2(double[] a, int[] c, int n, out int info, out double threshold, out double pal, out double pbl, out double par, out double pbr, out double cve)
-    {
-        info = 0;
-        threshold = 0;
-        pal = 0;
-        pbl = 0;
-        par = 0;
-        pbr = 0;
-        cve = 0;
-        bdss.dsoptimalsplit2(a, c, n, ref info, ref threshold, ref pal, ref pbl, ref par, ref pbr, ref cve);
-        return;
-    }
-
-    /*************************************************************************
-    Optimal partition, internal subroutine. Fast version.
-
-    Accepts:
-        A       array[0..N-1]       array of attributes     array[0..N-1]
-        C       array[0..N-1]       array of class labels
-        TiesBuf array[0..N]         temporaries (ties)
-        CntBuf  array[0..2*NC-1]    temporaries (counts)
-        Alpha                       centering factor (0<=alpha<=1, recommended value - 0.05)
-        BufR    array[0..N-1]       temporaries
-        BufI    array[0..N-1]       temporaries
-
-    Output:
-        Info    error code (">0"=OK, "<0"=bad)
-        RMS     training set RMS error
-        CVRMS   leave-one-out RMS error
-
-    Note:
-        content of all arrays is changed by subroutine;
-        it doesn't allocate temporaries.
-
-      -- ALGLIB --
-         Copyright 11.12.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void dsoptimalsplit2fast(ref double[] a, ref int[] c, ref int[] tiesbuf, ref int[] cntbuf, ref double[] bufr, ref int[] bufi, int n, int nc, double alpha, out int info, out double threshold, out double rms, out double cvrms)
-    {
-        info = 0;
-        threshold = 0;
-        rms = 0;
-        cvrms = 0;
-        bdss.dsoptimalsplit2fast(ref a, ref c, ref tiesbuf, ref cntbuf, ref bufr, ref bufi, n, nc, alpha, ref info, ref threshold, ref rms, ref cvrms);
-        return;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
     k-means++ clusterization
 
@@ -387,6 +477,5 @@
         C           -   array[0..NVars-1,0..K-1].matrix whose columns store
                         cluster's centers
-        XYC         -   array which contains number of clusters dataset points
-                        belong to.
+        XYC         -   array[NPoints], which contains cluster indexes
 
       -- ALGLIB --
@@ -701,4 +790,5 @@
     OUTPUT PARAMETERS:
         V           -   coefficients, array[0..NVars]
+                        constant term (intercept) is stored in the V[NVars].
         NVars       -   number of independent variables (one less than number
                         of coefficients)
@@ -817,4 +907,938 @@
         double result = linreg.lravgrelerror(lm.innerobj, xy, npoints);
         return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+
+    *************************************************************************/
+    public class multilayerperceptron
+    {
+        //
+        // Public declarations
+        //
+
+        public multilayerperceptron()
+        {
+            _innerobj = new mlpbase.multilayerperceptron();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private mlpbase.multilayerperceptron _innerobj;
+        public mlpbase.multilayerperceptron innerobj { get { return _innerobj; } }
+        public multilayerperceptron(mlpbase.multilayerperceptron obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    This function serializes data structure to string.
+
+    Important properties of s_out:
+    * it contains alphanumeric characters, dots, underscores, minus signs
+    * these symbols are grouped into words, which are separated by spaces
+      and Windows-style (CR+LF) newlines
+    * although  serializer  uses  spaces and CR+LF as separators, you can 
+      replace any separator character by arbitrary combination of spaces,
+      tabs, Windows or Unix newlines. It allows flexible reformatting  of
+      the  string  in  case you want to include it into text or XML file. 
+      But you should not insert separators into the middle of the "words"
+      nor you should change case of letters.
+    * s_out can be freely moved between 32-bit and 64-bit systems, little
+      and big endian machines, and so on. You can serialize structure  on
+      32-bit machine and unserialize it on 64-bit one (or vice versa), or
+      serialize  it  on  SPARC  and  unserialize  on  x86.  You  can also 
+      serialize  it  in  C# version of ALGLIB and unserialize in C++ one, 
+      and vice versa.
+    *************************************************************************/
+    public static void mlpserialize(multilayerperceptron obj, out string s_out)
+    {
+        alglib.serializer s = new alglib.serializer();
+        s.alloc_start();
+        mlpbase.mlpalloc(s, obj.innerobj);
+        s.sstart_str();
+        mlpbase.mlpserialize(s, obj.innerobj);
+        s.stop();
+        s_out = s.get_string();
+    }
+
+
+    /*************************************************************************
+    This function unserializes data structure from string.
+    *************************************************************************/
+    public static void mlpunserialize(string s_in, out multilayerperceptron obj)
+    {
+        alglib.serializer s = new alglib.serializer();
+        obj = new multilayerperceptron();
+        s.ustart_str(s_in);
+        mlpbase.mlpunserialize(s, obj.innerobj);
+        s.stop();
+    }
+
+    /*************************************************************************
+    Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
+    layers, with linear output layer. Network weights are  filled  with  small
+    random values.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreate0(int nin, int nout, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreate0(nin, nout, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same  as  MLPCreate0,  but  with  one  hidden  layer  (NHid  neurons) with
+    non-linear activation function. Output layer is linear.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreate1(int nin, int nhid, int nout, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreate1(nin, nhid, nout, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same as MLPCreate0, but with two hidden layers (NHid1 and  NHid2  neurons)
+    with non-linear activation function. Output layer is linear.
+     $ALL
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreate2(int nin, int nhid1, int nhid2, int nout, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreate2(nin, nhid1, nhid2, nout, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
+    layers with non-linear output layer. Network weights are filled with small
+    random values.
+
+    Activation function of the output layer takes values:
+
+        (B, +INF), if D>=0
+
+    or
+
+        (-INF, B), if D<0.
+
+
+      -- ALGLIB --
+         Copyright 30.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreateb0(int nin, int nout, double b, double d, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreateb0(nin, nout, b, d, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same as MLPCreateB0 but with non-linear hidden layer.
+
+      -- ALGLIB --
+         Copyright 30.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreateb1(int nin, int nhid, int nout, double b, double d, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreateb1(nin, nhid, nout, b, d, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same as MLPCreateB0 but with two non-linear hidden layers.
+
+      -- ALGLIB --
+         Copyright 30.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreateb2(int nin, int nhid1, int nhid2, int nout, double b, double d, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreateb2(nin, nhid1, nhid2, nout, b, d, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
+    layers with non-linear output layer. Network weights are filled with small
+    random values. Activation function of the output layer takes values [A,B].
+
+      -- ALGLIB --
+         Copyright 30.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreater0(int nin, int nout, double a, double b, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreater0(nin, nout, a, b, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same as MLPCreateR0, but with non-linear hidden layer.
+
+      -- ALGLIB --
+         Copyright 30.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreater1(int nin, int nhid, int nout, double a, double b, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreater1(nin, nhid, nout, a, b, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same as MLPCreateR0, but with two non-linear hidden layers.
+
+      -- ALGLIB --
+         Copyright 30.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreater2(int nin, int nhid1, int nhid2, int nout, double a, double b, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreater2(nin, nhid1, nhid2, nout, a, b, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Creates classifier network with NIn  inputs  and  NOut  possible  classes.
+    Network contains no hidden layers and linear output  layer  with  SOFTMAX-
+    normalization  (so  outputs  sums  up  to  1.0  and  converge to posterior
+    probabilities).
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreatec0(int nin, int nout, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreatec0(nin, nout, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same as MLPCreateC0, but with one non-linear hidden layer.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreatec1(int nin, int nhid, int nout, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreatec1(nin, nhid, nout, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Same as MLPCreateC0, but with two non-linear hidden layers.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpcreatec2(int nin, int nhid1, int nhid2, int nout, out multilayerperceptron network)
+    {
+        network = new multilayerperceptron();
+        mlpbase.mlpcreatec2(nin, nhid1, nhid2, nout, network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Randomization of neural network weights
+
+      -- ALGLIB --
+         Copyright 06.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlprandomize(multilayerperceptron network)
+    {
+
+        mlpbase.mlprandomize(network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Randomization of neural network weights and standartisator
+
+      -- ALGLIB --
+         Copyright 10.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlprandomizefull(multilayerperceptron network)
+    {
+
+        mlpbase.mlprandomizefull(network.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Returns information about initialized network: number of inputs, outputs,
+    weights.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpproperties(multilayerperceptron network, out int nin, out int nout, out int wcount)
+    {
+        nin = 0;
+        nout = 0;
+        wcount = 0;
+        mlpbase.mlpproperties(network.innerobj, ref nin, ref nout, ref wcount);
+        return;
+    }
+
+    /*************************************************************************
+    Tells whether network is SOFTMAX-normalized (i.e. classifier) or not.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static bool mlpissoftmax(multilayerperceptron network)
+    {
+
+        bool result = mlpbase.mlpissoftmax(network.innerobj);
+        return result;
+    }
+
+    /*************************************************************************
+    This function returns total number of layers (including input, hidden and
+    output layers).
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static int mlpgetlayerscount(multilayerperceptron network)
+    {
+
+        int result = mlpbase.mlpgetlayerscount(network.innerobj);
+        return result;
+    }
+
+    /*************************************************************************
+    This function returns size of K-th layer.
+
+    K=0 corresponds to input layer, K=CNT-1 corresponds to output layer.
+
+    Size of the output layer is always equal to the number of outputs, although
+    when we have softmax-normalized network, last neuron doesn't have any
+    connections - it is just zero.
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static int mlpgetlayersize(multilayerperceptron network, int k)
+    {
+
+        int result = mlpbase.mlpgetlayersize(network.innerobj, k);
+        return result;
+    }
+
+    /*************************************************************************
+    This function returns offset/scaling coefficients for I-th input of the
+    network.
+
+    INPUT PARAMETERS:
+        Network     -   network
+        I           -   input index
+
+    OUTPUT PARAMETERS:
+        Mean        -   mean term
+        Sigma       -   sigma term, guaranteed to be nonzero.
+
+    I-th input is passed through linear transformation
+        IN[i] = (IN[i]-Mean)/Sigma
+    before feeding to the network
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpgetinputscaling(multilayerperceptron network, int i, out double mean, out double sigma)
+    {
+        mean = 0;
+        sigma = 0;
+        mlpbase.mlpgetinputscaling(network.innerobj, i, ref mean, ref sigma);
+        return;
+    }
+
+    /*************************************************************************
+    This function returns offset/scaling coefficients for I-th output of the
+    network.
+
+    INPUT PARAMETERS:
+        Network     -   network
+        I           -   input index
+
+    OUTPUT PARAMETERS:
+        Mean        -   mean term
+        Sigma       -   sigma term, guaranteed to be nonzero.
+
+    I-th output is passed through linear transformation
+        OUT[i] = OUT[i]*Sigma+Mean
+    before returning it to user. In case we have SOFTMAX-normalized network,
+    we return (Mean,Sigma)=(0.0,1.0).
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpgetoutputscaling(multilayerperceptron network, int i, out double mean, out double sigma)
+    {
+        mean = 0;
+        sigma = 0;
+        mlpbase.mlpgetoutputscaling(network.innerobj, i, ref mean, ref sigma);
+        return;
+    }
+
+    /*************************************************************************
+    This function returns information about Ith neuron of Kth layer
+
+    INPUT PARAMETERS:
+        Network     -   network
+        K           -   layer index
+        I           -   neuron index (within layer)
+
+    OUTPUT PARAMETERS:
+        FKind       -   activation function type (used by MLPActivationFunction())
+                        this value is zero for input or linear neurons
+        Threshold   -   also called offset, bias
+                        zero for input neurons
+
+    NOTE: this function throws exception if layer or neuron with  given  index
+    do not exists.
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpgetneuroninfo(multilayerperceptron network, int k, int i, out int fkind, out double threshold)
+    {
+        fkind = 0;
+        threshold = 0;
+        mlpbase.mlpgetneuroninfo(network.innerobj, k, i, ref fkind, ref threshold);
+        return;
+    }
+
+    /*************************************************************************
+    This function returns information about connection from I0-th neuron of
+    K0-th layer to I1-th neuron of K1-th layer.
+
+    INPUT PARAMETERS:
+        Network     -   network
+        K0          -   layer index
+        I0          -   neuron index (within layer)
+        K1          -   layer index
+        I1          -   neuron index (within layer)
+
+    RESULT:
+        connection weight (zero for non-existent connections)
+
+    This function:
+    1. throws exception if layer or neuron with given index do not exists.
+    2. returns zero if neurons exist, but there is no connection between them
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpgetweight(multilayerperceptron network, int k0, int i0, int k1, int i1)
+    {
+
+        double result = mlpbase.mlpgetweight(network.innerobj, k0, i0, k1, i1);
+        return result;
+    }
+
+    /*************************************************************************
+    This function sets offset/scaling coefficients for I-th input of the
+    network.
+
+    INPUT PARAMETERS:
+        Network     -   network
+        I           -   input index
+        Mean        -   mean term
+        Sigma       -   sigma term (if zero, will be replaced by 1.0)
+
+    NTE: I-th input is passed through linear transformation
+        IN[i] = (IN[i]-Mean)/Sigma
+    before feeding to the network. This function sets Mean and Sigma.
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpsetinputscaling(multilayerperceptron network, int i, double mean, double sigma)
+    {
+
+        mlpbase.mlpsetinputscaling(network.innerobj, i, mean, sigma);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets offset/scaling coefficients for I-th output of the
+    network.
+
+    INPUT PARAMETERS:
+        Network     -   network
+        I           -   input index
+        Mean        -   mean term
+        Sigma       -   sigma term (if zero, will be replaced by 1.0)
+
+    OUTPUT PARAMETERS:
+
+    NOTE: I-th output is passed through linear transformation
+        OUT[i] = OUT[i]*Sigma+Mean
+    before returning it to user. This function sets Sigma/Mean. In case we
+    have SOFTMAX-normalized network, you can not set (Sigma,Mean) to anything
+    other than(0.0,1.0) - this function will throw exception.
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpsetoutputscaling(multilayerperceptron network, int i, double mean, double sigma)
+    {
+
+        mlpbase.mlpsetoutputscaling(network.innerobj, i, mean, sigma);
+        return;
+    }
+
+    /*************************************************************************
+    This function modifies information about Ith neuron of Kth layer
+
+    INPUT PARAMETERS:
+        Network     -   network
+        K           -   layer index
+        I           -   neuron index (within layer)
+        FKind       -   activation function type (used by MLPActivationFunction())
+                        this value must be zero for input neurons
+                        (you can not set activation function for input neurons)
+        Threshold   -   also called offset, bias
+                        this value must be zero for input neurons
+                        (you can not set threshold for input neurons)
+
+    NOTES:
+    1. this function throws exception if layer or neuron with given index do
+       not exists.
+    2. this function also throws exception when you try to set non-linear
+       activation function for input neurons (any kind of network) or for output
+       neurons of classifier network.
+    3. this function throws exception when you try to set non-zero threshold for
+       input neurons (any kind of network).
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpsetneuroninfo(multilayerperceptron network, int k, int i, int fkind, double threshold)
+    {
+
+        mlpbase.mlpsetneuroninfo(network.innerobj, k, i, fkind, threshold);
+        return;
+    }
+
+    /*************************************************************************
+    This function modifies information about connection from I0-th neuron of
+    K0-th layer to I1-th neuron of K1-th layer.
+
+    INPUT PARAMETERS:
+        Network     -   network
+        K0          -   layer index
+        I0          -   neuron index (within layer)
+        K1          -   layer index
+        I1          -   neuron index (within layer)
+        W           -   connection weight (must be zero for non-existent
+                        connections)
+
+    This function:
+    1. throws exception if layer or neuron with given index do not exists.
+    2. throws exception if you try to set non-zero weight for non-existent
+       connection
+
+      -- ALGLIB --
+         Copyright 25.03.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpsetweight(multilayerperceptron network, int k0, int i0, int k1, int i1, double w)
+    {
+
+        mlpbase.mlpsetweight(network.innerobj, k0, i0, k1, i1, w);
+        return;
+    }
+
+    /*************************************************************************
+    Neural network activation function
+
+    INPUT PARAMETERS:
+        NET         -   neuron input
+        K           -   function index (zero for linear function)
+
+    OUTPUT PARAMETERS:
+        F           -   function
+        DF          -   its derivative
+        D2F         -   its second derivative
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpactivationfunction(double net, int k, out double f, out double df, out double d2f)
+    {
+        f = 0;
+        df = 0;
+        d2f = 0;
+        mlpbase.mlpactivationfunction(net, k, ref f, ref df, ref d2f);
+        return;
+    }
+
+    /*************************************************************************
+    Procesing
+
+    INPUT PARAMETERS:
+        Network -   neural network
+        X       -   input vector,  array[0..NIn-1].
+
+    OUTPUT PARAMETERS:
+        Y       -   result. Regression estimate when solving regression  task,
+                    vector of posterior probabilities for classification task.
+
+    See also MLPProcessI
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpprocess(multilayerperceptron network, double[] x, ref double[] y)
+    {
+
+        mlpbase.mlpprocess(network.innerobj, x, ref y);
+        return;
+    }
+
+    /*************************************************************************
+    'interactive'  variant  of  MLPProcess  for  languages  like  Python which
+    support constructs like "Y = MLPProcess(NN,X)" and interactive mode of the
+    interpreter
+
+    This function allocates new array on each call,  so  it  is  significantly
+    slower than its 'non-interactive' counterpart, but it is  more  convenient
+    when you call it from command line.
+
+      -- ALGLIB --
+         Copyright 21.09.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpprocessi(multilayerperceptron network, double[] x, out double[] y)
+    {
+        y = new double[0];
+        mlpbase.mlpprocessi(network.innerobj, x, ref y);
+        return;
+    }
+
+    /*************************************************************************
+    Error function for neural network, internal subroutine.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlperror(multilayerperceptron network, double[,] xy, int ssize)
+    {
+
+        double result = mlpbase.mlperror(network.innerobj, xy, ssize);
+        return result;
+    }
+
+    /*************************************************************************
+    Natural error function for neural network, internal subroutine.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlperrorn(multilayerperceptron network, double[,] xy, int ssize)
+    {
+
+        double result = mlpbase.mlperrorn(network.innerobj, xy, ssize);
+        return result;
+    }
+
+    /*************************************************************************
+    Classification error
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static int mlpclserror(multilayerperceptron network, double[,] xy, int ssize)
+    {
+
+        int result = mlpbase.mlpclserror(network.innerobj, xy, ssize);
+        return result;
+    }
+
+    /*************************************************************************
+    Relative classification error on the test set
+
+    INPUT PARAMETERS:
+        Network -   network
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        percent of incorrectly classified cases. Works both for
+        classifier networks and general purpose networks used as
+        classifiers.
+
+      -- ALGLIB --
+         Copyright 25.12.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlprelclserror(multilayerperceptron network, double[,] xy, int npoints)
+    {
+
+        double result = mlpbase.mlprelclserror(network.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Average cross-entropy (in bits per element) on the test set
+
+    INPUT PARAMETERS:
+        Network -   neural network
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        CrossEntropy/(NPoints*LN(2)).
+        Zero if network solves regression task.
+
+      -- ALGLIB --
+         Copyright 08.01.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpavgce(multilayerperceptron network, double[,] xy, int npoints)
+    {
+
+        double result = mlpbase.mlpavgce(network.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    RMS error on the test set
+
+    INPUT PARAMETERS:
+        Network -   neural network
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        root mean square error.
+        Its meaning for regression task is obvious. As for
+        classification task, RMS error means error when estimating posterior
+        probabilities.
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlprmserror(multilayerperceptron network, double[,] xy, int npoints)
+    {
+
+        double result = mlpbase.mlprmserror(network.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Average error on the test set
+
+    INPUT PARAMETERS:
+        Network -   neural network
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        Its meaning for regression task is obvious. As for
+        classification task, it means average error when estimating posterior
+        probabilities.
+
+      -- ALGLIB --
+         Copyright 11.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpavgerror(multilayerperceptron network, double[,] xy, int npoints)
+    {
+
+        double result = mlpbase.mlpavgerror(network.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Average relative error on the test set
+
+    INPUT PARAMETERS:
+        Network -   neural network
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        Its meaning for regression task is obvious. As for
+        classification task, it means average relative error when estimating
+        posterior probability of belonging to the correct class.
+
+      -- ALGLIB --
+         Copyright 11.03.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpavgrelerror(multilayerperceptron network, double[,] xy, int npoints)
+    {
+
+        double result = mlpbase.mlpavgrelerror(network.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Gradient calculation
+
+    INPUT PARAMETERS:
+        Network -   network initialized with one of the network creation funcs
+        X       -   input vector, length of array must be at least NIn
+        DesiredY-   desired outputs, length of array must be at least NOut
+        Grad    -   possibly preallocated array. If size of array is smaller
+                    than WCount, it will be reallocated. It is recommended to
+                    reuse previously allocated array to reduce allocation
+                    overhead.
+
+    OUTPUT PARAMETERS:
+        E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
+        Grad    -   gradient of E with respect to weights of network, array[WCount]
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpgrad(multilayerperceptron network, double[] x, double[] desiredy, out double e, ref double[] grad)
+    {
+        e = 0;
+        mlpbase.mlpgrad(network.innerobj, x, desiredy, ref e, ref grad);
+        return;
+    }
+
+    /*************************************************************************
+    Gradient calculation (natural error function is used)
+
+    INPUT PARAMETERS:
+        Network -   network initialized with one of the network creation funcs
+        X       -   input vector, length of array must be at least NIn
+        DesiredY-   desired outputs, length of array must be at least NOut
+        Grad    -   possibly preallocated array. If size of array is smaller
+                    than WCount, it will be reallocated. It is recommended to
+                    reuse previously allocated array to reduce allocation
+                    overhead.
+
+    OUTPUT PARAMETERS:
+        E       -   error function, sum-of-squares for regression networks,
+                    cross-entropy for classification networks.
+        Grad    -   gradient of E with respect to weights of network, array[WCount]
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpgradn(multilayerperceptron network, double[] x, double[] desiredy, out double e, ref double[] grad)
+    {
+        e = 0;
+        mlpbase.mlpgradn(network.innerobj, x, desiredy, ref e, ref grad);
+        return;
+    }
+
+    /*************************************************************************
+    Batch gradient calculation for a set of inputs/outputs
+
+    INPUT PARAMETERS:
+        Network -   network initialized with one of the network creation funcs
+        XY      -   set of inputs/outputs; one sample = one row;
+                    first NIn columns contain inputs,
+                    next NOut columns - desired outputs.
+        SSize   -   number of elements in XY
+        Grad    -   possibly preallocated array. If size of array is smaller
+                    than WCount, it will be reallocated. It is recommended to
+                    reuse previously allocated array to reduce allocation
+                    overhead.
+
+    OUTPUT PARAMETERS:
+        E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
+        Grad    -   gradient of E with respect to weights of network, array[WCount]
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpgradbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad)
+    {
+        e = 0;
+        mlpbase.mlpgradbatch(network.innerobj, xy, ssize, ref e, ref grad);
+        return;
+    }
+
+    /*************************************************************************
+    Batch gradient calculation for a set of inputs/outputs
+    (natural error function is used)
+
+    INPUT PARAMETERS:
+        Network -   network initialized with one of the network creation funcs
+        XY      -   set of inputs/outputs; one sample = one row;
+                    first NIn columns contain inputs,
+                    next NOut columns - desired outputs.
+        SSize   -   number of elements in XY
+        Grad    -   possibly preallocated array. If size of array is smaller
+                    than WCount, it will be reallocated. It is recommended to
+                    reuse previously allocated array to reduce allocation
+                    overhead.
+
+    OUTPUT PARAMETERS:
+        E       -   error function, sum-of-squares for regression networks,
+                    cross-entropy for classification networks.
+        Grad    -   gradient of E with respect to weights of network, array[WCount]
+
+      -- ALGLIB --
+         Copyright 04.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpgradnbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad)
+    {
+        e = 0;
+        mlpbase.mlpgradnbatch(network.innerobj, xy, ssize, ref e, ref grad);
+        return;
+    }
+
+    /*************************************************************************
+    Batch Hessian calculation (natural error function) using R-algorithm.
+    Internal subroutine.
+
+      -- ALGLIB --
+         Copyright 26.01.2008 by Bochkanov Sergey.
+
+         Hessian calculation based on R-algorithm described in
+         "Fast Exact Multiplication by the Hessian",
+         B. A. Pearlmutter,
+         Neural Computation, 1994.
+    *************************************************************************/
+    public static void mlphessiannbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad, ref double[,] h)
+    {
+        e = 0;
+        mlpbase.mlphessiannbatch(network.innerobj, xy, ssize, ref e, ref grad, ref h);
+        return;
+    }
+
+    /*************************************************************************
+    Batch Hessian calculation using R-algorithm.
+    Internal subroutine.
+
+      -- ALGLIB --
+         Copyright 26.01.2008 by Bochkanov Sergey.
+
+         Hessian calculation based on R-algorithm described in
+         "Fast Exact Multiplication by the Hessian",
+         B. A. Pearlmutter,
+         Neural Computation, 1994.
+    *************************************************************************/
+    public static void mlphessianbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad, ref double[,] h)
+    {
+        e = 0;
+        mlpbase.mlphessianbatch(network.innerobj, xy, ssize, ref e, ref grad, ref h);
+        return;
     }
 
@@ -1134,7 +2158,12 @@
 
     /*************************************************************************
-
+    This structure is a MCPD (Markov Chains for Population Data) solver.
+
+    You should use ALGLIB functions in order to work with this object.
+
+      -- ALGLIB --
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public class multilayerperceptron
+    public class mcpdstate
     {
         //
@@ -1142,7 +2171,7 @@
         //
 
-        public multilayerperceptron()
-        {
-            _innerobj = new mlpbase.multilayerperceptron();
+        public mcpdstate()
+        {
+            _innerobj = new mcpd.mcpdstate();
         }
 
@@ -1151,7 +2180,7 @@
         // They are intended for internal use only
         //
-        private mlpbase.multilayerperceptron _innerobj;
-        public mlpbase.multilayerperceptron innerobj { get { return _innerobj; } }
-        public multilayerperceptron(mlpbase.multilayerperceptron obj)
+        private mcpd.mcpdstate _innerobj;
+        public mcpd.mcpdstate innerobj { get { return _innerobj; } }
+        public mcpdstate(mcpd.mcpdstate obj)
         {
             _innerobj = obj;
@@ -1159,598 +2188,33 @@
     }
 
+
     /*************************************************************************
-    Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
-    layers, with linear output layer. Network weights are  filled  with  small
-    random values.
+    This structure is a MCPD training report:
+        InnerIterationsCount    -   number of inner iterations of the
+                                    underlying optimization algorithm
+        OuterIterationsCount    -   number of outer iterations of the
+                                    underlying optimization algorithm
+        NFEV                    -   number of merit function evaluations
+        TerminationType         -   termination type
+                                    (same as for MinBLEIC optimizer, positive
+                                    values denote success, negative ones -
+                                    failure)
 
       -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpcreate0(int nin, int nout, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreate0(nin, nout, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same  as  MLPCreate0,  but  with  one  hidden  layer  (NHid  neurons) with
-    non-linear activation function. Output layer is linear.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreate1(int nin, int nhid, int nout, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreate1(nin, nhid, nout, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same as MLPCreate0, but with two hidden layers (NHid1 and  NHid2  neurons)
-    with non-linear activation function. Output layer is linear.
-     $ALL
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreate2(int nin, int nhid1, int nhid2, int nout, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreate2(nin, nhid1, nhid2, nout, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
-    layers with non-linear output layer. Network weights are filled with small
-    random values.
-
-    Activation function of the output layer takes values:
-
-        (B, +INF), if D>=0
-
-    or
-
-        (-INF, B), if D<0.
-
-
-      -- ALGLIB --
-         Copyright 30.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreateb0(int nin, int nout, double b, double d, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreateb0(nin, nout, b, d, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same as MLPCreateB0 but with non-linear hidden layer.
-
-      -- ALGLIB --
-         Copyright 30.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreateb1(int nin, int nhid, int nout, double b, double d, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreateb1(nin, nhid, nout, b, d, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same as MLPCreateB0 but with two non-linear hidden layers.
-
-      -- ALGLIB --
-         Copyright 30.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreateb2(int nin, int nhid1, int nhid2, int nout, double b, double d, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreateb2(nin, nhid1, nhid2, nout, b, d, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
-    layers with non-linear output layer. Network weights are filled with small
-    random values. Activation function of the output layer takes values [A,B].
-
-      -- ALGLIB --
-         Copyright 30.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreater0(int nin, int nout, double a, double b, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreater0(nin, nout, a, b, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same as MLPCreateR0, but with non-linear hidden layer.
-
-      -- ALGLIB --
-         Copyright 30.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreater1(int nin, int nhid, int nout, double a, double b, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreater1(nin, nhid, nout, a, b, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same as MLPCreateR0, but with two non-linear hidden layers.
-
-      -- ALGLIB --
-         Copyright 30.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreater2(int nin, int nhid1, int nhid2, int nout, double a, double b, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreater2(nin, nhid1, nhid2, nout, a, b, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Creates classifier network with NIn  inputs  and  NOut  possible  classes.
-    Network contains no hidden layers and linear output  layer  with  SOFTMAX-
-    normalization  (so  outputs  sums  up  to  1.0  and  converge to posterior
-    probabilities).
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreatec0(int nin, int nout, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreatec0(nin, nout, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same as MLPCreateC0, but with one non-linear hidden layer.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreatec1(int nin, int nhid, int nout, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreatec1(nin, nhid, nout, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Same as MLPCreateC0, but with two non-linear hidden layers.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpcreatec2(int nin, int nhid1, int nhid2, int nout, out multilayerperceptron network)
-    {
-        network = new multilayerperceptron();
-        mlpbase.mlpcreatec2(nin, nhid1, nhid2, nout, network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Randomization of neural network weights
-
-      -- ALGLIB --
-         Copyright 06.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlprandomize(multilayerperceptron network)
-    {
-
-        mlpbase.mlprandomize(network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Randomization of neural network weights and standartisator
-
-      -- ALGLIB --
-         Copyright 10.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlprandomizefull(multilayerperceptron network)
-    {
-
-        mlpbase.mlprandomizefull(network.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Returns information about initialized network: number of inputs, outputs,
-    weights.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpproperties(multilayerperceptron network, out int nin, out int nout, out int wcount)
-    {
-        nin = 0;
-        nout = 0;
-        wcount = 0;
-        mlpbase.mlpproperties(network.innerobj, ref nin, ref nout, ref wcount);
-        return;
-    }
-
-    /*************************************************************************
-    Tells whether network is SOFTMAX-normalized (i.e. classifier) or not.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static bool mlpissoftmax(multilayerperceptron network)
-    {
-
-        bool result = mlpbase.mlpissoftmax(network.innerobj);
-        return result;
-    }
-
-    /*************************************************************************
-    Procesing
-
-    INPUT PARAMETERS:
-        Network -   neural network
-        X       -   input vector,  array[0..NIn-1].
-
-    OUTPUT PARAMETERS:
-        Y       -   result. Regression estimate when solving regression  task,
-                    vector of posterior probabilities for classification task.
-
-    See also MLPProcessI
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpprocess(multilayerperceptron network, double[] x, ref double[] y)
-    {
-
-        mlpbase.mlpprocess(network.innerobj, x, ref y);
-        return;
-    }
-
-    /*************************************************************************
-    'interactive'  variant  of  MLPProcess  for  languages  like  Python which
-    support constructs like "Y = MLPProcess(NN,X)" and interactive mode of the
-    interpreter
-
-    This function allocates new array on each call,  so  it  is  significantly
-    slower than its 'non-interactive' counterpart, but it is  more  convenient
-    when you call it from command line.
-
-      -- ALGLIB --
-         Copyright 21.09.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpprocessi(multilayerperceptron network, double[] x, out double[] y)
-    {
-        y = new double[0];
-        mlpbase.mlpprocessi(network.innerobj, x, ref y);
-        return;
-    }
-
-    /*************************************************************************
-    Error function for neural network, internal subroutine.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlperror(multilayerperceptron network, double[,] xy, int ssize)
-    {
-
-        double result = mlpbase.mlperror(network.innerobj, xy, ssize);
-        return result;
-    }
-
-    /*************************************************************************
-    Natural error function for neural network, internal subroutine.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlperrorn(multilayerperceptron network, double[,] xy, int ssize)
-    {
-
-        double result = mlpbase.mlperrorn(network.innerobj, xy, ssize);
-        return result;
-    }
-
-    /*************************************************************************
-    Classification error
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static int mlpclserror(multilayerperceptron network, double[,] xy, int ssize)
-    {
-
-        int result = mlpbase.mlpclserror(network.innerobj, xy, ssize);
-        return result;
-    }
-
-    /*************************************************************************
-    Relative classification error on the test set
-
-    INPUT PARAMETERS:
-        Network -   network
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        percent of incorrectly classified cases. Works both for
-        classifier networks and general purpose networks used as
-        classifiers.
-
-      -- ALGLIB --
-         Copyright 25.12.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlprelclserror(multilayerperceptron network, double[,] xy, int npoints)
-    {
-
-        double result = mlpbase.mlprelclserror(network.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Average cross-entropy (in bits per element) on the test set
-
-    INPUT PARAMETERS:
-        Network -   neural network
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        CrossEntropy/(NPoints*LN(2)).
-        Zero if network solves regression task.
-
-      -- ALGLIB --
-         Copyright 08.01.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlpavgce(multilayerperceptron network, double[,] xy, int npoints)
-    {
-
-        double result = mlpbase.mlpavgce(network.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    RMS error on the test set
-
-    INPUT PARAMETERS:
-        Network -   neural network
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        root mean square error.
-        Its meaning for regression task is obvious. As for
-        classification task, RMS error means error when estimating posterior
-        probabilities.
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlprmserror(multilayerperceptron network, double[,] xy, int npoints)
-    {
-
-        double result = mlpbase.mlprmserror(network.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Average error on the test set
-
-    INPUT PARAMETERS:
-        Network -   neural network
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        Its meaning for regression task is obvious. As for
-        classification task, it means average error when estimating posterior
-        probabilities.
-
-      -- ALGLIB --
-         Copyright 11.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlpavgerror(multilayerperceptron network, double[,] xy, int npoints)
-    {
-
-        double result = mlpbase.mlpavgerror(network.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Average relative error on the test set
-
-    INPUT PARAMETERS:
-        Network -   neural network
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        Its meaning for regression task is obvious. As for
-        classification task, it means average relative error when estimating
-        posterior probability of belonging to the correct class.
-
-      -- ALGLIB --
-         Copyright 11.03.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlpavgrelerror(multilayerperceptron network, double[,] xy, int npoints)
-    {
-
-        double result = mlpbase.mlpavgrelerror(network.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Gradient calculation
-
-    INPUT PARAMETERS:
-        Network -   network initialized with one of the network creation funcs
-        X       -   input vector, length of array must be at least NIn
-        DesiredY-   desired outputs, length of array must be at least NOut
-        Grad    -   possibly preallocated array. If size of array is smaller
-                    than WCount, it will be reallocated. It is recommended to
-                    reuse previously allocated array to reduce allocation
-                    overhead.
-
-    OUTPUT PARAMETERS:
-        E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
-        Grad    -   gradient of E with respect to weights of network, array[WCount]
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpgrad(multilayerperceptron network, double[] x, double[] desiredy, out double e, ref double[] grad)
-    {
-        e = 0;
-        mlpbase.mlpgrad(network.innerobj, x, desiredy, ref e, ref grad);
-        return;
-    }
-
-    /*************************************************************************
-    Gradient calculation (natural error function is used)
-
-    INPUT PARAMETERS:
-        Network -   network initialized with one of the network creation funcs
-        X       -   input vector, length of array must be at least NIn
-        DesiredY-   desired outputs, length of array must be at least NOut
-        Grad    -   possibly preallocated array. If size of array is smaller
-                    than WCount, it will be reallocated. It is recommended to
-                    reuse previously allocated array to reduce allocation
-                    overhead.
-
-    OUTPUT PARAMETERS:
-        E       -   error function, sum-of-squares for regression networks,
-                    cross-entropy for classification networks.
-        Grad    -   gradient of E with respect to weights of network, array[WCount]
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpgradn(multilayerperceptron network, double[] x, double[] desiredy, out double e, ref double[] grad)
-    {
-        e = 0;
-        mlpbase.mlpgradn(network.innerobj, x, desiredy, ref e, ref grad);
-        return;
-    }
-
-    /*************************************************************************
-    Batch gradient calculation for a set of inputs/outputs
-
-    INPUT PARAMETERS:
-        Network -   network initialized with one of the network creation funcs
-        XY      -   set of inputs/outputs; one sample = one row;
-                    first NIn columns contain inputs,
-                    next NOut columns - desired outputs.
-        SSize   -   number of elements in XY
-        Grad    -   possibly preallocated array. If size of array is smaller
-                    than WCount, it will be reallocated. It is recommended to
-                    reuse previously allocated array to reduce allocation
-                    overhead.
-
-    OUTPUT PARAMETERS:
-        E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
-        Grad    -   gradient of E with respect to weights of network, array[WCount]
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpgradbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad)
-    {
-        e = 0;
-        mlpbase.mlpgradbatch(network.innerobj, xy, ssize, ref e, ref grad);
-        return;
-    }
-
-    /*************************************************************************
-    Batch gradient calculation for a set of inputs/outputs
-    (natural error function is used)
-
-    INPUT PARAMETERS:
-        Network -   network initialized with one of the network creation funcs
-        XY      -   set of inputs/outputs; one sample = one row;
-                    first NIn columns contain inputs,
-                    next NOut columns - desired outputs.
-        SSize   -   number of elements in XY
-        Grad    -   possibly preallocated array. If size of array is smaller
-                    than WCount, it will be reallocated. It is recommended to
-                    reuse previously allocated array to reduce allocation
-                    overhead.
-
-    OUTPUT PARAMETERS:
-        E       -   error function, sum-of-squares for regression networks,
-                    cross-entropy for classification networks.
-        Grad    -   gradient of E with respect to weights of network, array[WCount]
-
-      -- ALGLIB --
-         Copyright 04.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpgradnbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad)
-    {
-        e = 0;
-        mlpbase.mlpgradnbatch(network.innerobj, xy, ssize, ref e, ref grad);
-        return;
-    }
-
-    /*************************************************************************
-    Batch Hessian calculation (natural error function) using R-algorithm.
-    Internal subroutine.
-
-      -- ALGLIB --
-         Copyright 26.01.2008 by Bochkanov Sergey.
-
-         Hessian calculation based on R-algorithm described in
-         "Fast Exact Multiplication by the Hessian",
-         B. A. Pearlmutter,
-         Neural Computation, 1994.
-    *************************************************************************/
-    public static void mlphessiannbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad, ref double[,] h)
-    {
-        e = 0;
-        mlpbase.mlphessiannbatch(network.innerobj, xy, ssize, ref e, ref grad, ref h);
-        return;
-    }
-
-    /*************************************************************************
-    Batch Hessian calculation using R-algorithm.
-    Internal subroutine.
-
-      -- ALGLIB --
-         Copyright 26.01.2008 by Bochkanov Sergey.
-
-         Hessian calculation based on R-algorithm described in
-         "Fast Exact Multiplication by the Hessian",
-         B. A. Pearlmutter,
-         Neural Computation, 1994.
-    *************************************************************************/
-    public static void mlphessianbatch(multilayerperceptron network, double[,] xy, int ssize, out double e, ref double[] grad, ref double[,] h)
-    {
-        e = 0;
-        mlpbase.mlphessianbatch(network.innerobj, xy, ssize, ref e, ref grad, ref h);
-        return;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Neural networks ensemble
-    *************************************************************************/
-    public class mlpensemble
+    public class mcpdreport
     {
         //
         // Public declarations
         //
-
-        public mlpensemble()
-        {
-            _innerobj = new mlpe.mlpensemble();
+        public int inneriterationscount { get { return _innerobj.inneriterationscount; } set { _innerobj.inneriterationscount = value; } }
+        public int outeriterationscount { get { return _innerobj.outeriterationscount; } set { _innerobj.outeriterationscount = value; } }
+        public int nfev { get { return _innerobj.nfev; } set { _innerobj.nfev = value; } }
+        public int terminationtype { get { return _innerobj.terminationtype; } set { _innerobj.terminationtype = value; } }
+
+        public mcpdreport()
+        {
+            _innerobj = new mcpd.mcpdreport();
         }
 
@@ -1759,7 +2223,7 @@
         // They are intended for internal use only
         //
-        private mlpe.mlpensemble _innerobj;
-        public mlpe.mlpensemble innerobj { get { return _innerobj; } }
-        public mlpensemble(mlpe.mlpensemble obj)
+        private mcpd.mcpdreport _innerobj;
+        public mcpd.mcpdreport innerobj { get { return _innerobj; } }
+        public mcpdreport(mcpd.mcpdreport obj)
         {
             _innerobj = obj;
@@ -1768,468 +2232,704 @@
 
     /*************************************************************************
-    Like MLPCreate0, but for ensembles.
+    DESCRIPTION:
+
+    This function creates MCPD (Markov Chains for Population Data) solver.
+
+    This  solver  can  be  used  to find transition matrix P for N-dimensional
+    prediction  problem  where transition from X[i] to X[i+1] is  modelled  as
+        X[i+1] = P*X[i]
+    where X[i] and X[i+1] are N-dimensional population vectors (components  of
+    each X are non-negative), and P is a N*N transition matrix (elements of  P
+    are non-negative, each column sums to 1.0).
+
+    Such models arise when when:
+    * there is some population of individuals
+    * individuals can have different states
+    * individuals can transit from one state to another
+    * population size is constant, i.e. there is no new individuals and no one
+      leaves population
+    * you want to model transitions of individuals from one state into another
+
+    USAGE:
+
+    Here we give very brief outline of the MCPD. We strongly recommend you  to
+    read examples in the ALGLIB Reference Manual and to read ALGLIB User Guide
+    on data analysis which is available at http://www.alglib.net/dataanalysis/
+
+    1. User initializes algorithm state with MCPDCreate() call
+
+    2. User  adds  one  or  more  tracks -  sequences of states which describe
+       evolution of a system being modelled from different starting conditions
+
+    3. User may add optional boundary, equality  and/or  linear constraints on
+       the coefficients of P by calling one of the following functions:
+       * MCPDSetEC() to set equality constraints
+       * MCPDSetBC() to set bound constraints
+       * MCPDSetLC() to set linear constraints
+
+    4. Optionally,  user  may  set  custom  weights  for prediction errors (by
+       default, algorithm assigns non-equal, automatically chosen weights  for
+       errors in the prediction of different components of X). It can be  done
+       with a call of MCPDSetPredictionWeights() function.
+
+    5. User calls MCPDSolve() function which takes algorithm  state and
+       pointer (delegate, etc.) to callback function which calculates F/G.
+
+    6. User calls MCPDResults() to get solution
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>=1
+
+    OUTPUT PARAMETERS:
+        State   -   structure stores algorithm state
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreate0(int nin, int nout, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdcreate(int n, out mcpdstate s)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreate0(nin, nout, ensemblesize, ensemble.innerobj);
+        s = new mcpdstate();
+        mcpd.mcpdcreate(n, s.innerobj);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreate1, but for ensembles.
+    DESCRIPTION:
+
+    This function is a specialized version of MCPDCreate()  function,  and  we
+    recommend  you  to read comments for this function for general information
+    about MCPD solver.
+
+    This  function  creates  MCPD (Markov Chains for Population  Data)  solver
+    for "Entry-state" model,  i.e. model  where transition from X[i] to X[i+1]
+    is modelled as
+        X[i+1] = P*X[i]
+    where
+        X[i] and X[i+1] are N-dimensional state vectors
+        P is a N*N transition matrix
+    and  one  selected component of X[] is called "entry" state and is treated
+    in a special way:
+        system state always transits from "entry" state to some another state
+        system state can not transit from any state into "entry" state
+    Such conditions basically mean that row of P which corresponds to  "entry"
+    state is zero.
+
+    Such models arise when:
+    * there is some population of individuals
+    * individuals can have different states
+    * individuals can transit from one state to another
+    * population size is NOT constant -  at every moment of time there is some
+      (unpredictable) amount of "new" individuals, which can transit into  one
+      of the states at the next turn, but still no one leaves population
+    * you want to model transitions of individuals from one state into another
+    * but you do NOT want to predict amount of "new"  individuals  because  it
+      does not depends on individuals already present (hence  system  can  not
+      transit INTO entry state - it can only transit FROM it).
+
+    This model is discussed  in  more  details  in  the ALGLIB User Guide (see
+    http://www.alglib.net/dataanalysis/ for more data).
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>=2
+        EntryState- index of entry state, in 0..N-1
+
+    OUTPUT PARAMETERS:
+        State   -   structure stores algorithm state
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreate1(int nin, int nhid, int nout, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdcreateentry(int n, int entrystate, out mcpdstate s)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreate1(nin, nhid, nout, ensemblesize, ensemble.innerobj);
+        s = new mcpdstate();
+        mcpd.mcpdcreateentry(n, entrystate, s.innerobj);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreate2, but for ensembles.
+    DESCRIPTION:
+
+    This function is a specialized version of MCPDCreate()  function,  and  we
+    recommend  you  to read comments for this function for general information
+    about MCPD solver.
+
+    This  function  creates  MCPD (Markov Chains for Population  Data)  solver
+    for "Exit-state" model,  i.e. model  where  transition from X[i] to X[i+1]
+    is modelled as
+        X[i+1] = P*X[i]
+    where
+        X[i] and X[i+1] are N-dimensional state vectors
+        P is a N*N transition matrix
+    and  one  selected component of X[] is called "exit"  state and is treated
+    in a special way:
+        system state can transit from any state into "exit" state
+        system state can not transit from "exit" state into any other state
+        transition operator discards "exit" state (makes it zero at each turn)
+    Such  conditions  basically  mean  that  column  of P which corresponds to
+    "exit" state is zero. Multiplication by such P may decrease sum of  vector
+    components.
+
+    Such models arise when:
+    * there is some population of individuals
+    * individuals can have different states
+    * individuals can transit from one state to another
+    * population size is NOT constant - individuals can move into "exit" state
+      and leave population at the next turn, but there are no new individuals
+    * amount of individuals which leave population can be predicted
+    * you want to model transitions of individuals from one state into another
+      (including transitions into the "exit" state)
+
+    This model is discussed  in  more  details  in  the ALGLIB User Guide (see
+    http://www.alglib.net/dataanalysis/ for more data).
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>=2
+        ExitState-  index of exit state, in 0..N-1
+
+    OUTPUT PARAMETERS:
+        State   -   structure stores algorithm state
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreate2(int nin, int nhid1, int nhid2, int nout, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdcreateexit(int n, int exitstate, out mcpdstate s)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreate2(nin, nhid1, nhid2, nout, ensemblesize, ensemble.innerobj);
+        s = new mcpdstate();
+        mcpd.mcpdcreateexit(n, exitstate, s.innerobj);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreateB0, but for ensembles.
+    DESCRIPTION:
+
+    This function is a specialized version of MCPDCreate()  function,  and  we
+    recommend  you  to read comments for this function for general information
+    about MCPD solver.
+
+    This  function  creates  MCPD (Markov Chains for Population  Data)  solver
+    for "Entry-Exit-states" model, i.e. model where  transition  from  X[i] to
+    X[i+1] is modelled as
+        X[i+1] = P*X[i]
+    where
+        X[i] and X[i+1] are N-dimensional state vectors
+        P is a N*N transition matrix
+    one selected component of X[] is called "entry" state and is treated in  a
+    special way:
+        system state always transits from "entry" state to some another state
+        system state can not transit from any state into "entry" state
+    and another one component of X[] is called "exit" state and is treated  in
+    a special way too:
+        system state can transit from any state into "exit" state
+        system state can not transit from "exit" state into any other state
+        transition operator discards "exit" state (makes it zero at each turn)
+    Such conditions basically mean that:
+        row of P which corresponds to "entry" state is zero
+        column of P which corresponds to "exit" state is zero
+    Multiplication by such P may decrease sum of vector components.
+
+    Such models arise when:
+    * there is some population of individuals
+    * individuals can have different states
+    * individuals can transit from one state to another
+    * population size is NOT constant
+    * at every moment of time there is some (unpredictable)  amount  of  "new"
+      individuals, which can transit into one of the states at the next turn
+    * some  individuals  can  move  (predictably)  into "exit" state and leave
+      population at the next turn
+    * you want to model transitions of individuals from one state into another,
+      including transitions from the "entry" state and into the "exit" state.
+    * but you do NOT want to predict amount of "new"  individuals  because  it
+      does not depends on individuals already present (hence  system  can  not
+      transit INTO entry state - it can only transit FROM it).
+
+    This model is discussed  in  more  details  in  the ALGLIB User Guide (see
+    http://www.alglib.net/dataanalysis/ for more data).
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>=2
+        EntryState- index of entry state, in 0..N-1
+        ExitState-  index of exit state, in 0..N-1
+
+    OUTPUT PARAMETERS:
+        State   -   structure stores algorithm state
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreateb0(int nin, int nout, double b, double d, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdcreateentryexit(int n, int entrystate, int exitstate, out mcpdstate s)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreateb0(nin, nout, b, d, ensemblesize, ensemble.innerobj);
+        s = new mcpdstate();
+        mcpd.mcpdcreateentryexit(n, entrystate, exitstate, s.innerobj);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreateB1, but for ensembles.
+    This  function  is  used to add a track - sequence of system states at the
+    different moments of its evolution.
+
+    You  may  add  one  or several tracks to the MCPD solver. In case you have
+    several tracks, they won't overwrite each other. For example,  if you pass
+    two tracks, A1-A2-A3 (system at t=A+1, t=A+2 and t=A+3) and B1-B2-B3, then
+    solver will try to model transitions from t=A+1 to t=A+2, t=A+2 to  t=A+3,
+    t=B+1 to t=B+2, t=B+2 to t=B+3. But it WONT mix these two tracks - i.e. it
+    wont try to model transition from t=A+3 to t=B+1.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        XY      -   track, array[K,N]:
+                    * I-th row is a state at t=I
+                    * elements of XY must be non-negative (exception will be
+                      thrown on negative elements)
+        K       -   number of points in a track
+                    * if given, only leading K rows of XY are used
+                    * if not given, automatically determined from size of XY
+
+    NOTES:
+
+    1. Track may contain either proportional or population data:
+       * with proportional data all rows of XY must sum to 1.0, i.e. we have
+         proportions instead of absolute population values
+       * with population data rows of XY contain population counts and generally
+         do not sum to 1.0 (although they still must be non-negative)
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreateb1(int nin, int nhid, int nout, double b, double d, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdaddtrack(mcpdstate s, double[,] xy, int k)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreateb1(nin, nhid, nout, b, d, ensemblesize, ensemble.innerobj);
+
+        mcpd.mcpdaddtrack(s.innerobj, xy, k);
         return;
     }
-
-    /*************************************************************************
-    Like MLPCreateB2, but for ensembles.
-
-      -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpecreateb2(int nin, int nhid1, int nhid2, int nout, double b, double d, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdaddtrack(mcpdstate s, double[,] xy)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreateb2(nin, nhid1, nhid2, nout, b, d, ensemblesize, ensemble.innerobj);
+        int k;
+
+
+        k = ap.rows(xy);
+        mcpd.mcpdaddtrack(s.innerobj, xy, k);
+
         return;
     }
 
     /*************************************************************************
-    Like MLPCreateR0, but for ensembles.
+    This function is used to add equality constraints on the elements  of  the
+    transition matrix P.
+
+    MCPD solver has four types of constraints which can be placed on P:
+    * user-specified equality constraints (optional)
+    * user-specified bound constraints (optional)
+    * user-specified general linear constraints (optional)
+    * basic constraints (always present):
+      * non-negativity: P[i,j]>=0
+      * consistency: every column of P sums to 1.0
+
+    Final  constraints  which  are  passed  to  the  underlying  optimizer are
+    calculated  as  intersection  of all present constraints. For example, you
+    may specify boundary constraint on P[0,0] and equality one:
+        0.1<=P[0,0]<=0.9
+        P[0,0]=0.5
+    Such  combination  of  constraints  will  be  silently  reduced  to  their
+    intersection, which is P[0,0]=0.5.
+
+    This  function  can  be  used  to  place equality constraints on arbitrary
+    subset of elements of P. Set of constraints is specified by EC, which  may
+    contain either NAN's or finite numbers from [0,1]. NAN denotes absence  of
+    constraint, finite number denotes equality constraint on specific  element
+    of P.
+
+    You can also  use  MCPDAddEC()  function  which  allows  to  ADD  equality
+    constraint  for  one  element  of P without changing constraints for other
+    elements.
+
+    These functions (MCPDSetEC and MCPDAddEC) interact as follows:
+    * there is internal matrix of equality constraints which is stored in  the
+      MCPD solver
+    * MCPDSetEC() replaces this matrix by another one (SET)
+    * MCPDAddEC() modifies one element of this matrix and  leaves  other  ones
+      unchanged (ADD)
+    * thus  MCPDAddEC()  call  preserves  all  modifications  done by previous
+      calls,  while  MCPDSetEC()  completely discards all changes  done to the
+      equality constraints.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        EC      -   equality constraints, array[N,N]. Elements of  EC  can  be
+                    either NAN's or finite  numbers from  [0,1].  NAN  denotes
+                    absence  of  constraints,  while  finite  value    denotes
+                    equality constraint on the corresponding element of P.
+
+    NOTES:
+
+    1. infinite values of EC will lead to exception being thrown. Values  less
+    than 0.0 or greater than 1.0 will lead to error code being returned  after
+    call to MCPDSolve().
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreater0(int nin, int nout, double a, double b, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdsetec(mcpdstate s, double[,] ec)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreater0(nin, nout, a, b, ensemblesize, ensemble.innerobj);
+
+        mcpd.mcpdsetec(s.innerobj, ec);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreateR1, but for ensembles.
+    This function is used to add equality constraints on the elements  of  the
+    transition matrix P.
+
+    MCPD solver has four types of constraints which can be placed on P:
+    * user-specified equality constraints (optional)
+    * user-specified bound constraints (optional)
+    * user-specified general linear constraints (optional)
+    * basic constraints (always present):
+      * non-negativity: P[i,j]>=0
+      * consistency: every column of P sums to 1.0
+
+    Final  constraints  which  are  passed  to  the  underlying  optimizer are
+    calculated  as  intersection  of all present constraints. For example, you
+    may specify boundary constraint on P[0,0] and equality one:
+        0.1<=P[0,0]<=0.9
+        P[0,0]=0.5
+    Such  combination  of  constraints  will  be  silently  reduced  to  their
+    intersection, which is P[0,0]=0.5.
+
+    This function can be used to ADD equality constraint for one element of  P
+    without changing constraints for other elements.
+
+    You  can  also  use  MCPDSetEC()  function  which  allows  you  to specify
+    arbitrary set of equality constraints in one call.
+
+    These functions (MCPDSetEC and MCPDAddEC) interact as follows:
+    * there is internal matrix of equality constraints which is stored in the
+      MCPD solver
+    * MCPDSetEC() replaces this matrix by another one (SET)
+    * MCPDAddEC() modifies one element of this matrix and leaves  other  ones
+      unchanged (ADD)
+    * thus  MCPDAddEC()  call  preserves  all  modifications done by previous
+      calls,  while  MCPDSetEC()  completely discards all changes done to the
+      equality constraints.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        I       -   row index of element being constrained
+        J       -   column index of element being constrained
+        C       -   value (constraint for P[I,J]).  Can  be  either  NAN  (no
+                    constraint) or finite value from [0,1].
+
+    NOTES:
+
+    1. infinite values of C  will lead to exception being thrown. Values  less
+    than 0.0 or greater than 1.0 will lead to error code being returned  after
+    call to MCPDSolve().
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreater1(int nin, int nhid, int nout, double a, double b, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdaddec(mcpdstate s, int i, int j, double c)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreater1(nin, nhid, nout, a, b, ensemblesize, ensemble.innerobj);
+
+        mcpd.mcpdaddec(s.innerobj, i, j, c);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreateR2, but for ensembles.
+    This function is used to add bound constraints  on  the  elements  of  the
+    transition matrix P.
+
+    MCPD solver has four types of constraints which can be placed on P:
+    * user-specified equality constraints (optional)
+    * user-specified bound constraints (optional)
+    * user-specified general linear constraints (optional)
+    * basic constraints (always present):
+      * non-negativity: P[i,j]>=0
+      * consistency: every column of P sums to 1.0
+
+    Final  constraints  which  are  passed  to  the  underlying  optimizer are
+    calculated  as  intersection  of all present constraints. For example, you
+    may specify boundary constraint on P[0,0] and equality one:
+        0.1<=P[0,0]<=0.9
+        P[0,0]=0.5
+    Such  combination  of  constraints  will  be  silently  reduced  to  their
+    intersection, which is P[0,0]=0.5.
+
+    This  function  can  be  used  to  place bound   constraints  on arbitrary
+    subset  of  elements  of  P.  Set of constraints is specified by BndL/BndU
+    matrices, which may contain arbitrary combination  of  finite  numbers  or
+    infinities (like -INF<x<=0.5 or 0.1<=x<+INF).
+
+    You can also use MCPDAddBC() function which allows to ADD bound constraint
+    for one element of P without changing constraints for other elements.
+
+    These functions (MCPDSetBC and MCPDAddBC) interact as follows:
+    * there is internal matrix of bound constraints which is stored in the
+      MCPD solver
+    * MCPDSetBC() replaces this matrix by another one (SET)
+    * MCPDAddBC() modifies one element of this matrix and  leaves  other  ones
+      unchanged (ADD)
+    * thus  MCPDAddBC()  call  preserves  all  modifications  done by previous
+      calls,  while  MCPDSetBC()  completely discards all changes  done to the
+      equality constraints.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        BndL    -   lower bounds constraints, array[N,N]. Elements of BndL can
+                    be finite numbers or -INF.
+        BndU    -   upper bounds constraints, array[N,N]. Elements of BndU can
+                    be finite numbers or +INF.
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreater2(int nin, int nhid1, int nhid2, int nout, double a, double b, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdsetbc(mcpdstate s, double[,] bndl, double[,] bndu)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreater2(nin, nhid1, nhid2, nout, a, b, ensemblesize, ensemble.innerobj);
+
+        mcpd.mcpdsetbc(s.innerobj, bndl, bndu);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreateC0, but for ensembles.
+    This function is used to add bound constraints  on  the  elements  of  the
+    transition matrix P.
+
+    MCPD solver has four types of constraints which can be placed on P:
+    * user-specified equality constraints (optional)
+    * user-specified bound constraints (optional)
+    * user-specified general linear constraints (optional)
+    * basic constraints (always present):
+      * non-negativity: P[i,j]>=0
+      * consistency: every column of P sums to 1.0
+
+    Final  constraints  which  are  passed  to  the  underlying  optimizer are
+    calculated  as  intersection  of all present constraints. For example, you
+    may specify boundary constraint on P[0,0] and equality one:
+        0.1<=P[0,0]<=0.9
+        P[0,0]=0.5
+    Such  combination  of  constraints  will  be  silently  reduced  to  their
+    intersection, which is P[0,0]=0.5.
+
+    This  function  can  be  used to ADD bound constraint for one element of P
+    without changing constraints for other elements.
+
+    You  can  also  use  MCPDSetBC()  function  which  allows to  place  bound
+    constraints  on arbitrary subset of elements of P.   Set of constraints is
+    specified  by  BndL/BndU matrices, which may contain arbitrary combination
+    of finite numbers or infinities (like -INF<x<=0.5 or 0.1<=x<+INF).
+
+    These functions (MCPDSetBC and MCPDAddBC) interact as follows:
+    * there is internal matrix of bound constraints which is stored in the
+      MCPD solver
+    * MCPDSetBC() replaces this matrix by another one (SET)
+    * MCPDAddBC() modifies one element of this matrix and  leaves  other  ones
+      unchanged (ADD)
+    * thus  MCPDAddBC()  call  preserves  all  modifications  done by previous
+      calls,  while  MCPDSetBC()  completely discards all changes  done to the
+      equality constraints.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        I       -   row index of element being constrained
+        J       -   column index of element being constrained
+        BndL    -   lower bound
+        BndU    -   upper bound
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreatec0(int nin, int nout, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdaddbc(mcpdstate s, int i, int j, double bndl, double bndu)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreatec0(nin, nout, ensemblesize, ensemble.innerobj);
+
+        mcpd.mcpdaddbc(s.innerobj, i, j, bndl, bndu);
         return;
     }
 
     /*************************************************************************
-    Like MLPCreateC1, but for ensembles.
+    This function is used to set linear equality/inequality constraints on the
+    elements of the transition matrix P.
+
+    This function can be used to set one or several general linear constraints
+    on the elements of P. Two types of constraints are supported:
+    * equality constraints
+    * inequality constraints (both less-or-equal and greater-or-equal)
+
+    Coefficients  of  constraints  are  specified  by  matrix  C (one  of  the
+    parameters).  One  row  of  C  corresponds  to  one  constraint.   Because
+    transition  matrix P has N*N elements,  we  need  N*N columns to store all
+    coefficients  (they  are  stored row by row), and one more column to store
+    right part - hence C has N*N+1 columns.  Constraint  kind is stored in the
+    CT array.
+
+    Thus, I-th linear constraint is
+        P[0,0]*C[I,0] + P[0,1]*C[I,1] + .. + P[0,N-1]*C[I,N-1] +
+            + P[1,0]*C[I,N] + P[1,1]*C[I,N+1] + ... +
+            + P[N-1,N-1]*C[I,N*N-1]  ?=?  C[I,N*N]
+    where ?=? can be either "=" (CT[i]=0), "<=" (CT[i]<0) or ">=" (CT[i]>0).
+
+    Your constraint may involve only some subset of P (less than N*N elements).
+    For example it can be something like
+        P[0,0] + P[0,1] = 0.5
+    In this case you still should pass matrix  with N*N+1 columns, but all its
+    elements (except for C[0,0], C[0,1] and C[0,N*N-1]) will be zero.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        C       -   array[K,N*N+1] - coefficients of constraints
+                    (see above for complete description)
+        CT      -   array[K] - constraint types
+                    (see above for complete description)
+        K       -   number of equality/inequality constraints, K>=0:
+                    * if given, only leading K elements of C/CT are used
+                    * if not given, automatically determined from sizes of C/CT
 
       -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreatec1(int nin, int nhid, int nout, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdsetlc(mcpdstate s, double[,] c, int[] ct, int k)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreatec1(nin, nhid, nout, ensemblesize, ensemble.innerobj);
+
+        mcpd.mcpdsetlc(s.innerobj, c, ct, k);
         return;
     }
-
-    /*************************************************************************
-    Like MLPCreateC2, but for ensembles.
-
-      -- ALGLIB --
-         Copyright 18.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpecreatec2(int nin, int nhid1, int nhid2, int nout, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdsetlc(mcpdstate s, double[,] c, int[] ct)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreatec2(nin, nhid1, nhid2, nout, ensemblesize, ensemble.innerobj);
+        int k;
+        if( (ap.rows(c)!=ap.len(ct)))
+            throw new alglibexception("Error while calling 'mcpdsetlc': looks like one of arguments has wrong size");
+
+        k = ap.rows(c);
+        mcpd.mcpdsetlc(s.innerobj, c, ct, k);
+
         return;
     }
 
     /*************************************************************************
-    Creates ensemble from network. Only network geometry is copied.
+    This function allows to  tune  amount  of  Tikhonov  regularization  being
+    applied to your problem.
+
+    By default, regularizing term is equal to r*||P-prior_P||^2, where r is  a
+    small non-zero value,  P is transition matrix, prior_P is identity matrix,
+    ||X||^2 is a sum of squared elements of X.
+
+    This  function  allows  you to change coefficient r. You can  also  change
+    prior values with MCPDSetPrior() function.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        V       -   regularization  coefficient, finite non-negative value. It
+                    is  not  recommended  to specify zero value unless you are
+                    pretty sure that you want it.
 
       -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpecreatefromnetwork(multilayerperceptron network, int ensemblesize, out mlpensemble ensemble)
+    public static void mcpdsettikhonovregularizer(mcpdstate s, double v)
     {
-        ensemble = new mlpensemble();
-        mlpe.mlpecreatefromnetwork(network.innerobj, ensemblesize, ensemble.innerobj);
+
+        mcpd.mcpdsettikhonovregularizer(s.innerobj, v);
         return;
     }
 
     /*************************************************************************
-    Randomization of MLP ensemble
+    This  function  allows to set prior values used for regularization of your
+    problem.
+
+    By default, regularizing term is equal to r*||P-prior_P||^2, where r is  a
+    small non-zero value,  P is transition matrix, prior_P is identity matrix,
+    ||X||^2 is a sum of squared elements of X.
+
+    This  function  allows  you to change prior values prior_P. You  can  also
+    change r with MCPDSetTikhonovRegularizer() function.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        PP      -   array[N,N], matrix of prior values:
+                    1. elements must be real numbers from [0,1]
+                    2. columns must sum to 1.0.
+                    First property is checked (exception is thrown otherwise),
+                    while second one is not checked/enforced.
 
       -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlperandomize(mlpensemble ensemble)
+    public static void mcpdsetprior(mcpdstate s, double[,] pp)
     {
 
-        mlpe.mlperandomize(ensemble.innerobj);
+        mcpd.mcpdsetprior(s.innerobj, pp);
         return;
     }
 
     /*************************************************************************
-    Return ensemble properties (number of inputs and outputs).
+    This function is used to change prediction weights
+
+    MCPD solver scales prediction errors as follows
+        Error(P) = ||W*(y-P*x)||^2
+    where
+        x is a system state at time t
+        y is a system state at time t+1
+        P is a transition matrix
+        W is a diagonal scaling matrix
+
+    By default, weights are chosen in order  to  minimize  relative prediction
+    error instead of absolute one. For example, if one component of  state  is
+    about 0.5 in magnitude and another one is about 0.05, then algorithm  will
+    make corresponding weights equal to 2.0 and 20.0.
+
+    INPUT PARAMETERS:
+        S       -   solver
+        PW      -   array[N], weights:
+                    * must be non-negative values (exception will be thrown otherwise)
+                    * zero values will be replaced by automatically chosen values
 
       -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpeproperties(mlpensemble ensemble, out int nin, out int nout)
+    public static void mcpdsetpredictionweights(mcpdstate s, double[] pw)
     {
-        nin = 0;
-        nout = 0;
-        mlpe.mlpeproperties(ensemble.innerobj, ref nin, ref nout);
+
+        mcpd.mcpdsetpredictionweights(s.innerobj, pw);
         return;
     }
 
     /*************************************************************************
-    Return normalization type (whether ensemble is SOFTMAX-normalized or not).
+    This function is used to start solution of the MCPD problem.
+
+    After return from this function, you can use MCPDResults() to get solution
+    and completion code.
 
       -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static bool mlpeissoftmax(mlpensemble ensemble)
+    public static void mcpdsolve(mcpdstate s)
     {
 
-        bool result = mlpe.mlpeissoftmax(ensemble.innerobj);
-        return result;
-    }
-
-    /*************************************************************************
-    Procesing
-
-    INPUT PARAMETERS:
-        Ensemble-   neural networks ensemble
-        X       -   input vector,  array[0..NIn-1].
-        Y       -   (possibly) preallocated buffer; if size of Y is less than
-                    NOut, it will be reallocated. If it is large enough, it
-                    is NOT reallocated, so we can save some time on reallocation.
-
-
-    OUTPUT PARAMETERS:
-        Y       -   result. Regression estimate when solving regression  task,
-                    vector of posterior probabilities for classification task.
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpeprocess(mlpensemble ensemble, double[] x, ref double[] y)
-    {
-
-        mlpe.mlpeprocess(ensemble.innerobj, x, ref y);
+        mcpd.mcpdsolve(s.innerobj);
         return;
     }
 
     /*************************************************************************
-    'interactive'  variant  of  MLPEProcess  for  languages  like Python which
-    support constructs like "Y = MLPEProcess(LM,X)" and interactive mode of the
-    interpreter
-
-    This function allocates new array on each call,  so  it  is  significantly
-    slower than its 'non-interactive' counterpart, but it is  more  convenient
-    when you call it from command line.
+    MCPD results
+
+    INPUT PARAMETERS:
+        State   -   algorithm state
+
+    OUTPUT PARAMETERS:
+        P       -   array[N,N], transition matrix
+        Rep     -   optimization report. You should check Rep.TerminationType
+                    in  order  to  distinguish  successful  termination  from
+                    unsuccessful one. Speaking short, positive values  denote
+                    success, negative ones are failures.
+                    More information about fields of this  structure  can  be
+                    found in the comments on MCPDReport datatype.
+
 
       -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
+         Copyright 23.05.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void mlpeprocessi(mlpensemble ensemble, double[] x, out double[] y)
+    public static void mcpdresults(mcpdstate s, out double[,] p, out mcpdreport rep)
     {
-        y = new double[0];
-        mlpe.mlpeprocessi(ensemble.innerobj, x, ref y);
-        return;
-    }
-
-    /*************************************************************************
-    Relative classification error on the test set
-
-    INPUT PARAMETERS:
-        Ensemble-   ensemble
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        percent of incorrectly classified cases.
-        Works both for classifier betwork and for regression networks which
-    are used as classifiers.
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlperelclserror(mlpensemble ensemble, double[,] xy, int npoints)
-    {
-
-        double result = mlpe.mlperelclserror(ensemble.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Average cross-entropy (in bits per element) on the test set
-
-    INPUT PARAMETERS:
-        Ensemble-   ensemble
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        CrossEntropy/(NPoints*LN(2)).
-        Zero if ensemble solves regression task.
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlpeavgce(mlpensemble ensemble, double[,] xy, int npoints)
-    {
-
-        double result = mlpe.mlpeavgce(ensemble.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    RMS error on the test set
-
-    INPUT PARAMETERS:
-        Ensemble-   ensemble
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        root mean square error.
-        Its meaning for regression task is obvious. As for classification task
-    RMS error means error when estimating posterior probabilities.
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlpermserror(mlpensemble ensemble, double[,] xy, int npoints)
-    {
-
-        double result = mlpe.mlpermserror(ensemble.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Average error on the test set
-
-    INPUT PARAMETERS:
-        Ensemble-   ensemble
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        Its meaning for regression task is obvious. As for classification task
-    it means average error when estimating posterior probabilities.
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlpeavgerror(mlpensemble ensemble, double[,] xy, int npoints)
-    {
-
-        double result = mlpe.mlpeavgerror(ensemble.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Average relative error on the test set
-
-    INPUT PARAMETERS:
-        Ensemble-   ensemble
-        XY      -   test set
-        NPoints -   test set size
-
-    RESULT:
-        Its meaning for regression task is obvious. As for classification task
-    it means average relative error when estimating posterior probabilities.
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static double mlpeavgrelerror(mlpensemble ensemble, double[,] xy, int npoints)
-    {
-
-        double result = mlpe.mlpeavgrelerror(ensemble.innerobj, xy, npoints);
-        return result;
-    }
-
-    /*************************************************************************
-    Training neural networks ensemble using  bootstrap  aggregating (bagging).
-    Modified Levenberg-Marquardt algorithm is used as base training method.
-
-    INPUT PARAMETERS:
-        Ensemble    -   model with initialized geometry
-        XY          -   training set
-        NPoints     -   training set size
-        Decay       -   weight decay coefficient, >=0.001
-        Restarts    -   restarts, >0.
-
-    OUTPUT PARAMETERS:
-        Ensemble    -   trained model
-        Info        -   return code:
-                        * -2, if there is a point with class number
-                              outside of [0..NClasses-1].
-                        * -1, if incorrect parameters was passed
-                              (NPoints<0, Restarts<1).
-                        *  2, if task has been solved.
-        Rep         -   training report.
-        OOBErrors   -   out-of-bag generalization error estimate
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpebagginglm(mlpensemble ensemble, double[,] xy, int npoints, double decay, int restarts, out int info, out mlpreport rep, out mlpcvreport ooberrors)
-    {
-        info = 0;
-        rep = new mlpreport();
-        ooberrors = new mlpcvreport();
-        mlpe.mlpebagginglm(ensemble.innerobj, xy, npoints, decay, restarts, ref info, rep.innerobj, ooberrors.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Training neural networks ensemble using  bootstrap  aggregating (bagging).
-    L-BFGS algorithm is used as base training method.
-
-    INPUT PARAMETERS:
-        Ensemble    -   model with initialized geometry
-        XY          -   training set
-        NPoints     -   training set size
-        Decay       -   weight decay coefficient, >=0.001
-        Restarts    -   restarts, >0.
-        WStep       -   stopping criterion, same as in MLPTrainLBFGS
-        MaxIts      -   stopping criterion, same as in MLPTrainLBFGS
-
-    OUTPUT PARAMETERS:
-        Ensemble    -   trained model
-        Info        -   return code:
-                        * -8, if both WStep=0 and MaxIts=0
-                        * -2, if there is a point with class number
-                              outside of [0..NClasses-1].
-                        * -1, if incorrect parameters was passed
-                              (NPoints<0, Restarts<1).
-                        *  2, if task has been solved.
-        Rep         -   training report.
-        OOBErrors   -   out-of-bag generalization error estimate
-
-      -- ALGLIB --
-         Copyright 17.02.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpebagginglbfgs(mlpensemble ensemble, double[,] xy, int npoints, double decay, int restarts, double wstep, int maxits, out int info, out mlpreport rep, out mlpcvreport ooberrors)
-    {
-        info = 0;
-        rep = new mlpreport();
-        ooberrors = new mlpcvreport();
-        mlpe.mlpebagginglbfgs(ensemble.innerobj, xy, npoints, decay, restarts, wstep, maxits, ref info, rep.innerobj, ooberrors.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Training neural networks ensemble using early stopping.
-
-    INPUT PARAMETERS:
-        Ensemble    -   model with initialized geometry
-        XY          -   training set
-        NPoints     -   training set size
-        Decay       -   weight decay coefficient, >=0.001
-        Restarts    -   restarts, >0.
-
-    OUTPUT PARAMETERS:
-        Ensemble    -   trained model
-        Info        -   return code:
-                        * -2, if there is a point with class number
-                              outside of [0..NClasses-1].
-                        * -1, if incorrect parameters was passed
-                              (NPoints<0, Restarts<1).
-                        *  6, if task has been solved.
-        Rep         -   training report.
-        OOBErrors   -   out-of-bag generalization error estimate
-
-      -- ALGLIB --
-         Copyright 10.03.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mlpetraines(mlpensemble ensemble, double[,] xy, int npoints, double decay, int restarts, out int info, out mlpreport rep)
-    {
-        info = 0;
-        rep = new mlpreport();
-        mlpe.mlpetraines(ensemble.innerobj, xy, npoints, decay, restarts, ref info, rep.innerobj);
+        p = new double[0,0];
+        rep = new mcpdreport();
+        mcpd.mcpdresults(s.innerobj, ref p, rep.innerobj);
         return;
     }
@@ -2518,4 +3218,503 @@
 
     /*************************************************************************
+    Neural networks ensemble
+    *************************************************************************/
+    public class mlpensemble
+    {
+        //
+        // Public declarations
+        //
+
+        public mlpensemble()
+        {
+            _innerobj = new mlpe.mlpensemble();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private mlpe.mlpensemble _innerobj;
+        public mlpe.mlpensemble innerobj { get { return _innerobj; } }
+        public mlpensemble(mlpe.mlpensemble obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+    /*************************************************************************
+    Like MLPCreate0, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreate0(int nin, int nout, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreate0(nin, nout, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreate1, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreate1(int nin, int nhid, int nout, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreate1(nin, nhid, nout, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreate2, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreate2(int nin, int nhid1, int nhid2, int nout, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreate2(nin, nhid1, nhid2, nout, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateB0, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreateb0(int nin, int nout, double b, double d, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreateb0(nin, nout, b, d, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateB1, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreateb1(int nin, int nhid, int nout, double b, double d, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreateb1(nin, nhid, nout, b, d, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateB2, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreateb2(int nin, int nhid1, int nhid2, int nout, double b, double d, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreateb2(nin, nhid1, nhid2, nout, b, d, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateR0, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreater0(int nin, int nout, double a, double b, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreater0(nin, nout, a, b, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateR1, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreater1(int nin, int nhid, int nout, double a, double b, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreater1(nin, nhid, nout, a, b, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateR2, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreater2(int nin, int nhid1, int nhid2, int nout, double a, double b, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreater2(nin, nhid1, nhid2, nout, a, b, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateC0, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreatec0(int nin, int nout, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreatec0(nin, nout, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateC1, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreatec1(int nin, int nhid, int nout, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreatec1(nin, nhid, nout, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Like MLPCreateC2, but for ensembles.
+
+      -- ALGLIB --
+         Copyright 18.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreatec2(int nin, int nhid1, int nhid2, int nout, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreatec2(nin, nhid1, nhid2, nout, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Creates ensemble from network. Only network geometry is copied.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpecreatefromnetwork(multilayerperceptron network, int ensemblesize, out mlpensemble ensemble)
+    {
+        ensemble = new mlpensemble();
+        mlpe.mlpecreatefromnetwork(network.innerobj, ensemblesize, ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Randomization of MLP ensemble
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlperandomize(mlpensemble ensemble)
+    {
+
+        mlpe.mlperandomize(ensemble.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Return ensemble properties (number of inputs and outputs).
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpeproperties(mlpensemble ensemble, out int nin, out int nout)
+    {
+        nin = 0;
+        nout = 0;
+        mlpe.mlpeproperties(ensemble.innerobj, ref nin, ref nout);
+        return;
+    }
+
+    /*************************************************************************
+    Return normalization type (whether ensemble is SOFTMAX-normalized or not).
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static bool mlpeissoftmax(mlpensemble ensemble)
+    {
+
+        bool result = mlpe.mlpeissoftmax(ensemble.innerobj);
+        return result;
+    }
+
+    /*************************************************************************
+    Procesing
+
+    INPUT PARAMETERS:
+        Ensemble-   neural networks ensemble
+        X       -   input vector,  array[0..NIn-1].
+        Y       -   (possibly) preallocated buffer; if size of Y is less than
+                    NOut, it will be reallocated. If it is large enough, it
+                    is NOT reallocated, so we can save some time on reallocation.
+
+
+    OUTPUT PARAMETERS:
+        Y       -   result. Regression estimate when solving regression  task,
+                    vector of posterior probabilities for classification task.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpeprocess(mlpensemble ensemble, double[] x, ref double[] y)
+    {
+
+        mlpe.mlpeprocess(ensemble.innerobj, x, ref y);
+        return;
+    }
+
+    /*************************************************************************
+    'interactive'  variant  of  MLPEProcess  for  languages  like Python which
+    support constructs like "Y = MLPEProcess(LM,X)" and interactive mode of the
+    interpreter
+
+    This function allocates new array on each call,  so  it  is  significantly
+    slower than its 'non-interactive' counterpart, but it is  more  convenient
+    when you call it from command line.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpeprocessi(mlpensemble ensemble, double[] x, out double[] y)
+    {
+        y = new double[0];
+        mlpe.mlpeprocessi(ensemble.innerobj, x, ref y);
+        return;
+    }
+
+    /*************************************************************************
+    Relative classification error on the test set
+
+    INPUT PARAMETERS:
+        Ensemble-   ensemble
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        percent of incorrectly classified cases.
+        Works both for classifier betwork and for regression networks which
+    are used as classifiers.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlperelclserror(mlpensemble ensemble, double[,] xy, int npoints)
+    {
+
+        double result = mlpe.mlperelclserror(ensemble.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Average cross-entropy (in bits per element) on the test set
+
+    INPUT PARAMETERS:
+        Ensemble-   ensemble
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        CrossEntropy/(NPoints*LN(2)).
+        Zero if ensemble solves regression task.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpeavgce(mlpensemble ensemble, double[,] xy, int npoints)
+    {
+
+        double result = mlpe.mlpeavgce(ensemble.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    RMS error on the test set
+
+    INPUT PARAMETERS:
+        Ensemble-   ensemble
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        root mean square error.
+        Its meaning for regression task is obvious. As for classification task
+    RMS error means error when estimating posterior probabilities.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpermserror(mlpensemble ensemble, double[,] xy, int npoints)
+    {
+
+        double result = mlpe.mlpermserror(ensemble.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Average error on the test set
+
+    INPUT PARAMETERS:
+        Ensemble-   ensemble
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        Its meaning for regression task is obvious. As for classification task
+    it means average error when estimating posterior probabilities.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpeavgerror(mlpensemble ensemble, double[,] xy, int npoints)
+    {
+
+        double result = mlpe.mlpeavgerror(ensemble.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Average relative error on the test set
+
+    INPUT PARAMETERS:
+        Ensemble-   ensemble
+        XY      -   test set
+        NPoints -   test set size
+
+    RESULT:
+        Its meaning for regression task is obvious. As for classification task
+    it means average relative error when estimating posterior probabilities.
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static double mlpeavgrelerror(mlpensemble ensemble, double[,] xy, int npoints)
+    {
+
+        double result = mlpe.mlpeavgrelerror(ensemble.innerobj, xy, npoints);
+        return result;
+    }
+
+    /*************************************************************************
+    Training neural networks ensemble using  bootstrap  aggregating (bagging).
+    Modified Levenberg-Marquardt algorithm is used as base training method.
+
+    INPUT PARAMETERS:
+        Ensemble    -   model with initialized geometry
+        XY          -   training set
+        NPoints     -   training set size
+        Decay       -   weight decay coefficient, >=0.001
+        Restarts    -   restarts, >0.
+
+    OUTPUT PARAMETERS:
+        Ensemble    -   trained model
+        Info        -   return code:
+                        * -2, if there is a point with class number
+                              outside of [0..NClasses-1].
+                        * -1, if incorrect parameters was passed
+                              (NPoints<0, Restarts<1).
+                        *  2, if task has been solved.
+        Rep         -   training report.
+        OOBErrors   -   out-of-bag generalization error estimate
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpebagginglm(mlpensemble ensemble, double[,] xy, int npoints, double decay, int restarts, out int info, out mlpreport rep, out mlpcvreport ooberrors)
+    {
+        info = 0;
+        rep = new mlpreport();
+        ooberrors = new mlpcvreport();
+        mlpe.mlpebagginglm(ensemble.innerobj, xy, npoints, decay, restarts, ref info, rep.innerobj, ooberrors.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Training neural networks ensemble using  bootstrap  aggregating (bagging).
+    L-BFGS algorithm is used as base training method.
+
+    INPUT PARAMETERS:
+        Ensemble    -   model with initialized geometry
+        XY          -   training set
+        NPoints     -   training set size
+        Decay       -   weight decay coefficient, >=0.001
+        Restarts    -   restarts, >0.
+        WStep       -   stopping criterion, same as in MLPTrainLBFGS
+        MaxIts      -   stopping criterion, same as in MLPTrainLBFGS
+
+    OUTPUT PARAMETERS:
+        Ensemble    -   trained model
+        Info        -   return code:
+                        * -8, if both WStep=0 and MaxIts=0
+                        * -2, if there is a point with class number
+                              outside of [0..NClasses-1].
+                        * -1, if incorrect parameters was passed
+                              (NPoints<0, Restarts<1).
+                        *  2, if task has been solved.
+        Rep         -   training report.
+        OOBErrors   -   out-of-bag generalization error estimate
+
+      -- ALGLIB --
+         Copyright 17.02.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpebagginglbfgs(mlpensemble ensemble, double[,] xy, int npoints, double decay, int restarts, double wstep, int maxits, out int info, out mlpreport rep, out mlpcvreport ooberrors)
+    {
+        info = 0;
+        rep = new mlpreport();
+        ooberrors = new mlpcvreport();
+        mlpe.mlpebagginglbfgs(ensemble.innerobj, xy, npoints, decay, restarts, wstep, maxits, ref info, rep.innerobj, ooberrors.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Training neural networks ensemble using early stopping.
+
+    INPUT PARAMETERS:
+        Ensemble    -   model with initialized geometry
+        XY          -   training set
+        NPoints     -   training set size
+        Decay       -   weight decay coefficient, >=0.001
+        Restarts    -   restarts, >0.
+
+    OUTPUT PARAMETERS:
+        Ensemble    -   trained model
+        Info        -   return code:
+                        * -2, if there is a point with class number
+                              outside of [0..NClasses-1].
+                        * -1, if incorrect parameters was passed
+                              (NPoints<0, Restarts<1).
+                        *  6, if task has been solved.
+        Rep         -   training report.
+        OOBErrors   -   out-of-bag generalization error estimate
+
+      -- ALGLIB --
+         Copyright 10.03.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mlpetraines(mlpensemble ensemble, double[,] xy, int npoints, double decay, int restarts, out int info, out mlpreport rep)
+    {
+        info = 0;
+        rep = new mlpreport();
+        mlpe.mlpetraines(ensemble.innerobj, xy, npoints, decay, restarts, ref info, rep.innerobj);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
     Principal components analysis
 
@@ -2558,4 +3757,1472 @@
 public partial class alglib
 {
+    public class bdss
+    {
+        public class cvreport
+        {
+            public double relclserror;
+            public double avgce;
+            public double rmserror;
+            public double avgerror;
+            public double avgrelerror;
+        };
+
+
+
+
+        /*************************************************************************
+        This set of routines (DSErrAllocate, DSErrAccumulate, DSErrFinish)
+        calculates different error functions (classification error, cross-entropy,
+        rms, avg, avg.rel errors).
+
+        1. DSErrAllocate prepares buffer.
+        2. DSErrAccumulate accumulates individual errors:
+            * Y contains predicted output (posterior probabilities for classification)
+            * DesiredY contains desired output (class number for classification)
+        3. DSErrFinish outputs results:
+           * Buf[0] contains relative classification error (zero for regression tasks)
+           * Buf[1] contains avg. cross-entropy (zero for regression tasks)
+           * Buf[2] contains rms error (regression, classification)
+           * Buf[3] contains average error (regression, classification)
+           * Buf[4] contains average relative error (regression, classification)
+           
+        NOTES(1):
+            "NClasses>0" means that we have classification task.
+            "NClasses<0" means regression task with -NClasses real outputs.
+
+        NOTES(2):
+            rms. avg, avg.rel errors for classification tasks are interpreted as
+            errors in posterior probabilities with respect to probabilities given
+            by training/test set.
+
+          -- ALGLIB --
+             Copyright 11.01.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dserrallocate(int nclasses,
+            ref double[] buf)
+        {
+            buf = new double[0];
+
+            buf = new double[7+1];
+            buf[0] = 0;
+            buf[1] = 0;
+            buf[2] = 0;
+            buf[3] = 0;
+            buf[4] = 0;
+            buf[5] = nclasses;
+            buf[6] = 0;
+            buf[7] = 0;
+        }
+
+
+        /*************************************************************************
+        See DSErrAllocate for comments on this routine.
+
+          -- ALGLIB --
+             Copyright 11.01.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dserraccumulate(ref double[] buf,
+            double[] y,
+            double[] desiredy)
+        {
+            int nclasses = 0;
+            int nout = 0;
+            int offs = 0;
+            int mmax = 0;
+            int rmax = 0;
+            int j = 0;
+            double v = 0;
+            double ev = 0;
+
+            offs = 5;
+            nclasses = (int)Math.Round(buf[offs]);
+            if( nclasses>0 )
+            {
+                
+                //
+                // Classification
+                //
+                rmax = (int)Math.Round(desiredy[0]);
+                mmax = 0;
+                for(j=1; j<=nclasses-1; j++)
+                {
+                    if( (double)(y[j])>(double)(y[mmax]) )
+                    {
+                        mmax = j;
+                    }
+                }
+                if( mmax!=rmax )
+                {
+                    buf[0] = buf[0]+1;
+                }
+                if( (double)(y[rmax])>(double)(0) )
+                {
+                    buf[1] = buf[1]-Math.Log(y[rmax]);
+                }
+                else
+                {
+                    buf[1] = buf[1]+Math.Log(math.maxrealnumber);
+                }
+                for(j=0; j<=nclasses-1; j++)
+                {
+                    v = y[j];
+                    if( j==rmax )
+                    {
+                        ev = 1;
+                    }
+                    else
+                    {
+                        ev = 0;
+                    }
+                    buf[2] = buf[2]+math.sqr(v-ev);
+                    buf[3] = buf[3]+Math.Abs(v-ev);
+                    if( (double)(ev)!=(double)(0) )
+                    {
+                        buf[4] = buf[4]+Math.Abs((v-ev)/ev);
+                        buf[offs+2] = buf[offs+2]+1;
+                    }
+                }
+                buf[offs+1] = buf[offs+1]+1;
+            }
+            else
+            {
+                
+                //
+                // Regression
+                //
+                nout = -nclasses;
+                rmax = 0;
+                for(j=1; j<=nout-1; j++)
+                {
+                    if( (double)(desiredy[j])>(double)(desiredy[rmax]) )
+                    {
+                        rmax = j;
+                    }
+                }
+                mmax = 0;
+                for(j=1; j<=nout-1; j++)
+                {
+                    if( (double)(y[j])>(double)(y[mmax]) )
+                    {
+                        mmax = j;
+                    }
+                }
+                if( mmax!=rmax )
+                {
+                    buf[0] = buf[0]+1;
+                }
+                for(j=0; j<=nout-1; j++)
+                {
+                    v = y[j];
+                    ev = desiredy[j];
+                    buf[2] = buf[2]+math.sqr(v-ev);
+                    buf[3] = buf[3]+Math.Abs(v-ev);
+                    if( (double)(ev)!=(double)(0) )
+                    {
+                        buf[4] = buf[4]+Math.Abs((v-ev)/ev);
+                        buf[offs+2] = buf[offs+2]+1;
+                    }
+                }
+                buf[offs+1] = buf[offs+1]+1;
+            }
+        }
+
+
+        /*************************************************************************
+        See DSErrAllocate for comments on this routine.
+
+          -- ALGLIB --
+             Copyright 11.01.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dserrfinish(ref double[] buf)
+        {
+            int nout = 0;
+            int offs = 0;
+
+            offs = 5;
+            nout = Math.Abs((int)Math.Round(buf[offs]));
+            if( (double)(buf[offs+1])!=(double)(0) )
+            {
+                buf[0] = buf[0]/buf[offs+1];
+                buf[1] = buf[1]/buf[offs+1];
+                buf[2] = Math.Sqrt(buf[2]/(nout*buf[offs+1]));
+                buf[3] = buf[3]/(nout*buf[offs+1]);
+            }
+            if( (double)(buf[offs+2])!=(double)(0) )
+            {
+                buf[4] = buf[4]/buf[offs+2];
+            }
+        }
+
+
+        /*************************************************************************
+
+          -- ALGLIB --
+             Copyright 19.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dsnormalize(ref double[,] xy,
+            int npoints,
+            int nvars,
+            ref int info,
+            ref double[] means,
+            ref double[] sigmas)
+        {
+            int i = 0;
+            int j = 0;
+            double[] tmp = new double[0];
+            double mean = 0;
+            double variance = 0;
+            double skewness = 0;
+            double kurtosis = 0;
+            int i_ = 0;
+
+            info = 0;
+            means = new double[0];
+            sigmas = new double[0];
+
+            
+            //
+            // Test parameters
+            //
+            if( npoints<=0 | nvars<1 )
+            {
+                info = -1;
+                return;
+            }
+            info = 1;
+            
+            //
+            // Standartization
+            //
+            means = new double[nvars-1+1];
+            sigmas = new double[nvars-1+1];
+            tmp = new double[npoints-1+1];
+            for(j=0; j<=nvars-1; j++)
+            {
+                for(i_=0; i_<=npoints-1;i_++)
+                {
+                    tmp[i_] = xy[i_,j];
+                }
+                basestat.samplemoments(tmp, npoints, ref mean, ref variance, ref skewness, ref kurtosis);
+                means[j] = mean;
+                sigmas[j] = Math.Sqrt(variance);
+                if( (double)(sigmas[j])==(double)(0) )
+                {
+                    sigmas[j] = 1;
+                }
+                for(i=0; i<=npoints-1; i++)
+                {
+                    xy[i,j] = (xy[i,j]-means[j])/sigmas[j];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+
+          -- ALGLIB --
+             Copyright 19.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dsnormalizec(double[,] xy,
+            int npoints,
+            int nvars,
+            ref int info,
+            ref double[] means,
+            ref double[] sigmas)
+        {
+            int j = 0;
+            double[] tmp = new double[0];
+            double mean = 0;
+            double variance = 0;
+            double skewness = 0;
+            double kurtosis = 0;
+            int i_ = 0;
+
+            info = 0;
+            means = new double[0];
+            sigmas = new double[0];
+
+            
+            //
+            // Test parameters
+            //
+            if( npoints<=0 | nvars<1 )
+            {
+                info = -1;
+                return;
+            }
+            info = 1;
+            
+            //
+            // Standartization
+            //
+            means = new double[nvars-1+1];
+            sigmas = new double[nvars-1+1];
+            tmp = new double[npoints-1+1];
+            for(j=0; j<=nvars-1; j++)
+            {
+                for(i_=0; i_<=npoints-1;i_++)
+                {
+                    tmp[i_] = xy[i_,j];
+                }
+                basestat.samplemoments(tmp, npoints, ref mean, ref variance, ref skewness, ref kurtosis);
+                means[j] = mean;
+                sigmas[j] = Math.Sqrt(variance);
+                if( (double)(sigmas[j])==(double)(0) )
+                {
+                    sigmas[j] = 1;
+                }
+            }
+        }
+
+
+        /*************************************************************************
+
+          -- ALGLIB --
+             Copyright 19.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static double dsgetmeanmindistance(double[,] xy,
+            int npoints,
+            int nvars)
+        {
+            double result = 0;
+            int i = 0;
+            int j = 0;
+            double[] tmp = new double[0];
+            double[] tmp2 = new double[0];
+            double v = 0;
+            int i_ = 0;
+
+            
+            //
+            // Test parameters
+            //
+            if( npoints<=0 | nvars<1 )
+            {
+                result = 0;
+                return result;
+            }
+            
+            //
+            // Process
+            //
+            tmp = new double[npoints-1+1];
+            for(i=0; i<=npoints-1; i++)
+            {
+                tmp[i] = math.maxrealnumber;
+            }
+            tmp2 = new double[nvars-1+1];
+            for(i=0; i<=npoints-1; i++)
+            {
+                for(j=i+1; j<=npoints-1; j++)
+                {
+                    for(i_=0; i_<=nvars-1;i_++)
+                    {
+                        tmp2[i_] = xy[i,i_];
+                    }
+                    for(i_=0; i_<=nvars-1;i_++)
+                    {
+                        tmp2[i_] = tmp2[i_] - xy[j,i_];
+                    }
+                    v = 0.0;
+                    for(i_=0; i_<=nvars-1;i_++)
+                    {
+                        v += tmp2[i_]*tmp2[i_];
+                    }
+                    v = Math.Sqrt(v);
+                    tmp[i] = Math.Min(tmp[i], v);
+                    tmp[j] = Math.Min(tmp[j], v);
+                }
+            }
+            result = 0;
+            for(i=0; i<=npoints-1; i++)
+            {
+                result = result+tmp[i]/npoints;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+
+          -- ALGLIB --
+             Copyright 19.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dstie(ref double[] a,
+            int n,
+            ref int[] ties,
+            ref int tiecount,
+            ref int[] p1,
+            ref int[] p2)
+        {
+            int i = 0;
+            int k = 0;
+            int[] tmp = new int[0];
+
+            ties = new int[0];
+            tiecount = 0;
+            p1 = new int[0];
+            p2 = new int[0];
+
+            
+            //
+            // Special case
+            //
+            if( n<=0 )
+            {
+                tiecount = 0;
+                return;
+            }
+            
+            //
+            // Sort A
+            //
+            tsort.tagsort(ref a, n, ref p1, ref p2);
+            
+            //
+            // Process ties
+            //
+            tiecount = 1;
+            for(i=1; i<=n-1; i++)
+            {
+                if( (double)(a[i])!=(double)(a[i-1]) )
+                {
+                    tiecount = tiecount+1;
+                }
+            }
+            ties = new int[tiecount+1];
+            ties[0] = 0;
+            k = 1;
+            for(i=1; i<=n-1; i++)
+            {
+                if( (double)(a[i])!=(double)(a[i-1]) )
+                {
+                    ties[k] = i;
+                    k = k+1;
+                }
+            }
+            ties[tiecount] = n;
+        }
+
+
+        /*************************************************************************
+
+          -- ALGLIB --
+             Copyright 11.12.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dstiefasti(ref double[] a,
+            ref int[] b,
+            int n,
+            ref int[] ties,
+            ref int tiecount,
+            ref double[] bufr,
+            ref int[] bufi)
+        {
+            int i = 0;
+            int k = 0;
+            int[] tmp = new int[0];
+
+            tiecount = 0;
+
+            
+            //
+            // Special case
+            //
+            if( n<=0 )
+            {
+                tiecount = 0;
+                return;
+            }
+            
+            //
+            // Sort A
+            //
+            tsort.tagsortfasti(ref a, ref b, ref bufr, ref bufi, n);
+            
+            //
+            // Process ties
+            //
+            ties[0] = 0;
+            k = 1;
+            for(i=1; i<=n-1; i++)
+            {
+                if( (double)(a[i])!=(double)(a[i-1]) )
+                {
+                    ties[k] = i;
+                    k = k+1;
+                }
+            }
+            ties[k] = n;
+            tiecount = k;
+        }
+
+
+        /*************************************************************************
+        Optimal binary classification
+
+        Algorithms finds optimal (=with minimal cross-entropy) binary partition.
+        Internal subroutine.
+
+        INPUT PARAMETERS:
+            A       -   array[0..N-1], variable
+            C       -   array[0..N-1], class numbers (0 or 1).
+            N       -   array size
+
+        OUTPUT PARAMETERS:
+            Info    -   completetion code:
+                        * -3, all values of A[] are same (partition is impossible)
+                        * -2, one of C[] is incorrect (<0, >1)
+                        * -1, incorrect pararemets were passed (N<=0).
+                        *  1, OK
+            Threshold-  partiton boundary. Left part contains values which are
+                        strictly less than Threshold. Right part contains values
+                        which are greater than or equal to Threshold.
+            PAL, PBL-   probabilities P(0|v<Threshold) and P(1|v<Threshold)
+            PAR, PBR-   probabilities P(0|v>=Threshold) and P(1|v>=Threshold)
+            CVE     -   cross-validation estimate of cross-entropy
+
+          -- ALGLIB --
+             Copyright 22.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dsoptimalsplit2(double[] a,
+            int[] c,
+            int n,
+            ref int info,
+            ref double threshold,
+            ref double pal,
+            ref double pbl,
+            ref double par,
+            ref double pbr,
+            ref double cve)
+        {
+            int i = 0;
+            int t = 0;
+            double s = 0;
+            int[] ties = new int[0];
+            int tiecount = 0;
+            int[] p1 = new int[0];
+            int[] p2 = new int[0];
+            int k = 0;
+            int koptimal = 0;
+            double pak = 0;
+            double pbk = 0;
+            double cvoptimal = 0;
+            double cv = 0;
+
+            a = (double[])a.Clone();
+            c = (int[])c.Clone();
+            info = 0;
+            threshold = 0;
+            pal = 0;
+            pbl = 0;
+            par = 0;
+            pbr = 0;
+            cve = 0;
+
+            
+            //
+            // Test for errors in inputs
+            //
+            if( n<=0 )
+            {
+                info = -1;
+                return;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                if( c[i]!=0 & c[i]!=1 )
+                {
+                    info = -2;
+                    return;
+                }
+            }
+            info = 1;
+            
+            //
+            // Tie
+            //
+            dstie(ref a, n, ref ties, ref tiecount, ref p1, ref p2);
+            for(i=0; i<=n-1; i++)
+            {
+                if( p2[i]!=i )
+                {
+                    t = c[i];
+                    c[i] = c[p2[i]];
+                    c[p2[i]] = t;
+                }
+            }
+            
+            //
+            // Special case: number of ties is 1.
+            //
+            // NOTE: we assume that P[i,j] equals to 0 or 1,
+            //       intermediate values are not allowed.
+            //
+            if( tiecount==1 )
+            {
+                info = -3;
+                return;
+            }
+            
+            //
+            // General case, number of ties > 1
+            //
+            // NOTE: we assume that P[i,j] equals to 0 or 1,
+            //       intermediate values are not allowed.
+            //
+            pal = 0;
+            pbl = 0;
+            par = 0;
+            pbr = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                if( c[i]==0 )
+                {
+                    par = par+1;
+                }
+                if( c[i]==1 )
+                {
+                    pbr = pbr+1;
+                }
+            }
+            koptimal = -1;
+            cvoptimal = math.maxrealnumber;
+            for(k=0; k<=tiecount-2; k++)
+            {
+                
+                //
+                // first, obtain information about K-th tie which is
+                // moved from R-part to L-part
+                //
+                pak = 0;
+                pbk = 0;
+                for(i=ties[k]; i<=ties[k+1]-1; i++)
+                {
+                    if( c[i]==0 )
+                    {
+                        pak = pak+1;
+                    }
+                    if( c[i]==1 )
+                    {
+                        pbk = pbk+1;
+                    }
+                }
+                
+                //
+                // Calculate cross-validation CE
+                //
+                cv = 0;
+                cv = cv-xlny(pal+pak, (pal+pak)/(pal+pak+pbl+pbk+1));
+                cv = cv-xlny(pbl+pbk, (pbl+pbk)/(pal+pak+1+pbl+pbk));
+                cv = cv-xlny(par-pak, (par-pak)/(par-pak+pbr-pbk+1));
+                cv = cv-xlny(pbr-pbk, (pbr-pbk)/(par-pak+1+pbr-pbk));
+                
+                //
+                // Compare with best
+                //
+                if( (double)(cv)<(double)(cvoptimal) )
+                {
+                    cvoptimal = cv;
+                    koptimal = k;
+                }
+                
+                //
+                // update
+                //
+                pal = pal+pak;
+                pbl = pbl+pbk;
+                par = par-pak;
+                pbr = pbr-pbk;
+            }
+            cve = cvoptimal;
+            threshold = 0.5*(a[ties[koptimal]]+a[ties[koptimal+1]]);
+            pal = 0;
+            pbl = 0;
+            par = 0;
+            pbr = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                if( (double)(a[i])<(double)(threshold) )
+                {
+                    if( c[i]==0 )
+                    {
+                        pal = pal+1;
+                    }
+                    else
+                    {
+                        pbl = pbl+1;
+                    }
+                }
+                else
+                {
+                    if( c[i]==0 )
+                    {
+                        par = par+1;
+                    }
+                    else
+                    {
+                        pbr = pbr+1;
+                    }
+                }
+            }
+            s = pal+pbl;
+            pal = pal/s;
+            pbl = pbl/s;
+            s = par+pbr;
+            par = par/s;
+            pbr = pbr/s;
+        }
+
+
+        /*************************************************************************
+        Optimal partition, internal subroutine. Fast version.
+
+        Accepts:
+            A       array[0..N-1]       array of attributes     array[0..N-1]
+            C       array[0..N-1]       array of class labels
+            TiesBuf array[0..N]         temporaries (ties)
+            CntBuf  array[0..2*NC-1]    temporaries (counts)
+            Alpha                       centering factor (0<=alpha<=1, recommended value - 0.05)
+            BufR    array[0..N-1]       temporaries
+            BufI    array[0..N-1]       temporaries
+
+        Output:
+            Info    error code (">0"=OK, "<0"=bad)
+            RMS     training set RMS error
+            CVRMS   leave-one-out RMS error
+            
+        Note:
+            content of all arrays is changed by subroutine;
+            it doesn't allocate temporaries.
+
+          -- ALGLIB --
+             Copyright 11.12.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dsoptimalsplit2fast(ref double[] a,
+            ref int[] c,
+            ref int[] tiesbuf,
+            ref int[] cntbuf,
+            ref double[] bufr,
+            ref int[] bufi,
+            int n,
+            int nc,
+            double alpha,
+            ref int info,
+            ref double threshold,
+            ref double rms,
+            ref double cvrms)
+        {
+            int i = 0;
+            int k = 0;
+            int cl = 0;
+            int tiecount = 0;
+            double cbest = 0;
+            double cc = 0;
+            int koptimal = 0;
+            int sl = 0;
+            int sr = 0;
+            double v = 0;
+            double w = 0;
+            double x = 0;
+
+            info = 0;
+            threshold = 0;
+            rms = 0;
+            cvrms = 0;
+
+            
+            //
+            // Test for errors in inputs
+            //
+            if( n<=0 | nc<2 )
+            {
+                info = -1;
+                return;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                if( c[i]<0 | c[i]>=nc )
+                {
+                    info = -2;
+                    return;
+                }
+            }
+            info = 1;
+            
+            //
+            // Tie
+            //
+            dstiefasti(ref a, ref c, n, ref tiesbuf, ref tiecount, ref bufr, ref bufi);
+            
+            //
+            // Special case: number of ties is 1.
+            //
+            if( tiecount==1 )
+            {
+                info = -3;
+                return;
+            }
+            
+            //
+            // General case, number of ties > 1
+            //
+            for(i=0; i<=2*nc-1; i++)
+            {
+                cntbuf[i] = 0;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                cntbuf[nc+c[i]] = cntbuf[nc+c[i]]+1;
+            }
+            koptimal = -1;
+            threshold = a[n-1];
+            cbest = math.maxrealnumber;
+            sl = 0;
+            sr = n;
+            for(k=0; k<=tiecount-2; k++)
+            {
+                
+                //
+                // first, move Kth tie from right to left
+                //
+                for(i=tiesbuf[k]; i<=tiesbuf[k+1]-1; i++)
+                {
+                    cl = c[i];
+                    cntbuf[cl] = cntbuf[cl]+1;
+                    cntbuf[nc+cl] = cntbuf[nc+cl]-1;
+                }
+                sl = sl+(tiesbuf[k+1]-tiesbuf[k]);
+                sr = sr-(tiesbuf[k+1]-tiesbuf[k]);
+                
+                //
+                // Calculate RMS error
+                //
+                v = 0;
+                for(i=0; i<=nc-1; i++)
+                {
+                    w = cntbuf[i];
+                    v = v+w*math.sqr(w/sl-1);
+                    v = v+(sl-w)*math.sqr(w/sl);
+                    w = cntbuf[nc+i];
+                    v = v+w*math.sqr(w/sr-1);
+                    v = v+(sr-w)*math.sqr(w/sr);
+                }
+                v = Math.Sqrt(v/(nc*n));
+                
+                //
+                // Compare with best
+                //
+                x = (double)(2*sl)/(double)(sl+sr)-1;
+                cc = v*(1-alpha+alpha*math.sqr(x));
+                if( (double)(cc)<(double)(cbest) )
+                {
+                    
+                    //
+                    // store split
+                    //
+                    rms = v;
+                    koptimal = k;
+                    cbest = cc;
+                    
+                    //
+                    // calculate CVRMS error
+                    //
+                    cvrms = 0;
+                    for(i=0; i<=nc-1; i++)
+                    {
+                        if( sl>1 )
+                        {
+                            w = cntbuf[i];
+                            cvrms = cvrms+w*math.sqr((w-1)/(sl-1)-1);
+                            cvrms = cvrms+(sl-w)*math.sqr(w/(sl-1));
+                        }
+                        else
+                        {
+                            w = cntbuf[i];
+                            cvrms = cvrms+w*math.sqr((double)1/(double)nc-1);
+                            cvrms = cvrms+(sl-w)*math.sqr((double)1/(double)nc);
+                        }
+                        if( sr>1 )
+                        {
+                            w = cntbuf[nc+i];
+                            cvrms = cvrms+w*math.sqr((w-1)/(sr-1)-1);
+                            cvrms = cvrms+(sr-w)*math.sqr(w/(sr-1));
+                        }
+                        else
+                        {
+                            w = cntbuf[nc+i];
+                            cvrms = cvrms+w*math.sqr((double)1/(double)nc-1);
+                            cvrms = cvrms+(sr-w)*math.sqr((double)1/(double)nc);
+                        }
+                    }
+                    cvrms = Math.Sqrt(cvrms/(nc*n));
+                }
+            }
+            
+            //
+            // Calculate threshold.
+            // Code is a bit complicated because there can be such
+            // numbers that 0.5(A+B) equals to A or B (if A-B=epsilon)
+            //
+            threshold = 0.5*(a[tiesbuf[koptimal]]+a[tiesbuf[koptimal+1]]);
+            if( (double)(threshold)<=(double)(a[tiesbuf[koptimal]]) )
+            {
+                threshold = a[tiesbuf[koptimal+1]];
+            }
+        }
+
+
+        /*************************************************************************
+        Automatic non-optimal discretization, internal subroutine.
+
+          -- ALGLIB --
+             Copyright 22.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dssplitk(double[] a,
+            int[] c,
+            int n,
+            int nc,
+            int kmax,
+            ref int info,
+            ref double[] thresholds,
+            ref int ni,
+            ref double cve)
+        {
+            int i = 0;
+            int j = 0;
+            int j1 = 0;
+            int k = 0;
+            int[] ties = new int[0];
+            int tiecount = 0;
+            int[] p1 = new int[0];
+            int[] p2 = new int[0];
+            int[] cnt = new int[0];
+            double v2 = 0;
+            int bestk = 0;
+            double bestcve = 0;
+            int[] bestsizes = new int[0];
+            double curcve = 0;
+            int[] cursizes = new int[0];
+
+            a = (double[])a.Clone();
+            c = (int[])c.Clone();
+            info = 0;
+            thresholds = new double[0];
+            ni = 0;
+            cve = 0;
+
+            
+            //
+            // Test for errors in inputs
+            //
+            if( (n<=0 | nc<2) | kmax<2 )
+            {
+                info = -1;
+                return;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                if( c[i]<0 | c[i]>=nc )
+                {
+                    info = -2;
+                    return;
+                }
+            }
+            info = 1;
+            
+            //
+            // Tie
+            //
+            dstie(ref a, n, ref ties, ref tiecount, ref p1, ref p2);
+            for(i=0; i<=n-1; i++)
+            {
+                if( p2[i]!=i )
+                {
+                    k = c[i];
+                    c[i] = c[p2[i]];
+                    c[p2[i]] = k;
+                }
+            }
+            
+            //
+            // Special cases
+            //
+            if( tiecount==1 )
+            {
+                info = -3;
+                return;
+            }
+            
+            //
+            // General case:
+            // 0. allocate arrays
+            //
+            kmax = Math.Min(kmax, tiecount);
+            bestsizes = new int[kmax-1+1];
+            cursizes = new int[kmax-1+1];
+            cnt = new int[nc-1+1];
+            
+            //
+            // General case:
+            // 1. prepare "weak" solution (two subintervals, divided at median)
+            //
+            v2 = math.maxrealnumber;
+            j = -1;
+            for(i=1; i<=tiecount-1; i++)
+            {
+                if( (double)(Math.Abs(ties[i]-0.5*(n-1)))<(double)(v2) )
+                {
+                    v2 = Math.Abs(ties[i]-0.5*n);
+                    j = i;
+                }
+            }
+            ap.assert(j>0, "DSSplitK: internal error #1!");
+            bestk = 2;
+            bestsizes[0] = ties[j];
+            bestsizes[1] = n-j;
+            bestcve = 0;
+            for(i=0; i<=nc-1; i++)
+            {
+                cnt[i] = 0;
+            }
+            for(i=0; i<=j-1; i++)
+            {
+                tieaddc(c, ties, i, nc, ref cnt);
+            }
+            bestcve = bestcve+getcv(cnt, nc);
+            for(i=0; i<=nc-1; i++)
+            {
+                cnt[i] = 0;
+            }
+            for(i=j; i<=tiecount-1; i++)
+            {
+                tieaddc(c, ties, i, nc, ref cnt);
+            }
+            bestcve = bestcve+getcv(cnt, nc);
+            
+            //
+            // General case:
+            // 2. Use greedy algorithm to find sub-optimal split in O(KMax*N) time
+            //
+            for(k=2; k<=kmax; k++)
+            {
+                
+                //
+                // Prepare greedy K-interval split
+                //
+                for(i=0; i<=k-1; i++)
+                {
+                    cursizes[i] = 0;
+                }
+                i = 0;
+                j = 0;
+                while( j<=tiecount-1 & i<=k-1 )
+                {
+                    
+                    //
+                    // Rule: I-th bin is empty, fill it
+                    //
+                    if( cursizes[i]==0 )
+                    {
+                        cursizes[i] = ties[j+1]-ties[j];
+                        j = j+1;
+                        continue;
+                    }
+                    
+                    //
+                    // Rule: (K-1-I) bins left, (K-1-I) ties left (1 tie per bin); next bin
+                    //
+                    if( tiecount-j==k-1-i )
+                    {
+                        i = i+1;
+                        continue;
+                    }
+                    
+                    //
+                    // Rule: last bin, always place in current
+                    //
+                    if( i==k-1 )
+                    {
+                        cursizes[i] = cursizes[i]+ties[j+1]-ties[j];
+                        j = j+1;
+                        continue;
+                    }
+                    
+                    //
+                    // Place J-th tie in I-th bin, or leave for I+1-th bin.
+                    //
+                    if( (double)(Math.Abs(cursizes[i]+ties[j+1]-ties[j]-(double)n/(double)k))<(double)(Math.Abs(cursizes[i]-(double)n/(double)k)) )
+                    {
+                        cursizes[i] = cursizes[i]+ties[j+1]-ties[j];
+                        j = j+1;
+                    }
+                    else
+                    {
+                        i = i+1;
+                    }
+                }
+                ap.assert(cursizes[k-1]!=0 & j==tiecount, "DSSplitK: internal error #1");
+                
+                //
+                // Calculate CVE
+                //
+                curcve = 0;
+                j = 0;
+                for(i=0; i<=k-1; i++)
+                {
+                    for(j1=0; j1<=nc-1; j1++)
+                    {
+                        cnt[j1] = 0;
+                    }
+                    for(j1=j; j1<=j+cursizes[i]-1; j1++)
+                    {
+                        cnt[c[j1]] = cnt[c[j1]]+1;
+                    }
+                    curcve = curcve+getcv(cnt, nc);
+                    j = j+cursizes[i];
+                }
+                
+                //
+                // Choose best variant
+                //
+                if( (double)(curcve)<(double)(bestcve) )
+                {
+                    for(i=0; i<=k-1; i++)
+                    {
+                        bestsizes[i] = cursizes[i];
+                    }
+                    bestcve = curcve;
+                    bestk = k;
+                }
+            }
+            
+            //
+            // Transform from sizes to thresholds
+            //
+            cve = bestcve;
+            ni = bestk;
+            thresholds = new double[ni-2+1];
+            j = bestsizes[0];
+            for(i=1; i<=bestk-1; i++)
+            {
+                thresholds[i-1] = 0.5*(a[j-1]+a[j]);
+                j = j+bestsizes[i];
+            }
+        }
+
+
+        /*************************************************************************
+        Automatic optimal discretization, internal subroutine.
+
+          -- ALGLIB --
+             Copyright 22.05.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dsoptimalsplitk(double[] a,
+            int[] c,
+            int n,
+            int nc,
+            int kmax,
+            ref int info,
+            ref double[] thresholds,
+            ref int ni,
+            ref double cve)
+        {
+            int i = 0;
+            int j = 0;
+            int s = 0;
+            int jl = 0;
+            int jr = 0;
+            double v2 = 0;
+            int[] ties = new int[0];
+            int tiecount = 0;
+            int[] p1 = new int[0];
+            int[] p2 = new int[0];
+            double cvtemp = 0;
+            int[] cnt = new int[0];
+            int[] cnt2 = new int[0];
+            double[,] cv = new double[0,0];
+            int[,] splits = new int[0,0];
+            int k = 0;
+            int koptimal = 0;
+            double cvoptimal = 0;
+
+            a = (double[])a.Clone();
+            c = (int[])c.Clone();
+            info = 0;
+            thresholds = new double[0];
+            ni = 0;
+            cve = 0;
+
+            
+            //
+            // Test for errors in inputs
+            //
+            if( (n<=0 | nc<2) | kmax<2 )
+            {
+                info = -1;
+                return;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                if( c[i]<0 | c[i]>=nc )
+                {
+                    info = -2;
+                    return;
+                }
+            }
+            info = 1;
+            
+            //
+            // Tie
+            //
+            dstie(ref a, n, ref ties, ref tiecount, ref p1, ref p2);
+            for(i=0; i<=n-1; i++)
+            {
+                if( p2[i]!=i )
+                {
+                    k = c[i];
+                    c[i] = c[p2[i]];
+                    c[p2[i]] = k;
+                }
+            }
+            
+            //
+            // Special cases
+            //
+            if( tiecount==1 )
+            {
+                info = -3;
+                return;
+            }
+            
+            //
+            // General case
+            // Use dynamic programming to find best split in O(KMax*NC*TieCount^2) time
+            //
+            kmax = Math.Min(kmax, tiecount);
+            cv = new double[kmax-1+1, tiecount-1+1];
+            splits = new int[kmax-1+1, tiecount-1+1];
+            cnt = new int[nc-1+1];
+            cnt2 = new int[nc-1+1];
+            for(j=0; j<=nc-1; j++)
+            {
+                cnt[j] = 0;
+            }
+            for(j=0; j<=tiecount-1; j++)
+            {
+                tieaddc(c, ties, j, nc, ref cnt);
+                splits[0,j] = 0;
+                cv[0,j] = getcv(cnt, nc);
+            }
+            for(k=1; k<=kmax-1; k++)
+            {
+                for(j=0; j<=nc-1; j++)
+                {
+                    cnt[j] = 0;
+                }
+                
+                //
+                // Subtask size J in [K..TieCount-1]:
+                // optimal K-splitting on ties from 0-th to J-th.
+                //
+                for(j=k; j<=tiecount-1; j++)
+                {
+                    
+                    //
+                    // Update Cnt - let it contain classes of ties from K-th to J-th
+                    //
+                    tieaddc(c, ties, j, nc, ref cnt);
+                    
+                    //
+                    // Search for optimal split point S in [K..J]
+                    //
+                    for(i=0; i<=nc-1; i++)
+                    {
+                        cnt2[i] = cnt[i];
+                    }
+                    cv[k,j] = cv[k-1,j-1]+getcv(cnt2, nc);
+                    splits[k,j] = j;
+                    for(s=k+1; s<=j; s++)
+                    {
+                        
+                        //
+                        // Update Cnt2 - let it contain classes of ties from S-th to J-th
+                        //
+                        tiesubc(c, ties, s-1, nc, ref cnt2);
+                        
+                        //
+                        // Calculate CVE
+                        //
+                        cvtemp = cv[k-1,s-1]+getcv(cnt2, nc);
+                        if( (double)(cvtemp)<(double)(cv[k,j]) )
+                        {
+                            cv[k,j] = cvtemp;
+                            splits[k,j] = s;
+                        }
+                    }
+                }
+            }
+            
+            //
+            // Choose best partition, output result
+            //
+            koptimal = -1;
+            cvoptimal = math.maxrealnumber;
+            for(k=0; k<=kmax-1; k++)
+            {
+                if( (double)(cv[k,tiecount-1])<(double)(cvoptimal) )
+                {
+                    cvoptimal = cv[k,tiecount-1];
+                    koptimal = k;
+                }
+            }
+            ap.assert(koptimal>=0, "DSOptimalSplitK: internal error #1!");
+            if( koptimal==0 )
+            {
+                
+                //
+                // Special case: best partition is one big interval.
+                // Even 2-partition is not better.
+                // This is possible when dealing with "weak" predictor variables.
+                //
+                // Make binary split as close to the median as possible.
+                //
+                v2 = math.maxrealnumber;
+                j = -1;
+                for(i=1; i<=tiecount-1; i++)
+                {
+                    if( (double)(Math.Abs(ties[i]-0.5*(n-1)))<(double)(v2) )
+                    {
+                        v2 = Math.Abs(ties[i]-0.5*(n-1));
+                        j = i;
+                    }
+                }
+                ap.assert(j>0, "DSOptimalSplitK: internal error #2!");
+                thresholds = new double[0+1];
+                thresholds[0] = 0.5*(a[ties[j-1]]+a[ties[j]]);
+                ni = 2;
+                cve = 0;
+                for(i=0; i<=nc-1; i++)
+                {
+                    cnt[i] = 0;
+                }
+                for(i=0; i<=j-1; i++)
+                {
+                    tieaddc(c, ties, i, nc, ref cnt);
+                }
+                cve = cve+getcv(cnt, nc);
+                for(i=0; i<=nc-1; i++)
+                {
+                    cnt[i] = 0;
+                }
+                for(i=j; i<=tiecount-1; i++)
+                {
+                    tieaddc(c, ties, i, nc, ref cnt);
+                }
+                cve = cve+getcv(cnt, nc);
+            }
+            else
+            {
+                
+                //
+                // General case: 2 or more intervals
+                //
+                thresholds = new double[koptimal-1+1];
+                ni = koptimal+1;
+                cve = cv[koptimal,tiecount-1];
+                jl = splits[koptimal,tiecount-1];
+                jr = tiecount-1;
+                for(k=koptimal; k>=1; k--)
+                {
+                    thresholds[k-1] = 0.5*(a[ties[jl-1]]+a[ties[jl]]);
+                    jr = jl-1;
+                    jl = splits[k-1,jl-1];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Internal function
+        *************************************************************************/
+        private static double xlny(double x,
+            double y)
+        {
+            double result = 0;
+
+            if( (double)(x)==(double)(0) )
+            {
+                result = 0;
+            }
+            else
+            {
+                result = x*Math.Log(y);
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Internal function,
+        returns number of samples of class I in Cnt[I]
+        *************************************************************************/
+        private static double getcv(int[] cnt,
+            int nc)
+        {
+            double result = 0;
+            int i = 0;
+            double s = 0;
+
+            s = 0;
+            for(i=0; i<=nc-1; i++)
+            {
+                s = s+cnt[i];
+            }
+            result = 0;
+            for(i=0; i<=nc-1; i++)
+            {
+                result = result-xlny(cnt[i], cnt[i]/(s+nc-1));
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Internal function, adds number of samples of class I in tie NTie to Cnt[I]
+        *************************************************************************/
+        private static void tieaddc(int[] c,
+            int[] ties,
+            int ntie,
+            int nc,
+            ref int[] cnt)
+        {
+            int i = 0;
+
+            for(i=ties[ntie]; i<=ties[ntie+1]-1; i++)
+            {
+                cnt[c[i]] = cnt[c[i]]+1;
+            }
+        }
+
+
+        /*************************************************************************
+        Internal function, subtracts number of samples of class I in tie NTie to Cnt[I]
+        *************************************************************************/
+        private static void tiesubc(int[] c,
+            int[] ties,
+            int ntie,
+            int nc,
+            ref int[] cnt)
+        {
+            int i = 0;
+
+            for(i=ties[ntie]; i<=ties[ntie+1]-1; i++)
+            {
+                cnt[c[i]] = cnt[c[i]]-1;
+            }
+        }
+
+
+    }
     public class dforest
     {
@@ -2627,4 +5294,5 @@
         public const int dfusestrongsplits = 1;
         public const int dfuseevs = 2;
+        public const int dffirstversion = 0;
 
 
@@ -2683,4 +5351,70 @@
             samplesize = Math.Max((int)Math.Round(r*npoints), 1);
             dfbuildinternal(xy, npoints, nvars, nclasses, ntrees, samplesize, Math.Max(nvars/2, 1), dfusestrongsplits+dfuseevs, ref info, df, rep);
+        }
+
+
+        /*************************************************************************
+        This subroutine builds random decision forest.
+        This function gives ability to tune number of variables used when choosing
+        best split.
+
+        INPUT PARAMETERS:
+            XY          -   training set
+            NPoints     -   training set size, NPoints>=1
+            NVars       -   number of independent variables, NVars>=1
+            NClasses    -   task type:
+                            * NClasses=1 - regression task with one
+                                           dependent variable
+                            * NClasses>1 - classification task with
+                                           NClasses classes.
+            NTrees      -   number of trees in a forest, NTrees>=1.
+                            recommended values: 50-100.
+            NRndVars    -   number of variables used when choosing best split
+            R           -   percent of a training set used to build
+                            individual trees. 0<R<=1.
+                            recommended values: 0.1 <= R <= 0.66.
+
+        OUTPUT PARAMETERS:
+            Info        -   return code:
+                            * -2, if there is a point with class number
+                                  outside of [0..NClasses-1].
+                            * -1, if incorrect parameters was passed
+                                  (NPoints<1, NVars<1, NClasses<1, NTrees<1, R<=0
+                                  or R>1).
+                            *  1, if task has been solved
+            DF          -   model built
+            Rep         -   training report, contains error on a training set
+                            and out-of-bag estimates of generalization error.
+
+          -- ALGLIB --
+             Copyright 19.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dfbuildrandomdecisionforestx1(double[,] xy,
+            int npoints,
+            int nvars,
+            int nclasses,
+            int ntrees,
+            int nrndvars,
+            double r,
+            ref int info,
+            decisionforest df,
+            dfreport rep)
+        {
+            int samplesize = 0;
+
+            info = 0;
+
+            if( (double)(r)<=(double)(0) | (double)(r)>(double)(1) )
+            {
+                info = -1;
+                return;
+            }
+            if( nrndvars<=0 | nrndvars>nvars )
+            {
+                info = -1;
+                return;
+            }
+            samplesize = Math.Max((int)Math.Round(r*npoints), 1);
+            dfbuildinternal(xy, npoints, nvars, nclasses, ntrees, samplesize, nrndvars, dfusestrongsplits+dfuseevs, ref info, df, rep);
         }
 
@@ -3490,4 +6224,74 @@
 
         /*************************************************************************
+        Serializer: allocation
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dfalloc(alglib.serializer s,
+            decisionforest forest)
+        {
+            s.alloc_entry();
+            s.alloc_entry();
+            s.alloc_entry();
+            s.alloc_entry();
+            s.alloc_entry();
+            s.alloc_entry();
+            apserv.allocrealarray(s, forest.trees, forest.bufsize);
+        }
+
+
+        /*************************************************************************
+        Serializer: serialization
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dfserialize(alglib.serializer s,
+            decisionforest forest)
+        {
+            s.serialize_int(scodes.getrdfserializationcode());
+            s.serialize_int(dffirstversion);
+            s.serialize_int(forest.nvars);
+            s.serialize_int(forest.nclasses);
+            s.serialize_int(forest.ntrees);
+            s.serialize_int(forest.bufsize);
+            apserv.serializerealarray(s, forest.trees, forest.bufsize);
+        }
+
+
+        /*************************************************************************
+        Serializer: unserialization
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void dfunserialize(alglib.serializer s,
+            decisionforest forest)
+        {
+            int i0 = 0;
+            int i1 = 0;
+
+            
+            //
+            // check correctness of header
+            //
+            i0 = s.unserialize_int();
+            ap.assert(i0==scodes.getrdfserializationcode(), "DFUnserialize: stream header corrupted");
+            i1 = s.unserialize_int();
+            ap.assert(i1==dffirstversion, "DFUnserialize: stream header corrupted");
+            
+            //
+            // Unserialize data
+            //
+            forest.nvars = s.unserialize_int();
+            forest.nclasses = s.unserialize_int();
+            forest.ntrees = s.unserialize_int();
+            forest.bufsize = s.unserialize_int();
+            apserv.unserializerealarray(s, ref forest.trees);
+        }
+
+
+        /*************************************************************************
         Classification error
         *************************************************************************/
@@ -4306,1472 +7110,4 @@
 
     }
-    public class bdss
-    {
-        public class cvreport
-        {
-            public double relclserror;
-            public double avgce;
-            public double rmserror;
-            public double avgerror;
-            public double avgrelerror;
-        };
-
-
-
-
-        /*************************************************************************
-        This set of routines (DSErrAllocate, DSErrAccumulate, DSErrFinish)
-        calculates different error functions (classification error, cross-entropy,
-        rms, avg, avg.rel errors).
-
-        1. DSErrAllocate prepares buffer.
-        2. DSErrAccumulate accumulates individual errors:
-            * Y contains predicted output (posterior probabilities for classification)
-            * DesiredY contains desired output (class number for classification)
-        3. DSErrFinish outputs results:
-           * Buf[0] contains relative classification error (zero for regression tasks)
-           * Buf[1] contains avg. cross-entropy (zero for regression tasks)
-           * Buf[2] contains rms error (regression, classification)
-           * Buf[3] contains average error (regression, classification)
-           * Buf[4] contains average relative error (regression, classification)
-           
-        NOTES(1):
-            "NClasses>0" means that we have classification task.
-            "NClasses<0" means regression task with -NClasses real outputs.
-
-        NOTES(2):
-            rms. avg, avg.rel errors for classification tasks are interpreted as
-            errors in posterior probabilities with respect to probabilities given
-            by training/test set.
-
-          -- ALGLIB --
-             Copyright 11.01.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dserrallocate(int nclasses,
-            ref double[] buf)
-        {
-            buf = new double[0];
-
-            buf = new double[7+1];
-            buf[0] = 0;
-            buf[1] = 0;
-            buf[2] = 0;
-            buf[3] = 0;
-            buf[4] = 0;
-            buf[5] = nclasses;
-            buf[6] = 0;
-            buf[7] = 0;
-        }
-
-
-        /*************************************************************************
-        See DSErrAllocate for comments on this routine.
-
-          -- ALGLIB --
-             Copyright 11.01.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dserraccumulate(ref double[] buf,
-            double[] y,
-            double[] desiredy)
-        {
-            int nclasses = 0;
-            int nout = 0;
-            int offs = 0;
-            int mmax = 0;
-            int rmax = 0;
-            int j = 0;
-            double v = 0;
-            double ev = 0;
-
-            offs = 5;
-            nclasses = (int)Math.Round(buf[offs]);
-            if( nclasses>0 )
-            {
-                
-                //
-                // Classification
-                //
-                rmax = (int)Math.Round(desiredy[0]);
-                mmax = 0;
-                for(j=1; j<=nclasses-1; j++)
-                {
-                    if( (double)(y[j])>(double)(y[mmax]) )
-                    {
-                        mmax = j;
-                    }
-                }
-                if( mmax!=rmax )
-                {
-                    buf[0] = buf[0]+1;
-                }
-                if( (double)(y[rmax])>(double)(0) )
-                {
-                    buf[1] = buf[1]-Math.Log(y[rmax]);
-                }
-                else
-                {
-                    buf[1] = buf[1]+Math.Log(math.maxrealnumber);
-                }
-                for(j=0; j<=nclasses-1; j++)
-                {
-                    v = y[j];
-                    if( j==rmax )
-                    {
-                        ev = 1;
-                    }
-                    else
-                    {
-                        ev = 0;
-                    }
-                    buf[2] = buf[2]+math.sqr(v-ev);
-                    buf[3] = buf[3]+Math.Abs(v-ev);
-                    if( (double)(ev)!=(double)(0) )
-                    {
-                        buf[4] = buf[4]+Math.Abs((v-ev)/ev);
-                        buf[offs+2] = buf[offs+2]+1;
-                    }
-                }
-                buf[offs+1] = buf[offs+1]+1;
-            }
-            else
-            {
-                
-                //
-                // Regression
-                //
-                nout = -nclasses;
-                rmax = 0;
-                for(j=1; j<=nout-1; j++)
-                {
-                    if( (double)(desiredy[j])>(double)(desiredy[rmax]) )
-                    {
-                        rmax = j;
-                    }
-                }
-                mmax = 0;
-                for(j=1; j<=nout-1; j++)
-                {
-                    if( (double)(y[j])>(double)(y[mmax]) )
-                    {
-                        mmax = j;
-                    }
-                }
-                if( mmax!=rmax )
-                {
-                    buf[0] = buf[0]+1;
-                }
-                for(j=0; j<=nout-1; j++)
-                {
-                    v = y[j];
-                    ev = desiredy[j];
-                    buf[2] = buf[2]+math.sqr(v-ev);
-                    buf[3] = buf[3]+Math.Abs(v-ev);
-                    if( (double)(ev)!=(double)(0) )
-                    {
-                        buf[4] = buf[4]+Math.Abs((v-ev)/ev);
-                        buf[offs+2] = buf[offs+2]+1;
-                    }
-                }
-                buf[offs+1] = buf[offs+1]+1;
-            }
-        }
-
-
-        /*************************************************************************
-        See DSErrAllocate for comments on this routine.
-
-          -- ALGLIB --
-             Copyright 11.01.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dserrfinish(ref double[] buf)
-        {
-            int nout = 0;
-            int offs = 0;
-
-            offs = 5;
-            nout = Math.Abs((int)Math.Round(buf[offs]));
-            if( (double)(buf[offs+1])!=(double)(0) )
-            {
-                buf[0] = buf[0]/buf[offs+1];
-                buf[1] = buf[1]/buf[offs+1];
-                buf[2] = Math.Sqrt(buf[2]/(nout*buf[offs+1]));
-                buf[3] = buf[3]/(nout*buf[offs+1]);
-            }
-            if( (double)(buf[offs+2])!=(double)(0) )
-            {
-                buf[4] = buf[4]/buf[offs+2];
-            }
-        }
-
-
-        /*************************************************************************
-
-          -- ALGLIB --
-             Copyright 19.05.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dsnormalize(ref double[,] xy,
-            int npoints,
-            int nvars,
-            ref int info,
-            ref double[] means,
-            ref double[] sigmas)
-        {
-            int i = 0;
-            int j = 0;
-            double[] tmp = new double[0];
-            double mean = 0;
-            double variance = 0;
-            double skewness = 0;
-            double kurtosis = 0;
-            int i_ = 0;
-
-            info = 0;
-            means = new double[0];
-            sigmas = new double[0];
-
-            
-            //
-            // Test parameters
-            //
-            if( npoints<=0 | nvars<1 )
-            {
-                info = -1;
-                return;
-            }
-            info = 1;
-            
-            //
-            // Standartization
-            //
-            means = new double[nvars-1+1];
-            sigmas = new double[nvars-1+1];
-            tmp = new double[npoints-1+1];
-            for(j=0; j<=nvars-1; j++)
-            {
-                for(i_=0; i_<=npoints-1;i_++)
-                {
-                    tmp[i_] = xy[i_,j];
-                }
-                basestat.samplemoments(tmp, npoints, ref mean, ref variance, ref skewness, ref kurtosis);
-                means[j] = mean;
-                sigmas[j] = Math.Sqrt(variance);
-                if( (double)(sigmas[j])==(double)(0) )
-                {
-                    sigmas[j] = 1;
-                }
-                for(i=0; i<=npoints-1; i++)
-                {
-                    xy[i,j] = (xy[i,j]-means[j])/sigmas[j];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-
-          -- ALGLIB --
-             Copyright 19.05.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dsnormalizec(double[,] xy,
-            int npoints,
-            int nvars,
-            ref int info,
-            ref double[] means,
-            ref double[] sigmas)
-        {
-            int j = 0;
-            double[] tmp = new double[0];
-            double mean = 0;
-            double variance = 0;
-            double skewness = 0;
-            double kurtosis = 0;
-            int i_ = 0;
-
-            info = 0;
-            means = new double[0];
-            sigmas = new double[0];
-
-            
-            //
-            // Test parameters
-            //
-            if( npoints<=0 | nvars<1 )
-            {
-                info = -1;
-                return;
-            }
-            info = 1;
-            
-            //
-            // Standartization
-            //
-            means = new double[nvars-1+1];
-            sigmas = new double[nvars-1+1];
-            tmp = new double[npoints-1+1];
-            for(j=0; j<=nvars-1; j++)
-            {
-                for(i_=0; i_<=npoints-1;i_++)
-                {
-                    tmp[i_] = xy[i_,j];
-                }
-                basestat.samplemoments(tmp, npoints, ref mean, ref variance, ref skewness, ref kurtosis);
-                means[j] = mean;
-                sigmas[j] = Math.Sqrt(variance);
-                if( (double)(sigmas[j])==(double)(0) )
-                {
-                    sigmas[j] = 1;
-                }
-            }
-        }
-
-
-        /*************************************************************************
-
-          -- ALGLIB --
-             Copyright 19.05.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static double dsgetmeanmindistance(double[,] xy,
-            int npoints,
-            int nvars)
-        {
-            double result = 0;
-            int i = 0;
-            int j = 0;
-            double[] tmp = new double[0];
-            double[] tmp2 = new double[0];
-            double v = 0;
-            int i_ = 0;
-
-            
-            //
-            // Test parameters
-            //
-            if( npoints<=0 | nvars<1 )
-            {
-                result = 0;
-                return result;
-            }
-            
-            //
-            // Process
-            //
-            tmp = new double[npoints-1+1];
-            for(i=0; i<=npoints-1; i++)
-            {
-                tmp[i] = math.maxrealnumber;
-            }
-            tmp2 = new double[nvars-1+1];
-            for(i=0; i<=npoints-1; i++)
-            {
-                for(j=i+1; j<=npoints-1; j++)
-                {
-                    for(i_=0; i_<=nvars-1;i_++)
-                    {
-                        tmp2[i_] = xy[i,i_];
-                    }
-                    for(i_=0; i_<=nvars-1;i_++)
-                    {
-                        tmp2[i_] = tmp2[i_] - xy[j,i_];
-                    }
-                    v = 0.0;
-                    for(i_=0; i_<=nvars-1;i_++)
-                    {
-                        v += tmp2[i_]*tmp2[i_];
-                    }
-                    v = Math.Sqrt(v);
-                    tmp[i] = Math.Min(tmp[i], v);
-                    tmp[j] = Math.Min(tmp[j], v);
-                }
-            }
-            result = 0;
-            for(i=0; i<=npoints-1; i++)
-            {
-                result = result+tmp[i]/npoints;
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-
-          -- ALGLIB --
-             Copyright 19.05.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dstie(ref double[] a,
-            int n,
-            ref int[] ties,
-            ref int tiecount,
-            ref int[] p1,
-            ref int[] p2)
-        {
-            int i = 0;
-            int k = 0;
-            int[] tmp = new int[0];
-
-            ties = new int[0];
-            tiecount = 0;
-            p1 = new int[0];
-            p2 = new int[0];
-
-            
-            //
-            // Special case
-            //
-            if( n<=0 )
-            {
-                tiecount = 0;
-                return;
-            }
-            
-            //
-            // Sort A
-            //
-            tsort.tagsort(ref a, n, ref p1, ref p2);
-            
-            //
-            // Process ties
-            //
-            tiecount = 1;
-            for(i=1; i<=n-1; i++)
-            {
-                if( (double)(a[i])!=(double)(a[i-1]) )
-                {
-                    tiecount = tiecount+1;
-                }
-            }
-            ties = new int[tiecount+1];
-            ties[0] = 0;
-            k = 1;
-            for(i=1; i<=n-1; i++)
-            {
-                if( (double)(a[i])!=(double)(a[i-1]) )
-                {
-                    ties[k] = i;
-                    k = k+1;
-                }
-            }
-            ties[tiecount] = n;
-        }
-
-
-        /*************************************************************************
-
-          -- ALGLIB --
-             Copyright 11.12.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dstiefasti(ref double[] a,
-            ref int[] b,
-            int n,
-            ref int[] ties,
-            ref int tiecount,
-            ref double[] bufr,
-            ref int[] bufi)
-        {
-            int i = 0;
-            int k = 0;
-            int[] tmp = new int[0];
-
-            tiecount = 0;
-
-            
-            //
-            // Special case
-            //
-            if( n<=0 )
-            {
-                tiecount = 0;
-                return;
-            }
-            
-            //
-            // Sort A
-            //
-            tsort.tagsortfasti(ref a, ref b, ref bufr, ref bufi, n);
-            
-            //
-            // Process ties
-            //
-            ties[0] = 0;
-            k = 1;
-            for(i=1; i<=n-1; i++)
-            {
-                if( (double)(a[i])!=(double)(a[i-1]) )
-                {
-                    ties[k] = i;
-                    k = k+1;
-                }
-            }
-            ties[k] = n;
-            tiecount = k;
-        }
-
-
-        /*************************************************************************
-        Optimal binary classification
-
-        Algorithms finds optimal (=with minimal cross-entropy) binary partition.
-        Internal subroutine.
-
-        INPUT PARAMETERS:
-            A       -   array[0..N-1], variable
-            C       -   array[0..N-1], class numbers (0 or 1).
-            N       -   array size
-
-        OUTPUT PARAMETERS:
-            Info    -   completetion code:
-                        * -3, all values of A[] are same (partition is impossible)
-                        * -2, one of C[] is incorrect (<0, >1)
-                        * -1, incorrect pararemets were passed (N<=0).
-                        *  1, OK
-            Threshold-  partiton boundary. Left part contains values which are
-                        strictly less than Threshold. Right part contains values
-                        which are greater than or equal to Threshold.
-            PAL, PBL-   probabilities P(0|v<Threshold) and P(1|v<Threshold)
-            PAR, PBR-   probabilities P(0|v>=Threshold) and P(1|v>=Threshold)
-            CVE     -   cross-validation estimate of cross-entropy
-
-          -- ALGLIB --
-             Copyright 22.05.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dsoptimalsplit2(double[] a,
-            int[] c,
-            int n,
-            ref int info,
-            ref double threshold,
-            ref double pal,
-            ref double pbl,
-            ref double par,
-            ref double pbr,
-            ref double cve)
-        {
-            int i = 0;
-            int t = 0;
-            double s = 0;
-            int[] ties = new int[0];
-            int tiecount = 0;
-            int[] p1 = new int[0];
-            int[] p2 = new int[0];
-            int k = 0;
-            int koptimal = 0;
-            double pak = 0;
-            double pbk = 0;
-            double cvoptimal = 0;
-            double cv = 0;
-
-            a = (double[])a.Clone();
-            c = (int[])c.Clone();
-            info = 0;
-            threshold = 0;
-            pal = 0;
-            pbl = 0;
-            par = 0;
-            pbr = 0;
-            cve = 0;
-
-            
-            //
-            // Test for errors in inputs
-            //
-            if( n<=0 )
-            {
-                info = -1;
-                return;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                if( c[i]!=0 & c[i]!=1 )
-                {
-                    info = -2;
-                    return;
-                }
-            }
-            info = 1;
-            
-            //
-            // Tie
-            //
-            dstie(ref a, n, ref ties, ref tiecount, ref p1, ref p2);
-            for(i=0; i<=n-1; i++)
-            {
-                if( p2[i]!=i )
-                {
-                    t = c[i];
-                    c[i] = c[p2[i]];
-                    c[p2[i]] = t;
-                }
-            }
-            
-            //
-            // Special case: number of ties is 1.
-            //
-            // NOTE: we assume that P[i,j] equals to 0 or 1,
-            //       intermediate values are not allowed.
-            //
-            if( tiecount==1 )
-            {
-                info = -3;
-                return;
-            }
-            
-            //
-            // General case, number of ties > 1
-            //
-            // NOTE: we assume that P[i,j] equals to 0 or 1,
-            //       intermediate values are not allowed.
-            //
-            pal = 0;
-            pbl = 0;
-            par = 0;
-            pbr = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                if( c[i]==0 )
-                {
-                    par = par+1;
-                }
-                if( c[i]==1 )
-                {
-                    pbr = pbr+1;
-                }
-            }
-            koptimal = -1;
-            cvoptimal = math.maxrealnumber;
-            for(k=0; k<=tiecount-2; k++)
-            {
-                
-                //
-                // first, obtain information about K-th tie which is
-                // moved from R-part to L-part
-                //
-                pak = 0;
-                pbk = 0;
-                for(i=ties[k]; i<=ties[k+1]-1; i++)
-                {
-                    if( c[i]==0 )
-                    {
-                        pak = pak+1;
-                    }
-                    if( c[i]==1 )
-                    {
-                        pbk = pbk+1;
-                    }
-                }
-                
-                //
-                // Calculate cross-validation CE
-                //
-                cv = 0;
-                cv = cv-xlny(pal+pak, (pal+pak)/(pal+pak+pbl+pbk+1));
-                cv = cv-xlny(pbl+pbk, (pbl+pbk)/(pal+pak+1+pbl+pbk));
-                cv = cv-xlny(par-pak, (par-pak)/(par-pak+pbr-pbk+1));
-                cv = cv-xlny(pbr-pbk, (pbr-pbk)/(par-pak+1+pbr-pbk));
-                
-                //
-                // Compare with best
-                //
-                if( (double)(cv)<(double)(cvoptimal) )
-                {
-                    cvoptimal = cv;
-                    koptimal = k;
-                }
-                
-                //
-                // update
-                //
-                pal = pal+pak;
-                pbl = pbl+pbk;
-                par = par-pak;
-                pbr = pbr-pbk;
-            }
-            cve = cvoptimal;
-            threshold = 0.5*(a[ties[koptimal]]+a[ties[koptimal+1]]);
-            pal = 0;
-            pbl = 0;
-            par = 0;
-            pbr = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                if( (double)(a[i])<(double)(threshold) )
-                {
-                    if( c[i]==0 )
-                    {
-                        pal = pal+1;
-                    }
-                    else
-                    {
-                        pbl = pbl+1;
-                    }
-                }
-                else
-                {
-                    if( c[i]==0 )
-                    {
-                        par = par+1;
-                    }
-                    else
-                    {
-                        pbr = pbr+1;
-                    }
-                }
-            }
-            s = pal+pbl;
-            pal = pal/s;
-            pbl = pbl/s;
-            s = par+pbr;
-            par = par/s;
-            pbr = pbr/s;
-        }
-
-
-        /*************************************************************************
-        Optimal partition, internal subroutine. Fast version.
-
-        Accepts:
-            A       array[0..N-1]       array of attributes     array[0..N-1]
-            C       array[0..N-1]       array of class labels
-            TiesBuf array[0..N]         temporaries (ties)
-            CntBuf  array[0..2*NC-1]    temporaries (counts)
-            Alpha                       centering factor (0<=alpha<=1, recommended value - 0.05)
-            BufR    array[0..N-1]       temporaries
-            BufI    array[0..N-1]       temporaries
-
-        Output:
-            Info    error code (">0"=OK, "<0"=bad)
-            RMS     training set RMS error
-            CVRMS   leave-one-out RMS error
-            
-        Note:
-            content of all arrays is changed by subroutine;
-            it doesn't allocate temporaries.
-
-          -- ALGLIB --
-             Copyright 11.12.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dsoptimalsplit2fast(ref double[] a,
-            ref int[] c,
-            ref int[] tiesbuf,
-            ref int[] cntbuf,
-            ref double[] bufr,
-            ref int[] bufi,
-            int n,
-            int nc,
-            double alpha,
-            ref int info,
-            ref double threshold,
-            ref double rms,
-            ref double cvrms)
-        {
-            int i = 0;
-            int k = 0;
-            int cl = 0;
-            int tiecount = 0;
-            double cbest = 0;
-            double cc = 0;
-            int koptimal = 0;
-            int sl = 0;
-            int sr = 0;
-            double v = 0;
-            double w = 0;
-            double x = 0;
-
-            info = 0;
-            threshold = 0;
-            rms = 0;
-            cvrms = 0;
-
-            
-            //
-            // Test for errors in inputs
-            //
-            if( n<=0 | nc<2 )
-            {
-                info = -1;
-                return;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                if( c[i]<0 | c[i]>=nc )
-                {
-                    info = -2;
-                    return;
-                }
-            }
-            info = 1;
-            
-            //
-            // Tie
-            //
-            dstiefasti(ref a, ref c, n, ref tiesbuf, ref tiecount, ref bufr, ref bufi);
-            
-            //
-            // Special case: number of ties is 1.
-            //
-            if( tiecount==1 )
-            {
-                info = -3;
-                return;
-            }
-            
-            //
-            // General case, number of ties > 1
-            //
-            for(i=0; i<=2*nc-1; i++)
-            {
-                cntbuf[i] = 0;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                cntbuf[nc+c[i]] = cntbuf[nc+c[i]]+1;
-            }
-            koptimal = -1;
-            threshold = a[n-1];
-            cbest = math.maxrealnumber;
-            sl = 0;
-            sr = n;
-            for(k=0; k<=tiecount-2; k++)
-            {
-                
-                //
-                // first, move Kth tie from right to left
-                //
-                for(i=tiesbuf[k]; i<=tiesbuf[k+1]-1; i++)
-                {
-                    cl = c[i];
-                    cntbuf[cl] = cntbuf[cl]+1;
-                    cntbuf[nc+cl] = cntbuf[nc+cl]-1;
-                }
-                sl = sl+(tiesbuf[k+1]-tiesbuf[k]);
-                sr = sr-(tiesbuf[k+1]-tiesbuf[k]);
-                
-                //
-                // Calculate RMS error
-                //
-                v = 0;
-                for(i=0; i<=nc-1; i++)
-                {
-                    w = cntbuf[i];
-                    v = v+w*math.sqr(w/sl-1);
-                    v = v+(sl-w)*math.sqr(w/sl);
-                    w = cntbuf[nc+i];
-                    v = v+w*math.sqr(w/sr-1);
-                    v = v+(sr-w)*math.sqr(w/sr);
-                }
-                v = Math.Sqrt(v/(nc*n));
-                
-                //
-                // Compare with best
-                //
-                x = (double)(2*sl)/(double)(sl+sr)-1;
-                cc = v*(1-alpha+alpha*math.sqr(x));
-                if( (double)(cc)<(double)(cbest) )
-                {
-                    
-                    //
-                    // store split
-                    //
-                    rms = v;
-                    koptimal = k;
-                    cbest = cc;
-                    
-                    //
-                    // calculate CVRMS error
-                    //
-                    cvrms = 0;
-                    for(i=0; i<=nc-1; i++)
-                    {
-                        if( sl>1 )
-                        {
-                            w = cntbuf[i];
-                            cvrms = cvrms+w*math.sqr((w-1)/(sl-1)-1);
-                            cvrms = cvrms+(sl-w)*math.sqr(w/(sl-1));
-                        }
-                        else
-                        {
-                            w = cntbuf[i];
-                            cvrms = cvrms+w*math.sqr((double)1/(double)nc-1);
-                            cvrms = cvrms+(sl-w)*math.sqr((double)1/(double)nc);
-                        }
-                        if( sr>1 )
-                        {
-                            w = cntbuf[nc+i];
-                            cvrms = cvrms+w*math.sqr((w-1)/(sr-1)-1);
-                            cvrms = cvrms+(sr-w)*math.sqr(w/(sr-1));
-                        }
-                        else
-                        {
-                            w = cntbuf[nc+i];
-                            cvrms = cvrms+w*math.sqr((double)1/(double)nc-1);
-                            cvrms = cvrms+(sr-w)*math.sqr((double)1/(double)nc);
-                        }
-                    }
-                    cvrms = Math.Sqrt(cvrms/(nc*n));
-                }
-            }
-            
-            //
-            // Calculate threshold.
-            // Code is a bit complicated because there can be such
-            // numbers that 0.5(A+B) equals to A or B (if A-B=epsilon)
-            //
-            threshold = 0.5*(a[tiesbuf[koptimal]]+a[tiesbuf[koptimal+1]]);
-            if( (double)(threshold)<=(double)(a[tiesbuf[koptimal]]) )
-            {
-                threshold = a[tiesbuf[koptimal+1]];
-            }
-        }
-
-
-        /*************************************************************************
-        Automatic non-optimal discretization, internal subroutine.
-
-          -- ALGLIB --
-             Copyright 22.05.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dssplitk(double[] a,
-            int[] c,
-            int n,
-            int nc,
-            int kmax,
-            ref int info,
-            ref double[] thresholds,
-            ref int ni,
-            ref double cve)
-        {
-            int i = 0;
-            int j = 0;
-            int j1 = 0;
-            int k = 0;
-            int[] ties = new int[0];
-            int tiecount = 0;
-            int[] p1 = new int[0];
-            int[] p2 = new int[0];
-            int[] cnt = new int[0];
-            double v2 = 0;
-            int bestk = 0;
-            double bestcve = 0;
-            int[] bestsizes = new int[0];
-            double curcve = 0;
-            int[] cursizes = new int[0];
-
-            a = (double[])a.Clone();
-            c = (int[])c.Clone();
-            info = 0;
-            thresholds = new double[0];
-            ni = 0;
-            cve = 0;
-
-            
-            //
-            // Test for errors in inputs
-            //
-            if( (n<=0 | nc<2) | kmax<2 )
-            {
-                info = -1;
-                return;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                if( c[i]<0 | c[i]>=nc )
-                {
-                    info = -2;
-                    return;
-                }
-            }
-            info = 1;
-            
-            //
-            // Tie
-            //
-            dstie(ref a, n, ref ties, ref tiecount, ref p1, ref p2);
-            for(i=0; i<=n-1; i++)
-            {
-                if( p2[i]!=i )
-                {
-                    k = c[i];
-                    c[i] = c[p2[i]];
-                    c[p2[i]] = k;
-                }
-            }
-            
-            //
-            // Special cases
-            //
-            if( tiecount==1 )
-            {
-                info = -3;
-                return;
-            }
-            
-            //
-            // General case:
-            // 0. allocate arrays
-            //
-            kmax = Math.Min(kmax, tiecount);
-            bestsizes = new int[kmax-1+1];
-            cursizes = new int[kmax-1+1];
-            cnt = new int[nc-1+1];
-            
-            //
-            // General case:
-            // 1. prepare "weak" solution (two subintervals, divided at median)
-            //
-            v2 = math.maxrealnumber;
-            j = -1;
-            for(i=1; i<=tiecount-1; i++)
-            {
-                if( (double)(Math.Abs(ties[i]-0.5*(n-1)))<(double)(v2) )
-                {
-                    v2 = Math.Abs(ties[i]-0.5*n);
-                    j = i;
-                }
-            }
-            ap.assert(j>0, "DSSplitK: internal error #1!");
-            bestk = 2;
-            bestsizes[0] = ties[j];
-            bestsizes[1] = n-j;
-            bestcve = 0;
-            for(i=0; i<=nc-1; i++)
-            {
-                cnt[i] = 0;
-            }
-            for(i=0; i<=j-1; i++)
-            {
-                tieaddc(c, ties, i, nc, ref cnt);
-            }
-            bestcve = bestcve+getcv(cnt, nc);
-            for(i=0; i<=nc-1; i++)
-            {
-                cnt[i] = 0;
-            }
-            for(i=j; i<=tiecount-1; i++)
-            {
-                tieaddc(c, ties, i, nc, ref cnt);
-            }
-            bestcve = bestcve+getcv(cnt, nc);
-            
-            //
-            // General case:
-            // 2. Use greedy algorithm to find sub-optimal split in O(KMax*N) time
-            //
-            for(k=2; k<=kmax; k++)
-            {
-                
-                //
-                // Prepare greedy K-interval split
-                //
-                for(i=0; i<=k-1; i++)
-                {
-                    cursizes[i] = 0;
-                }
-                i = 0;
-                j = 0;
-                while( j<=tiecount-1 & i<=k-1 )
-                {
-                    
-                    //
-                    // Rule: I-th bin is empty, fill it
-                    //
-                    if( cursizes[i]==0 )
-                    {
-                        cursizes[i] = ties[j+1]-ties[j];
-                        j = j+1;
-                        continue;
-                    }
-                    
-                    //
-                    // Rule: (K-1-I) bins left, (K-1-I) ties left (1 tie per bin); next bin
-                    //
-                    if( tiecount-j==k-1-i )
-                    {
-                        i = i+1;
-                        continue;
-                    }
-                    
-                    //
-                    // Rule: last bin, always place in current
-                    //
-                    if( i==k-1 )
-                    {
-                        cursizes[i] = cursizes[i]+ties[j+1]-ties[j];
-                        j = j+1;
-                        continue;
-                    }
-                    
-                    //
-                    // Place J-th tie in I-th bin, or leave for I+1-th bin.
-                    //
-                    if( (double)(Math.Abs(cursizes[i]+ties[j+1]-ties[j]-(double)n/(double)k))<(double)(Math.Abs(cursizes[i]-(double)n/(double)k)) )
-                    {
-                        cursizes[i] = cursizes[i]+ties[j+1]-ties[j];
-                        j = j+1;
-                    }
-                    else
-                    {
-                        i = i+1;
-                    }
-                }
-                ap.assert(cursizes[k-1]!=0 & j==tiecount, "DSSplitK: internal error #1");
-                
-                //
-                // Calculate CVE
-                //
-                curcve = 0;
-                j = 0;
-                for(i=0; i<=k-1; i++)
-                {
-                    for(j1=0; j1<=nc-1; j1++)
-                    {
-                        cnt[j1] = 0;
-                    }
-                    for(j1=j; j1<=j+cursizes[i]-1; j1++)
-                    {
-                        cnt[c[j1]] = cnt[c[j1]]+1;
-                    }
-                    curcve = curcve+getcv(cnt, nc);
-                    j = j+cursizes[i];
-                }
-                
-                //
-                // Choose best variant
-                //
-                if( (double)(curcve)<(double)(bestcve) )
-                {
-                    for(i=0; i<=k-1; i++)
-                    {
-                        bestsizes[i] = cursizes[i];
-                    }
-                    bestcve = curcve;
-                    bestk = k;
-                }
-            }
-            
-            //
-            // Transform from sizes to thresholds
-            //
-            cve = bestcve;
-            ni = bestk;
-            thresholds = new double[ni-2+1];
-            j = bestsizes[0];
-            for(i=1; i<=bestk-1; i++)
-            {
-                thresholds[i-1] = 0.5*(a[j-1]+a[j]);
-                j = j+bestsizes[i];
-            }
-        }
-
-
-        /*************************************************************************
-        Automatic optimal discretization, internal subroutine.
-
-          -- ALGLIB --
-             Copyright 22.05.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void dsoptimalsplitk(double[] a,
-            int[] c,
-            int n,
-            int nc,
-            int kmax,
-            ref int info,
-            ref double[] thresholds,
-            ref int ni,
-            ref double cve)
-        {
-            int i = 0;
-            int j = 0;
-            int s = 0;
-            int jl = 0;
-            int jr = 0;
-            double v2 = 0;
-            int[] ties = new int[0];
-            int tiecount = 0;
-            int[] p1 = new int[0];
-            int[] p2 = new int[0];
-            double cvtemp = 0;
-            int[] cnt = new int[0];
-            int[] cnt2 = new int[0];
-            double[,] cv = new double[0,0];
-            int[,] splits = new int[0,0];
-            int k = 0;
-            int koptimal = 0;
-            double cvoptimal = 0;
-
-            a = (double[])a.Clone();
-            c = (int[])c.Clone();
-            info = 0;
-            thresholds = new double[0];
-            ni = 0;
-            cve = 0;
-
-            
-            //
-            // Test for errors in inputs
-            //
-            if( (n<=0 | nc<2) | kmax<2 )
-            {
-                info = -1;
-                return;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                if( c[i]<0 | c[i]>=nc )
-                {
-                    info = -2;
-                    return;
-                }
-            }
-            info = 1;
-            
-            //
-            // Tie
-            //
-            dstie(ref a, n, ref ties, ref tiecount, ref p1, ref p2);
-            for(i=0; i<=n-1; i++)
-            {
-                if( p2[i]!=i )
-                {
-                    k = c[i];
-                    c[i] = c[p2[i]];
-                    c[p2[i]] = k;
-                }
-            }
-            
-            //
-            // Special cases
-            //
-            if( tiecount==1 )
-            {
-                info = -3;
-                return;
-            }
-            
-            //
-            // General case
-            // Use dynamic programming to find best split in O(KMax*NC*TieCount^2) time
-            //
-            kmax = Math.Min(kmax, tiecount);
-            cv = new double[kmax-1+1, tiecount-1+1];
-            splits = new int[kmax-1+1, tiecount-1+1];
-            cnt = new int[nc-1+1];
-            cnt2 = new int[nc-1+1];
-            for(j=0; j<=nc-1; j++)
-            {
-                cnt[j] = 0;
-            }
-            for(j=0; j<=tiecount-1; j++)
-            {
-                tieaddc(c, ties, j, nc, ref cnt);
-                splits[0,j] = 0;
-                cv[0,j] = getcv(cnt, nc);
-            }
-            for(k=1; k<=kmax-1; k++)
-            {
-                for(j=0; j<=nc-1; j++)
-                {
-                    cnt[j] = 0;
-                }
-                
-                //
-                // Subtask size J in [K..TieCount-1]:
-                // optimal K-splitting on ties from 0-th to J-th.
-                //
-                for(j=k; j<=tiecount-1; j++)
-                {
-                    
-                    //
-                    // Update Cnt - let it contain classes of ties from K-th to J-th
-                    //
-                    tieaddc(c, ties, j, nc, ref cnt);
-                    
-                    //
-                    // Search for optimal split point S in [K..J]
-                    //
-                    for(i=0; i<=nc-1; i++)
-                    {
-                        cnt2[i] = cnt[i];
-                    }
-                    cv[k,j] = cv[k-1,j-1]+getcv(cnt2, nc);
-                    splits[k,j] = j;
-                    for(s=k+1; s<=j; s++)
-                    {
-                        
-                        //
-                        // Update Cnt2 - let it contain classes of ties from S-th to J-th
-                        //
-                        tiesubc(c, ties, s-1, nc, ref cnt2);
-                        
-                        //
-                        // Calculate CVE
-                        //
-                        cvtemp = cv[k-1,s-1]+getcv(cnt2, nc);
-                        if( (double)(cvtemp)<(double)(cv[k,j]) )
-                        {
-                            cv[k,j] = cvtemp;
-                            splits[k,j] = s;
-                        }
-                    }
-                }
-            }
-            
-            //
-            // Choose best partition, output result
-            //
-            koptimal = -1;
-            cvoptimal = math.maxrealnumber;
-            for(k=0; k<=kmax-1; k++)
-            {
-                if( (double)(cv[k,tiecount-1])<(double)(cvoptimal) )
-                {
-                    cvoptimal = cv[k,tiecount-1];
-                    koptimal = k;
-                }
-            }
-            ap.assert(koptimal>=0, "DSOptimalSplitK: internal error #1!");
-            if( koptimal==0 )
-            {
-                
-                //
-                // Special case: best partition is one big interval.
-                // Even 2-partition is not better.
-                // This is possible when dealing with "weak" predictor variables.
-                //
-                // Make binary split as close to the median as possible.
-                //
-                v2 = math.maxrealnumber;
-                j = -1;
-                for(i=1; i<=tiecount-1; i++)
-                {
-                    if( (double)(Math.Abs(ties[i]-0.5*(n-1)))<(double)(v2) )
-                    {
-                        v2 = Math.Abs(ties[i]-0.5*(n-1));
-                        j = i;
-                    }
-                }
-                ap.assert(j>0, "DSOptimalSplitK: internal error #2!");
-                thresholds = new double[0+1];
-                thresholds[0] = 0.5*(a[ties[j-1]]+a[ties[j]]);
-                ni = 2;
-                cve = 0;
-                for(i=0; i<=nc-1; i++)
-                {
-                    cnt[i] = 0;
-                }
-                for(i=0; i<=j-1; i++)
-                {
-                    tieaddc(c, ties, i, nc, ref cnt);
-                }
-                cve = cve+getcv(cnt, nc);
-                for(i=0; i<=nc-1; i++)
-                {
-                    cnt[i] = 0;
-                }
-                for(i=j; i<=tiecount-1; i++)
-                {
-                    tieaddc(c, ties, i, nc, ref cnt);
-                }
-                cve = cve+getcv(cnt, nc);
-            }
-            else
-            {
-                
-                //
-                // General case: 2 or more intervals
-                //
-                thresholds = new double[koptimal-1+1];
-                ni = koptimal+1;
-                cve = cv[koptimal,tiecount-1];
-                jl = splits[koptimal,tiecount-1];
-                jr = tiecount-1;
-                for(k=koptimal; k>=1; k--)
-                {
-                    thresholds[k-1] = 0.5*(a[ties[jl-1]]+a[ties[jl]]);
-                    jr = jl-1;
-                    jl = splits[k-1,jl-1];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal function
-        *************************************************************************/
-        private static double xlny(double x,
-            double y)
-        {
-            double result = 0;
-
-            if( (double)(x)==(double)(0) )
-            {
-                result = 0;
-            }
-            else
-            {
-                result = x*Math.Log(y);
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Internal function,
-        returns number of samples of class I in Cnt[I]
-        *************************************************************************/
-        private static double getcv(int[] cnt,
-            int nc)
-        {
-            double result = 0;
-            int i = 0;
-            double s = 0;
-
-            s = 0;
-            for(i=0; i<=nc-1; i++)
-            {
-                s = s+cnt[i];
-            }
-            result = 0;
-            for(i=0; i<=nc-1; i++)
-            {
-                result = result-xlny(cnt[i], cnt[i]/(s+nc-1));
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Internal function, adds number of samples of class I in tie NTie to Cnt[I]
-        *************************************************************************/
-        private static void tieaddc(int[] c,
-            int[] ties,
-            int ntie,
-            int nc,
-            ref int[] cnt)
-        {
-            int i = 0;
-
-            for(i=ties[ntie]; i<=ties[ntie+1]-1; i++)
-            {
-                cnt[c[i]] = cnt[c[i]]+1;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal function, subtracts number of samples of class I in tie NTie to Cnt[I]
-        *************************************************************************/
-        private static void tiesubc(int[] c,
-            int[] ties,
-            int ntie,
-            int nc,
-            ref int[] cnt)
-        {
-            int i = 0;
-
-            for(i=ties[ntie]; i<=ties[ntie+1]-1; i++)
-            {
-                cnt[c[i]] = cnt[c[i]]-1;
-            }
-        }
-
-
-    }
     public class kmeans
     {
@@ -5794,6 +7130,5 @@
             C           -   array[0..NVars-1,0..K-1].matrix whose columns store
                             cluster's centers
-            XYC         -   array which contains number of clusters dataset points
-                            belong to.
+            XYC         -   array[NPoints], which contains cluster indexes
 
           -- ALGLIB --
@@ -7079,4 +8414,5 @@
         OUTPUT PARAMETERS:
             V           -   coefficients, array[0..NVars]
+                            constant term (intercept) is stored in the V[NVars].
             NVars       -   number of independent variables (one less than number
                             of coefficients)
@@ -7897,4 +9233,4699 @@
 
     }
+    public class mlpbase
+    {
+        public class multilayerperceptron
+        {
+            public int hlnetworktype;
+            public int hlnormtype;
+            public int[] hllayersizes;
+            public int[] hlconnections;
+            public int[] hlneurons;
+            public int[] structinfo;
+            public double[] weights;
+            public double[] columnmeans;
+            public double[] columnsigmas;
+            public double[] neurons;
+            public double[] dfdnet;
+            public double[] derror;
+            public double[] x;
+            public double[] y;
+            public double[,] chunks;
+            public double[] nwbuf;
+            public int[] integerbuf;
+            public multilayerperceptron()
+            {
+                hllayersizes = new int[0];
+                hlconnections = new int[0];
+                hlneurons = new int[0];
+                structinfo = new int[0];
+                weights = new double[0];
+                columnmeans = new double[0];
+                columnsigmas = new double[0];
+                neurons = new double[0];
+                dfdnet = new double[0];
+                derror = new double[0];
+                x = new double[0];
+                y = new double[0];
+                chunks = new double[0,0];
+                nwbuf = new double[0];
+                integerbuf = new int[0];
+            }
+        };
+
+
+
+
+        public const int mlpvnum = 7;
+        public const int mlpfirstversion = 0;
+        public const int nfieldwidth = 4;
+        public const int hlconnfieldwidth = 5;
+        public const int hlnfieldwidth = 4;
+        public const int chunksize = 32;
+
+
+        /*************************************************************************
+        Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
+        layers, with linear output layer. Network weights are  filled  with  small
+        random values.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreate0(int nin,
+            int nout,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+
+            layerscount = 1+3;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(-5, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, 0, 0, nout, false, true);
+        }
+
+
+        /*************************************************************************
+        Same  as  MLPCreate0,  but  with  one  hidden  layer  (NHid  neurons) with
+        non-linear activation function. Output layer is linear.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreate1(int nin,
+            int nhid,
+            int nout,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+
+            layerscount = 1+3+3;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(-5, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, nhid, 0, nout, false, true);
+        }
+
+
+        /*************************************************************************
+        Same as MLPCreate0, but with two hidden layers (NHid1 and  NHid2  neurons)
+        with non-linear activation function. Output layer is linear.
+         $ALL
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreate2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+
+            layerscount = 1+3+3+3;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(-5, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, nhid1, nhid2, nout, false, true);
+        }
+
+
+        /*************************************************************************
+        Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
+        layers with non-linear output layer. Network weights are filled with small
+        random values.
+
+        Activation function of the output layer takes values:
+
+            (B, +INF), if D>=0
+
+        or
+
+            (-INF, B), if D<0.
+
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreateb0(int nin,
+            int nout,
+            double b,
+            double d,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+            int i = 0;
+
+            layerscount = 1+3;
+            if( (double)(d)>=(double)(0) )
+            {
+                d = 1;
+            }
+            else
+            {
+                d = -1;
+            }
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(3, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, 0, 0, nout, false, false);
+            
+            //
+            // Turn on ouputs shift/scaling.
+            //
+            for(i=nin; i<=nin+nout-1; i++)
+            {
+                network.columnmeans[i] = b;
+                network.columnsigmas[i] = d;
+            }
+        }
+
+
+        /*************************************************************************
+        Same as MLPCreateB0 but with non-linear hidden layer.
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreateb1(int nin,
+            int nhid,
+            int nout,
+            double b,
+            double d,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+            int i = 0;
+
+            layerscount = 1+3+3;
+            if( (double)(d)>=(double)(0) )
+            {
+                d = 1;
+            }
+            else
+            {
+                d = -1;
+            }
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(3, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, nhid, 0, nout, false, false);
+            
+            //
+            // Turn on ouputs shift/scaling.
+            //
+            for(i=nin; i<=nin+nout-1; i++)
+            {
+                network.columnmeans[i] = b;
+                network.columnsigmas[i] = d;
+            }
+        }
+
+
+        /*************************************************************************
+        Same as MLPCreateB0 but with two non-linear hidden layers.
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreateb2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            double b,
+            double d,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+            int i = 0;
+
+            layerscount = 1+3+3+3;
+            if( (double)(d)>=(double)(0) )
+            {
+                d = 1;
+            }
+            else
+            {
+                d = -1;
+            }
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(3, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, nhid1, nhid2, nout, false, false);
+            
+            //
+            // Turn on ouputs shift/scaling.
+            //
+            for(i=nin; i<=nin+nout-1; i++)
+            {
+                network.columnmeans[i] = b;
+                network.columnsigmas[i] = d;
+            }
+        }
+
+
+        /*************************************************************************
+        Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
+        layers with non-linear output layer. Network weights are filled with small
+        random values. Activation function of the output layer takes values [A,B].
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreater0(int nin,
+            int nout,
+            double a,
+            double b,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+            int i = 0;
+
+            layerscount = 1+3;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, 0, 0, nout, false, false);
+            
+            //
+            // Turn on outputs shift/scaling.
+            //
+            for(i=nin; i<=nin+nout-1; i++)
+            {
+                network.columnmeans[i] = 0.5*(a+b);
+                network.columnsigmas[i] = 0.5*(a-b);
+            }
+        }
+
+
+        /*************************************************************************
+        Same as MLPCreateR0, but with non-linear hidden layer.
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreater1(int nin,
+            int nhid,
+            int nout,
+            double a,
+            double b,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+            int i = 0;
+
+            layerscount = 1+3+3;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, nhid, 0, nout, false, false);
+            
+            //
+            // Turn on outputs shift/scaling.
+            //
+            for(i=nin; i<=nin+nout-1; i++)
+            {
+                network.columnmeans[i] = 0.5*(a+b);
+                network.columnsigmas[i] = 0.5*(a-b);
+            }
+        }
+
+
+        /*************************************************************************
+        Same as MLPCreateR0, but with two non-linear hidden layers.
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreater2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            double a,
+            double b,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+            int i = 0;
+
+            layerscount = 1+3+3+3;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+            fillhighlevelinformation(network, nin, nhid1, nhid2, nout, false, false);
+            
+            //
+            // Turn on outputs shift/scaling.
+            //
+            for(i=nin; i<=nin+nout-1; i++)
+            {
+                network.columnmeans[i] = 0.5*(a+b);
+                network.columnsigmas[i] = 0.5*(a-b);
+            }
+        }
+
+
+        /*************************************************************************
+        Creates classifier network with NIn  inputs  and  NOut  possible  classes.
+        Network contains no hidden layers and linear output  layer  with  SOFTMAX-
+        normalization  (so  outputs  sums  up  to  1.0  and  converge to posterior
+        probabilities).
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreatec0(int nin,
+            int nout,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+
+            ap.assert(nout>=2, "MLPCreateC0: NOut<2!");
+            layerscount = 1+2+1;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout-1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addzerolayer(ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, true, network);
+            fillhighlevelinformation(network, nin, 0, 0, nout, true, true);
+        }
+
+
+        /*************************************************************************
+        Same as MLPCreateC0, but with one non-linear hidden layer.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreatec1(int nin,
+            int nhid,
+            int nout,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+
+            ap.assert(nout>=2, "MLPCreateC1: NOut<2!");
+            layerscount = 1+3+2+1;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout-1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addzerolayer(ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, true, network);
+            fillhighlevelinformation(network, nin, nhid, 0, nout, true, true);
+        }
+
+
+        /*************************************************************************
+        Same as MLPCreateC0, but with two non-linear hidden layers.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcreatec2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            multilayerperceptron network)
+        {
+            int[] lsizes = new int[0];
+            int[] ltypes = new int[0];
+            int[] lconnfirst = new int[0];
+            int[] lconnlast = new int[0];
+            int layerscount = 0;
+            int lastproc = 0;
+
+            ap.assert(nout>=2, "MLPCreateC2: NOut<2!");
+            layerscount = 1+3+3+2+1;
+            
+            //
+            // Allocate arrays
+            //
+            lsizes = new int[layerscount-1+1];
+            ltypes = new int[layerscount-1+1];
+            lconnfirst = new int[layerscount-1+1];
+            lconnlast = new int[layerscount-1+1];
+            
+            //
+            // Layers
+            //
+            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addbiasedsummatorlayer(nout-1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            addzerolayer(ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
+            
+            //
+            // Create
+            //
+            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, true, network);
+            fillhighlevelinformation(network, nin, nhid1, nhid2, nout, true, true);
+        }
+
+
+        /*************************************************************************
+        Copying of neural network
+
+        INPUT PARAMETERS:
+            Network1 -   original
+
+        OUTPUT PARAMETERS:
+            Network2 -   copy
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpcopy(multilayerperceptron network1,
+            multilayerperceptron network2)
+        {
+            network2.hlnetworktype = network1.hlnetworktype;
+            network2.hlnormtype = network1.hlnormtype;
+            apserv.copyintegerarray(network1.hllayersizes, ref network2.hllayersizes);
+            apserv.copyintegerarray(network1.hlconnections, ref network2.hlconnections);
+            apserv.copyintegerarray(network1.hlneurons, ref network2.hlneurons);
+            apserv.copyintegerarray(network1.structinfo, ref network2.structinfo);
+            apserv.copyrealarray(network1.weights, ref network2.weights);
+            apserv.copyrealarray(network1.columnmeans, ref network2.columnmeans);
+            apserv.copyrealarray(network1.columnsigmas, ref network2.columnsigmas);
+            apserv.copyrealarray(network1.neurons, ref network2.neurons);
+            apserv.copyrealarray(network1.dfdnet, ref network2.dfdnet);
+            apserv.copyrealarray(network1.derror, ref network2.derror);
+            apserv.copyrealarray(network1.x, ref network2.x);
+            apserv.copyrealarray(network1.y, ref network2.y);
+            apserv.copyrealmatrix(network1.chunks, ref network2.chunks);
+            apserv.copyrealarray(network1.nwbuf, ref network2.nwbuf);
+            apserv.copyintegerarray(network1.integerbuf, ref network2.integerbuf);
+        }
+
+
+        /*************************************************************************
+        Serialization of MultiLayerPerceptron strucure
+
+        INPUT PARAMETERS:
+            Network -   original
+
+        OUTPUT PARAMETERS:
+            RA      -   array of real numbers which stores network,
+                        array[0..RLen-1]
+            RLen    -   RA lenght
+
+          -- ALGLIB --
+             Copyright 29.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpserializeold(multilayerperceptron network,
+            ref double[] ra,
+            ref int rlen)
+        {
+            int i = 0;
+            int ssize = 0;
+            int ntotal = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            int sigmalen = 0;
+            int offs = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            ra = new double[0];
+            rlen = 0;
+
+            
+            //
+            // Unload info
+            //
+            ssize = network.structinfo[0];
+            nin = network.structinfo[1];
+            nout = network.structinfo[2];
+            ntotal = network.structinfo[3];
+            wcount = network.structinfo[4];
+            if( mlpissoftmax(network) )
+            {
+                sigmalen = nin;
+            }
+            else
+            {
+                sigmalen = nin+nout;
+            }
+            
+            //
+            //  RA format:
+            //      LEN         DESRC.
+            //      1           RLen
+            //      1           version (MLPVNum)
+            //      1           StructInfo size
+            //      SSize       StructInfo
+            //      WCount      Weights
+            //      SigmaLen    ColumnMeans
+            //      SigmaLen    ColumnSigmas
+            //
+            rlen = 3+ssize+wcount+2*sigmalen;
+            ra = new double[rlen-1+1];
+            ra[0] = rlen;
+            ra[1] = mlpvnum;
+            ra[2] = ssize;
+            offs = 3;
+            for(i=0; i<=ssize-1; i++)
+            {
+                ra[offs+i] = network.structinfo[i];
+            }
+            offs = offs+ssize;
+            i1_ = (0) - (offs);
+            for(i_=offs; i_<=offs+wcount-1;i_++)
+            {
+                ra[i_] = network.weights[i_+i1_];
+            }
+            offs = offs+wcount;
+            i1_ = (0) - (offs);
+            for(i_=offs; i_<=offs+sigmalen-1;i_++)
+            {
+                ra[i_] = network.columnmeans[i_+i1_];
+            }
+            offs = offs+sigmalen;
+            i1_ = (0) - (offs);
+            for(i_=offs; i_<=offs+sigmalen-1;i_++)
+            {
+                ra[i_] = network.columnsigmas[i_+i1_];
+            }
+            offs = offs+sigmalen;
+        }
+
+
+        /*************************************************************************
+        Unserialization of MultiLayerPerceptron strucure
+
+        INPUT PARAMETERS:
+            RA      -   real array which stores network
+
+        OUTPUT PARAMETERS:
+            Network -   restored network
+
+          -- ALGLIB --
+             Copyright 29.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpunserializeold(double[] ra,
+            multilayerperceptron network)
+        {
+            int i = 0;
+            int ssize = 0;
+            int ntotal = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            int sigmalen = 0;
+            int offs = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            ap.assert((int)Math.Round(ra[1])==mlpvnum, "MLPUnserialize: incorrect array!");
+            
+            //
+            // Unload StructInfo from IA
+            //
+            offs = 3;
+            ssize = (int)Math.Round(ra[2]);
+            network.structinfo = new int[ssize-1+1];
+            for(i=0; i<=ssize-1; i++)
+            {
+                network.structinfo[i] = (int)Math.Round(ra[offs+i]);
+            }
+            offs = offs+ssize;
+            
+            //
+            // Unload info from StructInfo
+            //
+            ssize = network.structinfo[0];
+            nin = network.structinfo[1];
+            nout = network.structinfo[2];
+            ntotal = network.structinfo[3];
+            wcount = network.structinfo[4];
+            if( network.structinfo[6]==0 )
+            {
+                sigmalen = nin+nout;
+            }
+            else
+            {
+                sigmalen = nin;
+            }
+            
+            //
+            // Allocate space for other fields
+            //
+            network.weights = new double[wcount-1+1];
+            network.columnmeans = new double[sigmalen-1+1];
+            network.columnsigmas = new double[sigmalen-1+1];
+            network.neurons = new double[ntotal-1+1];
+            network.chunks = new double[3*ntotal+1, chunksize-1+1];
+            network.nwbuf = new double[Math.Max(wcount, 2*nout)-1+1];
+            network.dfdnet = new double[ntotal-1+1];
+            network.x = new double[nin-1+1];
+            network.y = new double[nout-1+1];
+            network.derror = new double[ntotal-1+1];
+            
+            //
+            // Copy parameters from RA
+            //
+            i1_ = (offs) - (0);
+            for(i_=0; i_<=wcount-1;i_++)
+            {
+                network.weights[i_] = ra[i_+i1_];
+            }
+            offs = offs+wcount;
+            i1_ = (offs) - (0);
+            for(i_=0; i_<=sigmalen-1;i_++)
+            {
+                network.columnmeans[i_] = ra[i_+i1_];
+            }
+            offs = offs+sigmalen;
+            i1_ = (offs) - (0);
+            for(i_=0; i_<=sigmalen-1;i_++)
+            {
+                network.columnsigmas[i_] = ra[i_+i1_];
+            }
+            offs = offs+sigmalen;
+        }
+
+
+        /*************************************************************************
+        Randomization of neural network weights
+
+          -- ALGLIB --
+             Copyright 06.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlprandomize(multilayerperceptron network)
+        {
+            int i = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            for(i=0; i<=wcount-1; i++)
+            {
+                network.weights[i] = math.randomreal()-0.5;
+            }
+        }
+
+
+        /*************************************************************************
+        Randomization of neural network weights and standartisator
+
+          -- ALGLIB --
+             Copyright 10.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlprandomizefull(multilayerperceptron network)
+        {
+            int i = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            int ntotal = 0;
+            int istart = 0;
+            int offs = 0;
+            int ntype = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            ntotal = network.structinfo[3];
+            istart = network.structinfo[5];
+            
+            //
+            // Process network
+            //
+            for(i=0; i<=wcount-1; i++)
+            {
+                network.weights[i] = math.randomreal()-0.5;
+            }
+            for(i=0; i<=nin-1; i++)
+            {
+                network.columnmeans[i] = 2*math.randomreal()-1;
+                network.columnsigmas[i] = 1.5*math.randomreal()+0.5;
+            }
+            if( !mlpissoftmax(network) )
+            {
+                for(i=0; i<=nout-1; i++)
+                {
+                    offs = istart+(ntotal-nout+i)*nfieldwidth;
+                    ntype = network.structinfo[offs+0];
+                    if( ntype==0 )
+                    {
+                        
+                        //
+                        // Shifts are changed only for linear outputs neurons
+                        //
+                        network.columnmeans[nin+i] = 2*math.randomreal()-1;
+                    }
+                    if( ntype==0 | ntype==3 )
+                    {
+                        
+                        //
+                        // Scales are changed only for linear or bounded outputs neurons.
+                        // Note that scale randomization preserves sign.
+                        //
+                        network.columnsigmas[nin+i] = Math.Sign(network.columnsigmas[nin+i])*(1.5*math.randomreal()+0.5);
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine.
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpinitpreprocessor(multilayerperceptron network,
+            double[,] xy,
+            int ssize)
+        {
+            int i = 0;
+            int j = 0;
+            int jmax = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            int ntotal = 0;
+            int istart = 0;
+            int offs = 0;
+            int ntype = 0;
+            double[] means = new double[0];
+            double[] sigmas = new double[0];
+            double s = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            ntotal = network.structinfo[3];
+            istart = network.structinfo[5];
+            
+            //
+            // Means/Sigmas
+            //
+            if( mlpissoftmax(network) )
+            {
+                jmax = nin-1;
+            }
+            else
+            {
+                jmax = nin+nout-1;
+            }
+            means = new double[jmax+1];
+            sigmas = new double[jmax+1];
+            for(j=0; j<=jmax; j++)
+            {
+                means[j] = 0;
+                for(i=0; i<=ssize-1; i++)
+                {
+                    means[j] = means[j]+xy[i,j];
+                }
+                means[j] = means[j]/ssize;
+                sigmas[j] = 0;
+                for(i=0; i<=ssize-1; i++)
+                {
+                    sigmas[j] = sigmas[j]+math.sqr(xy[i,j]-means[j]);
+                }
+                sigmas[j] = Math.Sqrt(sigmas[j]/ssize);
+            }
+            
+            //
+            // Inputs
+            //
+            for(i=0; i<=nin-1; i++)
+            {
+                network.columnmeans[i] = means[i];
+                network.columnsigmas[i] = sigmas[i];
+                if( (double)(network.columnsigmas[i])==(double)(0) )
+                {
+                    network.columnsigmas[i] = 1;
+                }
+            }
+            
+            //
+            // Outputs
+            //
+            if( !mlpissoftmax(network) )
+            {
+                for(i=0; i<=nout-1; i++)
+                {
+                    offs = istart+(ntotal-nout+i)*nfieldwidth;
+                    ntype = network.structinfo[offs+0];
+                    
+                    //
+                    // Linear outputs
+                    //
+                    if( ntype==0 )
+                    {
+                        network.columnmeans[nin+i] = means[nin+i];
+                        network.columnsigmas[nin+i] = sigmas[nin+i];
+                        if( (double)(network.columnsigmas[nin+i])==(double)(0) )
+                        {
+                            network.columnsigmas[nin+i] = 1;
+                        }
+                    }
+                    
+                    //
+                    // Bounded outputs (half-interval)
+                    //
+                    if( ntype==3 )
+                    {
+                        s = means[nin+i]-network.columnmeans[nin+i];
+                        if( (double)(s)==(double)(0) )
+                        {
+                            s = Math.Sign(network.columnsigmas[nin+i]);
+                        }
+                        if( (double)(s)==(double)(0) )
+                        {
+                            s = 1.0;
+                        }
+                        network.columnsigmas[nin+i] = Math.Sign(network.columnsigmas[nin+i])*Math.Abs(s);
+                        if( (double)(network.columnsigmas[nin+i])==(double)(0) )
+                        {
+                            network.columnsigmas[nin+i] = 1;
+                        }
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Returns information about initialized network: number of inputs, outputs,
+        weights.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpproperties(multilayerperceptron network,
+            ref int nin,
+            ref int nout,
+            ref int wcount)
+        {
+            nin = 0;
+            nout = 0;
+            wcount = 0;
+
+            nin = network.structinfo[1];
+            nout = network.structinfo[2];
+            wcount = network.structinfo[4];
+        }
+
+
+        /*************************************************************************
+        Tells whether network is SOFTMAX-normalized (i.e. classifier) or not.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool mlpissoftmax(multilayerperceptron network)
+        {
+            bool result = new bool();
+
+            result = network.structinfo[6]==1;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function returns total number of layers (including input, hidden and
+        output layers).
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static int mlpgetlayerscount(multilayerperceptron network)
+        {
+            int result = 0;
+
+            result = ap.len(network.hllayersizes);
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function returns size of K-th layer.
+
+        K=0 corresponds to input layer, K=CNT-1 corresponds to output layer.
+
+        Size of the output layer is always equal to the number of outputs, although
+        when we have softmax-normalized network, last neuron doesn't have any
+        connections - it is just zero.
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static int mlpgetlayersize(multilayerperceptron network,
+            int k)
+        {
+            int result = 0;
+
+            ap.assert(k>=0 & k<ap.len(network.hllayersizes), "MLPGetLayerSize: incorrect layer index");
+            result = network.hllayersizes[k];
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function returns offset/scaling coefficients for I-th input of the
+        network.
+
+        INPUT PARAMETERS:
+            Network     -   network
+            I           -   input index
+
+        OUTPUT PARAMETERS:
+            Mean        -   mean term
+            Sigma       -   sigma term, guaranteed to be nonzero.
+
+        I-th input is passed through linear transformation
+            IN[i] = (IN[i]-Mean)/Sigma
+        before feeding to the network
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpgetinputscaling(multilayerperceptron network,
+            int i,
+            ref double mean,
+            ref double sigma)
+        {
+            mean = 0;
+            sigma = 0;
+
+            ap.assert(i>=0 & i<network.hllayersizes[0], "MLPGetInputScaling: incorrect (nonexistent) I");
+            mean = network.columnmeans[i];
+            sigma = network.columnsigmas[i];
+            if( (double)(sigma)==(double)(0) )
+            {
+                sigma = 1;
+            }
+        }
+
+
+        /*************************************************************************
+        This function returns offset/scaling coefficients for I-th output of the
+        network.
+
+        INPUT PARAMETERS:
+            Network     -   network
+            I           -   input index
+
+        OUTPUT PARAMETERS:
+            Mean        -   mean term
+            Sigma       -   sigma term, guaranteed to be nonzero.
+
+        I-th output is passed through linear transformation
+            OUT[i] = OUT[i]*Sigma+Mean
+        before returning it to user. In case we have SOFTMAX-normalized network,
+        we return (Mean,Sigma)=(0.0,1.0).
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpgetoutputscaling(multilayerperceptron network,
+            int i,
+            ref double mean,
+            ref double sigma)
+        {
+            mean = 0;
+            sigma = 0;
+
+            ap.assert(i>=0 & i<network.hllayersizes[ap.len(network.hllayersizes)-1], "MLPGetOutputScaling: incorrect (nonexistent) I");
+            if( network.structinfo[6]==1 )
+            {
+                mean = 0;
+                sigma = 1;
+            }
+            else
+            {
+                mean = network.columnmeans[network.hllayersizes[0]+i];
+                sigma = network.columnsigmas[network.hllayersizes[0]+i];
+            }
+        }
+
+
+        /*************************************************************************
+        This function returns information about Ith neuron of Kth layer
+
+        INPUT PARAMETERS:
+            Network     -   network
+            K           -   layer index
+            I           -   neuron index (within layer)
+
+        OUTPUT PARAMETERS:
+            FKind       -   activation function type (used by MLPActivationFunction())
+                            this value is zero for input or linear neurons
+            Threshold   -   also called offset, bias
+                            zero for input neurons
+                            
+        NOTE: this function throws exception if layer or neuron with  given  index
+        do not exists.
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpgetneuroninfo(multilayerperceptron network,
+            int k,
+            int i,
+            ref int fkind,
+            ref double threshold)
+        {
+            int ncnt = 0;
+            int istart = 0;
+            int highlevelidx = 0;
+            int activationoffset = 0;
+
+            fkind = 0;
+            threshold = 0;
+
+            ncnt = ap.len(network.hlneurons)/hlnfieldwidth;
+            istart = network.structinfo[5];
+            
+            //
+            // search
+            //
+            network.integerbuf[0] = k;
+            network.integerbuf[1] = i;
+            highlevelidx = apserv.recsearch(ref network.hlneurons, hlnfieldwidth, 2, 0, ncnt, network.integerbuf);
+            ap.assert(highlevelidx>=0, "MLPGetNeuronInfo: incorrect (nonexistent) layer or neuron index");
+            
+            //
+            // 1. find offset of the activation function record in the
+            //
+            if( network.hlneurons[highlevelidx*hlnfieldwidth+2]>=0 )
+            {
+                activationoffset = istart+network.hlneurons[highlevelidx*hlnfieldwidth+2]*nfieldwidth;
+                fkind = network.structinfo[activationoffset+0];
+            }
+            else
+            {
+                fkind = 0;
+            }
+            if( network.hlneurons[highlevelidx*hlnfieldwidth+3]>=0 )
+            {
+                threshold = network.weights[network.hlneurons[highlevelidx*hlnfieldwidth+3]];
+            }
+            else
+            {
+                threshold = 0;
+            }
+        }
+
+
+        /*************************************************************************
+        This function returns information about connection from I0-th neuron of
+        K0-th layer to I1-th neuron of K1-th layer.
+
+        INPUT PARAMETERS:
+            Network     -   network
+            K0          -   layer index
+            I0          -   neuron index (within layer)
+            K1          -   layer index
+            I1          -   neuron index (within layer)
+
+        RESULT:
+            connection weight (zero for non-existent connections)
+
+        This function:
+        1. throws exception if layer or neuron with given index do not exists.
+        2. returns zero if neurons exist, but there is no connection between them
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpgetweight(multilayerperceptron network,
+            int k0,
+            int i0,
+            int k1,
+            int i1)
+        {
+            double result = 0;
+            int ccnt = 0;
+            int highlevelidx = 0;
+
+            ccnt = ap.len(network.hlconnections)/hlconnfieldwidth;
+            
+            //
+            // check params
+            //
+            ap.assert(k0>=0 & k0<ap.len(network.hllayersizes), "MLPGetWeight: incorrect (nonexistent) K0");
+            ap.assert(i0>=0 & i0<network.hllayersizes[k0], "MLPGetWeight: incorrect (nonexistent) I0");
+            ap.assert(k1>=0 & k1<ap.len(network.hllayersizes), "MLPGetWeight: incorrect (nonexistent) K1");
+            ap.assert(i1>=0 & i1<network.hllayersizes[k1], "MLPGetWeight: incorrect (nonexistent) I1");
+            
+            //
+            // search
+            //
+            network.integerbuf[0] = k0;
+            network.integerbuf[1] = i0;
+            network.integerbuf[2] = k1;
+            network.integerbuf[3] = i1;
+            highlevelidx = apserv.recsearch(ref network.hlconnections, hlconnfieldwidth, 4, 0, ccnt, network.integerbuf);
+            if( highlevelidx>=0 )
+            {
+                result = network.weights[network.hlconnections[highlevelidx*hlconnfieldwidth+4]];
+            }
+            else
+            {
+                result = 0;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function sets offset/scaling coefficients for I-th input of the
+        network.
+
+        INPUT PARAMETERS:
+            Network     -   network
+            I           -   input index
+            Mean        -   mean term
+            Sigma       -   sigma term (if zero, will be replaced by 1.0)
+
+        NTE: I-th input is passed through linear transformation
+            IN[i] = (IN[i]-Mean)/Sigma
+        before feeding to the network. This function sets Mean and Sigma.
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpsetinputscaling(multilayerperceptron network,
+            int i,
+            double mean,
+            double sigma)
+        {
+            ap.assert(i>=0 & i<network.hllayersizes[0], "MLPSetInputScaling: incorrect (nonexistent) I");
+            ap.assert(math.isfinite(mean), "MLPSetInputScaling: infinite or NAN Mean");
+            ap.assert(math.isfinite(sigma), "MLPSetInputScaling: infinite or NAN Sigma");
+            if( (double)(sigma)==(double)(0) )
+            {
+                sigma = 1;
+            }
+            network.columnmeans[i] = mean;
+            network.columnsigmas[i] = sigma;
+        }
+
+
+        /*************************************************************************
+        This function sets offset/scaling coefficients for I-th output of the
+        network.
+
+        INPUT PARAMETERS:
+            Network     -   network
+            I           -   input index
+            Mean        -   mean term
+            Sigma       -   sigma term (if zero, will be replaced by 1.0)
+
+        OUTPUT PARAMETERS:
+
+        NOTE: I-th output is passed through linear transformation
+            OUT[i] = OUT[i]*Sigma+Mean
+        before returning it to user. This function sets Sigma/Mean. In case we
+        have SOFTMAX-normalized network, you can not set (Sigma,Mean) to anything
+        other than(0.0,1.0) - this function will throw exception.
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpsetoutputscaling(multilayerperceptron network,
+            int i,
+            double mean,
+            double sigma)
+        {
+            ap.assert(i>=0 & i<network.hllayersizes[ap.len(network.hllayersizes)-1], "MLPSetOutputScaling: incorrect (nonexistent) I");
+            ap.assert(math.isfinite(mean), "MLPSetOutputScaling: infinite or NAN Mean");
+            ap.assert(math.isfinite(sigma), "MLPSetOutputScaling: infinite or NAN Sigma");
+            if( network.structinfo[6]==1 )
+            {
+                ap.assert((double)(mean)==(double)(0), "MLPSetOutputScaling: you can not set non-zero Mean term for classifier network");
+                ap.assert((double)(sigma)==(double)(1), "MLPSetOutputScaling: you can not set non-unit Sigma term for classifier network");
+            }
+            else
+            {
+                if( (double)(sigma)==(double)(0) )
+                {
+                    sigma = 1;
+                }
+                network.columnmeans[network.hllayersizes[0]+i] = mean;
+                network.columnsigmas[network.hllayersizes[0]+i] = sigma;
+            }
+        }
+
+
+        /*************************************************************************
+        This function modifies information about Ith neuron of Kth layer
+
+        INPUT PARAMETERS:
+            Network     -   network
+            K           -   layer index
+            I           -   neuron index (within layer)
+            FKind       -   activation function type (used by MLPActivationFunction())
+                            this value must be zero for input neurons
+                            (you can not set activation function for input neurons)
+            Threshold   -   also called offset, bias
+                            this value must be zero for input neurons
+                            (you can not set threshold for input neurons)
+
+        NOTES:
+        1. this function throws exception if layer or neuron with given index do
+           not exists.
+        2. this function also throws exception when you try to set non-linear
+           activation function for input neurons (any kind of network) or for output
+           neurons of classifier network.
+        3. this function throws exception when you try to set non-zero threshold for
+           input neurons (any kind of network).
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpsetneuroninfo(multilayerperceptron network,
+            int k,
+            int i,
+            int fkind,
+            double threshold)
+        {
+            int ncnt = 0;
+            int istart = 0;
+            int highlevelidx = 0;
+            int activationoffset = 0;
+
+            ap.assert(math.isfinite(threshold), "MLPSetNeuronInfo: infinite or NAN Threshold");
+            
+            //
+            // convenience vars
+            //
+            ncnt = ap.len(network.hlneurons)/hlnfieldwidth;
+            istart = network.structinfo[5];
+            
+            //
+            // search
+            //
+            network.integerbuf[0] = k;
+            network.integerbuf[1] = i;
+            highlevelidx = apserv.recsearch(ref network.hlneurons, hlnfieldwidth, 2, 0, ncnt, network.integerbuf);
+            ap.assert(highlevelidx>=0, "MLPSetNeuronInfo: incorrect (nonexistent) layer or neuron index");
+            
+            //
+            // activation function
+            //
+            if( network.hlneurons[highlevelidx*hlnfieldwidth+2]>=0 )
+            {
+                activationoffset = istart+network.hlneurons[highlevelidx*hlnfieldwidth+2]*nfieldwidth;
+                network.structinfo[activationoffset+0] = fkind;
+            }
+            else
+            {
+                ap.assert(fkind==0, "MLPSetNeuronInfo: you try to set activation function for neuron which can not have one");
+            }
+            
+            //
+            // Threshold
+            //
+            if( network.hlneurons[highlevelidx*hlnfieldwidth+3]>=0 )
+            {
+                network.weights[network.hlneurons[highlevelidx*hlnfieldwidth+3]] = threshold;
+            }
+            else
+            {
+                ap.assert((double)(threshold)==(double)(0), "MLPSetNeuronInfo: you try to set non-zero threshold for neuron which can not have one");
+            }
+        }
+
+
+        /*************************************************************************
+        This function modifies information about connection from I0-th neuron of
+        K0-th layer to I1-th neuron of K1-th layer.
+
+        INPUT PARAMETERS:
+            Network     -   network
+            K0          -   layer index
+            I0          -   neuron index (within layer)
+            K1          -   layer index
+            I1          -   neuron index (within layer)
+            W           -   connection weight (must be zero for non-existent
+                            connections)
+
+        This function:
+        1. throws exception if layer or neuron with given index do not exists.
+        2. throws exception if you try to set non-zero weight for non-existent
+           connection
+
+          -- ALGLIB --
+             Copyright 25.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpsetweight(multilayerperceptron network,
+            int k0,
+            int i0,
+            int k1,
+            int i1,
+            double w)
+        {
+            int ccnt = 0;
+            int highlevelidx = 0;
+
+            ccnt = ap.len(network.hlconnections)/hlconnfieldwidth;
+            
+            //
+            // check params
+            //
+            ap.assert(k0>=0 & k0<ap.len(network.hllayersizes), "MLPSetWeight: incorrect (nonexistent) K0");
+            ap.assert(i0>=0 & i0<network.hllayersizes[k0], "MLPSetWeight: incorrect (nonexistent) I0");
+            ap.assert(k1>=0 & k1<ap.len(network.hllayersizes), "MLPSetWeight: incorrect (nonexistent) K1");
+            ap.assert(i1>=0 & i1<network.hllayersizes[k1], "MLPSetWeight: incorrect (nonexistent) I1");
+            ap.assert(math.isfinite(w), "MLPSetWeight: infinite or NAN weight");
+            
+            //
+            // search
+            //
+            network.integerbuf[0] = k0;
+            network.integerbuf[1] = i0;
+            network.integerbuf[2] = k1;
+            network.integerbuf[3] = i1;
+            highlevelidx = apserv.recsearch(ref network.hlconnections, hlconnfieldwidth, 4, 0, ccnt, network.integerbuf);
+            if( highlevelidx>=0 )
+            {
+                network.weights[network.hlconnections[highlevelidx*hlconnfieldwidth+4]] = w;
+            }
+            else
+            {
+                ap.assert((double)(w)==(double)(0), "MLPSetWeight: you try to set non-zero weight for non-existent connection");
+            }
+        }
+
+
+        /*************************************************************************
+        Neural network activation function
+
+        INPUT PARAMETERS:
+            NET         -   neuron input
+            K           -   function index (zero for linear function)
+
+        OUTPUT PARAMETERS:
+            F           -   function
+            DF          -   its derivative
+            D2F         -   its second derivative
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpactivationfunction(double net,
+            int k,
+            ref double f,
+            ref double df,
+            ref double d2f)
+        {
+            double net2 = 0;
+            double arg = 0;
+            double root = 0;
+            double r = 0;
+
+            f = 0;
+            df = 0;
+            d2f = 0;
+
+            if( k==0 | k==-5 )
+            {
+                f = net;
+                df = 1;
+                d2f = 0;
+                return;
+            }
+            if( k==1 )
+            {
+                
+                //
+                // TanH activation function
+                //
+                if( (double)(Math.Abs(net))<(double)(100) )
+                {
+                    f = Math.Tanh(net);
+                }
+                else
+                {
+                    f = Math.Sign(net);
+                }
+                df = 1-math.sqr(f);
+                d2f = -(2*f*df);
+                return;
+            }
+            if( k==3 )
+            {
+                
+                //
+                // EX activation function
+                //
+                if( (double)(net)>=(double)(0) )
+                {
+                    net2 = net*net;
+                    arg = net2+1;
+                    root = Math.Sqrt(arg);
+                    f = net+root;
+                    r = net/root;
+                    df = 1+r;
+                    d2f = (root-net*r)/arg;
+                }
+                else
+                {
+                    f = Math.Exp(net);
+                    df = f;
+                    d2f = f;
+                }
+                return;
+            }
+            if( k==2 )
+            {
+                f = Math.Exp(-math.sqr(net));
+                df = -(2*net*f);
+                d2f = -(2*(f+df*net));
+                return;
+            }
+            f = 0;
+            df = 0;
+            d2f = 0;
+        }
+
+
+        /*************************************************************************
+        Procesing
+
+        INPUT PARAMETERS:
+            Network -   neural network
+            X       -   input vector,  array[0..NIn-1].
+
+        OUTPUT PARAMETERS:
+            Y       -   result. Regression estimate when solving regression  task,
+                        vector of posterior probabilities for classification task.
+
+        See also MLPProcessI
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpprocess(multilayerperceptron network,
+            double[] x,
+            ref double[] y)
+        {
+            if( ap.len(y)<network.structinfo[2] )
+            {
+                y = new double[network.structinfo[2]];
+            }
+            mlpinternalprocessvector(network.structinfo, network.weights, network.columnmeans, network.columnsigmas, ref network.neurons, ref network.dfdnet, x, ref y);
+        }
+
+
+        /*************************************************************************
+        'interactive'  variant  of  MLPProcess  for  languages  like  Python which
+        support constructs like "Y = MLPProcess(NN,X)" and interactive mode of the
+        interpreter
+
+        This function allocates new array on each call,  so  it  is  significantly
+        slower than its 'non-interactive' counterpart, but it is  more  convenient
+        when you call it from command line.
+
+          -- ALGLIB --
+             Copyright 21.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpprocessi(multilayerperceptron network,
+            double[] x,
+            ref double[] y)
+        {
+            y = new double[0];
+
+            mlpprocess(network, x, ref y);
+        }
+
+
+        /*************************************************************************
+        Error function for neural network, internal subroutine.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlperror(multilayerperceptron network,
+            double[,] xy,
+            int ssize)
+        {
+            double result = 0;
+            int i = 0;
+            int k = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            double e = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            result = 0;
+            for(i=0; i<=ssize-1; i++)
+            {
+                for(i_=0; i_<=nin-1;i_++)
+                {
+                    network.x[i_] = xy[i,i_];
+                }
+                mlpprocess(network, network.x, ref network.y);
+                if( mlpissoftmax(network) )
+                {
+                    
+                    //
+                    // class labels outputs
+                    //
+                    k = (int)Math.Round(xy[i,nin]);
+                    if( k>=0 & k<nout )
+                    {
+                        network.y[k] = network.y[k]-1;
+                    }
+                }
+                else
+                {
+                    
+                    //
+                    // real outputs
+                    //
+                    i1_ = (nin) - (0);
+                    for(i_=0; i_<=nout-1;i_++)
+                    {
+                        network.y[i_] = network.y[i_] - xy[i,i_+i1_];
+                    }
+                }
+                e = 0.0;
+                for(i_=0; i_<=nout-1;i_++)
+                {
+                    e += network.y[i_]*network.y[i_];
+                }
+                result = result+e/2;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Natural error function for neural network, internal subroutine.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlperrorn(multilayerperceptron network,
+            double[,] xy,
+            int ssize)
+        {
+            double result = 0;
+            int i = 0;
+            int k = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            double e = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            result = 0;
+            for(i=0; i<=ssize-1; i++)
+            {
+                
+                //
+                // Process vector
+                //
+                for(i_=0; i_<=nin-1;i_++)
+                {
+                    network.x[i_] = xy[i,i_];
+                }
+                mlpprocess(network, network.x, ref network.y);
+                
+                //
+                // Update error function
+                //
+                if( network.structinfo[6]==0 )
+                {
+                    
+                    //
+                    // Least squares error function
+                    //
+                    i1_ = (nin) - (0);
+                    for(i_=0; i_<=nout-1;i_++)
+                    {
+                        network.y[i_] = network.y[i_] - xy[i,i_+i1_];
+                    }
+                    e = 0.0;
+                    for(i_=0; i_<=nout-1;i_++)
+                    {
+                        e += network.y[i_]*network.y[i_];
+                    }
+                    result = result+e/2;
+                }
+                else
+                {
+                    
+                    //
+                    // Cross-entropy error function
+                    //
+                    k = (int)Math.Round(xy[i,nin]);
+                    if( k>=0 & k<nout )
+                    {
+                        result = result+safecrossentropy(1, network.y[k]);
+                    }
+                }
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Classification error
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static int mlpclserror(multilayerperceptron network,
+            double[,] xy,
+            int ssize)
+        {
+            int result = 0;
+            int i = 0;
+            int j = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            double[] workx = new double[0];
+            double[] worky = new double[0];
+            int nn = 0;
+            int ns = 0;
+            int nmax = 0;
+            int i_ = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            workx = new double[nin-1+1];
+            worky = new double[nout-1+1];
+            result = 0;
+            for(i=0; i<=ssize-1; i++)
+            {
+                
+                //
+                // Process
+                //
+                for(i_=0; i_<=nin-1;i_++)
+                {
+                    workx[i_] = xy[i,i_];
+                }
+                mlpprocess(network, workx, ref worky);
+                
+                //
+                // Network version of the answer
+                //
+                nmax = 0;
+                for(j=0; j<=nout-1; j++)
+                {
+                    if( (double)(worky[j])>(double)(worky[nmax]) )
+                    {
+                        nmax = j;
+                    }
+                }
+                nn = nmax;
+                
+                //
+                // Right answer
+                //
+                if( mlpissoftmax(network) )
+                {
+                    ns = (int)Math.Round(xy[i,nin]);
+                }
+                else
+                {
+                    nmax = 0;
+                    for(j=0; j<=nout-1; j++)
+                    {
+                        if( (double)(xy[i,nin+j])>(double)(xy[i,nin+nmax]) )
+                        {
+                            nmax = j;
+                        }
+                    }
+                    ns = nmax;
+                }
+                
+                //
+                // compare
+                //
+                if( nn!=ns )
+                {
+                    result = result+1;
+                }
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Relative classification error on the test set
+
+        INPUT PARAMETERS:
+            Network -   network
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            percent of incorrectly classified cases. Works both for
+            classifier networks and general purpose networks used as
+            classifiers.
+
+          -- ALGLIB --
+             Copyright 25.12.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlprelclserror(multilayerperceptron network,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+
+            result = (double)mlpclserror(network, xy, npoints)/(double)npoints;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Average cross-entropy (in bits per element) on the test set
+
+        INPUT PARAMETERS:
+            Network -   neural network
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            CrossEntropy/(NPoints*LN(2)).
+            Zero if network solves regression task.
+
+          -- ALGLIB --
+             Copyright 08.01.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpavgce(multilayerperceptron network,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+
+            if( mlpissoftmax(network) )
+            {
+                mlpproperties(network, ref nin, ref nout, ref wcount);
+                result = mlperrorn(network, xy, npoints)/(npoints*Math.Log(2));
+            }
+            else
+            {
+                result = 0;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        RMS error on the test set
+
+        INPUT PARAMETERS:
+            Network -   neural network
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            root mean square error.
+            Its meaning for regression task is obvious. As for
+            classification task, RMS error means error when estimating posterior
+            probabilities.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlprmserror(multilayerperceptron network,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            result = Math.Sqrt(2*mlperror(network, xy, npoints)/(npoints*nout));
+            return result;
+        }
+
+
+        /*************************************************************************
+        Average error on the test set
+
+        INPUT PARAMETERS:
+            Network -   neural network
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            Its meaning for regression task is obvious. As for
+            classification task, it means average error when estimating posterior
+            probabilities.
+
+          -- ALGLIB --
+             Copyright 11.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpavgerror(multilayerperceptron network,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            int i_ = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            result = 0;
+            for(i=0; i<=npoints-1; i++)
+            {
+                for(i_=0; i_<=nin-1;i_++)
+                {
+                    network.x[i_] = xy[i,i_];
+                }
+                mlpprocess(network, network.x, ref network.y);
+                if( mlpissoftmax(network) )
+                {
+                    
+                    //
+                    // class labels
+                    //
+                    k = (int)Math.Round(xy[i,nin]);
+                    for(j=0; j<=nout-1; j++)
+                    {
+                        if( j==k )
+                        {
+                            result = result+Math.Abs(1-network.y[j]);
+                        }
+                        else
+                        {
+                            result = result+Math.Abs(network.y[j]);
+                        }
+                    }
+                }
+                else
+                {
+                    
+                    //
+                    // real outputs
+                    //
+                    for(j=0; j<=nout-1; j++)
+                    {
+                        result = result+Math.Abs(xy[i,nin+j]-network.y[j]);
+                    }
+                }
+            }
+            result = result/(npoints*nout);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Average relative error on the test set
+
+        INPUT PARAMETERS:
+            Network -   neural network
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            Its meaning for regression task is obvious. As for
+            classification task, it means average relative error when estimating
+            posterior probability of belonging to the correct class.
+
+          -- ALGLIB --
+             Copyright 11.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpavgrelerror(multilayerperceptron network,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int lk = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            int i_ = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            result = 0;
+            k = 0;
+            for(i=0; i<=npoints-1; i++)
+            {
+                for(i_=0; i_<=nin-1;i_++)
+                {
+                    network.x[i_] = xy[i,i_];
+                }
+                mlpprocess(network, network.x, ref network.y);
+                if( mlpissoftmax(network) )
+                {
+                    
+                    //
+                    // class labels
+                    //
+                    lk = (int)Math.Round(xy[i,nin]);
+                    for(j=0; j<=nout-1; j++)
+                    {
+                        if( j==lk )
+                        {
+                            result = result+Math.Abs(1-network.y[j]);
+                            k = k+1;
+                        }
+                    }
+                }
+                else
+                {
+                    
+                    //
+                    // real outputs
+                    //
+                    for(j=0; j<=nout-1; j++)
+                    {
+                        if( (double)(xy[i,nin+j])!=(double)(0) )
+                        {
+                            result = result+Math.Abs(xy[i,nin+j]-network.y[j])/Math.Abs(xy[i,nin+j]);
+                            k = k+1;
+                        }
+                    }
+                }
+            }
+            if( k!=0 )
+            {
+                result = result/k;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Gradient calculation
+
+        INPUT PARAMETERS:
+            Network -   network initialized with one of the network creation funcs
+            X       -   input vector, length of array must be at least NIn
+            DesiredY-   desired outputs, length of array must be at least NOut
+            Grad    -   possibly preallocated array. If size of array is smaller
+                        than WCount, it will be reallocated. It is recommended to
+                        reuse previously allocated array to reduce allocation
+                        overhead.
+
+        OUTPUT PARAMETERS:
+            E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
+            Grad    -   gradient of E with respect to weights of network, array[WCount]
+            
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpgrad(multilayerperceptron network,
+            double[] x,
+            double[] desiredy,
+            ref double e,
+            ref double[] grad)
+        {
+            int i = 0;
+            int nout = 0;
+            int ntotal = 0;
+
+            e = 0;
+
+            
+            //
+            // Alloc
+            //
+            if( ap.len(grad)<network.structinfo[4] )
+            {
+                grad = new double[network.structinfo[4]];
+            }
+            
+            //
+            // Prepare dError/dOut, internal structures
+            //
+            mlpprocess(network, x, ref network.y);
+            nout = network.structinfo[2];
+            ntotal = network.structinfo[3];
+            e = 0;
+            for(i=0; i<=ntotal-1; i++)
+            {
+                network.derror[i] = 0;
+            }
+            for(i=0; i<=nout-1; i++)
+            {
+                network.derror[ntotal-nout+i] = network.y[i]-desiredy[i];
+                e = e+math.sqr(network.y[i]-desiredy[i])/2;
+            }
+            
+            //
+            // gradient
+            //
+            mlpinternalcalculategradient(network, network.neurons, network.weights, ref network.derror, ref grad, false);
+        }
+
+
+        /*************************************************************************
+        Gradient calculation (natural error function is used)
+
+        INPUT PARAMETERS:
+            Network -   network initialized with one of the network creation funcs
+            X       -   input vector, length of array must be at least NIn
+            DesiredY-   desired outputs, length of array must be at least NOut
+            Grad    -   possibly preallocated array. If size of array is smaller
+                        than WCount, it will be reallocated. It is recommended to
+                        reuse previously allocated array to reduce allocation
+                        overhead.
+
+        OUTPUT PARAMETERS:
+            E       -   error function, sum-of-squares for regression networks,
+                        cross-entropy for classification networks.
+            Grad    -   gradient of E with respect to weights of network, array[WCount]
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpgradn(multilayerperceptron network,
+            double[] x,
+            double[] desiredy,
+            ref double e,
+            ref double[] grad)
+        {
+            double s = 0;
+            int i = 0;
+            int nout = 0;
+            int ntotal = 0;
+
+            e = 0;
+
+            
+            //
+            // Alloc
+            //
+            if( ap.len(grad)<network.structinfo[4] )
+            {
+                grad = new double[network.structinfo[4]];
+            }
+            
+            //
+            // Prepare dError/dOut, internal structures
+            //
+            mlpprocess(network, x, ref network.y);
+            nout = network.structinfo[2];
+            ntotal = network.structinfo[3];
+            for(i=0; i<=ntotal-1; i++)
+            {
+                network.derror[i] = 0;
+            }
+            e = 0;
+            if( network.structinfo[6]==0 )
+            {
+                
+                //
+                // Regression network, least squares
+                //
+                for(i=0; i<=nout-1; i++)
+                {
+                    network.derror[ntotal-nout+i] = network.y[i]-desiredy[i];
+                    e = e+math.sqr(network.y[i]-desiredy[i])/2;
+                }
+            }
+            else
+            {
+                
+                //
+                // Classification network, cross-entropy
+                //
+                s = 0;
+                for(i=0; i<=nout-1; i++)
+                {
+                    s = s+desiredy[i];
+                }
+                for(i=0; i<=nout-1; i++)
+                {
+                    network.derror[ntotal-nout+i] = s*network.y[i]-desiredy[i];
+                    e = e+safecrossentropy(desiredy[i], network.y[i]);
+                }
+            }
+            
+            //
+            // gradient
+            //
+            mlpinternalcalculategradient(network, network.neurons, network.weights, ref network.derror, ref grad, true);
+        }
+
+
+        /*************************************************************************
+        Batch gradient calculation for a set of inputs/outputs
+
+        INPUT PARAMETERS:
+            Network -   network initialized with one of the network creation funcs
+            XY      -   set of inputs/outputs; one sample = one row;
+                        first NIn columns contain inputs,
+                        next NOut columns - desired outputs.
+            SSize   -   number of elements in XY
+            Grad    -   possibly preallocated array. If size of array is smaller
+                        than WCount, it will be reallocated. It is recommended to
+                        reuse previously allocated array to reduce allocation
+                        overhead.
+
+        OUTPUT PARAMETERS:
+            E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
+            Grad    -   gradient of E with respect to weights of network, array[WCount]
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpgradbatch(multilayerperceptron network,
+            double[,] xy,
+            int ssize,
+            ref double e,
+            ref double[] grad)
+        {
+            int i = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+
+            e = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            for(i=0; i<=wcount-1; i++)
+            {
+                grad[i] = 0;
+            }
+            e = 0;
+            i = 0;
+            while( i<=ssize-1 )
+            {
+                mlpchunkedgradient(network, xy, i, Math.Min(ssize, i+chunksize)-i, ref e, ref grad, false);
+                i = i+chunksize;
+            }
+        }
+
+
+        /*************************************************************************
+        Batch gradient calculation for a set of inputs/outputs
+        (natural error function is used)
+
+        INPUT PARAMETERS:
+            Network -   network initialized with one of the network creation funcs
+            XY      -   set of inputs/outputs; one sample = one row;
+                        first NIn columns contain inputs,
+                        next NOut columns - desired outputs.
+            SSize   -   number of elements in XY
+            Grad    -   possibly preallocated array. If size of array is smaller
+                        than WCount, it will be reallocated. It is recommended to
+                        reuse previously allocated array to reduce allocation
+                        overhead.
+
+        OUTPUT PARAMETERS:
+            E       -   error function, sum-of-squares for regression networks,
+                        cross-entropy for classification networks.
+            Grad    -   gradient of E with respect to weights of network, array[WCount]
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpgradnbatch(multilayerperceptron network,
+            double[,] xy,
+            int ssize,
+            ref double e,
+            ref double[] grad)
+        {
+            int i = 0;
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+
+            e = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            for(i=0; i<=wcount-1; i++)
+            {
+                grad[i] = 0;
+            }
+            e = 0;
+            i = 0;
+            while( i<=ssize-1 )
+            {
+                mlpchunkedgradient(network, xy, i, Math.Min(ssize, i+chunksize)-i, ref e, ref grad, true);
+                i = i+chunksize;
+            }
+        }
+
+
+        /*************************************************************************
+        Batch Hessian calculation (natural error function) using R-algorithm.
+        Internal subroutine.
+
+          -- ALGLIB --
+             Copyright 26.01.2008 by Bochkanov Sergey.
+             
+             Hessian calculation based on R-algorithm described in
+             "Fast Exact Multiplication by the Hessian",
+             B. A. Pearlmutter,
+             Neural Computation, 1994.
+        *************************************************************************/
+        public static void mlphessiannbatch(multilayerperceptron network,
+            double[,] xy,
+            int ssize,
+            ref double e,
+            ref double[] grad,
+            ref double[,] h)
+        {
+            e = 0;
+
+            mlphessianbatchinternal(network, xy, ssize, true, ref e, ref grad, ref h);
+        }
+
+
+        /*************************************************************************
+        Batch Hessian calculation using R-algorithm.
+        Internal subroutine.
+
+          -- ALGLIB --
+             Copyright 26.01.2008 by Bochkanov Sergey.
+
+             Hessian calculation based on R-algorithm described in
+             "Fast Exact Multiplication by the Hessian",
+             B. A. Pearlmutter,
+             Neural Computation, 1994.
+        *************************************************************************/
+        public static void mlphessianbatch(multilayerperceptron network,
+            double[,] xy,
+            int ssize,
+            ref double e,
+            ref double[] grad,
+            ref double[,] h)
+        {
+            e = 0;
+
+            mlphessianbatchinternal(network, xy, ssize, false, ref e, ref grad, ref h);
+        }
+
+
+        /*************************************************************************
+        Internal subroutine, shouldn't be called by user.
+        *************************************************************************/
+        public static void mlpinternalprocessvector(int[] structinfo,
+            double[] weights,
+            double[] columnmeans,
+            double[] columnsigmas,
+            ref double[] neurons,
+            ref double[] dfdnet,
+            double[] x,
+            ref double[] y)
+        {
+            int i = 0;
+            int n1 = 0;
+            int n2 = 0;
+            int w1 = 0;
+            int w2 = 0;
+            int ntotal = 0;
+            int nin = 0;
+            int nout = 0;
+            int istart = 0;
+            int offs = 0;
+            double net = 0;
+            double f = 0;
+            double df = 0;
+            double d2f = 0;
+            double mx = 0;
+            bool perr = new bool();
+            int i_ = 0;
+            int i1_ = 0;
+
+            
+            //
+            // Read network geometry
+            //
+            nin = structinfo[1];
+            nout = structinfo[2];
+            ntotal = structinfo[3];
+            istart = structinfo[5];
+            
+            //
+            // Inputs standartisation and putting in the network
+            //
+            for(i=0; i<=nin-1; i++)
+            {
+                if( (double)(columnsigmas[i])!=(double)(0) )
+                {
+                    neurons[i] = (x[i]-columnmeans[i])/columnsigmas[i];
+                }
+                else
+                {
+                    neurons[i] = x[i]-columnmeans[i];
+                }
+            }
+            
+            //
+            // Process network
+            //
+            for(i=0; i<=ntotal-1; i++)
+            {
+                offs = istart+i*nfieldwidth;
+                if( structinfo[offs+0]>0 | structinfo[offs+0]==-5 )
+                {
+                    
+                    //
+                    // Activation function
+                    //
+                    mlpactivationfunction(neurons[structinfo[offs+2]], structinfo[offs+0], ref f, ref df, ref d2f);
+                    neurons[i] = f;
+                    dfdnet[i] = df;
+                    continue;
+                }
+                if( structinfo[offs+0]==0 )
+                {
+                    
+                    //
+                    // Adaptive summator
+                    //
+                    n1 = structinfo[offs+2];
+                    n2 = n1+structinfo[offs+1]-1;
+                    w1 = structinfo[offs+3];
+                    w2 = w1+structinfo[offs+1]-1;
+                    i1_ = (n1)-(w1);
+                    net = 0.0;
+                    for(i_=w1; i_<=w2;i_++)
+                    {
+                        net += weights[i_]*neurons[i_+i1_];
+                    }
+                    neurons[i] = net;
+                    dfdnet[i] = 1.0;
+                    continue;
+                }
+                if( structinfo[offs+0]<0 )
+                {
+                    perr = true;
+                    if( structinfo[offs+0]==-2 )
+                    {
+                        
+                        //
+                        // input neuron, left unchanged
+                        //
+                        perr = false;
+                    }
+                    if( structinfo[offs+0]==-3 )
+                    {
+                        
+                        //
+                        // "-1" neuron
+                        //
+                        neurons[i] = -1;
+                        perr = false;
+                    }
+                    if( structinfo[offs+0]==-4 )
+                    {
+                        
+                        //
+                        // "0" neuron
+                        //
+                        neurons[i] = 0;
+                        perr = false;
+                    }
+                    ap.assert(!perr, "MLPInternalProcessVector: internal error - unknown neuron type!");
+                    continue;
+                }
+            }
+            
+            //
+            // Extract result
+            //
+            i1_ = (ntotal-nout) - (0);
+            for(i_=0; i_<=nout-1;i_++)
+            {
+                y[i_] = neurons[i_+i1_];
+            }
+            
+            //
+            // Softmax post-processing or standardisation if needed
+            //
+            ap.assert(structinfo[6]==0 | structinfo[6]==1, "MLPInternalProcessVector: unknown normalization type!");
+            if( structinfo[6]==1 )
+            {
+                
+                //
+                // Softmax
+                //
+                mx = y[0];
+                for(i=1; i<=nout-1; i++)
+                {
+                    mx = Math.Max(mx, y[i]);
+                }
+                net = 0;
+                for(i=0; i<=nout-1; i++)
+                {
+                    y[i] = Math.Exp(y[i]-mx);
+                    net = net+y[i];
+                }
+                for(i=0; i<=nout-1; i++)
+                {
+                    y[i] = y[i]/net;
+                }
+            }
+            else
+            {
+                
+                //
+                // Standardisation
+                //
+                for(i=0; i<=nout-1; i++)
+                {
+                    y[i] = y[i]*columnsigmas[nin+i]+columnmeans[nin+i];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Serializer: allocation
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpalloc(alglib.serializer s,
+            multilayerperceptron network)
+        {
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int fkind = 0;
+            double threshold = 0;
+            double v0 = 0;
+            double v1 = 0;
+            int nin = 0;
+            int nout = 0;
+
+            nin = network.hllayersizes[0];
+            nout = network.hllayersizes[ap.len(network.hllayersizes)-1];
+            s.alloc_entry();
+            s.alloc_entry();
+            s.alloc_entry();
+            apserv.allocintegerarray(s, network.hllayersizes, -1);
+            for(i=1; i<=ap.len(network.hllayersizes)-1; i++)
+            {
+                for(j=0; j<=network.hllayersizes[i]-1; j++)
+                {
+                    mlpgetneuroninfo(network, i, j, ref fkind, ref threshold);
+                    s.alloc_entry();
+                    s.alloc_entry();
+                    for(k=0; k<=network.hllayersizes[i-1]-1; k++)
+                    {
+                        s.alloc_entry();
+                    }
+                }
+            }
+            for(j=0; j<=nin-1; j++)
+            {
+                mlpgetinputscaling(network, j, ref v0, ref v1);
+                s.alloc_entry();
+                s.alloc_entry();
+            }
+            for(j=0; j<=nout-1; j++)
+            {
+                mlpgetoutputscaling(network, j, ref v0, ref v1);
+                s.alloc_entry();
+                s.alloc_entry();
+            }
+        }
+
+
+        /*************************************************************************
+        Serializer: serialization
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpserialize(alglib.serializer s,
+            multilayerperceptron network)
+        {
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int fkind = 0;
+            double threshold = 0;
+            double v0 = 0;
+            double v1 = 0;
+            int nin = 0;
+            int nout = 0;
+
+            nin = network.hllayersizes[0];
+            nout = network.hllayersizes[ap.len(network.hllayersizes)-1];
+            s.serialize_int(scodes.getmlpserializationcode());
+            s.serialize_int(mlpfirstversion);
+            s.serialize_bool(mlpissoftmax(network));
+            apserv.serializeintegerarray(s, network.hllayersizes, -1);
+            for(i=1; i<=ap.len(network.hllayersizes)-1; i++)
+            {
+                for(j=0; j<=network.hllayersizes[i]-1; j++)
+                {
+                    mlpgetneuroninfo(network, i, j, ref fkind, ref threshold);
+                    s.serialize_int(fkind);
+                    s.serialize_double(threshold);
+                    for(k=0; k<=network.hllayersizes[i-1]-1; k++)
+                    {
+                        s.serialize_double(mlpgetweight(network, i-1, k, i, j));
+                    }
+                }
+            }
+            for(j=0; j<=nin-1; j++)
+            {
+                mlpgetinputscaling(network, j, ref v0, ref v1);
+                s.serialize_double(v0);
+                s.serialize_double(v1);
+            }
+            for(j=0; j<=nout-1; j++)
+            {
+                mlpgetoutputscaling(network, j, ref v0, ref v1);
+                s.serialize_double(v0);
+                s.serialize_double(v1);
+            }
+        }
+
+
+        /*************************************************************************
+        Serializer: unserialization
+
+          -- ALGLIB --
+             Copyright 14.03.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpunserialize(alglib.serializer s,
+            multilayerperceptron network)
+        {
+            int i0 = 0;
+            int i1 = 0;
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int fkind = 0;
+            double threshold = 0;
+            double v0 = 0;
+            double v1 = 0;
+            int nin = 0;
+            int nout = 0;
+            bool issoftmax = new bool();
+            int[] layersizes = new int[0];
+
+            
+            //
+            // check correctness of header
+            //
+            i0 = s.unserialize_int();
+            ap.assert(i0==scodes.getmlpserializationcode(), "MLPUnserialize: stream header corrupted");
+            i1 = s.unserialize_int();
+            ap.assert(i1==mlpfirstversion, "MLPUnserialize: stream header corrupted");
+            
+            //
+            // Create network
+            //
+            issoftmax = s.unserialize_bool();
+            apserv.unserializeintegerarray(s, ref layersizes);
+            ap.assert((ap.len(layersizes)==2 | ap.len(layersizes)==3) | ap.len(layersizes)==4, "MLPUnserialize: too many hidden layers!");
+            nin = layersizes[0];
+            nout = layersizes[ap.len(layersizes)-1];
+            if( ap.len(layersizes)==2 )
+            {
+                if( issoftmax )
+                {
+                    mlpcreatec0(layersizes[0], layersizes[1], network);
+                }
+                else
+                {
+                    mlpcreate0(layersizes[0], layersizes[1], network);
+                }
+            }
+            if( ap.len(layersizes)==3 )
+            {
+                if( issoftmax )
+                {
+                    mlpcreatec1(layersizes[0], layersizes[1], layersizes[2], network);
+                }
+                else
+                {
+                    mlpcreate1(layersizes[0], layersizes[1], layersizes[2], network);
+                }
+            }
+            if( ap.len(layersizes)==4 )
+            {
+                if( issoftmax )
+                {
+                    mlpcreatec2(layersizes[0], layersizes[1], layersizes[2], layersizes[3], network);
+                }
+                else
+                {
+                    mlpcreate2(layersizes[0], layersizes[1], layersizes[2], layersizes[3], network);
+                }
+            }
+            
+            //
+            // Load neurons and weights
+            //
+            for(i=1; i<=ap.len(layersizes)-1; i++)
+            {
+                for(j=0; j<=layersizes[i]-1; j++)
+                {
+                    fkind = s.unserialize_int();
+                    threshold = s.unserialize_double();
+                    mlpsetneuroninfo(network, i, j, fkind, threshold);
+                    for(k=0; k<=layersizes[i-1]-1; k++)
+                    {
+                        v0 = s.unserialize_double();
+                        mlpsetweight(network, i-1, k, i, j, v0);
+                    }
+                }
+            }
+            
+            //
+            // Load standartizator
+            //
+            for(j=0; j<=nin-1; j++)
+            {
+                v0 = s.unserialize_double();
+                v1 = s.unserialize_double();
+                mlpsetinputscaling(network, j, v0, v1);
+            }
+            for(j=0; j<=nout-1; j++)
+            {
+                v0 = s.unserialize_double();
+                v1 = s.unserialize_double();
+                mlpsetoutputscaling(network, j, v0, v1);
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine: adding new input layer to network
+        *************************************************************************/
+        private static void addinputlayer(int ncount,
+            ref int[] lsizes,
+            ref int[] ltypes,
+            ref int[] lconnfirst,
+            ref int[] lconnlast,
+            ref int lastproc)
+        {
+            lsizes[0] = ncount;
+            ltypes[0] = -2;
+            lconnfirst[0] = 0;
+            lconnlast[0] = 0;
+            lastproc = 0;
+        }
+
+
+        /*************************************************************************
+        Internal subroutine: adding new summator layer to network
+        *************************************************************************/
+        private static void addbiasedsummatorlayer(int ncount,
+            ref int[] lsizes,
+            ref int[] ltypes,
+            ref int[] lconnfirst,
+            ref int[] lconnlast,
+            ref int lastproc)
+        {
+            lsizes[lastproc+1] = 1;
+            ltypes[lastproc+1] = -3;
+            lconnfirst[lastproc+1] = 0;
+            lconnlast[lastproc+1] = 0;
+            lsizes[lastproc+2] = ncount;
+            ltypes[lastproc+2] = 0;
+            lconnfirst[lastproc+2] = lastproc;
+            lconnlast[lastproc+2] = lastproc+1;
+            lastproc = lastproc+2;
+        }
+
+
+        /*************************************************************************
+        Internal subroutine: adding new summator layer to network
+        *************************************************************************/
+        private static void addactivationlayer(int functype,
+            ref int[] lsizes,
+            ref int[] ltypes,
+            ref int[] lconnfirst,
+            ref int[] lconnlast,
+            ref int lastproc)
+        {
+            ap.assert(functype>0 | functype==-5, "AddActivationLayer: incorrect function type");
+            lsizes[lastproc+1] = lsizes[lastproc];
+            ltypes[lastproc+1] = functype;
+            lconnfirst[lastproc+1] = lastproc;
+            lconnlast[lastproc+1] = lastproc;
+            lastproc = lastproc+1;
+        }
+
+
+        /*************************************************************************
+        Internal subroutine: adding new zero layer to network
+        *************************************************************************/
+        private static void addzerolayer(ref int[] lsizes,
+            ref int[] ltypes,
+            ref int[] lconnfirst,
+            ref int[] lconnlast,
+            ref int lastproc)
+        {
+            lsizes[lastproc+1] = 1;
+            ltypes[lastproc+1] = -4;
+            lconnfirst[lastproc+1] = 0;
+            lconnlast[lastproc+1] = 0;
+            lastproc = lastproc+1;
+        }
+
+
+        /*************************************************************************
+        This routine adds input layer to the high-level description of the network.
+
+        It modifies Network.HLConnections and Network.HLNeurons  and  assumes that
+        these  arrays  have  enough  place  to  store  data.  It accepts following
+        parameters:
+            Network     -   network
+            ConnIdx     -   index of the first free entry in the HLConnections
+            NeuroIdx    -   index of the first free entry in the HLNeurons
+            StructInfoIdx-  index of the first entry in the low level description
+                            of the current layer (in the StructInfo array)
+            NIn         -   number of inputs
+                            
+        It modified Network and indices.
+        *************************************************************************/
+        private static void hladdinputlayer(multilayerperceptron network,
+            ref int connidx,
+            ref int neuroidx,
+            ref int structinfoidx,
+            int nin)
+        {
+            int i = 0;
+            int offs = 0;
+
+            offs = hlnfieldwidth*neuroidx;
+            for(i=0; i<=nin-1; i++)
+            {
+                network.hlneurons[offs+0] = 0;
+                network.hlneurons[offs+1] = i;
+                network.hlneurons[offs+2] = -1;
+                network.hlneurons[offs+3] = -1;
+                offs = offs+hlnfieldwidth;
+            }
+            neuroidx = neuroidx+nin;
+            structinfoidx = structinfoidx+nin;
+        }
+
+
+        /*************************************************************************
+        This routine adds output layer to the high-level description of
+        the network.
+
+        It modifies Network.HLConnections and Network.HLNeurons  and  assumes that
+        these  arrays  have  enough  place  to  store  data.  It accepts following
+        parameters:
+            Network     -   network
+            ConnIdx     -   index of the first free entry in the HLConnections
+            NeuroIdx    -   index of the first free entry in the HLNeurons
+            StructInfoIdx-  index of the first entry in the low level description
+                            of the current layer (in the StructInfo array)
+            WeightsIdx  -   index of the first entry in the Weights array which
+                            corresponds to the current layer
+            K           -   current layer index
+            NPrev       -   number of neurons in the previous layer
+            NOut        -   number of outputs
+            IsCls       -   is it classifier network?
+            IsLinear    -   is it network with linear output?
+
+        It modified Network and ConnIdx/NeuroIdx/StructInfoIdx/WeightsIdx.
+        *************************************************************************/
+        private static void hladdoutputlayer(multilayerperceptron network,
+            ref int connidx,
+            ref int neuroidx,
+            ref int structinfoidx,
+            ref int weightsidx,
+            int k,
+            int nprev,
+            int nout,
+            bool iscls,
+            bool islinearout)
+        {
+            int i = 0;
+            int j = 0;
+            int neurooffs = 0;
+            int connoffs = 0;
+
+            ap.assert((iscls & islinearout) | !iscls, "HLAddOutputLayer: internal error");
+            neurooffs = hlnfieldwidth*neuroidx;
+            connoffs = hlconnfieldwidth*connidx;
+            if( !iscls )
+            {
+                
+                //
+                // Regression network
+                //
+                for(i=0; i<=nout-1; i++)
+                {
+                    network.hlneurons[neurooffs+0] = k;
+                    network.hlneurons[neurooffs+1] = i;
+                    network.hlneurons[neurooffs+2] = structinfoidx+1+nout+i;
+                    network.hlneurons[neurooffs+3] = weightsidx+nprev+(nprev+1)*i;
+                    neurooffs = neurooffs+hlnfieldwidth;
+                }
+                for(i=0; i<=nprev-1; i++)
+                {
+                    for(j=0; j<=nout-1; j++)
+                    {
+                        network.hlconnections[connoffs+0] = k-1;
+                        network.hlconnections[connoffs+1] = i;
+                        network.hlconnections[connoffs+2] = k;
+                        network.hlconnections[connoffs+3] = j;
+                        network.hlconnections[connoffs+4] = weightsidx+i+j*(nprev+1);
+                        connoffs = connoffs+hlconnfieldwidth;
+                    }
+                }
+                connidx = connidx+nprev*nout;
+                neuroidx = neuroidx+nout;
+                structinfoidx = structinfoidx+2*nout+1;
+                weightsidx = weightsidx+nout*(nprev+1);
+            }
+            else
+            {
+                
+                //
+                // Classification network
+                //
+                for(i=0; i<=nout-2; i++)
+                {
+                    network.hlneurons[neurooffs+0] = k;
+                    network.hlneurons[neurooffs+1] = i;
+                    network.hlneurons[neurooffs+2] = -1;
+                    network.hlneurons[neurooffs+3] = weightsidx+nprev+(nprev+1)*i;
+                    neurooffs = neurooffs+hlnfieldwidth;
+                }
+                network.hlneurons[neurooffs+0] = k;
+                network.hlneurons[neurooffs+1] = i;
+                network.hlneurons[neurooffs+2] = -1;
+                network.hlneurons[neurooffs+3] = -1;
+                for(i=0; i<=nprev-1; i++)
+                {
+                    for(j=0; j<=nout-2; j++)
+                    {
+                        network.hlconnections[connoffs+0] = k-1;
+                        network.hlconnections[connoffs+1] = i;
+                        network.hlconnections[connoffs+2] = k;
+                        network.hlconnections[connoffs+3] = j;
+                        network.hlconnections[connoffs+4] = weightsidx+i+j*(nprev+1);
+                        connoffs = connoffs+hlconnfieldwidth;
+                    }
+                }
+                connidx = connidx+nprev*(nout-1);
+                neuroidx = neuroidx+nout;
+                structinfoidx = structinfoidx+nout+2;
+                weightsidx = weightsidx+(nout-1)*(nprev+1);
+            }
+        }
+
+
+        /*************************************************************************
+        This routine adds hidden layer to the high-level description of
+        the network.
+
+        It modifies Network.HLConnections and Network.HLNeurons  and  assumes that
+        these  arrays  have  enough  place  to  store  data.  It accepts following
+        parameters:
+            Network     -   network
+            ConnIdx     -   index of the first free entry in the HLConnections
+            NeuroIdx    -   index of the first free entry in the HLNeurons
+            StructInfoIdx-  index of the first entry in the low level description
+                            of the current layer (in the StructInfo array)
+            WeightsIdx  -   index of the first entry in the Weights array which
+                            corresponds to the current layer
+            K           -   current layer index
+            NPrev       -   number of neurons in the previous layer
+            NCur        -   number of neurons in the current layer
+
+        It modified Network and ConnIdx/NeuroIdx/StructInfoIdx/WeightsIdx.
+        *************************************************************************/
+        private static void hladdhiddenlayer(multilayerperceptron network,
+            ref int connidx,
+            ref int neuroidx,
+            ref int structinfoidx,
+            ref int weightsidx,
+            int k,
+            int nprev,
+            int ncur)
+        {
+            int i = 0;
+            int j = 0;
+            int neurooffs = 0;
+            int connoffs = 0;
+
+            neurooffs = hlnfieldwidth*neuroidx;
+            connoffs = hlconnfieldwidth*connidx;
+            for(i=0; i<=ncur-1; i++)
+            {
+                network.hlneurons[neurooffs+0] = k;
+                network.hlneurons[neurooffs+1] = i;
+                network.hlneurons[neurooffs+2] = structinfoidx+1+ncur+i;
+                network.hlneurons[neurooffs+3] = weightsidx+nprev+(nprev+1)*i;
+                neurooffs = neurooffs+hlnfieldwidth;
+            }
+            for(i=0; i<=nprev-1; i++)
+            {
+                for(j=0; j<=ncur-1; j++)
+                {
+                    network.hlconnections[connoffs+0] = k-1;
+                    network.hlconnections[connoffs+1] = i;
+                    network.hlconnections[connoffs+2] = k;
+                    network.hlconnections[connoffs+3] = j;
+                    network.hlconnections[connoffs+4] = weightsidx+i+j*(nprev+1);
+                    connoffs = connoffs+hlconnfieldwidth;
+                }
+            }
+            connidx = connidx+nprev*ncur;
+            neuroidx = neuroidx+ncur;
+            structinfoidx = structinfoidx+2*ncur+1;
+            weightsidx = weightsidx+ncur*(nprev+1);
+        }
+
+
+        /*************************************************************************
+        This function fills high level information about network created using
+        internal MLPCreate() function.
+
+        This function does NOT examine StructInfo for low level information, it
+        just expects that network has following structure:
+
+            input neuron            \
+            ...                      | input layer
+            input neuron            /
+            
+            "-1" neuron             \
+            biased summator          |
+            ...                      |
+            biased summator          | hidden layer(s), if there are exists any
+            activation function      |
+            ...                      |
+            activation function     /
+            
+            "-1" neuron            \
+            biased summator         | output layer:
+            ...                     |
+            biased summator         | * we have NOut summators/activators for regression networks
+            activation function     | * we have only NOut-1 summators and no activators for classifiers
+            ...                     | * we have "0" neuron only when we have classifier
+            activation function     |
+            "0" neuron              /
+
+
+          -- ALGLIB --
+             Copyright 30.03.2008 by Bochkanov Sergey
+        *************************************************************************/
+        private static void fillhighlevelinformation(multilayerperceptron network,
+            int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            bool iscls,
+            bool islinearout)
+        {
+            int idxweights = 0;
+            int idxstruct = 0;
+            int idxneuro = 0;
+            int idxconn = 0;
+
+            ap.assert((iscls & islinearout) | !iscls, "FillHighLevelInformation: internal error");
+            
+            //
+            // Preparations common to all types of networks
+            //
+            idxweights = 0;
+            idxneuro = 0;
+            idxstruct = 0;
+            idxconn = 0;
+            network.hlnetworktype = 0;
+            
+            //
+            // network without hidden layers
+            //
+            if( nhid1==0 )
+            {
+                network.hllayersizes = new int[2];
+                network.hllayersizes[0] = nin;
+                network.hllayersizes[1] = nout;
+                if( !iscls )
+                {
+                    network.hlconnections = new int[hlconnfieldwidth*nin*nout];
+                    network.hlneurons = new int[hlnfieldwidth*(nin+nout)];
+                    network.hlnormtype = 0;
+                }
+                else
+                {
+                    network.hlconnections = new int[hlconnfieldwidth*nin*(nout-1)];
+                    network.hlneurons = new int[hlnfieldwidth*(nin+nout)];
+                    network.hlnormtype = 1;
+                }
+                hladdinputlayer(network, ref idxconn, ref idxneuro, ref idxstruct, nin);
+                hladdoutputlayer(network, ref idxconn, ref idxneuro, ref idxstruct, ref idxweights, 1, nin, nout, iscls, islinearout);
+                return;
+            }
+            
+            //
+            // network with one hidden layers
+            //
+            if( nhid2==0 )
+            {
+                network.hllayersizes = new int[3];
+                network.hllayersizes[0] = nin;
+                network.hllayersizes[1] = nhid1;
+                network.hllayersizes[2] = nout;
+                if( !iscls )
+                {
+                    network.hlconnections = new int[hlconnfieldwidth*(nin*nhid1+nhid1*nout)];
+                    network.hlneurons = new int[hlnfieldwidth*(nin+nhid1+nout)];
+                    network.hlnormtype = 0;
+                }
+                else
+                {
+                    network.hlconnections = new int[hlconnfieldwidth*(nin*nhid1+nhid1*(nout-1))];
+                    network.hlneurons = new int[hlnfieldwidth*(nin+nhid1+nout)];
+                    network.hlnormtype = 1;
+                }
+                hladdinputlayer(network, ref idxconn, ref idxneuro, ref idxstruct, nin);
+                hladdhiddenlayer(network, ref idxconn, ref idxneuro, ref idxstruct, ref idxweights, 1, nin, nhid1);
+                hladdoutputlayer(network, ref idxconn, ref idxneuro, ref idxstruct, ref idxweights, 2, nhid1, nout, iscls, islinearout);
+                return;
+            }
+            
+            //
+            // Two hidden layers
+            //
+            network.hllayersizes = new int[4];
+            network.hllayersizes[0] = nin;
+            network.hllayersizes[1] = nhid1;
+            network.hllayersizes[2] = nhid2;
+            network.hllayersizes[3] = nout;
+            if( !iscls )
+            {
+                network.hlconnections = new int[hlconnfieldwidth*(nin*nhid1+nhid1*nhid2+nhid2*nout)];
+                network.hlneurons = new int[hlnfieldwidth*(nin+nhid1+nhid2+nout)];
+                network.hlnormtype = 0;
+            }
+            else
+            {
+                network.hlconnections = new int[hlconnfieldwidth*(nin*nhid1+nhid1*nhid2+nhid2*(nout-1))];
+                network.hlneurons = new int[hlnfieldwidth*(nin+nhid1+nhid2+nout)];
+                network.hlnormtype = 1;
+            }
+            hladdinputlayer(network, ref idxconn, ref idxneuro, ref idxstruct, nin);
+            hladdhiddenlayer(network, ref idxconn, ref idxneuro, ref idxstruct, ref idxweights, 1, nin, nhid1);
+            hladdhiddenlayer(network, ref idxconn, ref idxneuro, ref idxstruct, ref idxweights, 2, nhid1, nhid2);
+            hladdoutputlayer(network, ref idxconn, ref idxneuro, ref idxstruct, ref idxweights, 3, nhid2, nout, iscls, islinearout);
+        }
+
+
+        /*************************************************************************
+        Internal subroutine.
+
+          -- ALGLIB --
+             Copyright 04.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        private static void mlpcreate(int nin,
+            int nout,
+            int[] lsizes,
+            int[] ltypes,
+            int[] lconnfirst,
+            int[] lconnlast,
+            int layerscount,
+            bool isclsnet,
+            multilayerperceptron network)
+        {
+            int i = 0;
+            int j = 0;
+            int ssize = 0;
+            int ntotal = 0;
+            int wcount = 0;
+            int offs = 0;
+            int nprocessed = 0;
+            int wallocated = 0;
+            int[] localtemp = new int[0];
+            int[] lnfirst = new int[0];
+            int[] lnsyn = new int[0];
+
+            
+            //
+            // Check
+            //
+            ap.assert(layerscount>0, "MLPCreate: wrong parameters!");
+            ap.assert(ltypes[0]==-2, "MLPCreate: wrong LTypes[0] (must be -2)!");
+            for(i=0; i<=layerscount-1; i++)
+            {
+                ap.assert(lsizes[i]>0, "MLPCreate: wrong LSizes!");
+                ap.assert(lconnfirst[i]>=0 & (lconnfirst[i]<i | i==0), "MLPCreate: wrong LConnFirst!");
+                ap.assert(lconnlast[i]>=lconnfirst[i] & (lconnlast[i]<i | i==0), "MLPCreate: wrong LConnLast!");
+            }
+            
+            //
+            // Build network geometry
+            //
+            lnfirst = new int[layerscount-1+1];
+            lnsyn = new int[layerscount-1+1];
+            ntotal = 0;
+            wcount = 0;
+            for(i=0; i<=layerscount-1; i++)
+            {
+                
+                //
+                // Analyze connections.
+                // This code must throw an assertion in case of unknown LTypes[I]
+                //
+                lnsyn[i] = -1;
+                if( ltypes[i]>=0 | ltypes[i]==-5 )
+                {
+                    lnsyn[i] = 0;
+                    for(j=lconnfirst[i]; j<=lconnlast[i]; j++)
+                    {
+                        lnsyn[i] = lnsyn[i]+lsizes[j];
+                    }
+                }
+                else
+                {
+                    if( (ltypes[i]==-2 | ltypes[i]==-3) | ltypes[i]==-4 )
+                    {
+                        lnsyn[i] = 0;
+                    }
+                }
+                ap.assert(lnsyn[i]>=0, "MLPCreate: internal error #0!");
+                
+                //
+                // Other info
+                //
+                lnfirst[i] = ntotal;
+                ntotal = ntotal+lsizes[i];
+                if( ltypes[i]==0 )
+                {
+                    wcount = wcount+lnsyn[i]*lsizes[i];
+                }
+            }
+            ssize = 7+ntotal*nfieldwidth;
+            
+            //
+            // Allocate
+            //
+            network.structinfo = new int[ssize-1+1];
+            network.weights = new double[wcount-1+1];
+            if( isclsnet )
+            {
+                network.columnmeans = new double[nin-1+1];
+                network.columnsigmas = new double[nin-1+1];
+            }
+            else
+            {
+                network.columnmeans = new double[nin+nout-1+1];
+                network.columnsigmas = new double[nin+nout-1+1];
+            }
+            network.neurons = new double[ntotal-1+1];
+            network.chunks = new double[3*ntotal+1, chunksize-1+1];
+            network.nwbuf = new double[Math.Max(wcount, 2*nout)-1+1];
+            network.integerbuf = new int[3+1];
+            network.dfdnet = new double[ntotal-1+1];
+            network.x = new double[nin-1+1];
+            network.y = new double[nout-1+1];
+            network.derror = new double[ntotal-1+1];
+            
+            //
+            // Fill structure: global info
+            //
+            network.structinfo[0] = ssize;
+            network.structinfo[1] = nin;
+            network.structinfo[2] = nout;
+            network.structinfo[3] = ntotal;
+            network.structinfo[4] = wcount;
+            network.structinfo[5] = 7;
+            if( isclsnet )
+            {
+                network.structinfo[6] = 1;
+            }
+            else
+            {
+                network.structinfo[6] = 0;
+            }
+            
+            //
+            // Fill structure: neuron connections
+            //
+            nprocessed = 0;
+            wallocated = 0;
+            for(i=0; i<=layerscount-1; i++)
+            {
+                for(j=0; j<=lsizes[i]-1; j++)
+                {
+                    offs = network.structinfo[5]+nprocessed*nfieldwidth;
+                    network.structinfo[offs+0] = ltypes[i];
+                    if( ltypes[i]==0 )
+                    {
+                        
+                        //
+                        // Adaptive summator:
+                        // * connections with weights to previous neurons
+                        //
+                        network.structinfo[offs+1] = lnsyn[i];
+                        network.structinfo[offs+2] = lnfirst[lconnfirst[i]];
+                        network.structinfo[offs+3] = wallocated;
+                        wallocated = wallocated+lnsyn[i];
+                        nprocessed = nprocessed+1;
+                    }
+                    if( ltypes[i]>0 | ltypes[i]==-5 )
+                    {
+                        
+                        //
+                        // Activation layer:
+                        // * each neuron connected to one (only one) of previous neurons.
+                        // * no weights
+                        //
+                        network.structinfo[offs+1] = 1;
+                        network.structinfo[offs+2] = lnfirst[lconnfirst[i]]+j;
+                        network.structinfo[offs+3] = -1;
+                        nprocessed = nprocessed+1;
+                    }
+                    if( (ltypes[i]==-2 | ltypes[i]==-3) | ltypes[i]==-4 )
+                    {
+                        nprocessed = nprocessed+1;
+                    }
+                }
+            }
+            ap.assert(wallocated==wcount, "MLPCreate: internal error #1!");
+            ap.assert(nprocessed==ntotal, "MLPCreate: internal error #2!");
+            
+            //
+            // Fill weights by small random values
+            // Initialize means and sigmas
+            //
+            for(i=0; i<=wcount-1; i++)
+            {
+                network.weights[i] = math.randomreal()-0.5;
+            }
+            for(i=0; i<=nin-1; i++)
+            {
+                network.columnmeans[i] = 0;
+                network.columnsigmas[i] = 1;
+            }
+            if( !isclsnet )
+            {
+                for(i=0; i<=nout-1; i++)
+                {
+                    network.columnmeans[nin+i] = 0;
+                    network.columnsigmas[nin+i] = 1;
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine for Hessian calculation.
+
+        WARNING!!! Unspeakable math far beyong human capabilities :)
+        *************************************************************************/
+        private static void mlphessianbatchinternal(multilayerperceptron network,
+            double[,] xy,
+            int ssize,
+            bool naturalerr,
+            ref double e,
+            ref double[] grad,
+            ref double[,] h)
+        {
+            int nin = 0;
+            int nout = 0;
+            int wcount = 0;
+            int ntotal = 0;
+            int istart = 0;
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int kl = 0;
+            int offs = 0;
+            int n1 = 0;
+            int n2 = 0;
+            int w1 = 0;
+            int w2 = 0;
+            double s = 0;
+            double t = 0;
+            double v = 0;
+            double et = 0;
+            bool bflag = new bool();
+            double f = 0;
+            double df = 0;
+            double d2f = 0;
+            double deidyj = 0;
+            double mx = 0;
+            double q = 0;
+            double z = 0;
+            double s2 = 0;
+            double expi = 0;
+            double expj = 0;
+            double[] x = new double[0];
+            double[] desiredy = new double[0];
+            double[] gt = new double[0];
+            double[] zeros = new double[0];
+            double[,] rx = new double[0,0];
+            double[,] ry = new double[0,0];
+            double[,] rdx = new double[0,0];
+            double[,] rdy = new double[0,0];
+            int i_ = 0;
+            int i1_ = 0;
+
+            e = 0;
+
+            mlpproperties(network, ref nin, ref nout, ref wcount);
+            ntotal = network.structinfo[3];
+            istart = network.structinfo[5];
+            
+            //
+            // Prepare
+            //
+            x = new double[nin-1+1];
+            desiredy = new double[nout-1+1];
+            zeros = new double[wcount-1+1];
+            gt = new double[wcount-1+1];
+            rx = new double[ntotal+nout-1+1, wcount-1+1];
+            ry = new double[ntotal+nout-1+1, wcount-1+1];
+            rdx = new double[ntotal+nout-1+1, wcount-1+1];
+            rdy = new double[ntotal+nout-1+1, wcount-1+1];
+            e = 0;
+            for(i=0; i<=wcount-1; i++)
+            {
+                zeros[i] = 0;
+            }
+            for(i_=0; i_<=wcount-1;i_++)
+            {
+                grad[i_] = zeros[i_];
+            }
+            for(i=0; i<=wcount-1; i++)
+            {
+                for(i_=0; i_<=wcount-1;i_++)
+                {
+                    h[i,i_] = zeros[i_];
+                }
+            }
+            
+            //
+            // Process
+            //
+            for(k=0; k<=ssize-1; k++)
+            {
+                
+                //
+                // Process vector with MLPGradN.
+                // Now Neurons, DFDNET and DError contains results of the last run.
+                //
+                for(i_=0; i_<=nin-1;i_++)
+                {
+                    x[i_] = xy[k,i_];
+                }
+                if( mlpissoftmax(network) )
+                {
+                    
+                    //
+                    // class labels outputs
+                    //
+                    kl = (int)Math.Round(xy[k,nin]);
+                    for(i=0; i<=nout-1; i++)
+                    {
+                        if( i==kl )
+                        {
+                            desiredy[i] = 1;
+                        }
+                        else
+                        {
+                            desiredy[i] = 0;
+                        }
+                    }
+                }
+                else
+                {
+                    
+                    //
+                    // real outputs
+                    //
+                    i1_ = (nin) - (0);
+                    for(i_=0; i_<=nout-1;i_++)
+                    {
+                        desiredy[i_] = xy[k,i_+i1_];
+                    }
+                }
+                if( naturalerr )
+                {
+                    mlpgradn(network, x, desiredy, ref et, ref gt);
+                }
+                else
+                {
+                    mlpgrad(network, x, desiredy, ref et, ref gt);
+                }
+                
+                //
+                // grad, error
+                //
+                e = e+et;
+                for(i_=0; i_<=wcount-1;i_++)
+                {
+                    grad[i_] = grad[i_] + gt[i_];
+                }
+                
+                //
+                // Hessian.
+                // Forward pass of the R-algorithm
+                //
+                for(i=0; i<=ntotal-1; i++)
+                {
+                    offs = istart+i*nfieldwidth;
+                    for(i_=0; i_<=wcount-1;i_++)
+                    {
+                        rx[i,i_] = zeros[i_];
+                    }
+                    for(i_=0; i_<=wcount-1;i_++)
+                    {
+                        ry[i,i_] = zeros[i_];
+                    }
+                    if( network.structinfo[offs+0]>0 | network.structinfo[offs+0]==-5 )
+                    {
+                        
+                        //
+                        // Activation function
+                        //
+                        n1 = network.structinfo[offs+2];
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rx[i,i_] = ry[n1,i_];
+                        }
+                        v = network.dfdnet[i];
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            ry[i,i_] = v*rx[i,i_];
+                        }
+                        continue;
+                    }
+                    if( network.structinfo[offs+0]==0 )
+                    {
+                        
+                        //
+                        // Adaptive summator
+                        //
+                        n1 = network.structinfo[offs+2];
+                        n2 = n1+network.structinfo[offs+1]-1;
+                        w1 = network.structinfo[offs+3];
+                        w2 = w1+network.structinfo[offs+1]-1;
+                        for(j=n1; j<=n2; j++)
+                        {
+                            v = network.weights[w1+j-n1];
+                            for(i_=0; i_<=wcount-1;i_++)
+                            {
+                                rx[i,i_] = rx[i,i_] + v*ry[j,i_];
+                            }
+                            rx[i,w1+j-n1] = rx[i,w1+j-n1]+network.neurons[j];
+                        }
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            ry[i,i_] = rx[i,i_];
+                        }
+                        continue;
+                    }
+                    if( network.structinfo[offs+0]<0 )
+                    {
+                        bflag = true;
+                        if( network.structinfo[offs+0]==-2 )
+                        {
+                            
+                            //
+                            // input neuron, left unchanged
+                            //
+                            bflag = false;
+                        }
+                        if( network.structinfo[offs+0]==-3 )
+                        {
+                            
+                            //
+                            // "-1" neuron, left unchanged
+                            //
+                            bflag = false;
+                        }
+                        if( network.structinfo[offs+0]==-4 )
+                        {
+                            
+                            //
+                            // "0" neuron, left unchanged
+                            //
+                            bflag = false;
+                        }
+                        ap.assert(!bflag, "MLPHessianNBatch: internal error - unknown neuron type!");
+                        continue;
+                    }
+                }
+                
+                //
+                // Hessian. Backward pass of the R-algorithm.
+                //
+                // Stage 1. Initialize RDY
+                //
+                for(i=0; i<=ntotal+nout-1; i++)
+                {
+                    for(i_=0; i_<=wcount-1;i_++)
+                    {
+                        rdy[i,i_] = zeros[i_];
+                    }
+                }
+                if( network.structinfo[6]==0 )
+                {
+                    
+                    //
+                    // Standardisation.
+                    //
+                    // In context of the Hessian calculation standardisation
+                    // is considered as additional layer with weightless
+                    // activation function:
+                    //
+                    // F(NET) := Sigma*NET
+                    //
+                    // So we add one more layer to forward pass, and
+                    // make forward/backward pass through this layer.
+                    //
+                    for(i=0; i<=nout-1; i++)
+                    {
+                        n1 = ntotal-nout+i;
+                        n2 = ntotal+i;
+                        
+                        //
+                        // Forward pass from N1 to N2
+                        //
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rx[n2,i_] = ry[n1,i_];
+                        }
+                        v = network.columnsigmas[nin+i];
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            ry[n2,i_] = v*rx[n2,i_];
+                        }
+                        
+                        //
+                        // Initialization of RDY
+                        //
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rdy[n2,i_] = ry[n2,i_];
+                        }
+                        
+                        //
+                        // Backward pass from N2 to N1:
+                        // 1. Calculate R(dE/dX).
+                        // 2. No R(dE/dWij) is needed since weight of activation neuron
+                        //    is fixed to 1. So we can update R(dE/dY) for
+                        //    the connected neuron (note that Vij=0, Wij=1)
+                        //
+                        df = network.columnsigmas[nin+i];
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rdx[n2,i_] = df*rdy[n2,i_];
+                        }
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rdy[n1,i_] = rdy[n1,i_] + rdx[n2,i_];
+                        }
+                    }
+                }
+                else
+                {
+                    
+                    //
+                    // Softmax.
+                    //
+                    // Initialize RDY using generalized expression for ei'(yi)
+                    // (see expression (9) from p. 5 of "Fast Exact Multiplication by the Hessian").
+                    //
+                    // When we are working with softmax network, generalized
+                    // expression for ei'(yi) is used because softmax
+                    // normalization leads to ei, which depends on all y's
+                    //
+                    if( naturalerr )
+                    {
+                        
+                        //
+                        // softmax + cross-entropy.
+                        // We have:
+                        //
+                        // S = sum(exp(yk)),
+                        // ei = sum(trn)*exp(yi)/S-trn_i
+                        //
+                        // j=i:   d(ei)/d(yj) = T*exp(yi)*(S-exp(yi))/S^2
+                        // j<>i:  d(ei)/d(yj) = -T*exp(yi)*exp(yj)/S^2
+                        //
+                        t = 0;
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            t = t+desiredy[i];
+                        }
+                        mx = network.neurons[ntotal-nout];
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            mx = Math.Max(mx, network.neurons[ntotal-nout+i]);
+                        }
+                        s = 0;
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            network.nwbuf[i] = Math.Exp(network.neurons[ntotal-nout+i]-mx);
+                            s = s+network.nwbuf[i];
+                        }
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            for(j=0; j<=nout-1; j++)
+                            {
+                                if( j==i )
+                                {
+                                    deidyj = t*network.nwbuf[i]*(s-network.nwbuf[i])/math.sqr(s);
+                                    for(i_=0; i_<=wcount-1;i_++)
+                                    {
+                                        rdy[ntotal-nout+i,i_] = rdy[ntotal-nout+i,i_] + deidyj*ry[ntotal-nout+i,i_];
+                                    }
+                                }
+                                else
+                                {
+                                    deidyj = -(t*network.nwbuf[i]*network.nwbuf[j]/math.sqr(s));
+                                    for(i_=0; i_<=wcount-1;i_++)
+                                    {
+                                        rdy[ntotal-nout+i,i_] = rdy[ntotal-nout+i,i_] + deidyj*ry[ntotal-nout+j,i_];
+                                    }
+                                }
+                            }
+                        }
+                    }
+                    else
+                    {
+                        
+                        //
+                        // For a softmax + squared error we have expression
+                        // far beyond human imagination so we dont even try
+                        // to comment on it. Just enjoy the code...
+                        //
+                        // P.S. That's why "natural error" is called "natural" -
+                        // compact beatiful expressions, fast code....
+                        //
+                        mx = network.neurons[ntotal-nout];
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            mx = Math.Max(mx, network.neurons[ntotal-nout+i]);
+                        }
+                        s = 0;
+                        s2 = 0;
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            network.nwbuf[i] = Math.Exp(network.neurons[ntotal-nout+i]-mx);
+                            s = s+network.nwbuf[i];
+                            s2 = s2+math.sqr(network.nwbuf[i]);
+                        }
+                        q = 0;
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            q = q+(network.y[i]-desiredy[i])*network.nwbuf[i];
+                        }
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            z = -q+(network.y[i]-desiredy[i])*s;
+                            expi = network.nwbuf[i];
+                            for(j=0; j<=nout-1; j++)
+                            {
+                                expj = network.nwbuf[j];
+                                if( j==i )
+                                {
+                                    deidyj = expi/math.sqr(s)*((z+expi)*(s-2*expi)/s+expi*s2/math.sqr(s));
+                                }
+                                else
+                                {
+                                    deidyj = expi*expj/math.sqr(s)*(s2/math.sqr(s)-2*z/s-(expi+expj)/s+(network.y[i]-desiredy[i])-(network.y[j]-desiredy[j]));
+                                }
+                                for(i_=0; i_<=wcount-1;i_++)
+                                {
+                                    rdy[ntotal-nout+i,i_] = rdy[ntotal-nout+i,i_] + deidyj*ry[ntotal-nout+j,i_];
+                                }
+                            }
+                        }
+                    }
+                }
+                
+                //
+                // Hessian. Backward pass of the R-algorithm
+                //
+                // Stage 2. Process.
+                //
+                for(i=ntotal-1; i>=0; i--)
+                {
+                    
+                    //
+                    // Possible variants:
+                    // 1. Activation function
+                    // 2. Adaptive summator
+                    // 3. Special neuron
+                    //
+                    offs = istart+i*nfieldwidth;
+                    if( network.structinfo[offs+0]>0 | network.structinfo[offs+0]==-5 )
+                    {
+                        n1 = network.structinfo[offs+2];
+                        
+                        //
+                        // First, calculate R(dE/dX).
+                        //
+                        mlpactivationfunction(network.neurons[n1], network.structinfo[offs+0], ref f, ref df, ref d2f);
+                        v = d2f*network.derror[i];
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rdx[i,i_] = df*rdy[i,i_];
+                        }
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rdx[i,i_] = rdx[i,i_] + v*rx[i,i_];
+                        }
+                        
+                        //
+                        // No R(dE/dWij) is needed since weight of activation neuron
+                        // is fixed to 1.
+                        //
+                        // So we can update R(dE/dY) for the connected neuron.
+                        // (note that Vij=0, Wij=1)
+                        //
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rdy[n1,i_] = rdy[n1,i_] + rdx[i,i_];
+                        }
+                        continue;
+                    }
+                    if( network.structinfo[offs+0]==0 )
+                    {
+                        
+                        //
+                        // Adaptive summator
+                        //
+                        n1 = network.structinfo[offs+2];
+                        n2 = n1+network.structinfo[offs+1]-1;
+                        w1 = network.structinfo[offs+3];
+                        w2 = w1+network.structinfo[offs+1]-1;
+                        
+                        //
+                        // First, calculate R(dE/dX).
+                        //
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            rdx[i,i_] = rdy[i,i_];
+                        }
+                        
+                        //
+                        // Then, calculate R(dE/dWij)
+                        //
+                        for(j=w1; j<=w2; j++)
+                        {
+                            v = network.neurons[n1+j-w1];
+                            for(i_=0; i_<=wcount-1;i_++)
+                            {
+                                h[j,i_] = h[j,i_] + v*rdx[i,i_];
+                            }
+                            v = network.derror[i];
+                            for(i_=0; i_<=wcount-1;i_++)
+                            {
+                                h[j,i_] = h[j,i_] + v*ry[n1+j-w1,i_];
+                            }
+                        }
+                        
+                        //
+                        // And finally, update R(dE/dY) for connected neurons.
+                        //
+                        for(j=w1; j<=w2; j++)
+                        {
+                            v = network.weights[j];
+                            for(i_=0; i_<=wcount-1;i_++)
+                            {
+                                rdy[n1+j-w1,i_] = rdy[n1+j-w1,i_] + v*rdx[i,i_];
+                            }
+                            rdy[n1+j-w1,j] = rdy[n1+j-w1,j]+network.derror[i];
+                        }
+                        continue;
+                    }
+                    if( network.structinfo[offs+0]<0 )
+                    {
+                        bflag = false;
+                        if( (network.structinfo[offs+0]==-2 | network.structinfo[offs+0]==-3) | network.structinfo[offs+0]==-4 )
+                        {
+                            
+                            //
+                            // Special neuron type, no back-propagation required
+                            //
+                            bflag = true;
+                        }
+                        ap.assert(bflag, "MLPHessianNBatch: unknown neuron type!");
+                        continue;
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine
+
+        Network must be processed by MLPProcess on X
+        *************************************************************************/
+        private static void mlpinternalcalculategradient(multilayerperceptron network,
+            double[] neurons,
+            double[] weights,
+            ref double[] derror,
+            ref double[] grad,
+            bool naturalerrorfunc)
+        {
+            int i = 0;
+            int n1 = 0;
+            int n2 = 0;
+            int w1 = 0;
+            int w2 = 0;
+            int ntotal = 0;
+            int istart = 0;
+            int nin = 0;
+            int nout = 0;
+            int offs = 0;
+            double dedf = 0;
+            double dfdnet = 0;
+            double v = 0;
+            double fown = 0;
+            double deown = 0;
+            double net = 0;
+            double mx = 0;
+            bool bflag = new bool();
+            int i_ = 0;
+            int i1_ = 0;
+
+            
+            //
+            // Read network geometry
+            //
+            nin = network.structinfo[1];
+            nout = network.structinfo[2];
+            ntotal = network.structinfo[3];
+            istart = network.structinfo[5];
+            
+            //
+            // Pre-processing of dError/dOut:
+            // from dError/dOut(normalized) to dError/dOut(non-normalized)
+            //
+            ap.assert(network.structinfo[6]==0 | network.structinfo[6]==1, "MLPInternalCalculateGradient: unknown normalization type!");
+            if( network.structinfo[6]==1 )
+            {
+                
+                //
+                // Softmax
+                //
+                if( !naturalerrorfunc )
+                {
+                    mx = network.neurons[ntotal-nout];
+                    for(i=0; i<=nout-1; i++)
+                    {
+                        mx = Math.Max(mx, network.neurons[ntotal-nout+i]);
+                    }
+                    net = 0;
+                    for(i=0; i<=nout-1; i++)
+                    {
+                        network.nwbuf[i] = Math.Exp(network.neurons[ntotal-nout+i]-mx);
+                        net = net+network.nwbuf[i];
+                    }
+                    i1_ = (0)-(ntotal-nout);
+                    v = 0.0;
+                    for(i_=ntotal-nout; i_<=ntotal-1;i_++)
+                    {
+                        v += network.derror[i_]*network.nwbuf[i_+i1_];
+                    }
+                    for(i=0; i<=nout-1; i++)
+                    {
+                        fown = network.nwbuf[i];
+                        deown = network.derror[ntotal-nout+i];
+                        network.nwbuf[nout+i] = (-v+deown*fown+deown*(net-fown))*fown/math.sqr(net);
+                    }
+                    for(i=0; i<=nout-1; i++)
+                    {
+                        network.derror[ntotal-nout+i] = network.nwbuf[nout+i];
+                    }
+                }
+            }
+            else
+            {
+                
+                //
+                // Un-standardisation
+                //
+                for(i=0; i<=nout-1; i++)
+                {
+                    network.derror[ntotal-nout+i] = network.derror[ntotal-nout+i]*network.columnsigmas[nin+i];
+                }
+            }
+            
+            //
+            // Backpropagation
+            //
+            for(i=ntotal-1; i>=0; i--)
+            {
+                
+                //
+                // Extract info
+                //
+                offs = istart+i*nfieldwidth;
+                if( network.structinfo[offs+0]>0 | network.structinfo[offs+0]==-5 )
+                {
+                    
+                    //
+                    // Activation function
+                    //
+                    dedf = network.derror[i];
+                    dfdnet = network.dfdnet[i];
+                    derror[network.structinfo[offs+2]] = derror[network.structinfo[offs+2]]+dedf*dfdnet;
+                    continue;
+                }
+                if( network.structinfo[offs+0]==0 )
+                {
+                    
+                    //
+                    // Adaptive summator
+                    //
+                    n1 = network.structinfo[offs+2];
+                    n2 = n1+network.structinfo[offs+1]-1;
+                    w1 = network.structinfo[offs+3];
+                    w2 = w1+network.structinfo[offs+1]-1;
+                    dedf = network.derror[i];
+                    dfdnet = 1.0;
+                    v = dedf*dfdnet;
+                    i1_ = (n1) - (w1);
+                    for(i_=w1; i_<=w2;i_++)
+                    {
+                        grad[i_] = v*neurons[i_+i1_];
+                    }
+                    i1_ = (w1) - (n1);
+                    for(i_=n1; i_<=n2;i_++)
+                    {
+                        derror[i_] = derror[i_] + v*weights[i_+i1_];
+                    }
+                    continue;
+                }
+                if( network.structinfo[offs+0]<0 )
+                {
+                    bflag = false;
+                    if( (network.structinfo[offs+0]==-2 | network.structinfo[offs+0]==-3) | network.structinfo[offs+0]==-4 )
+                    {
+                        
+                        //
+                        // Special neuron type, no back-propagation required
+                        //
+                        bflag = true;
+                    }
+                    ap.assert(bflag, "MLPInternalCalculateGradient: unknown neuron type!");
+                    continue;
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine, chunked gradient
+        *************************************************************************/
+        private static void mlpchunkedgradient(multilayerperceptron network,
+            double[,] xy,
+            int cstart,
+            int csize,
+            ref double e,
+            ref double[] grad,
+            bool naturalerrorfunc)
+        {
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int kl = 0;
+            int n1 = 0;
+            int n2 = 0;
+            int w1 = 0;
+            int w2 = 0;
+            int c1 = 0;
+            int c2 = 0;
+            int ntotal = 0;
+            int nin = 0;
+            int nout = 0;
+            int offs = 0;
+            double f = 0;
+            double df = 0;
+            double d2f = 0;
+            double v = 0;
+            double s = 0;
+            double fown = 0;
+            double deown = 0;
+            double net = 0;
+            double lnnet = 0;
+            double mx = 0;
+            bool bflag = new bool();
+            int istart = 0;
+            int ineurons = 0;
+            int idfdnet = 0;
+            int iderror = 0;
+            int izeros = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            
+            //
+            // Read network geometry, prepare data
+            //
+            nin = network.structinfo[1];
+            nout = network.structinfo[2];
+            ntotal = network.structinfo[3];
+            istart = network.structinfo[5];
+            c1 = cstart;
+            c2 = cstart+csize-1;
+            ineurons = 0;
+            idfdnet = ntotal;
+            iderror = 2*ntotal;
+            izeros = 3*ntotal;
+            for(j=0; j<=csize-1; j++)
+            {
+                network.chunks[izeros,j] = 0;
+            }
+            
+            //
+            // Forward pass:
+            // 1. Load inputs from XY to Chunks[0:NIn-1,0:CSize-1]
+            // 2. Forward pass
+            //
+            for(i=0; i<=nin-1; i++)
+            {
+                for(j=0; j<=csize-1; j++)
+                {
+                    if( (double)(network.columnsigmas[i])!=(double)(0) )
+                    {
+                        network.chunks[i,j] = (xy[c1+j,i]-network.columnmeans[i])/network.columnsigmas[i];
+                    }
+                    else
+                    {
+                        network.chunks[i,j] = xy[c1+j,i]-network.columnmeans[i];
+                    }
+                }
+            }
+            for(i=0; i<=ntotal-1; i++)
+            {
+                offs = istart+i*nfieldwidth;
+                if( network.structinfo[offs+0]>0 | network.structinfo[offs+0]==-5 )
+                {
+                    
+                    //
+                    // Activation function:
+                    // * calculate F vector, F(i) = F(NET(i))
+                    //
+                    n1 = network.structinfo[offs+2];
+                    for(i_=0; i_<=csize-1;i_++)
+                    {
+                        network.chunks[i,i_] = network.chunks[n1,i_];
+                    }
+                    for(j=0; j<=csize-1; j++)
+                    {
+                        mlpactivationfunction(network.chunks[i,j], network.structinfo[offs+0], ref f, ref df, ref d2f);
+                        network.chunks[i,j] = f;
+                        network.chunks[idfdnet+i,j] = df;
+                    }
+                    continue;
+                }
+                if( network.structinfo[offs+0]==0 )
+                {
+                    
+                    //
+                    // Adaptive summator:
+                    // * calculate NET vector, NET(i) = SUM(W(j,i)*Neurons(j),j=N1..N2)
+                    //
+                    n1 = network.structinfo[offs+2];
+                    n2 = n1+network.structinfo[offs+1]-1;
+                    w1 = network.structinfo[offs+3];
+                    w2 = w1+network.structinfo[offs+1]-1;
+                    for(i_=0; i_<=csize-1;i_++)
+                    {
+                        network.chunks[i,i_] = network.chunks[izeros,i_];
+                    }
+                    for(j=n1; j<=n2; j++)
+                    {
+                        v = network.weights[w1+j-n1];
+                        for(i_=0; i_<=csize-1;i_++)
+                        {
+                            network.chunks[i,i_] = network.chunks[i,i_] + v*network.chunks[j,i_];
+                        }
+                    }
+                    continue;
+                }
+                if( network.structinfo[offs+0]<0 )
+                {
+                    bflag = false;
+                    if( network.structinfo[offs+0]==-2 )
+                    {
+                        
+                        //
+                        // input neuron, left unchanged
+                        //
+                        bflag = true;
+                    }
+                    if( network.structinfo[offs+0]==-3 )
+                    {
+                        
+                        //
+                        // "-1" neuron
+                        //
+                        for(k=0; k<=csize-1; k++)
+                        {
+                            network.chunks[i,k] = -1;
+                        }
+                        bflag = true;
+                    }
+                    if( network.structinfo[offs+0]==-4 )
+                    {
+                        
+                        //
+                        // "0" neuron
+                        //
+                        for(k=0; k<=csize-1; k++)
+                        {
+                            network.chunks[i,k] = 0;
+                        }
+                        bflag = true;
+                    }
+                    ap.assert(bflag, "MLPChunkedGradient: internal error - unknown neuron type!");
+                    continue;
+                }
+            }
+            
+            //
+            // Post-processing, error, dError/dOut
+            //
+            for(i=0; i<=ntotal-1; i++)
+            {
+                for(i_=0; i_<=csize-1;i_++)
+                {
+                    network.chunks[iderror+i,i_] = network.chunks[izeros,i_];
+                }
+            }
+            ap.assert(network.structinfo[6]==0 | network.structinfo[6]==1, "MLPChunkedGradient: unknown normalization type!");
+            if( network.structinfo[6]==1 )
+            {
+                
+                //
+                // Softmax output, classification network.
+                //
+                // For each K = 0..CSize-1 do:
+                // 1. place exp(outputs[k]) to NWBuf[0:NOut-1]
+                // 2. place sum(exp(..)) to NET
+                // 3. calculate dError/dOut and place it to the second block of Chunks
+                //
+                for(k=0; k<=csize-1; k++)
+                {
+                    
+                    //
+                    // Normalize
+                    //
+                    mx = network.chunks[ntotal-nout,k];
+                    for(i=1; i<=nout-1; i++)
+                    {
+                        mx = Math.Max(mx, network.chunks[ntotal-nout+i,k]);
+                    }
+                    net = 0;
+                    for(i=0; i<=nout-1; i++)
+                    {
+                        network.nwbuf[i] = Math.Exp(network.chunks[ntotal-nout+i,k]-mx);
+                        net = net+network.nwbuf[i];
+                    }
+                    
+                    //
+                    // Calculate error function and dError/dOut
+                    //
+                    if( naturalerrorfunc )
+                    {
+                        
+                        //
+                        // Natural error func.
+                        //
+                        //
+                        s = 1;
+                        lnnet = Math.Log(net);
+                        kl = (int)Math.Round(xy[cstart+k,nin]);
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            if( i==kl )
+                            {
+                                v = 1;
+                            }
+                            else
+                            {
+                                v = 0;
+                            }
+                            network.chunks[iderror+ntotal-nout+i,k] = s*network.nwbuf[i]/net-v;
+                            e = e+safecrossentropy(v, network.nwbuf[i]/net);
+                        }
+                    }
+                    else
+                    {
+                        
+                        //
+                        // Least squares error func
+                        // Error, dError/dOut(normalized)
+                        //
+                        kl = (int)Math.Round(xy[cstart+k,nin]);
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            if( i==kl )
+                            {
+                                v = network.nwbuf[i]/net-1;
+                            }
+                            else
+                            {
+                                v = network.nwbuf[i]/net;
+                            }
+                            network.nwbuf[nout+i] = v;
+                            e = e+math.sqr(v)/2;
+                        }
+                        
+                        //
+                        // From dError/dOut(normalized) to dError/dOut(non-normalized)
+                        //
+                        i1_ = (0)-(nout);
+                        v = 0.0;
+                        for(i_=nout; i_<=2*nout-1;i_++)
+                        {
+                            v += network.nwbuf[i_]*network.nwbuf[i_+i1_];
+                        }
+                        for(i=0; i<=nout-1; i++)
+                        {
+                            fown = network.nwbuf[i];
+                            deown = network.nwbuf[nout+i];
+                            network.chunks[iderror+ntotal-nout+i,k] = (-v+deown*fown+deown*(net-fown))*fown/math.sqr(net);
+                        }
+                    }
+                }
+            }
+            else
+            {
+                
+                //
+                // Normal output, regression network
+                //
+                // For each K = 0..CSize-1 do:
+                // 1. calculate dError/dOut and place it to the second block of Chunks
+                //
+                for(i=0; i<=nout-1; i++)
+                {
+                    for(j=0; j<=csize-1; j++)
+                    {
+                        v = network.chunks[ntotal-nout+i,j]*network.columnsigmas[nin+i]+network.columnmeans[nin+i]-xy[cstart+j,nin+i];
+                        network.chunks[iderror+ntotal-nout+i,j] = v*network.columnsigmas[nin+i];
+                        e = e+math.sqr(v)/2;
+                    }
+                }
+            }
+            
+            //
+            // Backpropagation
+            //
+            for(i=ntotal-1; i>=0; i--)
+            {
+                
+                //
+                // Extract info
+                //
+                offs = istart+i*nfieldwidth;
+                if( network.structinfo[offs+0]>0 | network.structinfo[offs+0]==-5 )
+                {
+                    
+                    //
+                    // Activation function
+                    //
+                    n1 = network.structinfo[offs+2];
+                    for(k=0; k<=csize-1; k++)
+                    {
+                        network.chunks[iderror+i,k] = network.chunks[iderror+i,k]*network.chunks[idfdnet+i,k];
+                    }
+                    for(i_=0; i_<=csize-1;i_++)
+                    {
+                        network.chunks[iderror+n1,i_] = network.chunks[iderror+n1,i_] + network.chunks[iderror+i,i_];
+                    }
+                    continue;
+                }
+                if( network.structinfo[offs+0]==0 )
+                {
+                    
+                    //
+                    // "Normal" activation function
+                    //
+                    n1 = network.structinfo[offs+2];
+                    n2 = n1+network.structinfo[offs+1]-1;
+                    w1 = network.structinfo[offs+3];
+                    w2 = w1+network.structinfo[offs+1]-1;
+                    for(j=w1; j<=w2; j++)
+                    {
+                        v = 0.0;
+                        for(i_=0; i_<=csize-1;i_++)
+                        {
+                            v += network.chunks[n1+j-w1,i_]*network.chunks[iderror+i,i_];
+                        }
+                        grad[j] = grad[j]+v;
+                    }
+                    for(j=n1; j<=n2; j++)
+                    {
+                        v = network.weights[w1+j-n1];
+                        for(i_=0; i_<=csize-1;i_++)
+                        {
+                            network.chunks[iderror+j,i_] = network.chunks[iderror+j,i_] + v*network.chunks[iderror+i,i_];
+                        }
+                    }
+                    continue;
+                }
+                if( network.structinfo[offs+0]<0 )
+                {
+                    bflag = false;
+                    if( (network.structinfo[offs+0]==-2 | network.structinfo[offs+0]==-3) | network.structinfo[offs+0]==-4 )
+                    {
+                        
+                        //
+                        // Special neuron type, no back-propagation required
+                        //
+                        bflag = true;
+                    }
+                    ap.assert(bflag, "MLPInternalCalculateGradient: unknown neuron type!");
+                    continue;
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Returns T*Ln(T/Z), guarded against overflow/underflow.
+        Internal subroutine.
+        *************************************************************************/
+        private static double safecrossentropy(double t,
+            double z)
+        {
+            double result = 0;
+            double r = 0;
+
+            if( (double)(t)==(double)(0) )
+            {
+                result = 0;
+            }
+            else
+            {
+                if( (double)(Math.Abs(z))>(double)(1) )
+                {
+                    
+                    //
+                    // Shouldn't be the case with softmax,
+                    // but we just want to be sure.
+                    //
+                    if( (double)(t/z)==(double)(0) )
+                    {
+                        r = math.minrealnumber;
+                    }
+                    else
+                    {
+                        r = t/z;
+                    }
+                }
+                else
+                {
+                    
+                    //
+                    // Normal case
+                    //
+                    if( (double)(z)==(double)(0) | (double)(Math.Abs(t))>=(double)(math.maxrealnumber*Math.Abs(z)) )
+                    {
+                        r = math.maxrealnumber;
+                    }
+                    else
+                    {
+                        r = t/z;
+                    }
+                }
+                result = t*Math.Log(r);
+            }
+            return result;
+        }
+
+
+    }
     public class logit
     {
@@ -7962,4 +13993,30 @@
 
 
+        /*************************************************************************
+        This subroutine trains logit model.
+
+        INPUT PARAMETERS:
+            XY          -   training set, array[0..NPoints-1,0..NVars]
+                            First NVars columns store values of independent
+                            variables, next column stores number of class (from 0
+                            to NClasses-1) which dataset element belongs to. Fractional
+                            values are rounded to nearest integer.
+            NPoints     -   training set size, NPoints>=1
+            NVars       -   number of independent variables, NVars>=1
+            NClasses    -   number of classes, NClasses>=2
+
+        OUTPUT PARAMETERS:
+            Info        -   return code:
+                            * -2, if there is a point with class number
+                                  outside of [0..NClasses-1].
+                            * -1, if incorrect parameters was passed
+                                  (NPoints<NVars+2, NVars<1, NClasses<2).
+                            *  1, if task has been solved
+            LM          -   model built
+            Rep         -   training report
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static void mnltrainh(double[,] xy,
             int npoints,
@@ -8299,4 +14356,23 @@
 
 
+        /*************************************************************************
+        Procesing
+
+        INPUT PARAMETERS:
+            LM      -   logit model, passed by non-constant reference
+                        (some fields of structure are used as temporaries
+                        when calculating model output).
+            X       -   input vector,  array[0..NVars-1].
+            Y       -   (possibly) preallocated buffer; if size of Y is less than
+                        NClasses, it will be reallocated.If it is large enough, it
+                        is NOT reallocated, so we can save some time on reallocation.
+
+        OUTPUT PARAMETERS:
+            Y       -   result, array[0..NClasses-1]
+                        Vector of posterior probabilities for classification task.
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static void mnlprocess(logitmodel lm,
             double[] x,
@@ -8332,4 +14408,16 @@
 
 
+        /*************************************************************************
+        'interactive'  variant  of  MNLProcess  for  languages  like  Python which
+        support constructs like "Y = MNLProcess(LM,X)" and interactive mode of the
+        interpreter
+
+        This function allocates new array on each call,  so  it  is  significantly
+        slower than its 'non-interactive' counterpart, but it is  more  convenient
+        when you call it from command line.
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static void mnlprocessi(logitmodel lm,
             double[] x,
@@ -8342,4 +14430,22 @@
 
 
+        /*************************************************************************
+        Unpacks coefficients of logit model. Logit model have form:
+
+            P(class=i) = S(i) / (S(0) + S(1) + ... +S(M-1))
+                  S(i) = Exp(A[i,0]*X[0] + ... + A[i,N-1]*X[N-1] + A[i,N]), when i<M-1
+                S(M-1) = 1
+
+        INPUT PARAMETERS:
+            LM          -   logit model in ALGLIB format
+
+        OUTPUT PARAMETERS:
+            V           -   coefficients, array[0..NClasses-2,0..NVars]
+            NVars       -   number of independent variables
+            NClasses    -   number of classes
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static void mnlunpack(logitmodel lm,
             ref double[,] a,
@@ -8372,4 +14478,19 @@
 
 
+        /*************************************************************************
+        "Packs" coefficients and creates logit model in ALGLIB format (MNLUnpack
+        reversed).
+
+        INPUT PARAMETERS:
+            A           -   model (see MNLUnpack)
+            NVars       -   number of independent variables
+            NClasses    -   number of classes
+
+        OUTPUT PARAMETERS:
+            LM          -   logit model.
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static void mnlpack(double[,] a,
             int nvars,
@@ -8404,4 +14525,16 @@
 
 
+        /*************************************************************************
+        Copying of LogitModel strucure
+
+        INPUT PARAMETERS:
+            LM1 -   original
+
+        OUTPUT PARAMETERS:
+            LM2 -   copy
+
+          -- ALGLIB --
+             Copyright 15.03.2009 by Bochkanov Sergey
+        *************************************************************************/
         public static void mnlcopy(logitmodel lm1,
             logitmodel lm2)
@@ -8419,4 +14552,18 @@
 
 
+        /*************************************************************************
+        Average cross-entropy (in bits per element) on the test set
+
+        INPUT PARAMETERS:
+            LM      -   logit model
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            CrossEntropy/(NPoints*ln(2)).
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static double mnlavgce(logitmodel lm,
             double[,] xy,
@@ -8463,4 +14610,18 @@
 
 
+        /*************************************************************************
+        Relative classification error on the test set
+
+        INPUT PARAMETERS:
+            LM      -   logit model
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            percent of incorrectly classified cases.
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static double mnlrelclserror(logitmodel lm,
             double[,] xy,
@@ -8474,4 +14635,18 @@
 
 
+        /*************************************************************************
+        RMS error on the test set
+
+        INPUT PARAMETERS:
+            LM      -   logit model
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            root mean square error (error when estimating posterior probabilities).
+
+          -- ALGLIB --
+             Copyright 30.08.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static double mnlrmserror(logitmodel lm,
             double[,] xy,
@@ -8492,4 +14667,18 @@
 
 
+        /*************************************************************************
+        Average error on the test set
+
+        INPUT PARAMETERS:
+            LM      -   logit model
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            average error (error when estimating posterior probabilities).
+
+          -- ALGLIB --
+             Copyright 30.08.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static double mnlavgerror(logitmodel lm,
             double[,] xy,
@@ -8510,4 +14699,18 @@
 
 
+        /*************************************************************************
+        Average relative error on the test set
+
+        INPUT PARAMETERS:
+            LM      -   logit model
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            average relative error (error when estimating posterior probabilities).
+
+          -- ALGLIB --
+             Copyright 30.08.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static double mnlavgrelerror(logitmodel lm,
             double[,] xy,
@@ -8528,4 +14731,10 @@
 
 
+        /*************************************************************************
+        Classification error on test set = MNLRelClsError*NPoints
+
+          -- ALGLIB --
+             Copyright 10.09.2008 by Bochkanov Sergey
+        *************************************************************************/
         public static int mnlclserror(logitmodel lm,
             double[,] xy,
@@ -8584,4 +14793,8 @@
 
 
+        /*************************************************************************
+        Internal subroutine. Places exponents of the anti-overflow shifted
+        internal linear outputs into the service part of the W array.
+        *************************************************************************/
         private static void mnliexp(ref double[] w,
             double[] x)
@@ -8625,4 +14838,10 @@
 
 
+        /*************************************************************************
+        Calculation of all types of errors
+
+          -- ALGLIB --
+             Copyright 30.08.2008 by Bochkanov Sergey
+        *************************************************************************/
         private static void mnlallerrors(logitmodel lm,
             double[,] xy,
@@ -8675,4 +14894,91 @@
 
 
+        /*************************************************************************
+        THE  PURPOSE  OF  MCSRCH  IS  TO  FIND A STEP WHICH SATISFIES A SUFFICIENT
+        DECREASE CONDITION AND A CURVATURE CONDITION.
+
+        AT EACH STAGE THE SUBROUTINE  UPDATES  AN  INTERVAL  OF  UNCERTAINTY  WITH
+        ENDPOINTS  STX  AND  STY.  THE INTERVAL OF UNCERTAINTY IS INITIALLY CHOSEN
+        SO THAT IT CONTAINS A MINIMIZER OF THE MODIFIED FUNCTION
+
+            F(X+STP*S) - F(X) - FTOL*STP*(GRADF(X)'S).
+
+        IF  A STEP  IS OBTAINED FOR  WHICH THE MODIFIED FUNCTION HAS A NONPOSITIVE
+        FUNCTION  VALUE  AND  NONNEGATIVE  DERIVATIVE,   THEN   THE   INTERVAL  OF
+        UNCERTAINTY IS CHOSEN SO THAT IT CONTAINS A MINIMIZER OF F(X+STP*S).
+
+        THE  ALGORITHM  IS  DESIGNED TO FIND A STEP WHICH SATISFIES THE SUFFICIENT
+        DECREASE CONDITION
+
+            F(X+STP*S) .LE. F(X) + FTOL*STP*(GRADF(X)'S),
+
+        AND THE CURVATURE CONDITION
+
+            ABS(GRADF(X+STP*S)'S)) .LE. GTOL*ABS(GRADF(X)'S).
+
+        IF  FTOL  IS  LESS  THAN GTOL AND IF, FOR EXAMPLE, THE FUNCTION IS BOUNDED
+        BELOW,  THEN  THERE  IS  ALWAYS  A  STEP  WHICH SATISFIES BOTH CONDITIONS.
+        IF  NO  STEP  CAN BE FOUND  WHICH  SATISFIES  BOTH  CONDITIONS,  THEN  THE
+        ALGORITHM  USUALLY STOPS  WHEN  ROUNDING ERRORS  PREVENT FURTHER PROGRESS.
+        IN THIS CASE STP ONLY SATISFIES THE SUFFICIENT DECREASE CONDITION.
+
+        PARAMETERS DESCRIPRION
+
+        N IS A POSITIVE INTEGER INPUT VARIABLE SET TO THE NUMBER OF VARIABLES.
+
+        X IS  AN  ARRAY  OF  LENGTH N. ON INPUT IT MUST CONTAIN THE BASE POINT FOR
+        THE LINE SEARCH. ON OUTPUT IT CONTAINS X+STP*S.
+
+        F IS  A  VARIABLE. ON INPUT IT MUST CONTAIN THE VALUE OF F AT X. ON OUTPUT
+        IT CONTAINS THE VALUE OF F AT X + STP*S.
+
+        G IS AN ARRAY OF LENGTH N. ON INPUT IT MUST CONTAIN THE GRADIENT OF F AT X.
+        ON OUTPUT IT CONTAINS THE GRADIENT OF F AT X + STP*S.
+
+        S IS AN INPUT ARRAY OF LENGTH N WHICH SPECIFIES THE SEARCH DIRECTION.
+
+        STP  IS  A NONNEGATIVE VARIABLE. ON INPUT STP CONTAINS AN INITIAL ESTIMATE
+        OF A SATISFACTORY STEP. ON OUTPUT STP CONTAINS THE FINAL ESTIMATE.
+
+        FTOL AND GTOL ARE NONNEGATIVE INPUT VARIABLES. TERMINATION OCCURS WHEN THE
+        SUFFICIENT DECREASE CONDITION AND THE DIRECTIONAL DERIVATIVE CONDITION ARE
+        SATISFIED.
+
+        XTOL IS A NONNEGATIVE INPUT VARIABLE. TERMINATION OCCURS WHEN THE RELATIVE
+        WIDTH OF THE INTERVAL OF UNCERTAINTY IS AT MOST XTOL.
+
+        STPMIN AND STPMAX ARE NONNEGATIVE INPUT VARIABLES WHICH SPECIFY LOWER  AND
+        UPPER BOUNDS FOR THE STEP.
+
+        MAXFEV IS A POSITIVE INTEGER INPUT VARIABLE. TERMINATION OCCURS WHEN THE
+        NUMBER OF CALLS TO FCN IS AT LEAST MAXFEV BY THE END OF AN ITERATION.
+
+        INFO IS AN INTEGER OUTPUT VARIABLE SET AS FOLLOWS:
+            INFO = 0  IMPROPER INPUT PARAMETERS.
+
+            INFO = 1  THE SUFFICIENT DECREASE CONDITION AND THE
+                      DIRECTIONAL DERIVATIVE CONDITION HOLD.
+
+            INFO = 2  RELATIVE WIDTH OF THE INTERVAL OF UNCERTAINTY
+                      IS AT MOST XTOL.
+
+            INFO = 3  NUMBER OF CALLS TO FCN HAS REACHED MAXFEV.
+
+            INFO = 4  THE STEP IS AT THE LOWER BOUND STPMIN.
+
+            INFO = 5  THE STEP IS AT THE UPPER BOUND STPMAX.
+
+            INFO = 6  ROUNDING ERRORS PREVENT FURTHER PROGRESS.
+                      THERE MAY NOT BE A STEP WHICH SATISFIES THE
+                      SUFFICIENT DECREASE AND CURVATURE CONDITIONS.
+                      TOLERANCES MAY BE TOO SMALL.
+
+        NFEV IS AN INTEGER OUTPUT VARIABLE SET TO THE NUMBER OF CALLS TO FCN.
+
+        WA IS A WORK ARRAY OF LENGTH N.
+
+        ARGONNE NATIONAL LABORATORY. MINPACK PROJECT. JUNE 1983
+        JORGE J. MORE', DAVID J. THUENTE
+        *************************************************************************/
         private static void mnlmcsrch(int n,
             ref double[] x,
@@ -9241,1183 +15547,446 @@
 
     }
-    public class mlpbase
+    public class mcpd
     {
-        public class multilayerperceptron
-        {
-            public int[] structinfo;
-            public double[] weights;
-            public double[] columnmeans;
-            public double[] columnsigmas;
-            public double[] neurons;
-            public double[] dfdnet;
-            public double[] derror;
-            public double[] x;
-            public double[] y;
-            public double[,] chunks;
-            public double[] nwbuf;
-            public multilayerperceptron()
-            {
-                structinfo = new int[0];
-                weights = new double[0];
-                columnmeans = new double[0];
-                columnsigmas = new double[0];
-                neurons = new double[0];
-                dfdnet = new double[0];
-                derror = new double[0];
-                x = new double[0];
-                y = new double[0];
-                chunks = new double[0,0];
-                nwbuf = new double[0];
+        /*************************************************************************
+        This structure is a MCPD (Markov Chains for Population Data) solver.
+
+        You should use ALGLIB functions in order to work with this object.
+
+          -- ALGLIB --
+             Copyright 23.05.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public class mcpdstate
+        {
+            public int n;
+            public int[] states;
+            public int npairs;
+            public double[,] data;
+            public double[,] ec;
+            public double[,] bndl;
+            public double[,] bndu;
+            public double[,] c;
+            public int[] ct;
+            public int ccnt;
+            public double[] pw;
+            public double[,] priorp;
+            public double regterm;
+            public minbleic.minbleicstate bs;
+            public int repinneriterationscount;
+            public int repouteriterationscount;
+            public int repnfev;
+            public int repterminationtype;
+            public minbleic.minbleicreport br;
+            public double[] tmpp;
+            public double[] effectivew;
+            public double[] effectivebndl;
+            public double[] effectivebndu;
+            public double[,] effectivec;
+            public int[] effectivect;
+            public double[] h;
+            public double[,] p;
+            public mcpdstate()
+            {
+                states = new int[0];
+                data = new double[0,0];
+                ec = new double[0,0];
+                bndl = new double[0,0];
+                bndu = new double[0,0];
+                c = new double[0,0];
+                ct = new int[0];
+                pw = new double[0];
+                priorp = new double[0,0];
+                bs = new minbleic.minbleicstate();
+                br = new minbleic.minbleicreport();
+                tmpp = new double[0];
+                effectivew = new double[0];
+                effectivebndl = new double[0];
+                effectivebndu = new double[0];
+                effectivec = new double[0,0];
+                effectivect = new int[0];
+                h = new double[0];
+                p = new double[0,0];
             }
         };
 
 
-
-
-        public const int mlpvnum = 7;
-        public const int nfieldwidth = 4;
-        public const int chunksize = 32;
-
-
         /*************************************************************************
-        Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
-        layers, with linear output layer. Network weights are  filled  with  small
-        random values.
+        This structure is a MCPD training report:
+            InnerIterationsCount    -   number of inner iterations of the
+                                        underlying optimization algorithm
+            OuterIterationsCount    -   number of outer iterations of the
+                                        underlying optimization algorithm
+            NFEV                    -   number of merit function evaluations
+            TerminationType         -   termination type
+                                        (same as for MinBLEIC optimizer, positive
+                                        values denote success, negative ones -
+                                        failure)
 
           -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpcreate0(int nin,
-            int nout,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-
-            layerscount = 1+2;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
-        }
+        public class mcpdreport
+        {
+            public int inneriterationscount;
+            public int outeriterationscount;
+            public int nfev;
+            public int terminationtype;
+        };
+
+
+
+
+        public const double xtol = 1.0E-8;
 
 
         /*************************************************************************
-        Same  as  MLPCreate0,  but  with  one  hidden  layer  (NHid  neurons) with
-        non-linear activation function. Output layer is linear.
+        DESCRIPTION:
+
+        This function creates MCPD (Markov Chains for Population Data) solver.
+
+        This  solver  can  be  used  to find transition matrix P for N-dimensional
+        prediction  problem  where transition from X[i] to X[i+1] is  modelled  as
+            X[i+1] = P*X[i]
+        where X[i] and X[i+1] are N-dimensional population vectors (components  of
+        each X are non-negative), and P is a N*N transition matrix (elements of  P
+        are non-negative, each column sums to 1.0).
+
+        Such models arise when when:
+        * there is some population of individuals
+        * individuals can have different states
+        * individuals can transit from one state to another
+        * population size is constant, i.e. there is no new individuals and no one
+          leaves population
+        * you want to model transitions of individuals from one state into another
+
+        USAGE:
+
+        Here we give very brief outline of the MCPD. We strongly recommend you  to
+        read examples in the ALGLIB Reference Manual and to read ALGLIB User Guide
+        on data analysis which is available at http://www.alglib.net/dataanalysis/
+
+        1. User initializes algorithm state with MCPDCreate() call
+
+        2. User  adds  one  or  more  tracks -  sequences of states which describe
+           evolution of a system being modelled from different starting conditions
+
+        3. User may add optional boundary, equality  and/or  linear constraints on
+           the coefficients of P by calling one of the following functions:
+           * MCPDSetEC() to set equality constraints
+           * MCPDSetBC() to set bound constraints
+           * MCPDSetLC() to set linear constraints
+
+        4. Optionally,  user  may  set  custom  weights  for prediction errors (by
+           default, algorithm assigns non-equal, automatically chosen weights  for
+           errors in the prediction of different components of X). It can be  done
+           with a call of MCPDSetPredictionWeights() function.
+
+        5. User calls MCPDSolve() function which takes algorithm  state and
+           pointer (delegate, etc.) to callback function which calculates F/G.
+
+        6. User calls MCPDResults() to get solution
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>=1
+
+        OUTPUT PARAMETERS:
+            State   -   structure stores algorithm state
 
           -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpcreate1(int nin,
-            int nhid,
-            int nout,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-
-            layerscount = 1+3+2;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+        public static void mcpdcreate(int n,
+            mcpdstate s)
+        {
+            ap.assert(n>=1, "MCPDCreate: N<1");
+            mcpdinit(n, -1, -1, s);
         }
 
 
         /*************************************************************************
-        Same as MLPCreate0, but with two hidden layers (NHid1 and  NHid2  neurons)
-        with non-linear activation function. Output layer is linear.
-         $ALL
+        DESCRIPTION:
+
+        This function is a specialized version of MCPDCreate()  function,  and  we
+        recommend  you  to read comments for this function for general information
+        about MCPD solver.
+
+        This  function  creates  MCPD (Markov Chains for Population  Data)  solver
+        for "Entry-state" model,  i.e. model  where transition from X[i] to X[i+1]
+        is modelled as
+            X[i+1] = P*X[i]
+        where
+            X[i] and X[i+1] are N-dimensional state vectors
+            P is a N*N transition matrix
+        and  one  selected component of X[] is called "entry" state and is treated
+        in a special way:
+            system state always transits from "entry" state to some another state
+            system state can not transit from any state into "entry" state
+        Such conditions basically mean that row of P which corresponds to  "entry"
+        state is zero.
+
+        Such models arise when:
+        * there is some population of individuals
+        * individuals can have different states
+        * individuals can transit from one state to another
+        * population size is NOT constant -  at every moment of time there is some
+          (unpredictable) amount of "new" individuals, which can transit into  one
+          of the states at the next turn, but still no one leaves population
+        * you want to model transitions of individuals from one state into another
+        * but you do NOT want to predict amount of "new"  individuals  because  it
+          does not depends on individuals already present (hence  system  can  not
+          transit INTO entry state - it can only transit FROM it).
+
+        This model is discussed  in  more  details  in  the ALGLIB User Guide (see
+        http://www.alglib.net/dataanalysis/ for more data).
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>=2
+            EntryState- index of entry state, in 0..N-1
+
+        OUTPUT PARAMETERS:
+            State   -   structure stores algorithm state
 
           -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpcreate2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-
-            layerscount = 1+3+3+2;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
+        public static void mcpdcreateentry(int n,
+            int entrystate,
+            mcpdstate s)
+        {
+            ap.assert(n>=2, "MCPDCreateEntry: N<2");
+            ap.assert(entrystate>=0, "MCPDCreateEntry: EntryState<0");
+            ap.assert(entrystate<n, "MCPDCreateEntry: EntryState>=N");
+            mcpdinit(n, entrystate, -1, s);
         }
 
 
         /*************************************************************************
-        Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
-        layers with non-linear output layer. Network weights are filled with small
-        random values.
-
-        Activation function of the output layer takes values:
-
-            (B, +INF), if D>=0
-
-        or
-
-            (-INF, B), if D<0.
-
+        DESCRIPTION:
+
+        This function is a specialized version of MCPDCreate()  function,  and  we
+        recommend  you  to read comments for this function for general information
+        about MCPD solver.
+
+        This  function  creates  MCPD (Markov Chains for Population  Data)  solver
+        for "Exit-state" model,  i.e. model  where  transition from X[i] to X[i+1]
+        is modelled as
+            X[i+1] = P*X[i]
+        where
+            X[i] and X[i+1] are N-dimensional state vectors
+            P is a N*N transition matrix
+        and  one  selected component of X[] is called "exit"  state and is treated
+        in a special way:
+            system state can transit from any state into "exit" state
+            system state can not transit from "exit" state into any other state
+            transition operator discards "exit" state (makes it zero at each turn)
+        Such  conditions  basically  mean  that  column  of P which corresponds to
+        "exit" state is zero. Multiplication by such P may decrease sum of  vector
+        components.
+
+        Such models arise when:
+        * there is some population of individuals
+        * individuals can have different states
+        * individuals can transit from one state to another
+        * population size is NOT constant - individuals can move into "exit" state
+          and leave population at the next turn, but there are no new individuals
+        * amount of individuals which leave population can be predicted
+        * you want to model transitions of individuals from one state into another
+          (including transitions into the "exit" state)
+
+        This model is discussed  in  more  details  in  the ALGLIB User Guide (see
+        http://www.alglib.net/dataanalysis/ for more data).
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>=2
+            ExitState-  index of exit state, in 0..N-1
+
+        OUTPUT PARAMETERS:
+            State   -   structure stores algorithm state
 
           -- ALGLIB --
-             Copyright 30.03.2008 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpcreateb0(int nin,
-            int nout,
-            double b,
-            double d,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-            int i = 0;
-
-            layerscount = 1+3;
-            if( (double)(d)>=(double)(0) )
-            {
-                d = 1;
-            }
-            else
-            {
-                d = -1;
-            }
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(3, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
-            
-            //
-            // Turn on ouputs shift/scaling.
-            //
-            for(i=nin; i<=nin+nout-1; i++)
-            {
-                network.columnmeans[i] = b;
-                network.columnsigmas[i] = d;
-            }
+        public static void mcpdcreateexit(int n,
+            int exitstate,
+            mcpdstate s)
+        {
+            ap.assert(n>=2, "MCPDCreateExit: N<2");
+            ap.assert(exitstate>=0, "MCPDCreateExit: ExitState<0");
+            ap.assert(exitstate<n, "MCPDCreateExit: ExitState>=N");
+            mcpdinit(n, -1, exitstate, s);
         }
 
 
         /*************************************************************************
-        Same as MLPCreateB0 but with non-linear hidden layer.
+        DESCRIPTION:
+
+        This function is a specialized version of MCPDCreate()  function,  and  we
+        recommend  you  to read comments for this function for general information
+        about MCPD solver.
+
+        This  function  creates  MCPD (Markov Chains for Population  Data)  solver
+        for "Entry-Exit-states" model, i.e. model where  transition  from  X[i] to
+        X[i+1] is modelled as
+            X[i+1] = P*X[i]
+        where
+            X[i] and X[i+1] are N-dimensional state vectors
+            P is a N*N transition matrix
+        one selected component of X[] is called "entry" state and is treated in  a
+        special way:
+            system state always transits from "entry" state to some another state
+            system state can not transit from any state into "entry" state
+        and another one component of X[] is called "exit" state and is treated  in
+        a special way too:
+            system state can transit from any state into "exit" state
+            system state can not transit from "exit" state into any other state
+            transition operator discards "exit" state (makes it zero at each turn)
+        Such conditions basically mean that:
+            row of P which corresponds to "entry" state is zero
+            column of P which corresponds to "exit" state is zero
+        Multiplication by such P may decrease sum of vector components.
+
+        Such models arise when:
+        * there is some population of individuals
+        * individuals can have different states
+        * individuals can transit from one state to another
+        * population size is NOT constant
+        * at every moment of time there is some (unpredictable)  amount  of  "new"
+          individuals, which can transit into one of the states at the next turn
+        * some  individuals  can  move  (predictably)  into "exit" state and leave
+          population at the next turn
+        * you want to model transitions of individuals from one state into another,
+          including transitions from the "entry" state and into the "exit" state.
+        * but you do NOT want to predict amount of "new"  individuals  because  it
+          does not depends on individuals already present (hence  system  can  not
+          transit INTO entry state - it can only transit FROM it).
+
+        This model is discussed  in  more  details  in  the ALGLIB User Guide (see
+        http://www.alglib.net/dataanalysis/ for more data).
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>=2
+            EntryState- index of entry state, in 0..N-1
+            ExitState-  index of exit state, in 0..N-1
+
+        OUTPUT PARAMETERS:
+            State   -   structure stores algorithm state
 
           -- ALGLIB --
-             Copyright 30.03.2008 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpcreateb1(int nin,
-            int nhid,
-            int nout,
-            double b,
-            double d,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-            int i = 0;
-
-            layerscount = 1+3+3;
-            if( (double)(d)>=(double)(0) )
-            {
-                d = 1;
-            }
-            else
-            {
-                d = -1;
-            }
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(3, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
-            
-            //
-            // Turn on ouputs shift/scaling.
-            //
-            for(i=nin; i<=nin+nout-1; i++)
-            {
-                network.columnmeans[i] = b;
-                network.columnsigmas[i] = d;
-            }
+        public static void mcpdcreateentryexit(int n,
+            int entrystate,
+            int exitstate,
+            mcpdstate s)
+        {
+            ap.assert(n>=2, "MCPDCreateEntryExit: N<2");
+            ap.assert(entrystate>=0, "MCPDCreateEntryExit: EntryState<0");
+            ap.assert(entrystate<n, "MCPDCreateEntryExit: EntryState>=N");
+            ap.assert(exitstate>=0, "MCPDCreateEntryExit: ExitState<0");
+            ap.assert(exitstate<n, "MCPDCreateEntryExit: ExitState>=N");
+            ap.assert(entrystate!=exitstate, "MCPDCreateEntryExit: EntryState=ExitState");
+            mcpdinit(n, entrystate, exitstate, s);
         }
 
 
         /*************************************************************************
-        Same as MLPCreateB0 but with two non-linear hidden layers.
+        This  function  is  used to add a track - sequence of system states at the
+        different moments of its evolution.
+
+        You  may  add  one  or several tracks to the MCPD solver. In case you have
+        several tracks, they won't overwrite each other. For example,  if you pass
+        two tracks, A1-A2-A3 (system at t=A+1, t=A+2 and t=A+3) and B1-B2-B3, then
+        solver will try to model transitions from t=A+1 to t=A+2, t=A+2 to  t=A+3,
+        t=B+1 to t=B+2, t=B+2 to t=B+3. But it WONT mix these two tracks - i.e. it
+        wont try to model transition from t=A+3 to t=B+1.
+
+        INPUT PARAMETERS:
+            S       -   solver
+            XY      -   track, array[K,N]:
+                        * I-th row is a state at t=I
+                        * elements of XY must be non-negative (exception will be
+                          thrown on negative elements)
+            K       -   number of points in a track
+                        * if given, only leading K rows of XY are used
+                        * if not given, automatically determined from size of XY
+
+        NOTES:
+
+        1. Track may contain either proportional or population data:
+           * with proportional data all rows of XY must sum to 1.0, i.e. we have
+             proportions instead of absolute population values
+           * with population data rows of XY contain population counts and generally
+             do not sum to 1.0 (although they still must be non-negative)
 
           -- ALGLIB --
-             Copyright 30.03.2008 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpcreateb2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            double b,
-            double d,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-            int i = 0;
-
-            layerscount = 1+3+3+3;
-            if( (double)(d)>=(double)(0) )
-            {
-                d = 1;
-            }
-            else
-            {
-                d = -1;
-            }
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(3, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
-            
-            //
-            // Turn on ouputs shift/scaling.
-            //
-            for(i=nin; i<=nin+nout-1; i++)
-            {
-                network.columnmeans[i] = b;
-                network.columnsigmas[i] = d;
-            }
-        }
-
-
-        /*************************************************************************
-        Creates  neural  network  with  NIn  inputs,  NOut outputs, without hidden
-        layers with non-linear output layer. Network weights are filled with small
-        random values. Activation function of the output layer takes values [A,B].
-
-          -- ALGLIB --
-             Copyright 30.03.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpcreater0(int nin,
-            int nout,
-            double a,
-            double b,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-            int i = 0;
-
-            layerscount = 1+3;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
-            
-            //
-            // Turn on outputs shift/scaling.
-            //
-            for(i=nin; i<=nin+nout-1; i++)
-            {
-                network.columnmeans[i] = 0.5*(a+b);
-                network.columnsigmas[i] = 0.5*(a-b);
-            }
-        }
-
-
-        /*************************************************************************
-        Same as MLPCreateR0, but with non-linear hidden layer.
-
-          -- ALGLIB --
-             Copyright 30.03.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpcreater1(int nin,
-            int nhid,
-            int nout,
-            double a,
-            double b,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-            int i = 0;
-
-            layerscount = 1+3+3;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
-            
-            //
-            // Turn on outputs shift/scaling.
-            //
-            for(i=nin; i<=nin+nout-1; i++)
-            {
-                network.columnmeans[i] = 0.5*(a+b);
-                network.columnsigmas[i] = 0.5*(a-b);
-            }
-        }
-
-
-        /*************************************************************************
-        Same as MLPCreateR0, but with two non-linear hidden layers.
-
-          -- ALGLIB --
-             Copyright 30.03.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpcreater2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            double a,
-            double b,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-            int i = 0;
-
-            layerscount = 1+3+3+3;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, false, network);
-            
-            //
-            // Turn on outputs shift/scaling.
-            //
-            for(i=nin; i<=nin+nout-1; i++)
-            {
-                network.columnmeans[i] = 0.5*(a+b);
-                network.columnsigmas[i] = 0.5*(a-b);
-            }
-        }
-
-
-        /*************************************************************************
-        Creates classifier network with NIn  inputs  and  NOut  possible  classes.
-        Network contains no hidden layers and linear output  layer  with  SOFTMAX-
-        normalization  (so  outputs  sums  up  to  1.0  and  converge to posterior
-        probabilities).
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpcreatec0(int nin,
-            int nout,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-
-            ap.assert(nout>=2, "MLPCreateC0: NOut<2!");
-            layerscount = 1+2+1;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout-1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addzerolayer(ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, true, network);
-        }
-
-
-        /*************************************************************************
-        Same as MLPCreateC0, but with one non-linear hidden layer.
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpcreatec1(int nin,
-            int nhid,
-            int nout,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-
-            ap.assert(nout>=2, "MLPCreateC1: NOut<2!");
-            layerscount = 1+3+2+1;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout-1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addzerolayer(ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, true, network);
-        }
-
-
-        /*************************************************************************
-        Same as MLPCreateC0, but with two non-linear hidden layers.
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpcreatec2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            multilayerperceptron network)
-        {
-            int[] lsizes = new int[0];
-            int[] ltypes = new int[0];
-            int[] lconnfirst = new int[0];
-            int[] lconnlast = new int[0];
-            int layerscount = 0;
-            int lastproc = 0;
-
-            ap.assert(nout>=2, "MLPCreateC2: NOut<2!");
-            layerscount = 1+3+3+2+1;
-            
-            //
-            // Allocate arrays
-            //
-            lsizes = new int[layerscount-1+1];
-            ltypes = new int[layerscount-1+1];
-            lconnfirst = new int[layerscount-1+1];
-            lconnlast = new int[layerscount-1+1];
-            
-            //
-            // Layers
-            //
-            addinputlayer(nin, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nhid2, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addactivationlayer(1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addbiasedsummatorlayer(nout-1, ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            addzerolayer(ref lsizes, ref ltypes, ref lconnfirst, ref lconnlast, ref lastproc);
-            
-            //
-            // Create
-            //
-            mlpcreate(nin, nout, lsizes, ltypes, lconnfirst, lconnlast, layerscount, true, network);
-        }
-
-
-        /*************************************************************************
-        Copying of neural network
-
-        INPUT PARAMETERS:
-            Network1 -   original
-
-        OUTPUT PARAMETERS:
-            Network2 -   copy
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpcopy(multilayerperceptron network1,
-            multilayerperceptron network2)
-        {
-            int i = 0;
-            int ssize = 0;
-            int ntotal = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            int i_ = 0;
-
-            
-            //
-            // Unload info
-            //
-            ssize = network1.structinfo[0];
-            nin = network1.structinfo[1];
-            nout = network1.structinfo[2];
-            ntotal = network1.structinfo[3];
-            wcount = network1.structinfo[4];
-            
-            //
-            // Allocate space
-            //
-            network2.structinfo = new int[ssize-1+1];
-            network2.weights = new double[wcount-1+1];
-            if( mlpissoftmax(network1) )
-            {
-                network2.columnmeans = new double[nin-1+1];
-                network2.columnsigmas = new double[nin-1+1];
-            }
-            else
-            {
-                network2.columnmeans = new double[nin+nout-1+1];
-                network2.columnsigmas = new double[nin+nout-1+1];
-            }
-            network2.neurons = new double[ntotal-1+1];
-            network2.chunks = new double[3*ntotal+1, chunksize-1+1];
-            network2.nwbuf = new double[Math.Max(wcount, 2*nout)-1+1];
-            network2.dfdnet = new double[ntotal-1+1];
-            network2.x = new double[nin-1+1];
-            network2.y = new double[nout-1+1];
-            network2.derror = new double[ntotal-1+1];
-            
-            //
-            // Copy
-            //
-            for(i=0; i<=ssize-1; i++)
-            {
-                network2.structinfo[i] = network1.structinfo[i];
-            }
-            for(i_=0; i_<=wcount-1;i_++)
-            {
-                network2.weights[i_] = network1.weights[i_];
-            }
-            if( mlpissoftmax(network1) )
-            {
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    network2.columnmeans[i_] = network1.columnmeans[i_];
-                }
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    network2.columnsigmas[i_] = network1.columnsigmas[i_];
-                }
-            }
-            else
-            {
-                for(i_=0; i_<=nin+nout-1;i_++)
-                {
-                    network2.columnmeans[i_] = network1.columnmeans[i_];
-                }
-                for(i_=0; i_<=nin+nout-1;i_++)
-                {
-                    network2.columnsigmas[i_] = network1.columnsigmas[i_];
-                }
-            }
-            for(i_=0; i_<=ntotal-1;i_++)
-            {
-                network2.neurons[i_] = network1.neurons[i_];
-            }
-            for(i_=0; i_<=ntotal-1;i_++)
-            {
-                network2.dfdnet[i_] = network1.dfdnet[i_];
-            }
-            for(i_=0; i_<=nin-1;i_++)
-            {
-                network2.x[i_] = network1.x[i_];
-            }
-            for(i_=0; i_<=nout-1;i_++)
-            {
-                network2.y[i_] = network1.y[i_];
-            }
-            for(i_=0; i_<=ntotal-1;i_++)
-            {
-                network2.derror[i_] = network1.derror[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        Serialization of MultiLayerPerceptron strucure
-
-        INPUT PARAMETERS:
-            Network -   original
-
-        OUTPUT PARAMETERS:
-            RA      -   array of real numbers which stores network,
-                        array[0..RLen-1]
-            RLen    -   RA lenght
-
-          -- ALGLIB --
-             Copyright 29.03.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpserialize(multilayerperceptron network,
-            ref double[] ra,
-            ref int rlen)
-        {
-            int i = 0;
-            int ssize = 0;
-            int ntotal = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            int sigmalen = 0;
-            int offs = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            ra = new double[0];
-            rlen = 0;
-
-            
-            //
-            // Unload info
-            //
-            ssize = network.structinfo[0];
-            nin = network.structinfo[1];
-            nout = network.structinfo[2];
-            ntotal = network.structinfo[3];
-            wcount = network.structinfo[4];
-            if( mlpissoftmax(network) )
-            {
-                sigmalen = nin;
-            }
-            else
-            {
-                sigmalen = nin+nout;
-            }
-            
-            //
-            //  RA format:
-            //      LEN         DESRC.
-            //      1           RLen
-            //      1           version (MLPVNum)
-            //      1           StructInfo size
-            //      SSize       StructInfo
-            //      WCount      Weights
-            //      SigmaLen    ColumnMeans
-            //      SigmaLen    ColumnSigmas
-            //
-            rlen = 3+ssize+wcount+2*sigmalen;
-            ra = new double[rlen-1+1];
-            ra[0] = rlen;
-            ra[1] = mlpvnum;
-            ra[2] = ssize;
-            offs = 3;
-            for(i=0; i<=ssize-1; i++)
-            {
-                ra[offs+i] = network.structinfo[i];
-            }
-            offs = offs+ssize;
-            i1_ = (0) - (offs);
-            for(i_=offs; i_<=offs+wcount-1;i_++)
-            {
-                ra[i_] = network.weights[i_+i1_];
-            }
-            offs = offs+wcount;
-            i1_ = (0) - (offs);
-            for(i_=offs; i_<=offs+sigmalen-1;i_++)
-            {
-                ra[i_] = network.columnmeans[i_+i1_];
-            }
-            offs = offs+sigmalen;
-            i1_ = (0) - (offs);
-            for(i_=offs; i_<=offs+sigmalen-1;i_++)
-            {
-                ra[i_] = network.columnsigmas[i_+i1_];
-            }
-            offs = offs+sigmalen;
-        }
-
-
-        /*************************************************************************
-        Unserialization of MultiLayerPerceptron strucure
-
-        INPUT PARAMETERS:
-            RA      -   real array which stores network
-
-        OUTPUT PARAMETERS:
-            Network -   restored network
-
-          -- ALGLIB --
-             Copyright 29.03.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpunserialize(double[] ra,
-            multilayerperceptron network)
-        {
-            int i = 0;
-            int ssize = 0;
-            int ntotal = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            int sigmalen = 0;
-            int offs = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            ap.assert((int)Math.Round(ra[1])==mlpvnum, "MLPUnserialize: incorrect array!");
-            
-            //
-            // Unload StructInfo from IA
-            //
-            offs = 3;
-            ssize = (int)Math.Round(ra[2]);
-            network.structinfo = new int[ssize-1+1];
-            for(i=0; i<=ssize-1; i++)
-            {
-                network.structinfo[i] = (int)Math.Round(ra[offs+i]);
-            }
-            offs = offs+ssize;
-            
-            //
-            // Unload info from StructInfo
-            //
-            ssize = network.structinfo[0];
-            nin = network.structinfo[1];
-            nout = network.structinfo[2];
-            ntotal = network.structinfo[3];
-            wcount = network.structinfo[4];
-            if( network.structinfo[6]==0 )
-            {
-                sigmalen = nin+nout;
-            }
-            else
-            {
-                sigmalen = nin;
-            }
-            
-            //
-            // Allocate space for other fields
-            //
-            network.weights = new double[wcount-1+1];
-            network.columnmeans = new double[sigmalen-1+1];
-            network.columnsigmas = new double[sigmalen-1+1];
-            network.neurons = new double[ntotal-1+1];
-            network.chunks = new double[3*ntotal+1, chunksize-1+1];
-            network.nwbuf = new double[Math.Max(wcount, 2*nout)-1+1];
-            network.dfdnet = new double[ntotal-1+1];
-            network.x = new double[nin-1+1];
-            network.y = new double[nout-1+1];
-            network.derror = new double[ntotal-1+1];
-            
-            //
-            // Copy parameters from RA
-            //
-            i1_ = (offs) - (0);
-            for(i_=0; i_<=wcount-1;i_++)
-            {
-                network.weights[i_] = ra[i_+i1_];
-            }
-            offs = offs+wcount;
-            i1_ = (offs) - (0);
-            for(i_=0; i_<=sigmalen-1;i_++)
-            {
-                network.columnmeans[i_] = ra[i_+i1_];
-            }
-            offs = offs+sigmalen;
-            i1_ = (offs) - (0);
-            for(i_=0; i_<=sigmalen-1;i_++)
-            {
-                network.columnsigmas[i_] = ra[i_+i1_];
-            }
-            offs = offs+sigmalen;
-        }
-
-
-        /*************************************************************************
-        Randomization of neural network weights
-
-          -- ALGLIB --
-             Copyright 06.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlprandomize(multilayerperceptron network)
-        {
-            int i = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            for(i=0; i<=wcount-1; i++)
-            {
-                network.weights[i] = math.randomreal()-0.5;
-            }
-        }
-
-
-        /*************************************************************************
-        Randomization of neural network weights and standartisator
-
-          -- ALGLIB --
-             Copyright 10.03.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlprandomizefull(multilayerperceptron network)
-        {
-            int i = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            int ntotal = 0;
-            int istart = 0;
-            int offs = 0;
-            int ntype = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            ntotal = network.structinfo[3];
-            istart = network.structinfo[5];
-            
-            //
-            // Process network
-            //
-            for(i=0; i<=wcount-1; i++)
-            {
-                network.weights[i] = math.randomreal()-0.5;
-            }
-            for(i=0; i<=nin-1; i++)
-            {
-                network.columnmeans[i] = 2*math.randomreal()-1;
-                network.columnsigmas[i] = 1.5*math.randomreal()+0.5;
-            }
-            if( !mlpissoftmax(network) )
-            {
-                for(i=0; i<=nout-1; i++)
-                {
-                    offs = istart+(ntotal-nout+i)*nfieldwidth;
-                    ntype = network.structinfo[offs+0];
-                    if( ntype==0 )
-                    {
-                        
-                        //
-                        // Shifts are changed only for linear outputs neurons
-                        //
-                        network.columnmeans[nin+i] = 2*math.randomreal()-1;
-                    }
-                    if( ntype==0 | ntype==3 )
-                    {
-                        
-                        //
-                        // Scales are changed only for linear or bounded outputs neurons.
-                        // Note that scale randomization preserves sign.
-                        //
-                        network.columnsigmas[nin+i] = Math.Sign(network.columnsigmas[nin+i])*(1.5*math.randomreal()+0.5);
-                    }
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine.
-
-          -- ALGLIB --
-             Copyright 30.03.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpinitpreprocessor(multilayerperceptron network,
+        public static void mcpdaddtrack(mcpdstate s,
             double[,] xy,
-            int ssize)
+            int k)
         {
             int i = 0;
             int j = 0;
-            int jmax = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            int ntotal = 0;
-            int istart = 0;
-            int offs = 0;
-            int ntype = 0;
-            double[] means = new double[0];
-            double[] sigmas = new double[0];
-            double s = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            ntotal = network.structinfo[3];
-            istart = network.structinfo[5];
-            
-            //
-            // Means/Sigmas
-            //
-            if( mlpissoftmax(network) )
-            {
-                jmax = nin-1;
-            }
-            else
-            {
-                jmax = nin+nout-1;
-            }
-            means = new double[jmax+1];
-            sigmas = new double[jmax+1];
-            for(j=0; j<=jmax; j++)
-            {
-                means[j] = 0;
-                for(i=0; i<=ssize-1; i++)
-                {
-                    means[j] = means[j]+xy[i,j];
-                }
-                means[j] = means[j]/ssize;
-                sigmas[j] = 0;
-                for(i=0; i<=ssize-1; i++)
-                {
-                    sigmas[j] = sigmas[j]+math.sqr(xy[i,j]-means[j]);
-                }
-                sigmas[j] = Math.Sqrt(sigmas[j]/ssize);
-            }
-            
-            //
-            // Inputs
-            //
-            for(i=0; i<=nin-1; i++)
-            {
-                network.columnmeans[i] = means[i];
-                network.columnsigmas[i] = sigmas[i];
-                if( (double)(network.columnsigmas[i])==(double)(0) )
-                {
-                    network.columnsigmas[i] = 1;
-                }
-            }
-            
-            //
-            // Outputs
-            //
-            if( !mlpissoftmax(network) )
-            {
-                for(i=0; i<=nout-1; i++)
-                {
-                    offs = istart+(ntotal-nout+i)*nfieldwidth;
-                    ntype = network.structinfo[offs+0];
-                    
-                    //
-                    // Linear outputs
-                    //
-                    if( ntype==0 )
-                    {
-                        network.columnmeans[nin+i] = means[nin+i];
-                        network.columnsigmas[nin+i] = sigmas[nin+i];
-                        if( (double)(network.columnsigmas[nin+i])==(double)(0) )
+            int n = 0;
+            double s0 = 0;
+            double s1 = 0;
+
+            n = s.n;
+            ap.assert(k>=0, "MCPDAddTrack: K<0");
+            ap.assert(ap.cols(xy)>=n, "MCPDAddTrack: Cols(XY)<N");
+            ap.assert(ap.rows(xy)>=k, "MCPDAddTrack: Rows(XY)<K");
+            ap.assert(apserv.apservisfinitematrix(xy, k, n), "MCPDAddTrack: XY contains infinite or NaN elements");
+            for(i=0; i<=k-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    ap.assert((double)(xy[i,j])>=(double)(0), "MCPDAddTrack: XY contains negative elements");
+                }
+            }
+            if( k<2 )
+            {
+                return;
+            }
+            if( ap.rows(s.data)<s.npairs+k-1 )
+            {
+                apserv.rmatrixresize(ref s.data, Math.Max(2*ap.rows(s.data), s.npairs+k-1), 2*n);
+            }
+            for(i=0; i<=k-2; i++)
+            {
+                s0 = 0;
+                s1 = 0;
+                for(j=0; j<=n-1; j++)
+                {
+                    if( s.states[j]>=0 )
+                    {
+                        s0 = s0+xy[i,j];
+                    }
+                    if( s.states[j]<=0 )
+                    {
+                        s1 = s1+xy[i+1,j];
+                    }
+                }
+                if( (double)(s0)>(double)(0) & (double)(s1)>(double)(0) )
+                {
+                    for(j=0; j<=n-1; j++)
+                    {
+                        if( s.states[j]>=0 )
                         {
-                            network.columnsigmas[nin+i] = 1;
+                            s.data[s.npairs,j] = xy[i,j]/s0;
                         }
-                    }
-                    
-                    //
-                    // Bounded outputs (half-interval)
-                    //
-                    if( ntype==3 )
-                    {
-                        s = means[nin+i]-network.columnmeans[nin+i];
-                        if( (double)(s)==(double)(0) )
+                        else
                         {
-                            s = Math.Sign(network.columnsigmas[nin+i]);
+                            s.data[s.npairs,j] = 0.0;
                         }
-                        if( (double)(s)==(double)(0) )
+                        if( s.states[j]<=0 )
                         {
-                            s = 1.0;
+                            s.data[s.npairs,n+j] = xy[i+1,j]/s1;
                         }
-                        network.columnsigmas[nin+i] = Math.Sign(network.columnsigmas[nin+i])*Math.Abs(s);
-                        if( (double)(network.columnsigmas[nin+i])==(double)(0) )
+                        else
                         {
-                            network.columnsigmas[nin+i] = 1;
+                            s.data[s.npairs,n+j] = 0.0;
                         }
                     }
+                    s.npairs = s.npairs+1;
                 }
             }
@@ -10426,4039 +15995,855 @@
 
         /*************************************************************************
-        Returns information about initialized network: number of inputs, outputs,
-        weights.
+        This function is used to add equality constraints on the elements  of  the
+        transition matrix P.
+
+        MCPD solver has four types of constraints which can be placed on P:
+        * user-specified equality constraints (optional)
+        * user-specified bound constraints (optional)
+        * user-specified general linear constraints (optional)
+        * basic constraints (always present):
+          * non-negativity: P[i,j]>=0
+          * consistency: every column of P sums to 1.0
+
+        Final  constraints  which  are  passed  to  the  underlying  optimizer are
+        calculated  as  intersection  of all present constraints. For example, you
+        may specify boundary constraint on P[0,0] and equality one:
+            0.1<=P[0,0]<=0.9
+            P[0,0]=0.5
+        Such  combination  of  constraints  will  be  silently  reduced  to  their
+        intersection, which is P[0,0]=0.5.
+
+        This  function  can  be  used  to  place equality constraints on arbitrary
+        subset of elements of P. Set of constraints is specified by EC, which  may
+        contain either NAN's or finite numbers from [0,1]. NAN denotes absence  of
+        constraint, finite number denotes equality constraint on specific  element
+        of P.
+
+        You can also  use  MCPDAddEC()  function  which  allows  to  ADD  equality
+        constraint  for  one  element  of P without changing constraints for other
+        elements.
+
+        These functions (MCPDSetEC and MCPDAddEC) interact as follows:
+        * there is internal matrix of equality constraints which is stored in  the
+          MCPD solver
+        * MCPDSetEC() replaces this matrix by another one (SET)
+        * MCPDAddEC() modifies one element of this matrix and  leaves  other  ones
+          unchanged (ADD)
+        * thus  MCPDAddEC()  call  preserves  all  modifications  done by previous
+          calls,  while  MCPDSetEC()  completely discards all changes  done to the
+          equality constraints.
+
+        INPUT PARAMETERS:
+            S       -   solver
+            EC      -   equality constraints, array[N,N]. Elements of  EC  can  be
+                        either NAN's or finite  numbers from  [0,1].  NAN  denotes
+                        absence  of  constraints,  while  finite  value    denotes
+                        equality constraint on the corresponding element of P.
+
+        NOTES:
+
+        1. infinite values of EC will lead to exception being thrown. Values  less
+        than 0.0 or greater than 1.0 will lead to error code being returned  after
+        call to MCPDSolve().
 
           -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpproperties(multilayerperceptron network,
-            ref int nin,
-            ref int nout,
-            ref int wcount)
-        {
-            nin = 0;
-            nout = 0;
-            wcount = 0;
-
-            nin = network.structinfo[1];
-            nout = network.structinfo[2];
-            wcount = network.structinfo[4];
-        }
-
-
-        /*************************************************************************
-        Tells whether network is SOFTMAX-normalized (i.e. classifier) or not.
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool mlpissoftmax(multilayerperceptron network)
-        {
-            bool result = new bool();
-
-            result = network.structinfo[6]==1;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Procesing
-
-        INPUT PARAMETERS:
-            Network -   neural network
-            X       -   input vector,  array[0..NIn-1].
-
-        OUTPUT PARAMETERS:
-            Y       -   result. Regression estimate when solving regression  task,
-                        vector of posterior probabilities for classification task.
-
-        See also MLPProcessI
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpprocess(multilayerperceptron network,
-            double[] x,
-            ref double[] y)
-        {
-            if( ap.len(y)<network.structinfo[2] )
-            {
-                y = new double[network.structinfo[2]];
-            }
-            mlpinternalprocessvector(network.structinfo, network.weights, network.columnmeans, network.columnsigmas, ref network.neurons, ref network.dfdnet, x, ref y);
-        }
-
-
-        /*************************************************************************
-        'interactive'  variant  of  MLPProcess  for  languages  like  Python which
-        support constructs like "Y = MLPProcess(NN,X)" and interactive mode of the
-        interpreter
-
-        This function allocates new array on each call,  so  it  is  significantly
-        slower than its 'non-interactive' counterpart, but it is  more  convenient
-        when you call it from command line.
-
-          -- ALGLIB --
-             Copyright 21.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpprocessi(multilayerperceptron network,
-            double[] x,
-            ref double[] y)
-        {
-            y = new double[0];
-
-            mlpprocess(network, x, ref y);
-        }
-
-
-        /*************************************************************************
-        Error function for neural network, internal subroutine.
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static double mlperror(multilayerperceptron network,
-            double[,] xy,
-            int ssize)
-        {
-            double result = 0;
-            int i = 0;
-            int k = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            double e = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            result = 0;
-            for(i=0; i<=ssize-1; i++)
-            {
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    network.x[i_] = xy[i,i_];
-                }
-                mlpprocess(network, network.x, ref network.y);
-                if( mlpissoftmax(network) )
-                {
-                    
-                    //
-                    // class labels outputs
-                    //
-                    k = (int)Math.Round(xy[i,nin]);
-                    if( k>=0 & k<nout )
-                    {
-                        network.y[k] = network.y[k]-1;
-                    }
-                }
-                else
-                {
-                    
-                    //
-                    // real outputs
-                    //
-                    i1_ = (nin) - (0);
-                    for(i_=0; i_<=nout-1;i_++)
-                    {
-                        network.y[i_] = network.y[i_] - xy[i,i_+i1_];
-                    }
-                }
-                e = 0.0;
-                for(i_=0; i_<=nout-1;i_++)
-                {
-                    e += network.y[i_]*network.y[i_];
-                }
-                result = result+e/2;
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Natural error function for neural network, internal subroutine.
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static double mlperrorn(multilayerperceptron network,
-            double[,] xy,
-            int ssize)
-        {
-            double result = 0;
-            int i = 0;
-            int k = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            double e = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            result = 0;
-            for(i=0; i<=ssize-1; i++)
-            {
-                
-                //
-                // Process vector
-                //
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    network.x[i_] = xy[i,i_];
-                }
-                mlpprocess(network, network.x, ref network.y);
-                
-                //
-                // Update error function
-                //
-                if( network.structinfo[6]==0 )
-                {
-                    
-                    //
-                    // Least squares error function
-                    //
-                    i1_ = (nin) - (0);
-                    for(i_=0; i_<=nout-1;i_++)
-                    {
-                        network.y[i_] = network.y[i_] - xy[i,i_+i1_];
-                    }
-                    e = 0.0;
-                    for(i_=0; i_<=nout-1;i_++)
-                    {
-                        e += network.y[i_]*network.y[i_];
-                    }
-                    result = result+e/2;
-                }
-                else
-                {
-                    
-                    //
-                    // Cross-entropy error function
-                    //
-                    k = (int)Math.Round(xy[i,nin]);
-                    if( k>=0 & k<nout )
-                    {
-                        result = result+safecrossentropy(1, network.y[k]);
-                    }
-                }
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Classification error
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static int mlpclserror(multilayerperceptron network,
-            double[,] xy,
-            int ssize)
-        {
-            int result = 0;
+        public static void mcpdsetec(mcpdstate s,
+            double[,] ec)
+        {
             int i = 0;
             int j = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            double[] workx = new double[0];
-            double[] worky = new double[0];
-            int nn = 0;
-            int ns = 0;
-            int nmax = 0;
-            int i_ = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            workx = new double[nin-1+1];
-            worky = new double[nout-1+1];
-            result = 0;
-            for(i=0; i<=ssize-1; i++)
-            {
-                
-                //
-                // Process
-                //
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    workx[i_] = xy[i,i_];
-                }
-                mlpprocess(network, workx, ref worky);
-                
-                //
-                // Network version of the answer
-                //
-                nmax = 0;
-                for(j=0; j<=nout-1; j++)
-                {
-                    if( (double)(worky[j])>(double)(worky[nmax]) )
-                    {
-                        nmax = j;
-                    }
-                }
-                nn = nmax;
-                
-                //
-                // Right answer
-                //
-                if( mlpissoftmax(network) )
-                {
-                    ns = (int)Math.Round(xy[i,nin]);
-                }
-                else
-                {
-                    nmax = 0;
-                    for(j=0; j<=nout-1; j++)
-                    {
-                        if( (double)(xy[i,nin+j])>(double)(xy[i,nin+nmax]) )
-                        {
-                            nmax = j;
-                        }
-                    }
-                    ns = nmax;
-                }
-                
-                //
-                // compare
-                //
-                if( nn!=ns )
-                {
-                    result = result+1;
-                }
-            }
-            return result;
+            int n = 0;
+
+            n = s.n;
+            ap.assert(ap.cols(ec)>=n, "MCPDSetEC: Cols(EC)<N");
+            ap.assert(ap.rows(ec)>=n, "MCPDSetEC: Rows(EC)<N");
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    ap.assert(math.isfinite(ec[i,j]) | Double.IsNaN(ec[i,j]), "MCPDSetEC: EC containts infinite elements");
+                    s.ec[i,j] = ec[i,j];
+                }
+            }
         }
 
 
         /*************************************************************************
-        Relative classification error on the test set
+        This function is used to add equality constraints on the elements  of  the
+        transition matrix P.
+
+        MCPD solver has four types of constraints which can be placed on P:
+        * user-specified equality constraints (optional)
+        * user-specified bound constraints (optional)
+        * user-specified general linear constraints (optional)
+        * basic constraints (always present):
+          * non-negativity: P[i,j]>=0
+          * consistency: every column of P sums to 1.0
+
+        Final  constraints  which  are  passed  to  the  underlying  optimizer are
+        calculated  as  intersection  of all present constraints. For example, you
+        may specify boundary constraint on P[0,0] and equality one:
+            0.1<=P[0,0]<=0.9
+            P[0,0]=0.5
+        Such  combination  of  constraints  will  be  silently  reduced  to  their
+        intersection, which is P[0,0]=0.5.
+
+        This function can be used to ADD equality constraint for one element of  P
+        without changing constraints for other elements.
+
+        You  can  also  use  MCPDSetEC()  function  which  allows  you  to specify
+        arbitrary set of equality constraints in one call.
+
+        These functions (MCPDSetEC and MCPDAddEC) interact as follows:
+        * there is internal matrix of equality constraints which is stored in the
+          MCPD solver
+        * MCPDSetEC() replaces this matrix by another one (SET)
+        * MCPDAddEC() modifies one element of this matrix and leaves  other  ones
+          unchanged (ADD)
+        * thus  MCPDAddEC()  call  preserves  all  modifications done by previous
+          calls,  while  MCPDSetEC()  completely discards all changes done to the
+          equality constraints.
 
         INPUT PARAMETERS:
-            Network -   network
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            percent of incorrectly classified cases. Works both for
-            classifier networks and general purpose networks used as
-            classifiers.
+            S       -   solver
+            I       -   row index of element being constrained
+            J       -   column index of element being constrained
+            C       -   value (constraint for P[I,J]).  Can  be  either  NAN  (no
+                        constraint) or finite value from [0,1].
+                        
+        NOTES:
+
+        1. infinite values of C  will lead to exception being thrown. Values  less
+        than 0.0 or greater than 1.0 will lead to error code being returned  after
+        call to MCPDSolve().
 
           -- ALGLIB --
-             Copyright 25.12.2008 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static double mlprelclserror(multilayerperceptron network,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-
-            result = (double)mlpclserror(network, xy, npoints)/(double)npoints;
-            return result;
+        public static void mcpdaddec(mcpdstate s,
+            int i,
+            int j,
+            double c)
+        {
+            ap.assert(i>=0, "MCPDAddEC: I<0");
+            ap.assert(i<s.n, "MCPDAddEC: I>=N");
+            ap.assert(j>=0, "MCPDAddEC: J<0");
+            ap.assert(j<s.n, "MCPDAddEC: J>=N");
+            ap.assert(Double.IsNaN(c) | math.isfinite(c), "MCPDAddEC: C is not finite number or NAN");
+            s.ec[i,j] = c;
         }
 
 
         /*************************************************************************
-        Average cross-entropy (in bits per element) on the test set
+        This function is used to add bound constraints  on  the  elements  of  the
+        transition matrix P.
+
+        MCPD solver has four types of constraints which can be placed on P:
+        * user-specified equality constraints (optional)
+        * user-specified bound constraints (optional)
+        * user-specified general linear constraints (optional)
+        * basic constraints (always present):
+          * non-negativity: P[i,j]>=0
+          * consistency: every column of P sums to 1.0
+
+        Final  constraints  which  are  passed  to  the  underlying  optimizer are
+        calculated  as  intersection  of all present constraints. For example, you
+        may specify boundary constraint on P[0,0] and equality one:
+            0.1<=P[0,0]<=0.9
+            P[0,0]=0.5
+        Such  combination  of  constraints  will  be  silently  reduced  to  their
+        intersection, which is P[0,0]=0.5.
+
+        This  function  can  be  used  to  place bound   constraints  on arbitrary
+        subset  of  elements  of  P.  Set of constraints is specified by BndL/BndU
+        matrices, which may contain arbitrary combination  of  finite  numbers  or
+        infinities (like -INF<x<=0.5 or 0.1<=x<+INF).
+
+        You can also use MCPDAddBC() function which allows to ADD bound constraint
+        for one element of P without changing constraints for other elements.
+
+        These functions (MCPDSetBC and MCPDAddBC) interact as follows:
+        * there is internal matrix of bound constraints which is stored in the
+          MCPD solver
+        * MCPDSetBC() replaces this matrix by another one (SET)
+        * MCPDAddBC() modifies one element of this matrix and  leaves  other  ones
+          unchanged (ADD)
+        * thus  MCPDAddBC()  call  preserves  all  modifications  done by previous
+          calls,  while  MCPDSetBC()  completely discards all changes  done to the
+          equality constraints.
 
         INPUT PARAMETERS:
-            Network -   neural network
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            CrossEntropy/(NPoints*LN(2)).
-            Zero if network solves regression task.
+            S       -   solver
+            BndL    -   lower bounds constraints, array[N,N]. Elements of BndL can
+                        be finite numbers or -INF.
+            BndU    -   upper bounds constraints, array[N,N]. Elements of BndU can
+                        be finite numbers or +INF.
 
           -- ALGLIB --
-             Copyright 08.01.2009 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static double mlpavgce(multilayerperceptron network,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-
-            if( mlpissoftmax(network) )
-            {
-                mlpproperties(network, ref nin, ref nout, ref wcount);
-                result = mlperrorn(network, xy, npoints)/(npoints*Math.Log(2));
-            }
-            else
-            {
-                result = 0;
-            }
-            return result;
+        public static void mcpdsetbc(mcpdstate s,
+            double[,] bndl,
+            double[,] bndu)
+        {
+            int i = 0;
+            int j = 0;
+            int n = 0;
+
+            n = s.n;
+            ap.assert(ap.cols(bndl)>=n, "MCPDSetBC: Cols(BndL)<N");
+            ap.assert(ap.rows(bndl)>=n, "MCPDSetBC: Rows(BndL)<N");
+            ap.assert(ap.cols(bndu)>=n, "MCPDSetBC: Cols(BndU)<N");
+            ap.assert(ap.rows(bndu)>=n, "MCPDSetBC: Rows(BndU)<N");
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    ap.assert(math.isfinite(bndl[i,j]) | Double.IsNegativeInfinity(bndl[i,j]), "MCPDSetBC: BndL containts NAN or +INF");
+                    ap.assert(math.isfinite(bndu[i,j]) | Double.IsPositiveInfinity(bndu[i,j]), "MCPDSetBC: BndU containts NAN or -INF");
+                    s.bndl[i,j] = bndl[i,j];
+                    s.bndu[i,j] = bndu[i,j];
+                }
+            }
         }
 
 
         /*************************************************************************
-        RMS error on the test set
+        This function is used to add bound constraints  on  the  elements  of  the
+        transition matrix P.
+
+        MCPD solver has four types of constraints which can be placed on P:
+        * user-specified equality constraints (optional)
+        * user-specified bound constraints (optional)
+        * user-specified general linear constraints (optional)
+        * basic constraints (always present):
+          * non-negativity: P[i,j]>=0
+          * consistency: every column of P sums to 1.0
+
+        Final  constraints  which  are  passed  to  the  underlying  optimizer are
+        calculated  as  intersection  of all present constraints. For example, you
+        may specify boundary constraint on P[0,0] and equality one:
+            0.1<=P[0,0]<=0.9
+            P[0,0]=0.5
+        Such  combination  of  constraints  will  be  silently  reduced  to  their
+        intersection, which is P[0,0]=0.5.
+
+        This  function  can  be  used to ADD bound constraint for one element of P
+        without changing constraints for other elements.
+
+        You  can  also  use  MCPDSetBC()  function  which  allows to  place  bound
+        constraints  on arbitrary subset of elements of P.   Set of constraints is
+        specified  by  BndL/BndU matrices, which may contain arbitrary combination
+        of finite numbers or infinities (like -INF<x<=0.5 or 0.1<=x<+INF).
+
+        These functions (MCPDSetBC and MCPDAddBC) interact as follows:
+        * there is internal matrix of bound constraints which is stored in the
+          MCPD solver
+        * MCPDSetBC() replaces this matrix by another one (SET)
+        * MCPDAddBC() modifies one element of this matrix and  leaves  other  ones
+          unchanged (ADD)
+        * thus  MCPDAddBC()  call  preserves  all  modifications  done by previous
+          calls,  while  MCPDSetBC()  completely discards all changes  done to the
+          equality constraints.
 
         INPUT PARAMETERS:
-            Network -   neural network
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            root mean square error.
-            Its meaning for regression task is obvious. As for
-            classification task, RMS error means error when estimating posterior
-            probabilities.
+            S       -   solver
+            I       -   row index of element being constrained
+            J       -   column index of element being constrained
+            BndL    -   lower bound
+            BndU    -   upper bound
 
           -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static double mlprmserror(multilayerperceptron network,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            result = Math.Sqrt(2*mlperror(network, xy, npoints)/(npoints*nout));
-            return result;
+        public static void mcpdaddbc(mcpdstate s,
+            int i,
+            int j,
+            double bndl,
+            double bndu)
+        {
+            ap.assert(i>=0, "MCPDAddBC: I<0");
+            ap.assert(i<s.n, "MCPDAddBC: I>=N");
+            ap.assert(j>=0, "MCPDAddBC: J<0");
+            ap.assert(j<s.n, "MCPDAddBC: J>=N");
+            ap.assert(math.isfinite(bndl) | Double.IsNegativeInfinity(bndl), "MCPDAddBC: BndL is NAN or +INF");
+            ap.assert(math.isfinite(bndu) | Double.IsPositiveInfinity(bndu), "MCPDAddBC: BndU is NAN or -INF");
+            s.bndl[i,j] = bndl;
+            s.bndu[i,j] = bndu;
         }
 
 
         /*************************************************************************
-        Average error on the test set
+        This function is used to set linear equality/inequality constraints on the
+        elements of the transition matrix P.
+
+        This function can be used to set one or several general linear constraints
+        on the elements of P. Two types of constraints are supported:
+        * equality constraints
+        * inequality constraints (both less-or-equal and greater-or-equal)
+
+        Coefficients  of  constraints  are  specified  by  matrix  C (one  of  the
+        parameters).  One  row  of  C  corresponds  to  one  constraint.   Because
+        transition  matrix P has N*N elements,  we  need  N*N columns to store all
+        coefficients  (they  are  stored row by row), and one more column to store
+        right part - hence C has N*N+1 columns.  Constraint  kind is stored in the
+        CT array.
+
+        Thus, I-th linear constraint is
+            P[0,0]*C[I,0] + P[0,1]*C[I,1] + .. + P[0,N-1]*C[I,N-1] +
+                + P[1,0]*C[I,N] + P[1,1]*C[I,N+1] + ... +
+                + P[N-1,N-1]*C[I,N*N-1]  ?=?  C[I,N*N]
+        where ?=? can be either "=" (CT[i]=0), "<=" (CT[i]<0) or ">=" (CT[i]>0).
+
+        Your constraint may involve only some subset of P (less than N*N elements).
+        For example it can be something like
+            P[0,0] + P[0,1] = 0.5
+        In this case you still should pass matrix  with N*N+1 columns, but all its
+        elements (except for C[0,0], C[0,1] and C[0,N*N-1]) will be zero.
 
         INPUT PARAMETERS:
-            Network -   neural network
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            Its meaning for regression task is obvious. As for
-            classification task, it means average error when estimating posterior
-            probabilities.
+            S       -   solver
+            C       -   array[K,N*N+1] - coefficients of constraints
+                        (see above for complete description)
+            CT      -   array[K] - constraint types
+                        (see above for complete description)
+            K       -   number of equality/inequality constraints, K>=0:
+                        * if given, only leading K elements of C/CT are used
+                        * if not given, automatically determined from sizes of C/CT
 
           -- ALGLIB --
-             Copyright 11.03.2008 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static double mlpavgerror(multilayerperceptron network,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
+        public static void mcpdsetlc(mcpdstate s,
+            double[,] c,
+            int[] ct,
+            int k)
+        {
+            int i = 0;
+            int j = 0;
+            int n = 0;
+
+            n = s.n;
+            ap.assert(ap.cols(c)>=n*n+1, "MCPDSetLC: Cols(C)<N*N+1");
+            ap.assert(ap.rows(c)>=k, "MCPDSetLC: Rows(C)<K");
+            ap.assert(ap.len(ct)>=k, "MCPDSetLC: Len(CT)<K");
+            ap.assert(apserv.apservisfinitematrix(c, k, n*n+1), "MCPDSetLC: C contains infinite or NaN values!");
+            apserv.rmatrixsetlengthatleast(ref s.c, k, n*n+1);
+            apserv.ivectorsetlengthatleast(ref s.ct, k);
+            for(i=0; i<=k-1; i++)
+            {
+                for(j=0; j<=n*n; j++)
+                {
+                    s.c[i,j] = c[i,j];
+                }
+                s.ct[i] = ct[i];
+            }
+            s.ccnt = k;
+        }
+
+
+        /*************************************************************************
+        This function allows to  tune  amount  of  Tikhonov  regularization  being
+        applied to your problem.
+
+        By default, regularizing term is equal to r*||P-prior_P||^2, where r is  a
+        small non-zero value,  P is transition matrix, prior_P is identity matrix,
+        ||X||^2 is a sum of squared elements of X.
+
+        This  function  allows  you to change coefficient r. You can  also  change
+        prior values with MCPDSetPrior() function.
+
+        INPUT PARAMETERS:
+            S       -   solver
+            V       -   regularization  coefficient, finite non-negative value. It
+                        is  not  recommended  to specify zero value unless you are
+                        pretty sure that you want it.
+
+          -- ALGLIB --
+             Copyright 23.05.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mcpdsettikhonovregularizer(mcpdstate s,
+            double v)
+        {
+            ap.assert(math.isfinite(v), "MCPDSetTikhonovRegularizer: V is infinite or NAN");
+            ap.assert((double)(v)>=(double)(0.0), "MCPDSetTikhonovRegularizer: V is less than zero");
+            s.regterm = v;
+        }
+
+
+        /*************************************************************************
+        This  function  allows to set prior values used for regularization of your
+        problem.
+
+        By default, regularizing term is equal to r*||P-prior_P||^2, where r is  a
+        small non-zero value,  P is transition matrix, prior_P is identity matrix,
+        ||X||^2 is a sum of squared elements of X.
+
+        This  function  allows  you to change prior values prior_P. You  can  also
+        change r with MCPDSetTikhonovRegularizer() function.
+
+        INPUT PARAMETERS:
+            S       -   solver
+            PP      -   array[N,N], matrix of prior values:
+                        1. elements must be real numbers from [0,1]
+                        2. columns must sum to 1.0.
+                        First property is checked (exception is thrown otherwise),
+                        while second one is not checked/enforced.
+
+          -- ALGLIB --
+             Copyright 23.05.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mcpdsetprior(mcpdstate s,
+            double[,] pp)
+        {
+            int i = 0;
+            int j = 0;
+            int n = 0;
+
+            pp = (double[,])pp.Clone();
+
+            n = s.n;
+            ap.assert(ap.cols(pp)>=n, "MCPDSetPrior: Cols(PP)<N");
+            ap.assert(ap.rows(pp)>=n, "MCPDSetPrior: Rows(PP)<K");
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    ap.assert(math.isfinite(pp[i,j]), "MCPDSetPrior: PP containts infinite elements");
+                    ap.assert((double)(pp[i,j])>=(double)(0.0) & (double)(pp[i,j])<=(double)(1.0), "MCPDSetPrior: PP[i,j] is less than 0.0 or greater than 1.0");
+                    s.priorp[i,j] = pp[i,j];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function is used to change prediction weights
+
+        MCPD solver scales prediction errors as follows
+            Error(P) = ||W*(y-P*x)||^2
+        where
+            x is a system state at time t
+            y is a system state at time t+1
+            P is a transition matrix
+            W is a diagonal scaling matrix
+
+        By default, weights are chosen in order  to  minimize  relative prediction
+        error instead of absolute one. For example, if one component of  state  is
+        about 0.5 in magnitude and another one is about 0.05, then algorithm  will
+        make corresponding weights equal to 2.0 and 20.0.
+
+        INPUT PARAMETERS:
+            S       -   solver
+            PW      -   array[N], weights:
+                        * must be non-negative values (exception will be thrown otherwise)
+                        * zero values will be replaced by automatically chosen values
+
+          -- ALGLIB --
+             Copyright 23.05.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mcpdsetpredictionweights(mcpdstate s,
+            double[] pw)
+        {
+            int i = 0;
+            int n = 0;
+
+            n = s.n;
+            ap.assert(ap.len(pw)>=n, "MCPDSetPredictionWeights: Length(PW)<N");
+            for(i=0; i<=n-1; i++)
+            {
+                ap.assert(math.isfinite(pw[i]), "MCPDSetPredictionWeights: PW containts infinite or NAN elements");
+                ap.assert((double)(pw[i])>=(double)(0), "MCPDSetPredictionWeights: PW containts negative elements");
+                s.pw[i] = pw[i];
+            }
+        }
+
+
+        /*************************************************************************
+        This function is used to start solution of the MCPD problem.
+
+        After return from this function, you can use MCPDResults() to get solution
+        and completion code.
+
+          -- ALGLIB --
+             Copyright 23.05.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mcpdsolve(mcpdstate s)
+        {
+            int n = 0;
+            int npairs = 0;
+            int ccnt = 0;
             int i = 0;
             int j = 0;
             int k = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
+            int k2 = 0;
+            double v = 0;
+            double vv = 0;
             int i_ = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            result = 0;
-            for(i=0; i<=npoints-1; i++)
-            {
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    network.x[i_] = xy[i,i_];
-                }
-                mlpprocess(network, network.x, ref network.y);
-                if( mlpissoftmax(network) )
+            int i1_ = 0;
+
+            n = s.n;
+            npairs = s.npairs;
+            
+            //
+            // init fields of S
+            //
+            s.repterminationtype = 0;
+            s.repinneriterationscount = 0;
+            s.repouteriterationscount = 0;
+            s.repnfev = 0;
+            for(k=0; k<=n-1; k++)
+            {
+                for(k2=0; k2<=n-1; k2++)
+                {
+                    s.p[k,k2] = Double.NaN;
+                }
+            }
+            
+            //
+            // Generate "effective" weights for prediction and calculate preconditioner
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                if( (double)(s.pw[i])==(double)(0) )
+                {
+                    v = 0;
+                    k = 0;
+                    for(j=0; j<=npairs-1; j++)
+                    {
+                        if( (double)(s.data[j,n+i])!=(double)(0) )
+                        {
+                            v = v+s.data[j,n+i];
+                            k = k+1;
+                        }
+                    }
+                    if( k!=0 )
+                    {
+                        s.effectivew[i] = k/v;
+                    }
+                    else
+                    {
+                        s.effectivew[i] = 1.0;
+                    }
+                }
+                else
+                {
+                    s.effectivew[i] = s.pw[i];
+                }
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    s.h[i*n+j] = 2*s.regterm;
+                }
+            }
+            for(k=0; k<=npairs-1; k++)
+            {
+                for(i=0; i<=n-1; i++)
+                {
+                    for(j=0; j<=n-1; j++)
+                    {
+                        s.h[i*n+j] = s.h[i*n+j]+2*math.sqr(s.effectivew[i])*math.sqr(s.data[k,j]);
+                    }
+                }
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    if( (double)(s.h[i*n+j])==(double)(0) )
+                    {
+                        s.h[i*n+j] = 1;
+                    }
+                }
+            }
+            
+            //
+            // Generate "effective" BndL/BndU
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
                 {
                     
                     //
-                    // class labels
-                    //
-                    k = (int)Math.Round(xy[i,nin]);
-                    for(j=0; j<=nout-1; j++)
-                    {
-                        if( j==k )
+                    // Set default boundary constraints.
+                    // Lower bound is always zero, upper bound is calculated
+                    // with respect to entry/exit states.
+                    //
+                    s.effectivebndl[i*n+j] = 0.0;
+                    if( s.states[i]>0 | s.states[j]<0 )
+                    {
+                        s.effectivebndu[i*n+j] = 0.0;
+                    }
+                    else
+                    {
+                        s.effectivebndu[i*n+j] = 1.0;
+                    }
+                    
+                    //
+                    // Calculate intersection of the default and user-specified bound constraints.
+                    // This code checks consistency of such combination.
+                    //
+                    if( math.isfinite(s.bndl[i,j]) & (double)(s.bndl[i,j])>(double)(s.effectivebndl[i*n+j]) )
+                    {
+                        s.effectivebndl[i*n+j] = s.bndl[i,j];
+                    }
+                    if( math.isfinite(s.bndu[i,j]) & (double)(s.bndu[i,j])<(double)(s.effectivebndu[i*n+j]) )
+                    {
+                        s.effectivebndu[i*n+j] = s.bndu[i,j];
+                    }
+                    if( (double)(s.effectivebndl[i*n+j])>(double)(s.effectivebndu[i*n+j]) )
+                    {
+                        s.repterminationtype = -3;
+                        return;
+                    }
+                    
+                    //
+                    // Calculate intersection of the effective bound constraints
+                    // and user-specified equality constraints.
+                    // This code checks consistency of such combination.
+                    //
+                    if( math.isfinite(s.ec[i,j]) )
+                    {
+                        if( (double)(s.ec[i,j])<(double)(s.effectivebndl[i*n+j]) | (double)(s.ec[i,j])>(double)(s.effectivebndu[i*n+j]) )
                         {
-                            result = result+Math.Abs(1-network.y[j]);
+                            s.repterminationtype = -3;
+                            return;
                         }
-                        else
+                        s.effectivebndl[i*n+j] = s.ec[i,j];
+                        s.effectivebndu[i*n+j] = s.ec[i,j];
+                    }
+                }
+            }
+            
+            //
+            // Generate linear constraints:
+            // * "default" sums-to-one constraints (not generated for "exit" states)
+            //
+            apserv.rmatrixsetlengthatleast(ref s.effectivec, s.ccnt+n, n*n+1);
+            apserv.ivectorsetlengthatleast(ref s.effectivect, s.ccnt+n);
+            ccnt = s.ccnt;
+            for(i=0; i<=s.ccnt-1; i++)
+            {
+                for(j=0; j<=n*n; j++)
+                {
+                    s.effectivec[i,j] = s.c[i,j];
+                }
+                s.effectivect[i] = s.ct[i];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                if( s.states[i]>=0 )
+                {
+                    for(k=0; k<=n*n-1; k++)
+                    {
+                        s.effectivec[ccnt,k] = 0;
+                    }
+                    for(k=0; k<=n-1; k++)
+                    {
+                        s.effectivec[ccnt,k*n+i] = 1;
+                    }
+                    s.effectivec[ccnt,n*n] = 1.0;
+                    s.effectivect[ccnt] = 0;
+                    ccnt = ccnt+1;
+                }
+            }
+            
+            //
+            // create optimizer
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    s.tmpp[i*n+j] = (double)1/(double)n;
+                }
+            }
+            minbleic.minbleicrestartfrom(s.bs, s.tmpp);
+            minbleic.minbleicsetbc(s.bs, s.effectivebndl, s.effectivebndu);
+            minbleic.minbleicsetlc(s.bs, s.effectivec, s.effectivect, ccnt);
+            minbleic.minbleicsetinnercond(s.bs, 0, 0, xtol);
+            minbleic.minbleicsetoutercond(s.bs, xtol, 1.0E-5);
+            minbleic.minbleicsetprecdiag(s.bs, s.h);
+            
+            //
+            // solve problem
+            //
+            while( minbleic.minbleiciteration(s.bs) )
+            {
+                ap.assert(s.bs.needfg, "MCPDSolve: internal error");
+                if( s.bs.needfg )
+                {
+                    
+                    //
+                    // Calculate regularization term
+                    //
+                    s.bs.f = 0.0;
+                    vv = s.regterm;
+                    for(i=0; i<=n-1; i++)
+                    {
+                        for(j=0; j<=n-1; j++)
                         {
-                            result = result+Math.Abs(network.y[j]);
+                            s.bs.f = s.bs.f+vv*math.sqr(s.bs.x[i*n+j]-s.priorp[i,j]);
+                            s.bs.g[i*n+j] = 2*vv*(s.bs.x[i*n+j]-s.priorp[i,j]);
                         }
                     }
-                }
-                else
-                {
                     
                     //
-                    // real outputs
-                    //
-                    for(j=0; j<=nout-1; j++)
-                    {
-                        result = result+Math.Abs(xy[i,nin+j]-network.y[j]);
-                    }
-                }
-            }
-            result = result/(npoints*nout);
-            return result;
+                    // calculate prediction error/gradient for K-th pair
+                    //
+                    for(k=0; k<=npairs-1; k++)
+                    {
+                        for(i=0; i<=n-1; i++)
+                        {
+                            i1_ = (0)-(i*n);
+                            v = 0.0;
+                            for(i_=i*n; i_<=i*n+n-1;i_++)
+                            {
+                                v += s.bs.x[i_]*s.data[k,i_+i1_];
+                            }
+                            vv = s.effectivew[i];
+                            s.bs.f = s.bs.f+math.sqr(vv*(v-s.data[k,n+i]));
+                            for(j=0; j<=n-1; j++)
+                            {
+                                s.bs.g[i*n+j] = s.bs.g[i*n+j]+2*vv*vv*(v-s.data[k,n+i])*s.data[k,j];
+                            }
+                        }
+                    }
+                    
+                    //
+                    // continue
+                    //
+                    continue;
+                }
+            }
+            minbleic.minbleicresultsbuf(s.bs, ref s.tmpp, s.br);
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    s.p[i,j] = s.tmpp[i*n+j];
+                }
+            }
+            s.repterminationtype = s.br.terminationtype;
+            s.repinneriterationscount = s.br.inneriterationscount;
+            s.repouteriterationscount = s.br.outeriterationscount;
+            s.repnfev = s.br.nfev;
         }
 
 
         /*************************************************************************
-        Average relative error on the test set
+        MCPD results
 
         INPUT PARAMETERS:
-            Network -   neural network
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            Its meaning for regression task is obvious. As for
-            classification task, it means average relative error when estimating
-            posterior probability of belonging to the correct class.
+            State   -   algorithm state
+
+        OUTPUT PARAMETERS:
+            P       -   array[N,N], transition matrix
+            Rep     -   optimization report. You should check Rep.TerminationType
+                        in  order  to  distinguish  successful  termination  from
+                        unsuccessful one. Speaking short, positive values  denote
+                        success, negative ones are failures.
+                        More information about fields of this  structure  can  be
+                        found in the comments on MCPDReport datatype.
+
 
           -- ALGLIB --
-             Copyright 11.03.2008 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static double mlpavgrelerror(multilayerperceptron network,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
+        public static void mcpdresults(mcpdstate s,
+            ref double[,] p,
+            mcpdreport rep)
+        {
             int i = 0;
             int j = 0;
-            int k = 0;
-            int lk = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            int i_ = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            result = 0;
-            k = 0;
-            for(i=0; i<=npoints-1; i++)
-            {
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    network.x[i_] = xy[i,i_];
-                }
-                mlpprocess(network, network.x, ref network.y);
-                if( mlpissoftmax(network) )
-                {
-                    
-                    //
-                    // class labels
-                    //
-                    lk = (int)Math.Round(xy[i,nin]);
-                    for(j=0; j<=nout-1; j++)
-                    {
-                        if( j==lk )
-                        {
-                            result = result+Math.Abs(1-network.y[j]);
-                            k = k+1;
-                        }
-                    }
-                }
-                else
-                {
-                    
-                    //
-                    // real outputs
-                    //
-                    for(j=0; j<=nout-1; j++)
-                    {
-                        if( (double)(xy[i,nin+j])!=(double)(0) )
-                        {
-                            result = result+Math.Abs(xy[i,nin+j]-network.y[j])/Math.Abs(xy[i,nin+j]);
-                            k = k+1;
-                        }
-                    }
-                }
-            }
-            if( k!=0 )
-            {
-                result = result/k;
-            }
-            return result;
+
+            p = new double[0,0];
+
+            p = new double[s.n, s.n];
+            for(i=0; i<=s.n-1; i++)
+            {
+                for(j=0; j<=s.n-1; j++)
+                {
+                    p[i,j] = s.p[i,j];
+                }
+            }
+            rep.terminationtype = s.repterminationtype;
+            rep.inneriterationscount = s.repinneriterationscount;
+            rep.outeriterationscount = s.repouteriterationscount;
+            rep.nfev = s.repnfev;
         }
 
 
         /*************************************************************************
-        Gradient calculation
-
-        INPUT PARAMETERS:
-            Network -   network initialized with one of the network creation funcs
-            X       -   input vector, length of array must be at least NIn
-            DesiredY-   desired outputs, length of array must be at least NOut
-            Grad    -   possibly preallocated array. If size of array is smaller
-                        than WCount, it will be reallocated. It is recommended to
-                        reuse previously allocated array to reduce allocation
-                        overhead.
-
-        OUTPUT PARAMETERS:
-            E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
-            Grad    -   gradient of E with respect to weights of network, array[WCount]
-            
+        Internal initialization function
+
           -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
+             Copyright 23.05.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void mlpgrad(multilayerperceptron network,
-            double[] x,
-            double[] desiredy,
-            ref double e,
-            ref double[] grad)
-        {
-            int i = 0;
-            int nout = 0;
-            int ntotal = 0;
-
-            e = 0;
-
-            
-            //
-            // Alloc
-            //
-            if( ap.len(grad)<network.structinfo[4] )
-            {
-                grad = new double[network.structinfo[4]];
-            }
-            
-            //
-            // Prepare dError/dOut, internal structures
-            //
-            mlpprocess(network, x, ref network.y);
-            nout = network.structinfo[2];
-            ntotal = network.structinfo[3];
-            e = 0;
-            for(i=0; i<=ntotal-1; i++)
-            {
-                network.derror[i] = 0;
-            }
-            for(i=0; i<=nout-1; i++)
-            {
-                network.derror[ntotal-nout+i] = network.y[i]-desiredy[i];
-                e = e+math.sqr(network.y[i]-desiredy[i])/2;
-            }
-            
-            //
-            // gradient
-            //
-            mlpinternalcalculategradient(network, network.neurons, network.weights, ref network.derror, ref grad, false);
-        }
-
-
-        /*************************************************************************
-        Gradient calculation (natural error function is used)
-
-        INPUT PARAMETERS:
-            Network -   network initialized with one of the network creation funcs
-            X       -   input vector, length of array must be at least NIn
-            DesiredY-   desired outputs, length of array must be at least NOut
-            Grad    -   possibly preallocated array. If size of array is smaller
-                        than WCount, it will be reallocated. It is recommended to
-                        reuse previously allocated array to reduce allocation
-                        overhead.
-
-        OUTPUT PARAMETERS:
-            E       -   error function, sum-of-squares for regression networks,
-                        cross-entropy for classification networks.
-            Grad    -   gradient of E with respect to weights of network, array[WCount]
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpgradn(multilayerperceptron network,
-            double[] x,
-            double[] desiredy,
-            ref double e,
-            ref double[] grad)
-        {
-            double s = 0;
-            int i = 0;
-            int nout = 0;
-            int ntotal = 0;
-
-            e = 0;
-
-            
-            //
-            // Alloc
-            //
-            if( ap.len(grad)<network.structinfo[4] )
-            {
-                grad = new double[network.structinfo[4]];
-            }
-            
-            //
-            // Prepare dError/dOut, internal structures
-            //
-            mlpprocess(network, x, ref network.y);
-            nout = network.structinfo[2];
-            ntotal = network.structinfo[3];
-            for(i=0; i<=ntotal-1; i++)
-            {
-                network.derror[i] = 0;
-            }
-            e = 0;
-            if( network.structinfo[6]==0 )
-            {
-                
-                //
-                // Regression network, least squares
-                //
-                for(i=0; i<=nout-1; i++)
-                {
-                    network.derror[ntotal-nout+i] = network.y[i]-desiredy[i];
-                    e = e+math.sqr(network.y[i]-desiredy[i])/2;
-                }
-            }
-            else
-            {
-                
-                //
-                // Classification network, cross-entropy
-                //
-                s = 0;
-                for(i=0; i<=nout-1; i++)
-                {
-                    s = s+desiredy[i];
-                }
-                for(i=0; i<=nout-1; i++)
-                {
-                    network.derror[ntotal-nout+i] = s*network.y[i]-desiredy[i];
-                    e = e+safecrossentropy(desiredy[i], network.y[i]);
-                }
-            }
-            
-            //
-            // gradient
-            //
-            mlpinternalcalculategradient(network, network.neurons, network.weights, ref network.derror, ref grad, true);
-        }
-
-
-        /*************************************************************************
-        Batch gradient calculation for a set of inputs/outputs
-
-        INPUT PARAMETERS:
-            Network -   network initialized with one of the network creation funcs
-            XY      -   set of inputs/outputs; one sample = one row;
-                        first NIn columns contain inputs,
-                        next NOut columns - desired outputs.
-            SSize   -   number of elements in XY
-            Grad    -   possibly preallocated array. If size of array is smaller
-                        than WCount, it will be reallocated. It is recommended to
-                        reuse previously allocated array to reduce allocation
-                        overhead.
-
-        OUTPUT PARAMETERS:
-            E       -   error function, SUM(sqr(y[i]-desiredy[i])/2,i)
-            Grad    -   gradient of E with respect to weights of network, array[WCount]
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpgradbatch(multilayerperceptron network,
-            double[,] xy,
-            int ssize,
-            ref double e,
-            ref double[] grad)
-        {
-            int i = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-
-            e = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            for(i=0; i<=wcount-1; i++)
-            {
-                grad[i] = 0;
-            }
-            e = 0;
-            i = 0;
-            while( i<=ssize-1 )
-            {
-                mlpchunkedgradient(network, xy, i, Math.Min(ssize, i+chunksize)-i, ref e, ref grad, false);
-                i = i+chunksize;
-            }
-        }
-
-
-        /*************************************************************************
-        Batch gradient calculation for a set of inputs/outputs
-        (natural error function is used)
-
-        INPUT PARAMETERS:
-            Network -   network initialized with one of the network creation funcs
-            XY      -   set of inputs/outputs; one sample = one row;
-                        first NIn columns contain inputs,
-                        next NOut columns - desired outputs.
-            SSize   -   number of elements in XY
-            Grad    -   possibly preallocated array. If size of array is smaller
-                        than WCount, it will be reallocated. It is recommended to
-                        reuse previously allocated array to reduce allocation
-                        overhead.
-
-        OUTPUT PARAMETERS:
-            E       -   error function, sum-of-squares for regression networks,
-                        cross-entropy for classification networks.
-            Grad    -   gradient of E with respect to weights of network, array[WCount]
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpgradnbatch(multilayerperceptron network,
-            double[,] xy,
-            int ssize,
-            ref double e,
-            ref double[] grad)
-        {
-            int i = 0;
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-
-            e = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            for(i=0; i<=wcount-1; i++)
-            {
-                grad[i] = 0;
-            }
-            e = 0;
-            i = 0;
-            while( i<=ssize-1 )
-            {
-                mlpchunkedgradient(network, xy, i, Math.Min(ssize, i+chunksize)-i, ref e, ref grad, true);
-                i = i+chunksize;
-            }
-        }
-
-
-        /*************************************************************************
-        Batch Hessian calculation (natural error function) using R-algorithm.
-        Internal subroutine.
-
-          -- ALGLIB --
-             Copyright 26.01.2008 by Bochkanov Sergey.
-             
-             Hessian calculation based on R-algorithm described in
-             "Fast Exact Multiplication by the Hessian",
-             B. A. Pearlmutter,
-             Neural Computation, 1994.
-        *************************************************************************/
-        public static void mlphessiannbatch(multilayerperceptron network,
-            double[,] xy,
-            int ssize,
-            ref double e,
-            ref double[] grad,
-            ref double[,] h)
-        {
-            e = 0;
-
-            mlphessianbatchinternal(network, xy, ssize, true, ref e, ref grad, ref h);
-        }
-
-
-        /*************************************************************************
-        Batch Hessian calculation using R-algorithm.
-        Internal subroutine.
-
-          -- ALGLIB --
-             Copyright 26.01.2008 by Bochkanov Sergey.
-
-             Hessian calculation based on R-algorithm described in
-             "Fast Exact Multiplication by the Hessian",
-             B. A. Pearlmutter,
-             Neural Computation, 1994.
-        *************************************************************************/
-        public static void mlphessianbatch(multilayerperceptron network,
-            double[,] xy,
-            int ssize,
-            ref double e,
-            ref double[] grad,
-            ref double[,] h)
-        {
-            e = 0;
-
-            mlphessianbatchinternal(network, xy, ssize, false, ref e, ref grad, ref h);
-        }
-
-
-        /*************************************************************************
-        Internal subroutine, shouldn't be called by user.
-        *************************************************************************/
-        public static void mlpinternalprocessvector(int[] structinfo,
-            double[] weights,
-            double[] columnmeans,
-            double[] columnsigmas,
-            ref double[] neurons,
-            ref double[] dfdnet,
-            double[] x,
-            ref double[] y)
-        {
-            int i = 0;
-            int n1 = 0;
-            int n2 = 0;
-            int w1 = 0;
-            int w2 = 0;
-            int ntotal = 0;
-            int nin = 0;
-            int nout = 0;
-            int istart = 0;
-            int offs = 0;
-            double net = 0;
-            double f = 0;
-            double df = 0;
-            double d2f = 0;
-            double mx = 0;
-            bool perr = new bool();
-            int i_ = 0;
-            int i1_ = 0;
-
-            
-            //
-            // Read network geometry
-            //
-            nin = structinfo[1];
-            nout = structinfo[2];
-            ntotal = structinfo[3];
-            istart = structinfo[5];
-            
-            //
-            // Inputs standartisation and putting in the network
-            //
-            for(i=0; i<=nin-1; i++)
-            {
-                if( (double)(columnsigmas[i])!=(double)(0) )
-                {
-                    neurons[i] = (x[i]-columnmeans[i])/columnsigmas[i];
-                }
-                else
-                {
-                    neurons[i] = x[i]-columnmeans[i];
-                }
-            }
-            
-            //
-            // Process network
-            //
-            for(i=0; i<=ntotal-1; i++)
-            {
-                offs = istart+i*nfieldwidth;
-                if( structinfo[offs+0]>0 )
-                {
-                    
-                    //
-                    // Activation function
-                    //
-                    mlpactivationfunction(neurons[structinfo[offs+2]], structinfo[offs+0], ref f, ref df, ref d2f);
-                    neurons[i] = f;
-                    dfdnet[i] = df;
-                }
-                if( structinfo[offs+0]==0 )
-                {
-                    
-                    //
-                    // Adaptive summator
-                    //
-                    n1 = structinfo[offs+2];
-                    n2 = n1+structinfo[offs+1]-1;
-                    w1 = structinfo[offs+3];
-                    w2 = w1+structinfo[offs+1]-1;
-                    i1_ = (n1)-(w1);
-                    net = 0.0;
-                    for(i_=w1; i_<=w2;i_++)
-                    {
-                        net += weights[i_]*neurons[i_+i1_];
-                    }
-                    neurons[i] = net;
-                    dfdnet[i] = 1.0;
-                }
-                if( structinfo[offs+0]<0 )
-                {
-                    perr = true;
-                    if( structinfo[offs+0]==-2 )
-                    {
-                        
-                        //
-                        // input neuron, left unchanged
-                        //
-                        perr = false;
-                    }
-                    if( structinfo[offs+0]==-3 )
-                    {
-                        
-                        //
-                        // "-1" neuron
-                        //
-                        neurons[i] = -1;
-                        perr = false;
-                    }
-                    if( structinfo[offs+0]==-4 )
-                    {
-                        
-                        //
-                        // "0" neuron
-                        //
-                        neurons[i] = 0;
-                        perr = false;
-                    }
-                    ap.assert(!perr, "MLPInternalProcessVector: internal error - unknown neuron type!");
-                }
-            }
-            
-            //
-            // Extract result
-            //
-            i1_ = (ntotal-nout) - (0);
-            for(i_=0; i_<=nout-1;i_++)
-            {
-                y[i_] = neurons[i_+i1_];
-            }
-            
-            //
-            // Softmax post-processing or standardisation if needed
-            //
-            ap.assert(structinfo[6]==0 | structinfo[6]==1, "MLPInternalProcessVector: unknown normalization type!");
-            if( structinfo[6]==1 )
-            {
-                
-                //
-                // Softmax
-                //
-                mx = y[0];
-                for(i=1; i<=nout-1; i++)
-                {
-                    mx = Math.Max(mx, y[i]);
-                }
-                net = 0;
-                for(i=0; i<=nout-1; i++)
-                {
-                    y[i] = Math.Exp(y[i]-mx);
-                    net = net+y[i];
-                }
-                for(i=0; i<=nout-1; i++)
-                {
-                    y[i] = y[i]/net;
-                }
-            }
-            else
-            {
-                
-                //
-                // Standardisation
-                //
-                for(i=0; i<=nout-1; i++)
-                {
-                    y[i] = y[i]*columnsigmas[nin+i]+columnmeans[nin+i];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine: adding new input layer to network
-        *************************************************************************/
-        private static void addinputlayer(int ncount,
-            ref int[] lsizes,
-            ref int[] ltypes,
-            ref int[] lconnfirst,
-            ref int[] lconnlast,
-            ref int lastproc)
-        {
-            lsizes[0] = ncount;
-            ltypes[0] = -2;
-            lconnfirst[0] = 0;
-            lconnlast[0] = 0;
-            lastproc = 0;
-        }
-
-
-        /*************************************************************************
-        Internal subroutine: adding new summator layer to network
-        *************************************************************************/
-        private static void addbiasedsummatorlayer(int ncount,
-            ref int[] lsizes,
-            ref int[] ltypes,
-            ref int[] lconnfirst,
-            ref int[] lconnlast,
-            ref int lastproc)
-        {
-            lsizes[lastproc+1] = 1;
-            ltypes[lastproc+1] = -3;
-            lconnfirst[lastproc+1] = 0;
-            lconnlast[lastproc+1] = 0;
-            lsizes[lastproc+2] = ncount;
-            ltypes[lastproc+2] = 0;
-            lconnfirst[lastproc+2] = lastproc;
-            lconnlast[lastproc+2] = lastproc+1;
-            lastproc = lastproc+2;
-        }
-
-
-        /*************************************************************************
-        Internal subroutine: adding new summator layer to network
-        *************************************************************************/
-        private static void addactivationlayer(int functype,
-            ref int[] lsizes,
-            ref int[] ltypes,
-            ref int[] lconnfirst,
-            ref int[] lconnlast,
-            ref int lastproc)
-        {
-            ap.assert(functype>0, "AddActivationLayer: incorrect function type");
-            lsizes[lastproc+1] = lsizes[lastproc];
-            ltypes[lastproc+1] = functype;
-            lconnfirst[lastproc+1] = lastproc;
-            lconnlast[lastproc+1] = lastproc;
-            lastproc = lastproc+1;
-        }
-
-
-        /*************************************************************************
-        Internal subroutine: adding new zero layer to network
-        *************************************************************************/
-        private static void addzerolayer(ref int[] lsizes,
-            ref int[] ltypes,
-            ref int[] lconnfirst,
-            ref int[] lconnlast,
-            ref int lastproc)
-        {
-            lsizes[lastproc+1] = 1;
-            ltypes[lastproc+1] = -4;
-            lconnfirst[lastproc+1] = 0;
-            lconnlast[lastproc+1] = 0;
-            lastproc = lastproc+1;
-        }
-
-
-        /*************************************************************************
-        Internal subroutine.
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        private static void mlpcreate(int nin,
-            int nout,
-            int[] lsizes,
-            int[] ltypes,
-            int[] lconnfirst,
-            int[] lconnlast,
-            int layerscount,
-            bool isclsnet,
-            multilayerperceptron network)
+        private static void mcpdinit(int n,
+            int entrystate,
+            int exitstate,
+            mcpdstate s)
         {
             int i = 0;
             int j = 0;
-            int ssize = 0;
-            int ntotal = 0;
-            int wcount = 0;
-            int offs = 0;
-            int nprocessed = 0;
-            int wallocated = 0;
-            int[] localtemp = new int[0];
-            int[] lnfirst = new int[0];
-            int[] lnsyn = new int[0];
-
-            
-            //
-            // Check
-            //
-            ap.assert(layerscount>0, "MLPCreate: wrong parameters!");
-            ap.assert(ltypes[0]==-2, "MLPCreate: wrong LTypes[0] (must be -2)!");
-            for(i=0; i<=layerscount-1; i++)
-            {
-                ap.assert(lsizes[i]>0, "MLPCreate: wrong LSizes!");
-                ap.assert(lconnfirst[i]>=0 & (lconnfirst[i]<i | i==0), "MLPCreate: wrong LConnFirst!");
-                ap.assert(lconnlast[i]>=lconnfirst[i] & (lconnlast[i]<i | i==0), "MLPCreate: wrong LConnLast!");
-            }
-            
-            //
-            // Build network geometry
-            //
-            lnfirst = new int[layerscount-1+1];
-            lnsyn = new int[layerscount-1+1];
-            ntotal = 0;
-            wcount = 0;
-            for(i=0; i<=layerscount-1; i++)
-            {
-                
-                //
-                // Analyze connections.
-                // This code must throw an assertion in case of unknown LTypes[I]
-                //
-                lnsyn[i] = -1;
-                if( ltypes[i]>=0 )
-                {
-                    lnsyn[i] = 0;
-                    for(j=lconnfirst[i]; j<=lconnlast[i]; j++)
-                    {
-                        lnsyn[i] = lnsyn[i]+lsizes[j];
-                    }
-                }
-                else
-                {
-                    if( (ltypes[i]==-2 | ltypes[i]==-3) | ltypes[i]==-4 )
-                    {
-                        lnsyn[i] = 0;
-                    }
-                }
-                ap.assert(lnsyn[i]>=0, "MLPCreate: internal error #0!");
-                
-                //
-                // Other info
-                //
-                lnfirst[i] = ntotal;
-                ntotal = ntotal+lsizes[i];
-                if( ltypes[i]==0 )
-                {
-                    wcount = wcount+lnsyn[i]*lsizes[i];
-                }
-            }
-            ssize = 7+ntotal*nfieldwidth;
-            
-            //
-            // Allocate
-            //
-            network.structinfo = new int[ssize-1+1];
-            network.weights = new double[wcount-1+1];
-            if( isclsnet )
-            {
-                network.columnmeans = new double[nin-1+1];
-                network.columnsigmas = new double[nin-1+1];
-            }
-            else
-            {
-                network.columnmeans = new double[nin+nout-1+1];
-                network.columnsigmas = new double[nin+nout-1+1];
-            }
-            network.neurons = new double[ntotal-1+1];
-            network.chunks = new double[3*ntotal+1, chunksize-1+1];
-            network.nwbuf = new double[Math.Max(wcount, 2*nout)-1+1];
-            network.dfdnet = new double[ntotal-1+1];
-            network.x = new double[nin-1+1];
-            network.y = new double[nout-1+1];
-            network.derror = new double[ntotal-1+1];
-            
-            //
-            // Fill structure: global info
-            //
-            network.structinfo[0] = ssize;
-            network.structinfo[1] = nin;
-            network.structinfo[2] = nout;
-            network.structinfo[3] = ntotal;
-            network.structinfo[4] = wcount;
-            network.structinfo[5] = 7;
-            if( isclsnet )
-            {
-                network.structinfo[6] = 1;
-            }
-            else
-            {
-                network.structinfo[6] = 0;
-            }
-            
-            //
-            // Fill structure: neuron connections
-            //
-            nprocessed = 0;
-            wallocated = 0;
-            for(i=0; i<=layerscount-1; i++)
-            {
-                for(j=0; j<=lsizes[i]-1; j++)
-                {
-                    offs = network.structinfo[5]+nprocessed*nfieldwidth;
-                    network.structinfo[offs+0] = ltypes[i];
-                    if( ltypes[i]==0 )
-                    {
-                        
-                        //
-                        // Adaptive summator:
-                        // * connections with weights to previous neurons
-                        //
-                        network.structinfo[offs+1] = lnsyn[i];
-                        network.structinfo[offs+2] = lnfirst[lconnfirst[i]];
-                        network.structinfo[offs+3] = wallocated;
-                        wallocated = wallocated+lnsyn[i];
-                        nprocessed = nprocessed+1;
-                    }
-                    if( ltypes[i]>0 )
-                    {
-                        
-                        //
-                        // Activation layer:
-                        // * each neuron connected to one (only one) of previous neurons.
-                        // * no weights
-                        //
-                        network.structinfo[offs+1] = 1;
-                        network.structinfo[offs+2] = lnfirst[lconnfirst[i]]+j;
-                        network.structinfo[offs+3] = -1;
-                        nprocessed = nprocessed+1;
-                    }
-                    if( (ltypes[i]==-2 | ltypes[i]==-3) | ltypes[i]==-4 )
-                    {
-                        nprocessed = nprocessed+1;
-                    }
-                }
-            }
-            ap.assert(wallocated==wcount, "MLPCreate: internal error #1!");
-            ap.assert(nprocessed==ntotal, "MLPCreate: internal error #2!");
-            
-            //
-            // Fill weights by small random values
-            // Initialize means and sigmas
-            //
-            for(i=0; i<=wcount-1; i++)
-            {
-                network.weights[i] = math.randomreal()-0.5;
-            }
-            for(i=0; i<=nin-1; i++)
-            {
-                network.columnmeans[i] = 0;
-                network.columnsigmas[i] = 1;
-            }
-            if( !isclsnet )
-            {
-                for(i=0; i<=nout-1; i++)
-                {
-                    network.columnmeans[nin+i] = 0;
-                    network.columnsigmas[nin+i] = 1;
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine
-
-          -- ALGLIB --
-             Copyright 04.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        private static void mlpactivationfunction(double net,
-            int k,
-            ref double f,
-            ref double df,
-            ref double d2f)
-        {
-            double net2 = 0;
-            double arg = 0;
-            double root = 0;
-            double r = 0;
-
-            f = 0;
-            df = 0;
-            d2f = 0;
-
-            f = 0;
-            df = 0;
-            if( k==1 )
-            {
-                
-                //
-                // TanH activation function
-                //
-                if( (double)(Math.Abs(net))<(double)(100) )
-                {
-                    f = Math.Tanh(net);
-                }
-                else
-                {
-                    f = Math.Sign(net);
-                }
-                df = 1-math.sqr(f);
-                d2f = -(2*f*df);
-                return;
-            }
-            if( k==3 )
-            {
-                
-                //
-                // EX activation function
-                //
-                if( (double)(net)>=(double)(0) )
-                {
-                    net2 = net*net;
-                    arg = net2+1;
-                    root = Math.Sqrt(arg);
-                    f = net+root;
-                    r = net/root;
-                    df = 1+r;
-                    d2f = (root-net*r)/arg;
-                }
-                else
-                {
-                    f = Math.Exp(net);
-                    df = f;
-                    d2f = f;
-                }
-                return;
-            }
-            if( k==2 )
-            {
-                f = Math.Exp(-math.sqr(net));
-                df = -(2*net*f);
-                d2f = -(2*(f+df*net));
-                return;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine for Hessian calculation.
-
-        WARNING!!! Unspeakable math far beyong human capabilities :)
-        *************************************************************************/
-        private static void mlphessianbatchinternal(multilayerperceptron network,
-            double[,] xy,
-            int ssize,
-            bool naturalerr,
-            ref double e,
-            ref double[] grad,
-            ref double[,] h)
-        {
-            int nin = 0;
-            int nout = 0;
-            int wcount = 0;
-            int ntotal = 0;
-            int istart = 0;
-            int i = 0;
-            int j = 0;
-            int k = 0;
-            int kl = 0;
-            int offs = 0;
-            int n1 = 0;
-            int n2 = 0;
-            int w1 = 0;
-            int w2 = 0;
-            double s = 0;
-            double t = 0;
-            double v = 0;
-            double et = 0;
-            bool bflag = new bool();
-            double f = 0;
-            double df = 0;
-            double d2f = 0;
-            double deidyj = 0;
-            double mx = 0;
-            double q = 0;
-            double z = 0;
-            double s2 = 0;
-            double expi = 0;
-            double expj = 0;
-            double[] x = new double[0];
-            double[] desiredy = new double[0];
-            double[] gt = new double[0];
-            double[] zeros = new double[0];
-            double[,] rx = new double[0,0];
-            double[,] ry = new double[0,0];
-            double[,] rdx = new double[0,0];
-            double[,] rdy = new double[0,0];
-            int i_ = 0;
-            int i1_ = 0;
-
-            e = 0;
-
-            mlpproperties(network, ref nin, ref nout, ref wcount);
-            ntotal = network.structinfo[3];
-            istart = network.structinfo[5];
-            
-            //
-            // Prepare
-            //
-            x = new double[nin-1+1];
-            desiredy = new double[nout-1+1];
-            zeros = new double[wcount-1+1];
-            gt = new double[wcount-1+1];
-            rx = new double[ntotal+nout-1+1, wcount-1+1];
-            ry = new double[ntotal+nout-1+1, wcount-1+1];
-            rdx = new double[ntotal+nout-1+1, wcount-1+1];
-            rdy = new double[ntotal+nout-1+1, wcount-1+1];
-            e = 0;
-            for(i=0; i<=wcount-1; i++)
-            {
-                zeros[i] = 0;
-            }
-            for(i_=0; i_<=wcount-1;i_++)
-            {
-                grad[i_] = zeros[i_];
-            }
-            for(i=0; i<=wcount-1; i++)
-            {
-                for(i_=0; i_<=wcount-1;i_++)
-                {
-                    h[i,i_] = zeros[i_];
-                }
-            }
-            
-            //
-            // Process
-            //
-            for(k=0; k<=ssize-1; k++)
-            {
-                
-                //
-                // Process vector with MLPGradN.
-                // Now Neurons, DFDNET and DError contains results of the last run.
-                //
-                for(i_=0; i_<=nin-1;i_++)
-                {
-                    x[i_] = xy[k,i_];
-                }
-                if( mlpissoftmax(network) )
-                {
-                    
-                    //
-                    // class labels outputs
-                    //
-                    kl = (int)Math.Round(xy[k,nin]);
-                    for(i=0; i<=nout-1; i++)
-                    {
-                        if( i==kl )
-                        {
-                            desiredy[i] = 1;
-                        }
-                        else
-                        {
-                            desiredy[i] = 0;
-                        }
-                    }
-                }
-                else
-                {
-                    
-                    //
-                    // real outputs
-                    //
-                    i1_ = (nin) - (0);
-                    for(i_=0; i_<=nout-1;i_++)
-                    {
-                        desiredy[i_] = xy[k,i_+i1_];
-                    }
-                }
-                if( naturalerr )
-                {
-                    mlpgradn(network, x, desiredy, ref et, ref gt);
-                }
-                else
-                {
-                    mlpgrad(network, x, desiredy, ref et, ref gt);
-                }
-                
-                //
-                // grad, error
-                //
-                e = e+et;
-                for(i_=0; i_<=wcount-1;i_++)
-                {
-                    grad[i_] = grad[i_] + gt[i_];
-                }
-                
-                //
-                // Hessian.
-                // Forward pass of the R-algorithm
-                //
-                for(i=0; i<=ntotal-1; i++)
-                {
-                    offs = istart+i*nfieldwidth;
-                    for(i_=0; i_<=wcount-1;i_++)
-                    {
-                        rx[i,i_] = zeros[i_];
-                    }
-                    for(i_=0; i_<=wcount-1;i_++)
-                    {
-                        ry[i,i_] = zeros[i_];
-                    }
-                    if( network.structinfo[offs+0]>0 )
-                    {
-                        
-                        //
-                        // Activation function
-                        //
-                        n1 = network.structinfo[offs+2];
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rx[i,i_] = ry[n1,i_];
-                        }
-                        v = network.dfdnet[i];
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            ry[i,i_] = v*rx[i,i_];
-                        }
-                    }
-                    if( network.structinfo[offs+0]==0 )
-                    {
-                        
-                        //
-                        // Adaptive summator
-                        //
-                        n1 = network.structinfo[offs+2];
-                        n2 = n1+network.structinfo[offs+1]-1;
-                        w1 = network.structinfo[offs+3];
-                        w2 = w1+network.structinfo[offs+1]-1;
-                        for(j=n1; j<=n2; j++)
-                        {
-                            v = network.weights[w1+j-n1];
-                            for(i_=0; i_<=wcount-1;i_++)
-                            {
-                                rx[i,i_] = rx[i,i_] + v*ry[j,i_];
-                            }
-                            rx[i,w1+j-n1] = rx[i,w1+j-n1]+network.neurons[j];
-                        }
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            ry[i,i_] = rx[i,i_];
-                        }
-                    }
-                    if( network.structinfo[offs+0]<0 )
-                    {
-                        bflag = true;
-                        if( network.structinfo[offs+0]==-2 )
-                        {
-                            
-                            //
-                            // input neuron, left unchanged
-                            //
-                            bflag = false;
-                        }
-                        if( network.structinfo[offs+0]==-3 )
-                        {
-                            
-                            //
-                            // "-1" neuron, left unchanged
-                            //
-                            bflag = false;
-                        }
-                        if( network.structinfo[offs+0]==-4 )
-                        {
-                            
-                            //
-                            // "0" neuron, left unchanged
-                            //
-                            bflag = false;
-                        }
-                        ap.assert(!bflag, "MLPHessianNBatch: internal error - unknown neuron type!");
-                    }
-                }
-                
-                //
-                // Hessian. Backward pass of the R-algorithm.
-                //
-                // Stage 1. Initialize RDY
-                //
-                for(i=0; i<=ntotal+nout-1; i++)
-                {
-                    for(i_=0; i_<=wcount-1;i_++)
-                    {
-                        rdy[i,i_] = zeros[i_];
-                    }
-                }
-                if( network.structinfo[6]==0 )
-                {
-                    
-                    //
-                    // Standardisation.
-                    //
-                    // In context of the Hessian calculation standardisation
-                    // is considered as additional layer with weightless
-                    // activation function:
-                    //
-                    // F(NET) := Sigma*NET
-                    //
-                    // So we add one more layer to forward pass, and
-                    // make forward/backward pass through this layer.
-                    //
-                    for(i=0; i<=nout-1; i++)
-                    {
-                        n1 = ntotal-nout+i;
-                        n2 = ntotal+i;
-                        
-                        //
-                        // Forward pass from N1 to N2
-                        //
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rx[n2,i_] = ry[n1,i_];
-                        }
-                        v = network.columnsigmas[nin+i];
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            ry[n2,i_] = v*rx[n2,i_];
-                        }
-                        
-                        //
-                        // Initialization of RDY
-                        //
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rdy[n2,i_] = ry[n2,i_];
-                        }
-                        
-                        //
-                        // Backward pass from N2 to N1:
-                        // 1. Calculate R(dE/dX).
-                        // 2. No R(dE/dWij) is needed since weight of activation neuron
-                        //    is fixed to 1. So we can update R(dE/dY) for
-                        //    the connected neuron (note that Vij=0, Wij=1)
-                        //
-                        df = network.columnsigmas[nin+i];
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rdx[n2,i_] = df*rdy[n2,i_];
-                        }
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rdy[n1,i_] = rdy[n1,i_] + rdx[n2,i_];
-                        }
-                    }
-                }
-                else
-                {
-                    
-                    //
-                    // Softmax.
-                    //
-                    // Initialize RDY using generalized expression for ei'(yi)
-                    // (see expression (9) from p. 5 of "Fast Exact Multiplication by the Hessian").
-                    //
-                    // When we are working with softmax network, generalized
-                    // expression for ei'(yi) is used because softmax
-                    // normalization leads to ei, which depends on all y's
-                    //
-                    if( naturalerr )
-                    {
-                        
-                        //
-                        // softmax + cross-entropy.
-                        // We have:
-                        //
-                        // S = sum(exp(yk)),
-                        // ei = sum(trn)*exp(yi)/S-trn_i
-                        //
-                        // j=i:   d(ei)/d(yj) = T*exp(yi)*(S-exp(yi))/S^2
-                        // j<>i:  d(ei)/d(yj) = -T*exp(yi)*exp(yj)/S^2
-                        //
-                        t = 0;
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            t = t+desiredy[i];
-                        }
-                        mx = network.neurons[ntotal-nout];
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            mx = Math.Max(mx, network.neurons[ntotal-nout+i]);
-                        }
-                        s = 0;
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            network.nwbuf[i] = Math.Exp(network.neurons[ntotal-nout+i]-mx);
-                            s = s+network.nwbuf[i];
-                        }
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            for(j=0; j<=nout-1; j++)
-                            {
-                                if( j==i )
-                                {
-                                    deidyj = t*network.nwbuf[i]*(s-network.nwbuf[i])/math.sqr(s);
-                                    for(i_=0; i_<=wcount-1;i_++)
-                                    {
-                                        rdy[ntotal-nout+i,i_] = rdy[ntotal-nout+i,i_] + deidyj*ry[ntotal-nout+i,i_];
-                                    }
-                                }
-                                else
-                                {
-                                    deidyj = -(t*network.nwbuf[i]*network.nwbuf[j]/math.sqr(s));
-                                    for(i_=0; i_<=wcount-1;i_++)
-                                    {
-                                        rdy[ntotal-nout+i,i_] = rdy[ntotal-nout+i,i_] + deidyj*ry[ntotal-nout+j,i_];
-                                    }
-                                }
-                            }
-                        }
-                    }
-                    else
-                    {
-                        
-                        //
-                        // For a softmax + squared error we have expression
-                        // far beyond human imagination so we dont even try
-                        // to comment on it. Just enjoy the code...
-                        //
-                        // P.S. That's why "natural error" is called "natural" -
-                        // compact beatiful expressions, fast code....
-                        //
-                        mx = network.neurons[ntotal-nout];
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            mx = Math.Max(mx, network.neurons[ntotal-nout+i]);
-                        }
-                        s = 0;
-                        s2 = 0;
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            network.nwbuf[i] = Math.Exp(network.neurons[ntotal-nout+i]-mx);
-                            s = s+network.nwbuf[i];
-                            s2 = s2+math.sqr(network.nwbuf[i]);
-                        }
-                        q = 0;
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            q = q+(network.y[i]-desiredy[i])*network.nwbuf[i];
-                        }
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            z = -q+(network.y[i]-desiredy[i])*s;
-                            expi = network.nwbuf[i];
-                            for(j=0; j<=nout-1; j++)
-                            {
-                                expj = network.nwbuf[j];
-                                if( j==i )
-                                {
-                                    deidyj = expi/math.sqr(s)*((z+expi)*(s-2*expi)/s+expi*s2/math.sqr(s));
-                                }
-                                else
-                                {
-                                    deidyj = expi*expj/math.sqr(s)*(s2/math.sqr(s)-2*z/s-(expi+expj)/s+(network.y[i]-desiredy[i])-(network.y[j]-desiredy[j]));
-                                }
-                                for(i_=0; i_<=wcount-1;i_++)
-                                {
-                                    rdy[ntotal-nout+i,i_] = rdy[ntotal-nout+i,i_] + deidyj*ry[ntotal-nout+j,i_];
-                                }
-                            }
-                        }
-                    }
-                }
-                
-                //
-                // Hessian. Backward pass of the R-algorithm
-                //
-                // Stage 2. Process.
-                //
-                for(i=ntotal-1; i>=0; i--)
-                {
-                    
-                    //
-                    // Possible variants:
-                    // 1. Activation function
-                    // 2. Adaptive summator
-                    // 3. Special neuron
-                    //
-                    offs = istart+i*nfieldwidth;
-                    if( network.structinfo[offs+0]>0 )
-                    {
-                        n1 = network.structinfo[offs+2];
-                        
-                        //
-                        // First, calculate R(dE/dX).
-                        //
-                        mlpactivationfunction(network.neurons[n1], network.structinfo[offs+0], ref f, ref df, ref d2f);
-                        v = d2f*network.derror[i];
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rdx[i,i_] = df*rdy[i,i_];
-                        }
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rdx[i,i_] = rdx[i,i_] + v*rx[i,i_];
-                        }
-                        
-                        //
-                        // No R(dE/dWij) is needed since weight of activation neuron
-                        // is fixed to 1.
-                        //
-                        // So we can update R(dE/dY) for the connected neuron.
-                        // (note that Vij=0, Wij=1)
-                        //
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rdy[n1,i_] = rdy[n1,i_] + rdx[i,i_];
-                        }
-                    }
-                    if( network.structinfo[offs+0]==0 )
-                    {
-                        
-                        //
-                        // Adaptive summator
-                        //
-                        n1 = network.structinfo[offs+2];
-                        n2 = n1+network.structinfo[offs+1]-1;
-                        w1 = network.structinfo[offs+3];
-                        w2 = w1+network.structinfo[offs+1]-1;
-                        
-                        //
-                        // First, calculate R(dE/dX).
-                        //
-                        for(i_=0; i_<=wcount-1;i_++)
-                        {
-                            rdx[i,i_] = rdy[i,i_];
-                        }
-                        
-                        //
-                        // Then, calculate R(dE/dWij)
-                        //
-                        for(j=w1; j<=w2; j++)
-                        {
-                            v = network.neurons[n1+j-w1];
-                            for(i_=0; i_<=wcount-1;i_++)
-                            {
-                                h[j,i_] = h[j,i_] + v*rdx[i,i_];
-                            }
-                            v = network.derror[i];
-                            for(i_=0; i_<=wcount-1;i_++)
-                            {
-                                h[j,i_] = h[j,i_] + v*ry[n1+j-w1,i_];
-                            }
-                        }
-                        
-                        //
-                        // And finally, update R(dE/dY) for connected neurons.
-                        //
-                        for(j=w1; j<=w2; j++)
-                        {
-                            v = network.weights[j];
-                            for(i_=0; i_<=wcount-1;i_++)
-                            {
-                                rdy[n1+j-w1,i_] = rdy[n1+j-w1,i_] + v*rdx[i,i_];
-                            }
-                            rdy[n1+j-w1,j] = rdy[n1+j-w1,j]+network.derror[i];
-                        }
-                    }
-                    if( network.structinfo[offs+0]<0 )
-                    {
-                        bflag = false;
-                        if( (network.structinfo[offs+0]==-2 | network.structinfo[offs+0]==-3) | network.structinfo[offs+0]==-4 )
-                        {
-                            
-                            //
-                            // Special neuron type, no back-propagation required
-                            //
-                            bflag = true;
-                        }
-                        ap.assert(bflag, "MLPHessianNBatch: unknown neuron type!");
-                    }
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine
-
-        Network must be processed by MLPProcess on X
-        *************************************************************************/
-        private static void mlpinternalcalculategradient(multilayerperceptron network,
-            double[] neurons,
-            double[] weights,
-            ref double[] derror,
-            ref double[] grad,
-            bool naturalerrorfunc)
-        {
-            int i = 0;
-            int n1 = 0;
-            int n2 = 0;
-            int w1 = 0;
-            int w2 = 0;
-            int ntotal = 0;
-            int istart = 0;
-            int nin = 0;
-            int nout = 0;
-            int offs = 0;
-            double dedf = 0;
-            double dfdnet = 0;
-            double v = 0;
-            double fown = 0;
-            double deown = 0;
-            double net = 0;
-            double mx = 0;
-            bool bflag = new bool();
-            int i_ = 0;
-            int i1_ = 0;
-
-            
-            //
-            // Read network geometry
-            //
-            nin = network.structinfo[1];
-            nout = network.structinfo[2];
-            ntotal = network.structinfo[3];
-            istart = network.structinfo[5];
-            
-            //
-            // Pre-processing of dError/dOut:
-            // from dError/dOut(normalized) to dError/dOut(non-normalized)
-            //
-            ap.assert(network.structinfo[6]==0 | network.structinfo[6]==1, "MLPInternalCalculateGradient: unknown normalization type!");
-            if( network.structinfo[6]==1 )
-            {
-                
-                //
-                // Softmax
-                //
-                if( !naturalerrorfunc )
-                {
-                    mx = network.neurons[ntotal-nout];
-                    for(i=0; i<=nout-1; i++)
-                    {
-                        mx = Math.Max(mx, network.neurons[ntotal-nout+i]);
-                    }
-                    net = 0;
-                    for(i=0; i<=nout-1; i++)
-                    {
-                        network.nwbuf[i] = Math.Exp(network.neurons[ntotal-nout+i]-mx);
-                        net = net+network.nwbuf[i];
-                    }
-                    i1_ = (0)-(ntotal-nout);
-                    v = 0.0;
-                    for(i_=ntotal-nout; i_<=ntotal-1;i_++)
-                    {
-                        v += network.derror[i_]*network.nwbuf[i_+i1_];
-                    }
-                    for(i=0; i<=nout-1; i++)
-                    {
-                        fown = network.nwbuf[i];
-                        deown = network.derror[ntotal-nout+i];
-                        network.nwbuf[nout+i] = (-v+deown*fown+deown*(net-fown))*fown/math.sqr(net);
-                    }
-                    for(i=0; i<=nout-1; i++)
-                    {
-                        network.derror[ntotal-nout+i] = network.nwbuf[nout+i];
-                    }
-                }
-            }
-            else
-            {
-                
-                //
-                // Un-standardisation
-                //
-                for(i=0; i<=nout-1; i++)
-                {
-                    network.derror[ntotal-nout+i] = network.derror[ntotal-nout+i]*network.columnsigmas[nin+i];
-                }
-            }
-            
-            //
-            // Backpropagation
-            //
-            for(i=ntotal-1; i>=0; i--)
-            {
-                
-                //
-                // Extract info
-                //
-                offs = istart+i*nfieldwidth;
-                if( network.structinfo[offs+0]>0 )
-                {
-                    
-                    //
-                    // Activation function
-                    //
-                    dedf = network.derror[i];
-                    dfdnet = network.dfdnet[i];
-                    derror[network.structinfo[offs+2]] = derror[network.structinfo[offs+2]]+dedf*dfdnet;
-                }
-                if( network.structinfo[offs+0]==0 )
-                {
-                    
-                    //
-                    // Adaptive summator
-                    //
-                    n1 = network.structinfo[offs+2];
-                    n2 = n1+network.structinfo[offs+1]-1;
-                    w1 = network.structinfo[offs+3];
-                    w2 = w1+network.structinfo[offs+1]-1;
-                    dedf = network.derror[i];
-                    dfdnet = 1.0;
-                    v = dedf*dfdnet;
-                    i1_ = (n1) - (w1);
-                    for(i_=w1; i_<=w2;i_++)
-                    {
-                        grad[i_] = v*neurons[i_+i1_];
-                    }
-                    i1_ = (w1) - (n1);
-                    for(i_=n1; i_<=n2;i_++)
-                    {
-                        derror[i_] = derror[i_] + v*weights[i_+i1_];
-                    }
-                }
-                if( network.structinfo[offs+0]<0 )
-                {
-                    bflag = false;
-                    if( (network.structinfo[offs+0]==-2 | network.structinfo[offs+0]==-3) | network.structinfo[offs+0]==-4 )
-                    {
-                        
-                        //
-                        // Special neuron type, no back-propagation required
-                        //
-                        bflag = true;
-                    }
-                    ap.assert(bflag, "MLPInternalCalculateGradient: unknown neuron type!");
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine, chunked gradient
-        *************************************************************************/
-        private static void mlpchunkedgradient(multilayerperceptron network,
-            double[,] xy,
-            int cstart,
-            int csize,
-            ref double e,
-            ref double[] grad,
-            bool naturalerrorfunc)
-        {
-            int i = 0;
-            int j = 0;
-            int k = 0;
-            int kl = 0;
-            int n1 = 0;
-            int n2 = 0;
-            int w1 = 0;
-            int w2 = 0;
-            int c1 = 0;
-            int c2 = 0;
-            int ntotal = 0;
-            int nin = 0;
-            int nout = 0;
-            int offs = 0;
-            double f = 0;
-            double df = 0;
-            double d2f = 0;
-            double v = 0;
-            double s = 0;
-            double fown = 0;
-            double deown = 0;
-            double net = 0;
-            double lnnet = 0;
-            double mx = 0;
-            bool bflag = new bool();
-            int istart = 0;
-            int ineurons = 0;
-            int idfdnet = 0;
-            int iderror = 0;
-            int izeros = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            
-            //
-            // Read network geometry, prepare data
-            //
-            nin = network.structinfo[1];
-            nout = network.structinfo[2];
-            ntotal = network.structinfo[3];
-            istart = network.structinfo[5];
-            c1 = cstart;
-            c2 = cstart+csize-1;
-            ineurons = 0;
-            idfdnet = ntotal;
-            iderror = 2*ntotal;
-            izeros = 3*ntotal;
-            for(j=0; j<=csize-1; j++)
-            {
-                network.chunks[izeros,j] = 0;
-            }
-            
-            //
-            // Forward pass:
-            // 1. Load inputs from XY to Chunks[0:NIn-1,0:CSize-1]
-            // 2. Forward pass
-            //
-            for(i=0; i<=nin-1; i++)
-            {
-                for(j=0; j<=csize-1; j++)
-                {
-                    if( (double)(network.columnsigmas[i])!=(double)(0) )
-                    {
-                        network.chunks[i,j] = (xy[c1+j,i]-network.columnmeans[i])/network.columnsigmas[i];
-                    }
-                    else
-                    {
-                        network.chunks[i,j] = xy[c1+j,i]-network.columnmeans[i];
-                    }
-                }
-            }
-            for(i=0; i<=ntotal-1; i++)
-            {
-                offs = istart+i*nfieldwidth;
-                if( network.structinfo[offs+0]>0 )
-                {
-                    
-                    //
-                    // Activation function:
-                    // * calculate F vector, F(i) = F(NET(i))
-                    //
-                    n1 = network.structinfo[offs+2];
-                    for(i_=0; i_<=csize-1;i_++)
-                    {
-                        network.chunks[i,i_] = network.chunks[n1,i_];
-                    }
-                    for(j=0; j<=csize-1; j++)
-                    {
-                        mlpactivationfunction(network.chunks[i,j], network.structinfo[offs+0], ref f, ref df, ref d2f);
-                        network.chunks[i,j] = f;
-                        network.chunks[idfdnet+i,j] = df;
-                    }
-                }
-                if( network.structinfo[offs+0]==0 )
-                {
-                    
-                    //
-                    // Adaptive summator:
-                    // * calculate NET vector, NET(i) = SUM(W(j,i)*Neurons(j),j=N1..N2)
-                    //
-                    n1 = network.structinfo[offs+2];
-                    n2 = n1+network.structinfo[offs+1]-1;
-                    w1 = network.structinfo[offs+3];
-                    w2 = w1+network.structinfo[offs+1]-1;
-                    for(i_=0; i_<=csize-1;i_++)
-                    {
-                        network.chunks[i,i_] = network.chunks[izeros,i_];
-                    }
-                    for(j=n1; j<=n2; j++)
-                    {
-                        v = network.weights[w1+j-n1];
-                        for(i_=0; i_<=csize-1;i_++)
-                        {
-                            network.chunks[i,i_] = network.chunks[i,i_] + v*network.chunks[j,i_];
-                        }
-                    }
-                }
-                if( network.structinfo[offs+0]<0 )
-                {
-                    bflag = false;
-                    if( network.structinfo[offs+0]==-2 )
-                    {
-                        
-                        //
-                        // input neuron, left unchanged
-                        //
-                        bflag = true;
-                    }
-                    if( network.structinfo[offs+0]==-3 )
-                    {
-                        
-                        //
-                        // "-1" neuron
-                        //
-                        for(k=0; k<=csize-1; k++)
-                        {
-                            network.chunks[i,k] = -1;
-                        }
-                        bflag = true;
-                    }
-                    if( network.structinfo[offs+0]==-4 )
-                    {
-                        
-                        //
-                        // "0" neuron
-                        //
-                        for(k=0; k<=csize-1; k++)
-                        {
-                            network.chunks[i,k] = 0;
-                        }
-                        bflag = true;
-                    }
-                    ap.assert(bflag, "MLPChunkedGradient: internal error - unknown neuron type!");
-                }
-            }
-            
-            //
-            // Post-processing, error, dError/dOut
-            //
-            for(i=0; i<=ntotal-1; i++)
-            {
-                for(i_=0; i_<=csize-1;i_++)
-                {
-                    network.chunks[iderror+i,i_] = network.chunks[izeros,i_];
-                }
-            }
-            ap.assert(network.structinfo[6]==0 | network.structinfo[6]==1, "MLPChunkedGradient: unknown normalization type!");
-            if( network.structinfo[6]==1 )
-            {
-                
-                //
-                // Softmax output, classification network.
-                //
-                // For each K = 0..CSize-1 do:
-                // 1. place exp(outputs[k]) to NWBuf[0:NOut-1]
-                // 2. place sum(exp(..)) to NET
-                // 3. calculate dError/dOut and place it to the second block of Chunks
-                //
-                for(k=0; k<=csize-1; k++)
-                {
-                    
-                    //
-                    // Normalize
-                    //
-                    mx = network.chunks[ntotal-nout,k];
-                    for(i=1; i<=nout-1; i++)
-                    {
-                        mx = Math.Max(mx, network.chunks[ntotal-nout+i,k]);
-                    }
-                    net = 0;
-                    for(i=0; i<=nout-1; i++)
-                    {
-                        network.nwbuf[i] = Math.Exp(network.chunks[ntotal-nout+i,k]-mx);
-                        net = net+network.nwbuf[i];
-                    }
-                    
-                    //
-                    // Calculate error function and dError/dOut
-                    //
-                    if( naturalerrorfunc )
-                    {
-                        
-                        //
-                        // Natural error func.
-                        //
-                        //
-                        s = 1;
-                        lnnet = Math.Log(net);
-                        kl = (int)Math.Round(xy[cstart+k,nin]);
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            if( i==kl )
-                            {
-                                v = 1;
-                            }
-                            else
-                            {
-                                v = 0;
-                            }
-                            network.chunks[iderror+ntotal-nout+i,k] = s*network.nwbuf[i]/net-v;
-                            e = e+safecrossentropy(v, network.nwbuf[i]/net);
-                        }
-                    }
-                    else
-                    {
-                        
-                        //
-                        // Least squares error func
-                        // Error, dError/dOut(normalized)
-                        //
-                        kl = (int)Math.Round(xy[cstart+k,nin]);
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            if( i==kl )
-                            {
-                                v = network.nwbuf[i]/net-1;
-                            }
-                            else
-                            {
-                                v = network.nwbuf[i]/net;
-                            }
-                            network.nwbuf[nout+i] = v;
-                            e = e+math.sqr(v)/2;
-                        }
-                        
-                        //
-                        // From dError/dOut(normalized) to dError/dOut(non-normalized)
-                        //
-                        i1_ = (0)-(nout);
-                        v = 0.0;
-                        for(i_=nout; i_<=2*nout-1;i_++)
-                        {
-                            v += network.nwbuf[i_]*network.nwbuf[i_+i1_];
-                        }
-                        for(i=0; i<=nout-1; i++)
-                        {
-                            fown = network.nwbuf[i];
-                            deown = network.nwbuf[nout+i];
-                            network.chunks[iderror+ntotal-nout+i,k] = (-v+deown*fown+deown*(net-fown))*fown/math.sqr(net);
-                        }
-                    }
-                }
-            }
-            else
-            {
-                
-                //
-                // Normal output, regression network
-                //
-                // For each K = 0..CSize-1 do:
-                // 1. calculate dError/dOut and place it to the second block of Chunks
-                //
-                for(i=0; i<=nout-1; i++)
-                {
-                    for(j=0; j<=csize-1; j++)
-                    {
-                        v = network.chunks[ntotal-nout+i,j]*network.columnsigmas[nin+i]+network.columnmeans[nin+i]-xy[cstart+j,nin+i];
-                        network.chunks[iderror+ntotal-nout+i,j] = v*network.columnsigmas[nin+i];
-                        e = e+math.sqr(v)/2;
-                    }
-                }
-            }
-            
-            //
-            // Backpropagation
-            //
-            for(i=ntotal-1; i>=0; i--)
-            {
-                
-                //
-                // Extract info
-                //
-                offs = istart+i*nfieldwidth;
-                if( network.structinfo[offs+0]>0 )
-                {
-                    
-                    //
-                    // Activation function
-                    //
-                    n1 = network.structinfo[offs+2];
-                    for(k=0; k<=csize-1; k++)
-                    {
-                        network.chunks[iderror+i,k] = network.chunks[iderror+i,k]*network.chunks[idfdnet+i,k];
-                    }
-                    for(i_=0; i_<=csize-1;i_++)
-                    {
-                        network.chunks[iderror+n1,i_] = network.chunks[iderror+n1,i_] + network.chunks[iderror+i,i_];
-                    }
-                }
-                if( network.structinfo[offs+0]==0 )
-                {
-                    
-                    //
-                    // "Normal" activation function
-                    //
-                    n1 = network.structinfo[offs+2];
-                    n2 = n1+network.structinfo[offs+1]-1;
-                    w1 = network.structinfo[offs+3];
-                    w2 = w1+network.structinfo[offs+1]-1;
-                    for(j=w1; j<=w2; j++)
-                    {
-                        v = 0.0;
-                        for(i_=0; i_<=csize-1;i_++)
-                        {
-                            v += network.chunks[n1+j-w1,i_]*network.chunks[iderror+i,i_];
-                        }
-                        grad[j] = grad[j]+v;
-                    }
-                    for(j=n1; j<=n2; j++)
-                    {
-                        v = network.weights[w1+j-n1];
-                        for(i_=0; i_<=csize-1;i_++)
-                        {
-                            network.chunks[iderror+j,i_] = network.chunks[iderror+j,i_] + v*network.chunks[iderror+i,i_];
-                        }
-                    }
-                }
-                if( network.structinfo[offs+0]<0 )
-                {
-                    bflag = false;
-                    if( (network.structinfo[offs+0]==-2 | network.structinfo[offs+0]==-3) | network.structinfo[offs+0]==-4 )
-                    {
-                        
-                        //
-                        // Special neuron type, no back-propagation required
-                        //
-                        bflag = true;
-                    }
-                    ap.assert(bflag, "MLPInternalCalculateGradient: unknown neuron type!");
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Returns T*Ln(T/Z), guarded against overflow/underflow.
-        Internal subroutine.
-        *************************************************************************/
-        private static double safecrossentropy(double t,
-            double z)
-        {
-            double result = 0;
-            double r = 0;
-
-            if( (double)(t)==(double)(0) )
-            {
-                result = 0;
-            }
-            else
-            {
-                if( (double)(Math.Abs(z))>(double)(1) )
-                {
-                    
-                    //
-                    // Shouldn't be the case with softmax,
-                    // but we just want to be sure.
-                    //
-                    if( (double)(t/z)==(double)(0) )
-                    {
-                        r = math.minrealnumber;
-                    }
-                    else
-                    {
-                        r = t/z;
-                    }
-                }
-                else
-                {
-                    
-                    //
-                    // Normal case
-                    //
-                    if( (double)(z)==(double)(0) | (double)(Math.Abs(t))>=(double)(math.maxrealnumber*Math.Abs(z)) )
-                    {
-                        r = math.maxrealnumber;
-                    }
-                    else
-                    {
-                        r = t/z;
-                    }
-                }
-                result = t*Math.Log(r);
-            }
-            return result;
-        }
-
-
-    }
-    public class mlpe
-    {
-        /*************************************************************************
-        Neural networks ensemble
-        *************************************************************************/
-        public class mlpensemble
-        {
-            public int[] structinfo;
-            public int ensemblesize;
-            public int nin;
-            public int nout;
-            public int wcount;
-            public bool issoftmax;
-            public bool postprocessing;
-            public double[] weights;
-            public double[] columnmeans;
-            public double[] columnsigmas;
-            public int serializedlen;
-            public double[] serializedmlp;
-            public double[] tmpweights;
-            public double[] tmpmeans;
-            public double[] tmpsigmas;
-            public double[] neurons;
-            public double[] dfdnet;
-            public double[] y;
-            public mlpensemble()
-            {
-                structinfo = new int[0];
-                weights = new double[0];
-                columnmeans = new double[0];
-                columnsigmas = new double[0];
-                serializedmlp = new double[0];
-                tmpweights = new double[0];
-                tmpmeans = new double[0];
-                tmpsigmas = new double[0];
-                neurons = new double[0];
-                dfdnet = new double[0];
-                y = new double[0];
-            }
-        };
-
-
-
-
-        public const int mlpntotaloffset = 3;
-        public const int mlpevnum = 9;
-
-
-        /*************************************************************************
-        Like MLPCreate0, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreate0(int nin,
-            int nout,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreate0(nin, nout, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreate1, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreate1(int nin,
-            int nhid,
-            int nout,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreate1(nin, nhid, nout, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreate2, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreate2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreate2(nin, nhid1, nhid2, nout, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateB0, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreateb0(int nin,
-            int nout,
-            double b,
-            double d,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreateb0(nin, nout, b, d, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateB1, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreateb1(int nin,
-            int nhid,
-            int nout,
-            double b,
-            double d,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreateb1(nin, nhid, nout, b, d, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateB2, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreateb2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            double b,
-            double d,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreateb2(nin, nhid1, nhid2, nout, b, d, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateR0, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreater0(int nin,
-            int nout,
-            double a,
-            double b,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreater0(nin, nout, a, b, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateR1, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreater1(int nin,
-            int nhid,
-            int nout,
-            double a,
-            double b,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreater1(nin, nhid, nout, a, b, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateR2, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreater2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            double a,
-            double b,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreater2(nin, nhid1, nhid2, nout, a, b, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateC0, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreatec0(int nin,
-            int nout,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreatec0(nin, nout, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateC1, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreatec1(int nin,
-            int nhid,
-            int nout,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreatec1(nin, nhid, nout, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Like MLPCreateC2, but for ensembles.
-
-          -- ALGLIB --
-             Copyright 18.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreatec2(int nin,
-            int nhid1,
-            int nhid2,
-            int nout,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
-
-            mlpbase.mlpcreatec2(nin, nhid1, nhid2, nout, net);
-            mlpecreatefromnetwork(net, ensemblesize, ensemble);
-        }
-
-
-        /*************************************************************************
-        Creates ensemble from network. Only network geometry is copied.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecreatefromnetwork(mlpbase.multilayerperceptron network,
-            int ensemblesize,
-            mlpensemble ensemble)
-        {
-            int i = 0;
-            int ccount = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            ap.assert(ensemblesize>0, "MLPECreate: incorrect ensemble size!");
-            
-            //
-            // network properties
-            //
-            mlpbase.mlpproperties(network, ref ensemble.nin, ref ensemble.nout, ref ensemble.wcount);
-            if( mlpbase.mlpissoftmax(network) )
-            {
-                ccount = ensemble.nin;
-            }
-            else
-            {
-                ccount = ensemble.nin+ensemble.nout;
-            }
-            ensemble.postprocessing = false;
-            ensemble.issoftmax = mlpbase.mlpissoftmax(network);
-            ensemble.ensemblesize = ensemblesize;
-            
-            //
-            // structure information
-            //
-            ensemble.structinfo = new int[network.structinfo[0]-1+1];
-            for(i=0; i<=network.structinfo[0]-1; i++)
-            {
-                ensemble.structinfo[i] = network.structinfo[i];
-            }
-            
-            //
-            // weights, means, sigmas
-            //
-            ensemble.weights = new double[ensemblesize*ensemble.wcount-1+1];
-            ensemble.columnmeans = new double[ensemblesize*ccount-1+1];
-            ensemble.columnsigmas = new double[ensemblesize*ccount-1+1];
-            for(i=0; i<=ensemblesize*ensemble.wcount-1; i++)
-            {
-                ensemble.weights[i] = math.randomreal()-0.5;
-            }
-            for(i=0; i<=ensemblesize-1; i++)
-            {
-                i1_ = (0) - (i*ccount);
-                for(i_=i*ccount; i_<=(i+1)*ccount-1;i_++)
-                {
-                    ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
-                }
-                i1_ = (0) - (i*ccount);
-                for(i_=i*ccount; i_<=(i+1)*ccount-1;i_++)
-                {
-                    ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
-                }
-            }
-            
-            //
-            // serialized part
-            //
-            mlpbase.mlpserialize(network, ref ensemble.serializedmlp, ref ensemble.serializedlen);
-            
-            //
-            // temporaries, internal buffers
-            //
-            ensemble.tmpweights = new double[ensemble.wcount-1+1];
-            ensemble.tmpmeans = new double[ccount-1+1];
-            ensemble.tmpsigmas = new double[ccount-1+1];
-            ensemble.neurons = new double[ensemble.structinfo[mlpntotaloffset]-1+1];
-            ensemble.dfdnet = new double[ensemble.structinfo[mlpntotaloffset]-1+1];
-            ensemble.y = new double[ensemble.nout-1+1];
-        }
-
-
-        /*************************************************************************
-        Copying of MLPEnsemble strucure
-
-        INPUT PARAMETERS:
-            Ensemble1 -   original
-
-        OUTPUT PARAMETERS:
-            Ensemble2 -   copy
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpecopy(mlpensemble ensemble1,
-            mlpensemble ensemble2)
-        {
-            int i = 0;
-            int ssize = 0;
-            int ccount = 0;
-            int ntotal = 0;
-            int i_ = 0;
-
-            
-            //
-            // Unload info
-            //
-            ssize = ensemble1.structinfo[0];
-            if( ensemble1.issoftmax )
-            {
-                ccount = ensemble1.nin;
-            }
-            else
-            {
-                ccount = ensemble1.nin+ensemble1.nout;
-            }
-            ntotal = ensemble1.structinfo[mlpntotaloffset];
-            
-            //
-            // Allocate space
-            //
-            ensemble2.structinfo = new int[ssize-1+1];
-            ensemble2.weights = new double[ensemble1.ensemblesize*ensemble1.wcount-1+1];
-            ensemble2.columnmeans = new double[ensemble1.ensemblesize*ccount-1+1];
-            ensemble2.columnsigmas = new double[ensemble1.ensemblesize*ccount-1+1];
-            ensemble2.tmpweights = new double[ensemble1.wcount-1+1];
-            ensemble2.tmpmeans = new double[ccount-1+1];
-            ensemble2.tmpsigmas = new double[ccount-1+1];
-            ensemble2.serializedmlp = new double[ensemble1.serializedlen-1+1];
-            ensemble2.neurons = new double[ntotal-1+1];
-            ensemble2.dfdnet = new double[ntotal-1+1];
-            ensemble2.y = new double[ensemble1.nout-1+1];
-            
-            //
-            // Copy
-            //
-            ensemble2.nin = ensemble1.nin;
-            ensemble2.nout = ensemble1.nout;
-            ensemble2.wcount = ensemble1.wcount;
-            ensemble2.ensemblesize = ensemble1.ensemblesize;
-            ensemble2.issoftmax = ensemble1.issoftmax;
-            ensemble2.postprocessing = ensemble1.postprocessing;
-            ensemble2.serializedlen = ensemble1.serializedlen;
-            for(i=0; i<=ssize-1; i++)
-            {
-                ensemble2.structinfo[i] = ensemble1.structinfo[i];
-            }
-            for(i_=0; i_<=ensemble1.ensemblesize*ensemble1.wcount-1;i_++)
-            {
-                ensemble2.weights[i_] = ensemble1.weights[i_];
-            }
-            for(i_=0; i_<=ensemble1.ensemblesize*ccount-1;i_++)
-            {
-                ensemble2.columnmeans[i_] = ensemble1.columnmeans[i_];
-            }
-            for(i_=0; i_<=ensemble1.ensemblesize*ccount-1;i_++)
-            {
-                ensemble2.columnsigmas[i_] = ensemble1.columnsigmas[i_];
-            }
-            for(i_=0; i_<=ensemble1.serializedlen-1;i_++)
-            {
-                ensemble2.serializedmlp[i_] = ensemble1.serializedmlp[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        Serialization of MLPEnsemble strucure
-
-        INPUT PARAMETERS:
-            Ensemble-   original
-
-        OUTPUT PARAMETERS:
-            RA      -   array of real numbers which stores ensemble,
-                        array[0..RLen-1]
-            RLen    -   RA lenght
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpeserialize(mlpensemble ensemble,
-            ref double[] ra,
-            ref int rlen)
-        {
-            int i = 0;
-            int ssize = 0;
-            int ntotal = 0;
-            int ccount = 0;
-            int hsize = 0;
-            int offs = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            ra = new double[0];
-            rlen = 0;
-
-            hsize = 13;
-            ssize = ensemble.structinfo[0];
-            if( ensemble.issoftmax )
-            {
-                ccount = ensemble.nin;
-            }
-            else
-            {
-                ccount = ensemble.nin+ensemble.nout;
-            }
-            ntotal = ensemble.structinfo[mlpntotaloffset];
-            rlen = hsize+ssize+ensemble.ensemblesize*ensemble.wcount+2*ccount*ensemble.ensemblesize+ensemble.serializedlen;
-            
-            //
-            //  RA format:
-            //  [0]     RLen
-            //  [1]     Version (MLPEVNum)
-            //  [2]     EnsembleSize
-            //  [3]     NIn
-            //  [4]     NOut
-            //  [5]     WCount
-            //  [6]     IsSoftmax 0/1
-            //  [7]     PostProcessing 0/1
-            //  [8]     sizeof(StructInfo)
-            //  [9]     NTotal (sizeof(Neurons), sizeof(DFDNET))
-            //  [10]    CCount (sizeof(ColumnMeans), sizeof(ColumnSigmas))
-            //  [11]    data offset
-            //  [12]    SerializedLen
-            //
-            //  [..]    StructInfo
-            //  [..]    Weights
-            //  [..]    ColumnMeans
-            //  [..]    ColumnSigmas
-            //
-            ra = new double[rlen-1+1];
-            ra[0] = rlen;
-            ra[1] = mlpevnum;
-            ra[2] = ensemble.ensemblesize;
-            ra[3] = ensemble.nin;
-            ra[4] = ensemble.nout;
-            ra[5] = ensemble.wcount;
-            if( ensemble.issoftmax )
-            {
-                ra[6] = 1;
-            }
-            else
-            {
-                ra[6] = 0;
-            }
-            if( ensemble.postprocessing )
-            {
-                ra[7] = 1;
-            }
-            else
-            {
-                ra[7] = 9;
-            }
-            ra[8] = ssize;
-            ra[9] = ntotal;
-            ra[10] = ccount;
-            ra[11] = hsize;
-            ra[12] = ensemble.serializedlen;
-            offs = hsize;
-            for(i=offs; i<=offs+ssize-1; i++)
-            {
-                ra[i] = ensemble.structinfo[i-offs];
-            }
-            offs = offs+ssize;
-            i1_ = (0) - (offs);
-            for(i_=offs; i_<=offs+ensemble.ensemblesize*ensemble.wcount-1;i_++)
-            {
-                ra[i_] = ensemble.weights[i_+i1_];
-            }
-            offs = offs+ensemble.ensemblesize*ensemble.wcount;
-            i1_ = (0) - (offs);
-            for(i_=offs; i_<=offs+ensemble.ensemblesize*ccount-1;i_++)
-            {
-                ra[i_] = ensemble.columnmeans[i_+i1_];
-            }
-            offs = offs+ensemble.ensemblesize*ccount;
-            i1_ = (0) - (offs);
-            for(i_=offs; i_<=offs+ensemble.ensemblesize*ccount-1;i_++)
-            {
-                ra[i_] = ensemble.columnsigmas[i_+i1_];
-            }
-            offs = offs+ensemble.ensemblesize*ccount;
-            i1_ = (0) - (offs);
-            for(i_=offs; i_<=offs+ensemble.serializedlen-1;i_++)
-            {
-                ra[i_] = ensemble.serializedmlp[i_+i1_];
-            }
-            offs = offs+ensemble.serializedlen;
-        }
-
-
-        /*************************************************************************
-        Unserialization of MLPEnsemble strucure
-
-        INPUT PARAMETERS:
-            RA      -   real array which stores ensemble
-
-        OUTPUT PARAMETERS:
-            Ensemble-   restored structure
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpeunserialize(double[] ra,
-            mlpensemble ensemble)
-        {
-            int i = 0;
-            int ssize = 0;
-            int ntotal = 0;
-            int ccount = 0;
-            int hsize = 0;
-            int offs = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            ap.assert((int)Math.Round(ra[1])==mlpevnum, "MLPEUnserialize: incorrect array!");
-            
-            //
-            // load info
-            //
-            hsize = 13;
-            ensemble.ensemblesize = (int)Math.Round(ra[2]);
-            ensemble.nin = (int)Math.Round(ra[3]);
-            ensemble.nout = (int)Math.Round(ra[4]);
-            ensemble.wcount = (int)Math.Round(ra[5]);
-            ensemble.issoftmax = (int)Math.Round(ra[6])==1;
-            ensemble.postprocessing = (int)Math.Round(ra[7])==1;
-            ssize = (int)Math.Round(ra[8]);
-            ntotal = (int)Math.Round(ra[9]);
-            ccount = (int)Math.Round(ra[10]);
-            offs = (int)Math.Round(ra[11]);
-            ensemble.serializedlen = (int)Math.Round(ra[12]);
-            
-            //
-            //  Allocate arrays
-            //
-            ensemble.structinfo = new int[ssize-1+1];
-            ensemble.weights = new double[ensemble.ensemblesize*ensemble.wcount-1+1];
-            ensemble.columnmeans = new double[ensemble.ensemblesize*ccount-1+1];
-            ensemble.columnsigmas = new double[ensemble.ensemblesize*ccount-1+1];
-            ensemble.tmpweights = new double[ensemble.wcount-1+1];
-            ensemble.tmpmeans = new double[ccount-1+1];
-            ensemble.tmpsigmas = new double[ccount-1+1];
-            ensemble.neurons = new double[ntotal-1+1];
-            ensemble.dfdnet = new double[ntotal-1+1];
-            ensemble.serializedmlp = new double[ensemble.serializedlen-1+1];
-            ensemble.y = new double[ensemble.nout-1+1];
-            
-            //
-            // load data
-            //
-            for(i=offs; i<=offs+ssize-1; i++)
-            {
-                ensemble.structinfo[i-offs] = (int)Math.Round(ra[i]);
-            }
-            offs = offs+ssize;
-            i1_ = (offs) - (0);
-            for(i_=0; i_<=ensemble.ensemblesize*ensemble.wcount-1;i_++)
-            {
-                ensemble.weights[i_] = ra[i_+i1_];
-            }
-            offs = offs+ensemble.ensemblesize*ensemble.wcount;
-            i1_ = (offs) - (0);
-            for(i_=0; i_<=ensemble.ensemblesize*ccount-1;i_++)
-            {
-                ensemble.columnmeans[i_] = ra[i_+i1_];
-            }
-            offs = offs+ensemble.ensemblesize*ccount;
-            i1_ = (offs) - (0);
-            for(i_=0; i_<=ensemble.ensemblesize*ccount-1;i_++)
-            {
-                ensemble.columnsigmas[i_] = ra[i_+i1_];
-            }
-            offs = offs+ensemble.ensemblesize*ccount;
-            i1_ = (offs) - (0);
-            for(i_=0; i_<=ensemble.serializedlen-1;i_++)
-            {
-                ensemble.serializedmlp[i_] = ra[i_+i1_];
-            }
-            offs = offs+ensemble.serializedlen;
-        }
-
-
-        /*************************************************************************
-        Randomization of MLP ensemble
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlperandomize(mlpensemble ensemble)
-        {
-            int i = 0;
-
-            for(i=0; i<=ensemble.ensemblesize*ensemble.wcount-1; i++)
-            {
-                ensemble.weights[i] = math.randomreal()-0.5;
-            }
-        }
-
-
-        /*************************************************************************
-        Return ensemble properties (number of inputs and outputs).
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpeproperties(mlpensemble ensemble,
-            ref int nin,
-            ref int nout)
-        {
-            nin = 0;
-            nout = 0;
-
-            nin = ensemble.nin;
-            nout = ensemble.nout;
-        }
-
-
-        /*************************************************************************
-        Return normalization type (whether ensemble is SOFTMAX-normalized or not).
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool mlpeissoftmax(mlpensemble ensemble)
-        {
-            bool result = new bool();
-
-            result = ensemble.issoftmax;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Procesing
-
-        INPUT PARAMETERS:
-            Ensemble-   neural networks ensemble
-            X       -   input vector,  array[0..NIn-1].
-            Y       -   (possibly) preallocated buffer; if size of Y is less than
-                        NOut, it will be reallocated. If it is large enough, it
-                        is NOT reallocated, so we can save some time on reallocation.
-
-
-        OUTPUT PARAMETERS:
-            Y       -   result. Regression estimate when solving regression  task,
-                        vector of posterior probabilities for classification task.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpeprocess(mlpensemble ensemble,
-            double[] x,
-            ref double[] y)
-        {
-            int i = 0;
-            int es = 0;
-            int wc = 0;
-            int cc = 0;
-            double v = 0;
-            int i_ = 0;
-            int i1_ = 0;
-
-            if( ap.len(y)<ensemble.nout )
-            {
-                y = new double[ensemble.nout];
-            }
-            es = ensemble.ensemblesize;
-            wc = ensemble.wcount;
-            if( ensemble.issoftmax )
-            {
-                cc = ensemble.nin;
-            }
-            else
-            {
-                cc = ensemble.nin+ensemble.nout;
-            }
-            v = (double)1/(double)es;
-            for(i=0; i<=ensemble.nout-1; i++)
-            {
-                y[i] = 0;
-            }
-            for(i=0; i<=es-1; i++)
-            {
-                i1_ = (i*wc) - (0);
-                for(i_=0; i_<=wc-1;i_++)
-                {
-                    ensemble.tmpweights[i_] = ensemble.weights[i_+i1_];
-                }
-                i1_ = (i*cc) - (0);
-                for(i_=0; i_<=cc-1;i_++)
-                {
-                    ensemble.tmpmeans[i_] = ensemble.columnmeans[i_+i1_];
-                }
-                i1_ = (i*cc) - (0);
-                for(i_=0; i_<=cc-1;i_++)
-                {
-                    ensemble.tmpsigmas[i_] = ensemble.columnsigmas[i_+i1_];
-                }
-                mlpbase.mlpinternalprocessvector(ensemble.structinfo, ensemble.tmpweights, ensemble.tmpmeans, ensemble.tmpsigmas, ref ensemble.neurons, ref ensemble.dfdnet, x, ref ensemble.y);
-                for(i_=0; i_<=ensemble.nout-1;i_++)
-                {
-                    y[i_] = y[i_] + v*ensemble.y[i_];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        'interactive'  variant  of  MLPEProcess  for  languages  like Python which
-        support constructs like "Y = MLPEProcess(LM,X)" and interactive mode of the
-        interpreter
-
-        This function allocates new array on each call,  so  it  is  significantly
-        slower than its 'non-interactive' counterpart, but it is  more  convenient
-        when you call it from command line.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpeprocessi(mlpensemble ensemble,
-            double[] x,
-            ref double[] y)
-        {
-            y = new double[0];
-
-            mlpeprocess(ensemble, x, ref y);
-        }
-
-
-        /*************************************************************************
-        Relative classification error on the test set
-
-        INPUT PARAMETERS:
-            Ensemble-   ensemble
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            percent of incorrectly classified cases.
-            Works both for classifier betwork and for regression networks which
-        are used as classifiers.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double mlperelclserror(mlpensemble ensemble,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-            double relcls = 0;
-            double avgce = 0;
-            double rms = 0;
-            double avg = 0;
-            double avgrel = 0;
-
-            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
-            result = relcls;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Average cross-entropy (in bits per element) on the test set
-
-        INPUT PARAMETERS:
-            Ensemble-   ensemble
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            CrossEntropy/(NPoints*LN(2)).
-            Zero if ensemble solves regression task.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double mlpeavgce(mlpensemble ensemble,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-            double relcls = 0;
-            double avgce = 0;
-            double rms = 0;
-            double avg = 0;
-            double avgrel = 0;
-
-            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
-            result = avgce;
-            return result;
-        }
-
-
-        /*************************************************************************
-        RMS error on the test set
-
-        INPUT PARAMETERS:
-            Ensemble-   ensemble
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            root mean square error.
-            Its meaning for regression task is obvious. As for classification task
-        RMS error means error when estimating posterior probabilities.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double mlpermserror(mlpensemble ensemble,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-            double relcls = 0;
-            double avgce = 0;
-            double rms = 0;
-            double avg = 0;
-            double avgrel = 0;
-
-            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
-            result = rms;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Average error on the test set
-
-        INPUT PARAMETERS:
-            Ensemble-   ensemble
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            Its meaning for regression task is obvious. As for classification task
-        it means average error when estimating posterior probabilities.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double mlpeavgerror(mlpensemble ensemble,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-            double relcls = 0;
-            double avgce = 0;
-            double rms = 0;
-            double avg = 0;
-            double avgrel = 0;
-
-            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
-            result = avg;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Average relative error on the test set
-
-        INPUT PARAMETERS:
-            Ensemble-   ensemble
-            XY      -   test set
-            NPoints -   test set size
-
-        RESULT:
-            Its meaning for regression task is obvious. As for classification task
-        it means average relative error when estimating posterior probabilities.
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double mlpeavgrelerror(mlpensemble ensemble,
-            double[,] xy,
-            int npoints)
-        {
-            double result = 0;
-            double relcls = 0;
-            double avgce = 0;
-            double rms = 0;
-            double avg = 0;
-            double avgrel = 0;
-
-            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
-            result = avgrel;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Training neural networks ensemble using  bootstrap  aggregating (bagging).
-        Modified Levenberg-Marquardt algorithm is used as base training method.
-
-        INPUT PARAMETERS:
-            Ensemble    -   model with initialized geometry
-            XY          -   training set
-            NPoints     -   training set size
-            Decay       -   weight decay coefficient, >=0.001
-            Restarts    -   restarts, >0.
-
-        OUTPUT PARAMETERS:
-            Ensemble    -   trained model
-            Info        -   return code:
-                            * -2, if there is a point with class number
-                                  outside of [0..NClasses-1].
-                            * -1, if incorrect parameters was passed
-                                  (NPoints<0, Restarts<1).
-                            *  2, if task has been solved.
-            Rep         -   training report.
-            OOBErrors   -   out-of-bag generalization error estimate
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpebagginglm(mlpensemble ensemble,
-            double[,] xy,
-            int npoints,
-            double decay,
-            int restarts,
-            ref int info,
-            mlptrain.mlpreport rep,
-            mlptrain.mlpcvreport ooberrors)
-        {
-            info = 0;
-
-            mlpebagginginternal(ensemble, xy, npoints, decay, restarts, 0.0, 0, true, ref info, rep, ooberrors);
-        }
-
-
-        /*************************************************************************
-        Training neural networks ensemble using  bootstrap  aggregating (bagging).
-        L-BFGS algorithm is used as base training method.
-
-        INPUT PARAMETERS:
-            Ensemble    -   model with initialized geometry
-            XY          -   training set
-            NPoints     -   training set size
-            Decay       -   weight decay coefficient, >=0.001
-            Restarts    -   restarts, >0.
-            WStep       -   stopping criterion, same as in MLPTrainLBFGS
-            MaxIts      -   stopping criterion, same as in MLPTrainLBFGS
-
-        OUTPUT PARAMETERS:
-            Ensemble    -   trained model
-            Info        -   return code:
-                            * -8, if both WStep=0 and MaxIts=0
-                            * -2, if there is a point with class number
-                                  outside of [0..NClasses-1].
-                            * -1, if incorrect parameters was passed
-                                  (NPoints<0, Restarts<1).
-                            *  2, if task has been solved.
-            Rep         -   training report.
-            OOBErrors   -   out-of-bag generalization error estimate
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpebagginglbfgs(mlpensemble ensemble,
-            double[,] xy,
-            int npoints,
-            double decay,
-            int restarts,
-            double wstep,
-            int maxits,
-            ref int info,
-            mlptrain.mlpreport rep,
-            mlptrain.mlpcvreport ooberrors)
-        {
-            info = 0;
-
-            mlpebagginginternal(ensemble, xy, npoints, decay, restarts, wstep, maxits, false, ref info, rep, ooberrors);
-        }
-
-
-        /*************************************************************************
-        Training neural networks ensemble using early stopping.
-
-        INPUT PARAMETERS:
-            Ensemble    -   model with initialized geometry
-            XY          -   training set
-            NPoints     -   training set size
-            Decay       -   weight decay coefficient, >=0.001
-            Restarts    -   restarts, >0.
-
-        OUTPUT PARAMETERS:
-            Ensemble    -   trained model
-            Info        -   return code:
-                            * -2, if there is a point with class number
-                                  outside of [0..NClasses-1].
-                            * -1, if incorrect parameters was passed
-                                  (NPoints<0, Restarts<1).
-                            *  6, if task has been solved.
-            Rep         -   training report.
-            OOBErrors   -   out-of-bag generalization error estimate
-
-          -- ALGLIB --
-             Copyright 10.03.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mlpetraines(mlpensemble ensemble,
-            double[,] xy,
-            int npoints,
-            double decay,
-            int restarts,
-            ref int info,
-            mlptrain.mlpreport rep)
-        {
-            int i = 0;
-            int k = 0;
-            int ccount = 0;
-            int pcount = 0;
-            double[,] trnxy = new double[0,0];
-            double[,] valxy = new double[0,0];
-            int trnsize = 0;
-            int valsize = 0;
-            mlpbase.multilayerperceptron network = new mlpbase.multilayerperceptron();
-            int tmpinfo = 0;
-            mlptrain.mlpreport tmprep = new mlptrain.mlpreport();
-            int i_ = 0;
-            int i1_ = 0;
-
-            info = 0;
-
-            if( (npoints<2 | restarts<1) | (double)(decay)<(double)(0) )
-            {
-                info = -1;
-                return;
-            }
-            if( ensemble.issoftmax )
-            {
-                for(i=0; i<=npoints-1; i++)
-                {
-                    if( (int)Math.Round(xy[i,ensemble.nin])<0 | (int)Math.Round(xy[i,ensemble.nin])>=ensemble.nout )
-                    {
-                        info = -2;
-                        return;
-                    }
-                }
-            }
-            info = 6;
-            
-            //
-            // allocate
-            //
-            if( ensemble.issoftmax )
-            {
-                ccount = ensemble.nin+1;
-                pcount = ensemble.nin;
-            }
-            else
-            {
-                ccount = ensemble.nin+ensemble.nout;
-                pcount = ensemble.nin+ensemble.nout;
-            }
-            trnxy = new double[npoints-1+1, ccount-1+1];
-            valxy = new double[npoints-1+1, ccount-1+1];
-            mlpbase.mlpunserialize(ensemble.serializedmlp, network);
-            rep.ngrad = 0;
-            rep.nhess = 0;
-            rep.ncholesky = 0;
-            
-            //
-            // train networks
-            //
-            for(k=0; k<=ensemble.ensemblesize-1; k++)
-            {
-                
-                //
-                // Split set
-                //
-                do
-                {
-                    trnsize = 0;
-                    valsize = 0;
-                    for(i=0; i<=npoints-1; i++)
-                    {
-                        if( (double)(math.randomreal())<(double)(0.66) )
-                        {
-                            
-                            //
-                            // Assign sample to training set
-                            //
-                            for(i_=0; i_<=ccount-1;i_++)
-                            {
-                                trnxy[trnsize,i_] = xy[i,i_];
-                            }
-                            trnsize = trnsize+1;
-                        }
-                        else
-                        {
-                            
-                            //
-                            // Assign sample to validation set
-                            //
-                            for(i_=0; i_<=ccount-1;i_++)
-                            {
-                                valxy[valsize,i_] = xy[i,i_];
-                            }
-                            valsize = valsize+1;
-                        }
-                    }
-                }
-                while( !(trnsize!=0 & valsize!=0) );
-                
-                //
-                // Train
-                //
-                mlptrain.mlptraines(network, trnxy, trnsize, valxy, valsize, decay, restarts, ref tmpinfo, tmprep);
-                if( tmpinfo<0 )
-                {
-                    info = tmpinfo;
-                    return;
-                }
-                
-                //
-                // save results
-                //
-                i1_ = (0) - (k*ensemble.wcount);
-                for(i_=k*ensemble.wcount; i_<=(k+1)*ensemble.wcount-1;i_++)
-                {
-                    ensemble.weights[i_] = network.weights[i_+i1_];
-                }
-                i1_ = (0) - (k*pcount);
-                for(i_=k*pcount; i_<=(k+1)*pcount-1;i_++)
-                {
-                    ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
-                }
-                i1_ = (0) - (k*pcount);
-                for(i_=k*pcount; i_<=(k+1)*pcount-1;i_++)
-                {
-                    ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
-                }
-                rep.ngrad = rep.ngrad+tmprep.ngrad;
-                rep.nhess = rep.nhess+tmprep.nhess;
-                rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
-            }
-        }
-
-
-        /*************************************************************************
-        Calculation of all types of errors
-
-          -- ALGLIB --
-             Copyright 17.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        private static void mlpeallerrors(mlpensemble ensemble,
-            double[,] xy,
-            int npoints,
-            ref double relcls,
-            ref double avgce,
-            ref double rms,
-            ref double avg,
-            ref double avgrel)
-        {
-            int i = 0;
-            double[] buf = new double[0];
-            double[] workx = new double[0];
-            double[] y = new double[0];
-            double[] dy = new double[0];
-            int i_ = 0;
-            int i1_ = 0;
-
-            relcls = 0;
-            avgce = 0;
-            rms = 0;
-            avg = 0;
-            avgrel = 0;
-
-            workx = new double[ensemble.nin-1+1];
-            y = new double[ensemble.nout-1+1];
-            if( ensemble.issoftmax )
-            {
-                dy = new double[0+1];
-                bdss.dserrallocate(ensemble.nout, ref buf);
-            }
-            else
-            {
-                dy = new double[ensemble.nout-1+1];
-                bdss.dserrallocate(-ensemble.nout, ref buf);
-            }
-            for(i=0; i<=npoints-1; i++)
-            {
-                for(i_=0; i_<=ensemble.nin-1;i_++)
-                {
-                    workx[i_] = xy[i,i_];
-                }
-                mlpeprocess(ensemble, workx, ref y);
-                if( ensemble.issoftmax )
-                {
-                    dy[0] = xy[i,ensemble.nin];
-                }
-                else
-                {
-                    i1_ = (ensemble.nin) - (0);
-                    for(i_=0; i_<=ensemble.nout-1;i_++)
-                    {
-                        dy[i_] = xy[i,i_+i1_];
-                    }
-                }
-                bdss.dserraccumulate(ref buf, y, dy);
-            }
-            bdss.dserrfinish(ref buf);
-            relcls = buf[0];
-            avgce = buf[1];
-            rms = buf[2];
-            avg = buf[3];
-            avgrel = buf[4];
-        }
-
-
-        /*************************************************************************
-        Internal bagging subroutine.
-
-          -- ALGLIB --
-             Copyright 19.02.2009 by Bochkanov Sergey
-        *************************************************************************/
-        private static void mlpebagginginternal(mlpensemble ensemble,
-            double[,] xy,
-            int npoints,
-            double decay,
-            int restarts,
-            double wstep,
-            int maxits,
-            bool lmalgorithm,
-            ref int info,
-            mlptrain.mlpreport rep,
-            mlptrain.mlpcvreport ooberrors)
-        {
-            double[,] xys = new double[0,0];
-            bool[] s = new bool[0];
-            double[,] oobbuf = new double[0,0];
-            int[] oobcntbuf = new int[0];
-            double[] x = new double[0];
-            double[] y = new double[0];
-            double[] dy = new double[0];
-            double[] dsbuf = new double[0];
-            int nin = 0;
-            int nout = 0;
-            int ccnt = 0;
-            int pcnt = 0;
-            int i = 0;
-            int j = 0;
-            int k = 0;
-            double v = 0;
-            mlptrain.mlpreport tmprep = new mlptrain.mlpreport();
-            mlpbase.multilayerperceptron network = new mlpbase.multilayerperceptron();
-            int i_ = 0;
-            int i1_ = 0;
-
-            info = 0;
-
-            
-            //
-            // Test for inputs
-            //
-            if( (!lmalgorithm & (double)(wstep)==(double)(0)) & maxits==0 )
-            {
-                info = -8;
-                return;
-            }
-            if( ((npoints<=0 | restarts<1) | (double)(wstep)<(double)(0)) | maxits<0 )
-            {
-                info = -1;
-                return;
-            }
-            if( ensemble.issoftmax )
-            {
-                for(i=0; i<=npoints-1; i++)
-                {
-                    if( (int)Math.Round(xy[i,ensemble.nin])<0 | (int)Math.Round(xy[i,ensemble.nin])>=ensemble.nout )
-                    {
-                        info = -2;
-                        return;
-                    }
-                }
-            }
-            
-            //
-            // allocate temporaries
-            //
-            info = 2;
-            rep.ngrad = 0;
-            rep.nhess = 0;
-            rep.ncholesky = 0;
-            ooberrors.relclserror = 0;
-            ooberrors.avgce = 0;
-            ooberrors.rmserror = 0;
-            ooberrors.avgerror = 0;
-            ooberrors.avgrelerror = 0;
-            nin = ensemble.nin;
-            nout = ensemble.nout;
-            if( ensemble.issoftmax )
-            {
-                ccnt = nin+1;
-                pcnt = nin;
-            }
-            else
-            {
-                ccnt = nin+nout;
-                pcnt = nin+nout;
-            }
-            xys = new double[npoints-1+1, ccnt-1+1];
-            s = new bool[npoints-1+1];
-            oobbuf = new double[npoints-1+1, nout-1+1];
-            oobcntbuf = new int[npoints-1+1];
-            x = new double[nin-1+1];
-            y = new double[nout-1+1];
-            if( ensemble.issoftmax )
-            {
-                dy = new double[0+1];
-            }
-            else
-            {
-                dy = new double[nout-1+1];
-            }
-            for(i=0; i<=npoints-1; i++)
-            {
-                for(j=0; j<=nout-1; j++)
-                {
-                    oobbuf[i,j] = 0;
-                }
-            }
-            for(i=0; i<=npoints-1; i++)
-            {
-                oobcntbuf[i] = 0;
-            }
-            mlpbase.mlpunserialize(ensemble.serializedmlp, network);
-            
-            //
-            // main bagging cycle
-            //
-            for(k=0; k<=ensemble.ensemblesize-1; k++)
-            {
-                
-                //
-                // prepare dataset
-                //
-                for(i=0; i<=npoints-1; i++)
-                {
-                    s[i] = false;
-                }
-                for(i=0; i<=npoints-1; i++)
-                {
-                    j = math.randominteger(npoints);
-                    s[j] = true;
-                    for(i_=0; i_<=ccnt-1;i_++)
-                    {
-                        xys[i,i_] = xy[j,i_];
-                    }
-                }
-                
-                //
-                // train
-                //
-                if( lmalgorithm )
-                {
-                    mlptrain.mlptrainlm(network, xys, npoints, decay, restarts, ref info, tmprep);
-                }
-                else
-                {
-                    mlptrain.mlptrainlbfgs(network, xys, npoints, decay, restarts, wstep, maxits, ref info, tmprep);
-                }
-                if( info<0 )
-                {
-                    return;
-                }
-                
-                //
-                // save results
-                //
-                rep.ngrad = rep.ngrad+tmprep.ngrad;
-                rep.nhess = rep.nhess+tmprep.nhess;
-                rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
-                i1_ = (0) - (k*ensemble.wcount);
-                for(i_=k*ensemble.wcount; i_<=(k+1)*ensemble.wcount-1;i_++)
-                {
-                    ensemble.weights[i_] = network.weights[i_+i1_];
-                }
-                i1_ = (0) - (k*pcnt);
-                for(i_=k*pcnt; i_<=(k+1)*pcnt-1;i_++)
-                {
-                    ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
-                }
-                i1_ = (0) - (k*pcnt);
-                for(i_=k*pcnt; i_<=(k+1)*pcnt-1;i_++)
-                {
-                    ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
-                }
-                
-                //
-                // OOB estimates
-                //
-                for(i=0; i<=npoints-1; i++)
-                {
-                    if( !s[i] )
-                    {
-                        for(i_=0; i_<=nin-1;i_++)
-                        {
-                            x[i_] = xy[i,i_];
-                        }
-                        mlpbase.mlpprocess(network, x, ref y);
-                        for(i_=0; i_<=nout-1;i_++)
-                        {
-                            oobbuf[i,i_] = oobbuf[i,i_] + y[i_];
-                        }
-                        oobcntbuf[i] = oobcntbuf[i]+1;
-                    }
-                }
-            }
-            
-            //
-            // OOB estimates
-            //
-            if( ensemble.issoftmax )
-            {
-                bdss.dserrallocate(nout, ref dsbuf);
-            }
-            else
-            {
-                bdss.dserrallocate(-nout, ref dsbuf);
-            }
-            for(i=0; i<=npoints-1; i++)
-            {
-                if( oobcntbuf[i]!=0 )
-                {
-                    v = (double)1/(double)oobcntbuf[i];
-                    for(i_=0; i_<=nout-1;i_++)
-                    {
-                        y[i_] = v*oobbuf[i,i_];
-                    }
-                    if( ensemble.issoftmax )
-                    {
-                        dy[0] = xy[i,nin];
-                    }
-                    else
-                    {
-                        i1_ = (nin) - (0);
-                        for(i_=0; i_<=nout-1;i_++)
-                        {
-                            dy[i_] = v*xy[i,i_+i1_];
-                        }
-                    }
-                    bdss.dserraccumulate(ref dsbuf, y, dy);
-                }
-            }
-            bdss.dserrfinish(ref dsbuf);
-            ooberrors.relclserror = dsbuf[0];
-            ooberrors.avgce = dsbuf[1];
-            ooberrors.rmserror = dsbuf[2];
-            ooberrors.avgerror = dsbuf[3];
-            ooberrors.avgrelerror = dsbuf[4];
+
+            ap.assert(n>=1, "MCPDCreate: N<1");
+            s.n = n;
+            s.states = new int[n];
+            for(i=0; i<=n-1; i++)
+            {
+                s.states[i] = 0;
+            }
+            if( entrystate>=0 )
+            {
+                s.states[entrystate] = 1;
+            }
+            if( exitstate>=0 )
+            {
+                s.states[exitstate] = -1;
+            }
+            s.npairs = 0;
+            s.regterm = 1.0E-8;
+            s.ccnt = 0;
+            s.p = new double[n, n];
+            s.ec = new double[n, n];
+            s.bndl = new double[n, n];
+            s.bndu = new double[n, n];
+            s.pw = new double[n];
+            s.priorp = new double[n, n];
+            s.tmpp = new double[n*n];
+            s.effectivew = new double[n];
+            s.effectivebndl = new double[n*n];
+            s.effectivebndu = new double[n*n];
+            s.h = new double[n*n];
+            for(i=0; i<=n-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    s.p[i,j] = 0.0;
+                    s.priorp[i,j] = 0.0;
+                    s.bndl[i,j] = Double.NegativeInfinity;
+                    s.bndu[i,j] = Double.PositiveInfinity;
+                    s.ec[i,j] = Double.NaN;
+                }
+                s.pw[i] = 0.0;
+                s.priorp[i,i] = 1.0;
+            }
+            s.data = new double[1, 2*n];
+            for(i=0; i<=2*n-1; i++)
+            {
+                s.data[0,i] = 0.0;
+            }
+            for(i=0; i<=n*n-1; i++)
+            {
+                s.tmpp[i] = 0.0;
+            }
+            minbleic.minbleiccreate(n*n, s.tmpp, s.bs);
         }
 
@@ -14802,5 +17187,5 @@
                         wt[i] = 0;
                     }
-                    minlbfgs.minlbfgscreatex(wcount, wcount, wt, 1, state);
+                    minlbfgs.minlbfgscreatex(wcount, wcount, wt, 1, 0.0, state);
                     minlbfgs.minlbfgssetcond(state, 0, 0, 0, 5);
                     while( minlbfgs.minlbfgsiteration(state) )
@@ -15269,7 +17654,7 @@
                     if( state.xupdated )
                     {
-                        minlbfgs.minlbfgsresults(state, ref w, internalrep);
-                        for (i_ = 0; i_ <= wcount - 1; i_++) {
-                          network.weights[i_] = w[i_];
+                        for(i_=0; i_<=wcount-1;i_++)
+                        {
+                            network.weights[i_] = w[i_];
                         }
                         e = mlpbase.mlperror(network, valxy, valsize);
@@ -15660,4 +18045,1550 @@
 
     }
+    public class mlpe
+    {
+        /*************************************************************************
+        Neural networks ensemble
+        *************************************************************************/
+        public class mlpensemble
+        {
+            public int[] structinfo;
+            public int ensemblesize;
+            public int nin;
+            public int nout;
+            public int wcount;
+            public bool issoftmax;
+            public bool postprocessing;
+            public double[] weights;
+            public double[] columnmeans;
+            public double[] columnsigmas;
+            public int serializedlen;
+            public double[] serializedmlp;
+            public double[] tmpweights;
+            public double[] tmpmeans;
+            public double[] tmpsigmas;
+            public double[] neurons;
+            public double[] dfdnet;
+            public double[] y;
+            public mlpensemble()
+            {
+                structinfo = new int[0];
+                weights = new double[0];
+                columnmeans = new double[0];
+                columnsigmas = new double[0];
+                serializedmlp = new double[0];
+                tmpweights = new double[0];
+                tmpmeans = new double[0];
+                tmpsigmas = new double[0];
+                neurons = new double[0];
+                dfdnet = new double[0];
+                y = new double[0];
+            }
+        };
+
+
+
+
+        public const int mlpntotaloffset = 3;
+        public const int mlpevnum = 9;
+
+
+        /*************************************************************************
+        Like MLPCreate0, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreate0(int nin,
+            int nout,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreate0(nin, nout, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreate1, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreate1(int nin,
+            int nhid,
+            int nout,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreate1(nin, nhid, nout, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreate2, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreate2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreate2(nin, nhid1, nhid2, nout, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateB0, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreateb0(int nin,
+            int nout,
+            double b,
+            double d,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreateb0(nin, nout, b, d, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateB1, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreateb1(int nin,
+            int nhid,
+            int nout,
+            double b,
+            double d,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreateb1(nin, nhid, nout, b, d, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateB2, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreateb2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            double b,
+            double d,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreateb2(nin, nhid1, nhid2, nout, b, d, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateR0, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreater0(int nin,
+            int nout,
+            double a,
+            double b,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreater0(nin, nout, a, b, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateR1, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreater1(int nin,
+            int nhid,
+            int nout,
+            double a,
+            double b,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreater1(nin, nhid, nout, a, b, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateR2, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreater2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            double a,
+            double b,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreater2(nin, nhid1, nhid2, nout, a, b, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateC0, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreatec0(int nin,
+            int nout,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreatec0(nin, nout, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateC1, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreatec1(int nin,
+            int nhid,
+            int nout,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreatec1(nin, nhid, nout, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Like MLPCreateC2, but for ensembles.
+
+          -- ALGLIB --
+             Copyright 18.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreatec2(int nin,
+            int nhid1,
+            int nhid2,
+            int nout,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
+
+            mlpbase.mlpcreatec2(nin, nhid1, nhid2, nout, net);
+            mlpecreatefromnetwork(net, ensemblesize, ensemble);
+        }
+
+
+        /*************************************************************************
+        Creates ensemble from network. Only network geometry is copied.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecreatefromnetwork(mlpbase.multilayerperceptron network,
+            int ensemblesize,
+            mlpensemble ensemble)
+        {
+            int i = 0;
+            int ccount = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            ap.assert(ensemblesize>0, "MLPECreate: incorrect ensemble size!");
+            
+            //
+            // network properties
+            //
+            mlpbase.mlpproperties(network, ref ensemble.nin, ref ensemble.nout, ref ensemble.wcount);
+            if( mlpbase.mlpissoftmax(network) )
+            {
+                ccount = ensemble.nin;
+            }
+            else
+            {
+                ccount = ensemble.nin+ensemble.nout;
+            }
+            ensemble.postprocessing = false;
+            ensemble.issoftmax = mlpbase.mlpissoftmax(network);
+            ensemble.ensemblesize = ensemblesize;
+            
+            //
+            // structure information
+            //
+            ensemble.structinfo = new int[network.structinfo[0]-1+1];
+            for(i=0; i<=network.structinfo[0]-1; i++)
+            {
+                ensemble.structinfo[i] = network.structinfo[i];
+            }
+            
+            //
+            // weights, means, sigmas
+            //
+            ensemble.weights = new double[ensemblesize*ensemble.wcount-1+1];
+            ensemble.columnmeans = new double[ensemblesize*ccount-1+1];
+            ensemble.columnsigmas = new double[ensemblesize*ccount-1+1];
+            for(i=0; i<=ensemblesize*ensemble.wcount-1; i++)
+            {
+                ensemble.weights[i] = math.randomreal()-0.5;
+            }
+            for(i=0; i<=ensemblesize-1; i++)
+            {
+                i1_ = (0) - (i*ccount);
+                for(i_=i*ccount; i_<=(i+1)*ccount-1;i_++)
+                {
+                    ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
+                }
+                i1_ = (0) - (i*ccount);
+                for(i_=i*ccount; i_<=(i+1)*ccount-1;i_++)
+                {
+                    ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
+                }
+            }
+            
+            //
+            // serialized part
+            //
+            mlpbase.mlpserializeold(network, ref ensemble.serializedmlp, ref ensemble.serializedlen);
+            
+            //
+            // temporaries, internal buffers
+            //
+            ensemble.tmpweights = new double[ensemble.wcount-1+1];
+            ensemble.tmpmeans = new double[ccount-1+1];
+            ensemble.tmpsigmas = new double[ccount-1+1];
+            ensemble.neurons = new double[ensemble.structinfo[mlpntotaloffset]-1+1];
+            ensemble.dfdnet = new double[ensemble.structinfo[mlpntotaloffset]-1+1];
+            ensemble.y = new double[ensemble.nout-1+1];
+        }
+
+
+        /*************************************************************************
+        Copying of MLPEnsemble strucure
+
+        INPUT PARAMETERS:
+            Ensemble1 -   original
+
+        OUTPUT PARAMETERS:
+            Ensemble2 -   copy
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpecopy(mlpensemble ensemble1,
+            mlpensemble ensemble2)
+        {
+            int i = 0;
+            int ssize = 0;
+            int ccount = 0;
+            int ntotal = 0;
+            int i_ = 0;
+
+            
+            //
+            // Unload info
+            //
+            ssize = ensemble1.structinfo[0];
+            if( ensemble1.issoftmax )
+            {
+                ccount = ensemble1.nin;
+            }
+            else
+            {
+                ccount = ensemble1.nin+ensemble1.nout;
+            }
+            ntotal = ensemble1.structinfo[mlpntotaloffset];
+            
+            //
+            // Allocate space
+            //
+            ensemble2.structinfo = new int[ssize-1+1];
+            ensemble2.weights = new double[ensemble1.ensemblesize*ensemble1.wcount-1+1];
+            ensemble2.columnmeans = new double[ensemble1.ensemblesize*ccount-1+1];
+            ensemble2.columnsigmas = new double[ensemble1.ensemblesize*ccount-1+1];
+            ensemble2.tmpweights = new double[ensemble1.wcount-1+1];
+            ensemble2.tmpmeans = new double[ccount-1+1];
+            ensemble2.tmpsigmas = new double[ccount-1+1];
+            ensemble2.serializedmlp = new double[ensemble1.serializedlen-1+1];
+            ensemble2.neurons = new double[ntotal-1+1];
+            ensemble2.dfdnet = new double[ntotal-1+1];
+            ensemble2.y = new double[ensemble1.nout-1+1];
+            
+            //
+            // Copy
+            //
+            ensemble2.nin = ensemble1.nin;
+            ensemble2.nout = ensemble1.nout;
+            ensemble2.wcount = ensemble1.wcount;
+            ensemble2.ensemblesize = ensemble1.ensemblesize;
+            ensemble2.issoftmax = ensemble1.issoftmax;
+            ensemble2.postprocessing = ensemble1.postprocessing;
+            ensemble2.serializedlen = ensemble1.serializedlen;
+            for(i=0; i<=ssize-1; i++)
+            {
+                ensemble2.structinfo[i] = ensemble1.structinfo[i];
+            }
+            for(i_=0; i_<=ensemble1.ensemblesize*ensemble1.wcount-1;i_++)
+            {
+                ensemble2.weights[i_] = ensemble1.weights[i_];
+            }
+            for(i_=0; i_<=ensemble1.ensemblesize*ccount-1;i_++)
+            {
+                ensemble2.columnmeans[i_] = ensemble1.columnmeans[i_];
+            }
+            for(i_=0; i_<=ensemble1.ensemblesize*ccount-1;i_++)
+            {
+                ensemble2.columnsigmas[i_] = ensemble1.columnsigmas[i_];
+            }
+            for(i_=0; i_<=ensemble1.serializedlen-1;i_++)
+            {
+                ensemble2.serializedmlp[i_] = ensemble1.serializedmlp[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        Serialization of MLPEnsemble strucure
+
+        INPUT PARAMETERS:
+            Ensemble-   original
+
+        OUTPUT PARAMETERS:
+            RA      -   array of real numbers which stores ensemble,
+                        array[0..RLen-1]
+            RLen    -   RA lenght
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpeserialize(mlpensemble ensemble,
+            ref double[] ra,
+            ref int rlen)
+        {
+            int i = 0;
+            int ssize = 0;
+            int ntotal = 0;
+            int ccount = 0;
+            int hsize = 0;
+            int offs = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            ra = new double[0];
+            rlen = 0;
+
+            hsize = 13;
+            ssize = ensemble.structinfo[0];
+            if( ensemble.issoftmax )
+            {
+                ccount = ensemble.nin;
+            }
+            else
+            {
+                ccount = ensemble.nin+ensemble.nout;
+            }
+            ntotal = ensemble.structinfo[mlpntotaloffset];
+            rlen = hsize+ssize+ensemble.ensemblesize*ensemble.wcount+2*ccount*ensemble.ensemblesize+ensemble.serializedlen;
+            
+            //
+            //  RA format:
+            //  [0]     RLen
+            //  [1]     Version (MLPEVNum)
+            //  [2]     EnsembleSize
+            //  [3]     NIn
+            //  [4]     NOut
+            //  [5]     WCount
+            //  [6]     IsSoftmax 0/1
+            //  [7]     PostProcessing 0/1
+            //  [8]     sizeof(StructInfo)
+            //  [9]     NTotal (sizeof(Neurons), sizeof(DFDNET))
+            //  [10]    CCount (sizeof(ColumnMeans), sizeof(ColumnSigmas))
+            //  [11]    data offset
+            //  [12]    SerializedLen
+            //
+            //  [..]    StructInfo
+            //  [..]    Weights
+            //  [..]    ColumnMeans
+            //  [..]    ColumnSigmas
+            //
+            ra = new double[rlen-1+1];
+            ra[0] = rlen;
+            ra[1] = mlpevnum;
+            ra[2] = ensemble.ensemblesize;
+            ra[3] = ensemble.nin;
+            ra[4] = ensemble.nout;
+            ra[5] = ensemble.wcount;
+            if( ensemble.issoftmax )
+            {
+                ra[6] = 1;
+            }
+            else
+            {
+                ra[6] = 0;
+            }
+            if( ensemble.postprocessing )
+            {
+                ra[7] = 1;
+            }
+            else
+            {
+                ra[7] = 9;
+            }
+            ra[8] = ssize;
+            ra[9] = ntotal;
+            ra[10] = ccount;
+            ra[11] = hsize;
+            ra[12] = ensemble.serializedlen;
+            offs = hsize;
+            for(i=offs; i<=offs+ssize-1; i++)
+            {
+                ra[i] = ensemble.structinfo[i-offs];
+            }
+            offs = offs+ssize;
+            i1_ = (0) - (offs);
+            for(i_=offs; i_<=offs+ensemble.ensemblesize*ensemble.wcount-1;i_++)
+            {
+                ra[i_] = ensemble.weights[i_+i1_];
+            }
+            offs = offs+ensemble.ensemblesize*ensemble.wcount;
+            i1_ = (0) - (offs);
+            for(i_=offs; i_<=offs+ensemble.ensemblesize*ccount-1;i_++)
+            {
+                ra[i_] = ensemble.columnmeans[i_+i1_];
+            }
+            offs = offs+ensemble.ensemblesize*ccount;
+            i1_ = (0) - (offs);
+            for(i_=offs; i_<=offs+ensemble.ensemblesize*ccount-1;i_++)
+            {
+                ra[i_] = ensemble.columnsigmas[i_+i1_];
+            }
+            offs = offs+ensemble.ensemblesize*ccount;
+            i1_ = (0) - (offs);
+            for(i_=offs; i_<=offs+ensemble.serializedlen-1;i_++)
+            {
+                ra[i_] = ensemble.serializedmlp[i_+i1_];
+            }
+            offs = offs+ensemble.serializedlen;
+        }
+
+
+        /*************************************************************************
+        Unserialization of MLPEnsemble strucure
+
+        INPUT PARAMETERS:
+            RA      -   real array which stores ensemble
+
+        OUTPUT PARAMETERS:
+            Ensemble-   restored structure
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpeunserialize(double[] ra,
+            mlpensemble ensemble)
+        {
+            int i = 0;
+            int ssize = 0;
+            int ntotal = 0;
+            int ccount = 0;
+            int hsize = 0;
+            int offs = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            ap.assert((int)Math.Round(ra[1])==mlpevnum, "MLPEUnserialize: incorrect array!");
+            
+            //
+            // load info
+            //
+            hsize = 13;
+            ensemble.ensemblesize = (int)Math.Round(ra[2]);
+            ensemble.nin = (int)Math.Round(ra[3]);
+            ensemble.nout = (int)Math.Round(ra[4]);
+            ensemble.wcount = (int)Math.Round(ra[5]);
+            ensemble.issoftmax = (int)Math.Round(ra[6])==1;
+            ensemble.postprocessing = (int)Math.Round(ra[7])==1;
+            ssize = (int)Math.Round(ra[8]);
+            ntotal = (int)Math.Round(ra[9]);
+            ccount = (int)Math.Round(ra[10]);
+            offs = (int)Math.Round(ra[11]);
+            ensemble.serializedlen = (int)Math.Round(ra[12]);
+            
+            //
+            //  Allocate arrays
+            //
+            ensemble.structinfo = new int[ssize-1+1];
+            ensemble.weights = new double[ensemble.ensemblesize*ensemble.wcount-1+1];
+            ensemble.columnmeans = new double[ensemble.ensemblesize*ccount-1+1];
+            ensemble.columnsigmas = new double[ensemble.ensemblesize*ccount-1+1];
+            ensemble.tmpweights = new double[ensemble.wcount-1+1];
+            ensemble.tmpmeans = new double[ccount-1+1];
+            ensemble.tmpsigmas = new double[ccount-1+1];
+            ensemble.neurons = new double[ntotal-1+1];
+            ensemble.dfdnet = new double[ntotal-1+1];
+            ensemble.serializedmlp = new double[ensemble.serializedlen-1+1];
+            ensemble.y = new double[ensemble.nout-1+1];
+            
+            //
+            // load data
+            //
+            for(i=offs; i<=offs+ssize-1; i++)
+            {
+                ensemble.structinfo[i-offs] = (int)Math.Round(ra[i]);
+            }
+            offs = offs+ssize;
+            i1_ = (offs) - (0);
+            for(i_=0; i_<=ensemble.ensemblesize*ensemble.wcount-1;i_++)
+            {
+                ensemble.weights[i_] = ra[i_+i1_];
+            }
+            offs = offs+ensemble.ensemblesize*ensemble.wcount;
+            i1_ = (offs) - (0);
+            for(i_=0; i_<=ensemble.ensemblesize*ccount-1;i_++)
+            {
+                ensemble.columnmeans[i_] = ra[i_+i1_];
+            }
+            offs = offs+ensemble.ensemblesize*ccount;
+            i1_ = (offs) - (0);
+            for(i_=0; i_<=ensemble.ensemblesize*ccount-1;i_++)
+            {
+                ensemble.columnsigmas[i_] = ra[i_+i1_];
+            }
+            offs = offs+ensemble.ensemblesize*ccount;
+            i1_ = (offs) - (0);
+            for(i_=0; i_<=ensemble.serializedlen-1;i_++)
+            {
+                ensemble.serializedmlp[i_] = ra[i_+i1_];
+            }
+            offs = offs+ensemble.serializedlen;
+        }
+
+
+        /*************************************************************************
+        Randomization of MLP ensemble
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlperandomize(mlpensemble ensemble)
+        {
+            int i = 0;
+
+            for(i=0; i<=ensemble.ensemblesize*ensemble.wcount-1; i++)
+            {
+                ensemble.weights[i] = math.randomreal()-0.5;
+            }
+        }
+
+
+        /*************************************************************************
+        Return ensemble properties (number of inputs and outputs).
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpeproperties(mlpensemble ensemble,
+            ref int nin,
+            ref int nout)
+        {
+            nin = 0;
+            nout = 0;
+
+            nin = ensemble.nin;
+            nout = ensemble.nout;
+        }
+
+
+        /*************************************************************************
+        Return normalization type (whether ensemble is SOFTMAX-normalized or not).
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool mlpeissoftmax(mlpensemble ensemble)
+        {
+            bool result = new bool();
+
+            result = ensemble.issoftmax;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Procesing
+
+        INPUT PARAMETERS:
+            Ensemble-   neural networks ensemble
+            X       -   input vector,  array[0..NIn-1].
+            Y       -   (possibly) preallocated buffer; if size of Y is less than
+                        NOut, it will be reallocated. If it is large enough, it
+                        is NOT reallocated, so we can save some time on reallocation.
+
+
+        OUTPUT PARAMETERS:
+            Y       -   result. Regression estimate when solving regression  task,
+                        vector of posterior probabilities for classification task.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpeprocess(mlpensemble ensemble,
+            double[] x,
+            ref double[] y)
+        {
+            int i = 0;
+            int es = 0;
+            int wc = 0;
+            int cc = 0;
+            double v = 0;
+            int i_ = 0;
+            int i1_ = 0;
+
+            if( ap.len(y)<ensemble.nout )
+            {
+                y = new double[ensemble.nout];
+            }
+            es = ensemble.ensemblesize;
+            wc = ensemble.wcount;
+            if( ensemble.issoftmax )
+            {
+                cc = ensemble.nin;
+            }
+            else
+            {
+                cc = ensemble.nin+ensemble.nout;
+            }
+            v = (double)1/(double)es;
+            for(i=0; i<=ensemble.nout-1; i++)
+            {
+                y[i] = 0;
+            }
+            for(i=0; i<=es-1; i++)
+            {
+                i1_ = (i*wc) - (0);
+                for(i_=0; i_<=wc-1;i_++)
+                {
+                    ensemble.tmpweights[i_] = ensemble.weights[i_+i1_];
+                }
+                i1_ = (i*cc) - (0);
+                for(i_=0; i_<=cc-1;i_++)
+                {
+                    ensemble.tmpmeans[i_] = ensemble.columnmeans[i_+i1_];
+                }
+                i1_ = (i*cc) - (0);
+                for(i_=0; i_<=cc-1;i_++)
+                {
+                    ensemble.tmpsigmas[i_] = ensemble.columnsigmas[i_+i1_];
+                }
+                mlpbase.mlpinternalprocessvector(ensemble.structinfo, ensemble.tmpweights, ensemble.tmpmeans, ensemble.tmpsigmas, ref ensemble.neurons, ref ensemble.dfdnet, x, ref ensemble.y);
+                for(i_=0; i_<=ensemble.nout-1;i_++)
+                {
+                    y[i_] = y[i_] + v*ensemble.y[i_];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        'interactive'  variant  of  MLPEProcess  for  languages  like Python which
+        support constructs like "Y = MLPEProcess(LM,X)" and interactive mode of the
+        interpreter
+
+        This function allocates new array on each call,  so  it  is  significantly
+        slower than its 'non-interactive' counterpart, but it is  more  convenient
+        when you call it from command line.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpeprocessi(mlpensemble ensemble,
+            double[] x,
+            ref double[] y)
+        {
+            y = new double[0];
+
+            mlpeprocess(ensemble, x, ref y);
+        }
+
+
+        /*************************************************************************
+        Relative classification error on the test set
+
+        INPUT PARAMETERS:
+            Ensemble-   ensemble
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            percent of incorrectly classified cases.
+            Works both for classifier betwork and for regression networks which
+        are used as classifiers.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlperelclserror(mlpensemble ensemble,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            double relcls = 0;
+            double avgce = 0;
+            double rms = 0;
+            double avg = 0;
+            double avgrel = 0;
+
+            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
+            result = relcls;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Average cross-entropy (in bits per element) on the test set
+
+        INPUT PARAMETERS:
+            Ensemble-   ensemble
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            CrossEntropy/(NPoints*LN(2)).
+            Zero if ensemble solves regression task.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpeavgce(mlpensemble ensemble,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            double relcls = 0;
+            double avgce = 0;
+            double rms = 0;
+            double avg = 0;
+            double avgrel = 0;
+
+            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
+            result = avgce;
+            return result;
+        }
+
+
+        /*************************************************************************
+        RMS error on the test set
+
+        INPUT PARAMETERS:
+            Ensemble-   ensemble
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            root mean square error.
+            Its meaning for regression task is obvious. As for classification task
+        RMS error means error when estimating posterior probabilities.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpermserror(mlpensemble ensemble,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            double relcls = 0;
+            double avgce = 0;
+            double rms = 0;
+            double avg = 0;
+            double avgrel = 0;
+
+            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
+            result = rms;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Average error on the test set
+
+        INPUT PARAMETERS:
+            Ensemble-   ensemble
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            Its meaning for regression task is obvious. As for classification task
+        it means average error when estimating posterior probabilities.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpeavgerror(mlpensemble ensemble,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            double relcls = 0;
+            double avgce = 0;
+            double rms = 0;
+            double avg = 0;
+            double avgrel = 0;
+
+            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
+            result = avg;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Average relative error on the test set
+
+        INPUT PARAMETERS:
+            Ensemble-   ensemble
+            XY      -   test set
+            NPoints -   test set size
+
+        RESULT:
+            Its meaning for regression task is obvious. As for classification task
+        it means average relative error when estimating posterior probabilities.
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double mlpeavgrelerror(mlpensemble ensemble,
+            double[,] xy,
+            int npoints)
+        {
+            double result = 0;
+            double relcls = 0;
+            double avgce = 0;
+            double rms = 0;
+            double avg = 0;
+            double avgrel = 0;
+
+            mlpeallerrors(ensemble, xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
+            result = avgrel;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Training neural networks ensemble using  bootstrap  aggregating (bagging).
+        Modified Levenberg-Marquardt algorithm is used as base training method.
+
+        INPUT PARAMETERS:
+            Ensemble    -   model with initialized geometry
+            XY          -   training set
+            NPoints     -   training set size
+            Decay       -   weight decay coefficient, >=0.001
+            Restarts    -   restarts, >0.
+
+        OUTPUT PARAMETERS:
+            Ensemble    -   trained model
+            Info        -   return code:
+                            * -2, if there is a point with class number
+                                  outside of [0..NClasses-1].
+                            * -1, if incorrect parameters was passed
+                                  (NPoints<0, Restarts<1).
+                            *  2, if task has been solved.
+            Rep         -   training report.
+            OOBErrors   -   out-of-bag generalization error estimate
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpebagginglm(mlpensemble ensemble,
+            double[,] xy,
+            int npoints,
+            double decay,
+            int restarts,
+            ref int info,
+            mlptrain.mlpreport rep,
+            mlptrain.mlpcvreport ooberrors)
+        {
+            info = 0;
+
+            mlpebagginginternal(ensemble, xy, npoints, decay, restarts, 0.0, 0, true, ref info, rep, ooberrors);
+        }
+
+
+        /*************************************************************************
+        Training neural networks ensemble using  bootstrap  aggregating (bagging).
+        L-BFGS algorithm is used as base training method.
+
+        INPUT PARAMETERS:
+            Ensemble    -   model with initialized geometry
+            XY          -   training set
+            NPoints     -   training set size
+            Decay       -   weight decay coefficient, >=0.001
+            Restarts    -   restarts, >0.
+            WStep       -   stopping criterion, same as in MLPTrainLBFGS
+            MaxIts      -   stopping criterion, same as in MLPTrainLBFGS
+
+        OUTPUT PARAMETERS:
+            Ensemble    -   trained model
+            Info        -   return code:
+                            * -8, if both WStep=0 and MaxIts=0
+                            * -2, if there is a point with class number
+                                  outside of [0..NClasses-1].
+                            * -1, if incorrect parameters was passed
+                                  (NPoints<0, Restarts<1).
+                            *  2, if task has been solved.
+            Rep         -   training report.
+            OOBErrors   -   out-of-bag generalization error estimate
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpebagginglbfgs(mlpensemble ensemble,
+            double[,] xy,
+            int npoints,
+            double decay,
+            int restarts,
+            double wstep,
+            int maxits,
+            ref int info,
+            mlptrain.mlpreport rep,
+            mlptrain.mlpcvreport ooberrors)
+        {
+            info = 0;
+
+            mlpebagginginternal(ensemble, xy, npoints, decay, restarts, wstep, maxits, false, ref info, rep, ooberrors);
+        }
+
+
+        /*************************************************************************
+        Training neural networks ensemble using early stopping.
+
+        INPUT PARAMETERS:
+            Ensemble    -   model with initialized geometry
+            XY          -   training set
+            NPoints     -   training set size
+            Decay       -   weight decay coefficient, >=0.001
+            Restarts    -   restarts, >0.
+
+        OUTPUT PARAMETERS:
+            Ensemble    -   trained model
+            Info        -   return code:
+                            * -2, if there is a point with class number
+                                  outside of [0..NClasses-1].
+                            * -1, if incorrect parameters was passed
+                                  (NPoints<0, Restarts<1).
+                            *  6, if task has been solved.
+            Rep         -   training report.
+            OOBErrors   -   out-of-bag generalization error estimate
+
+          -- ALGLIB --
+             Copyright 10.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mlpetraines(mlpensemble ensemble,
+            double[,] xy,
+            int npoints,
+            double decay,
+            int restarts,
+            ref int info,
+            mlptrain.mlpreport rep)
+        {
+            int i = 0;
+            int k = 0;
+            int ccount = 0;
+            int pcount = 0;
+            double[,] trnxy = new double[0,0];
+            double[,] valxy = new double[0,0];
+            int trnsize = 0;
+            int valsize = 0;
+            mlpbase.multilayerperceptron network = new mlpbase.multilayerperceptron();
+            int tmpinfo = 0;
+            mlptrain.mlpreport tmprep = new mlptrain.mlpreport();
+            int i_ = 0;
+            int i1_ = 0;
+
+            info = 0;
+
+            if( (npoints<2 | restarts<1) | (double)(decay)<(double)(0) )
+            {
+                info = -1;
+                return;
+            }
+            if( ensemble.issoftmax )
+            {
+                for(i=0; i<=npoints-1; i++)
+                {
+                    if( (int)Math.Round(xy[i,ensemble.nin])<0 | (int)Math.Round(xy[i,ensemble.nin])>=ensemble.nout )
+                    {
+                        info = -2;
+                        return;
+                    }
+                }
+            }
+            info = 6;
+            
+            //
+            // allocate
+            //
+            if( ensemble.issoftmax )
+            {
+                ccount = ensemble.nin+1;
+                pcount = ensemble.nin;
+            }
+            else
+            {
+                ccount = ensemble.nin+ensemble.nout;
+                pcount = ensemble.nin+ensemble.nout;
+            }
+            trnxy = new double[npoints-1+1, ccount-1+1];
+            valxy = new double[npoints-1+1, ccount-1+1];
+            mlpbase.mlpunserializeold(ensemble.serializedmlp, network);
+            rep.ngrad = 0;
+            rep.nhess = 0;
+            rep.ncholesky = 0;
+            
+            //
+            // train networks
+            //
+            for(k=0; k<=ensemble.ensemblesize-1; k++)
+            {
+                
+                //
+                // Split set
+                //
+                do
+                {
+                    trnsize = 0;
+                    valsize = 0;
+                    for(i=0; i<=npoints-1; i++)
+                    {
+                        if( (double)(math.randomreal())<(double)(0.66) )
+                        {
+                            
+                            //
+                            // Assign sample to training set
+                            //
+                            for(i_=0; i_<=ccount-1;i_++)
+                            {
+                                trnxy[trnsize,i_] = xy[i,i_];
+                            }
+                            trnsize = trnsize+1;
+                        }
+                        else
+                        {
+                            
+                            //
+                            // Assign sample to validation set
+                            //
+                            for(i_=0; i_<=ccount-1;i_++)
+                            {
+                                valxy[valsize,i_] = xy[i,i_];
+                            }
+                            valsize = valsize+1;
+                        }
+                    }
+                }
+                while( !(trnsize!=0 & valsize!=0) );
+                
+                //
+                // Train
+                //
+                mlptrain.mlptraines(network, trnxy, trnsize, valxy, valsize, decay, restarts, ref tmpinfo, tmprep);
+                if( tmpinfo<0 )
+                {
+                    info = tmpinfo;
+                    return;
+                }
+                
+                //
+                // save results
+                //
+                i1_ = (0) - (k*ensemble.wcount);
+                for(i_=k*ensemble.wcount; i_<=(k+1)*ensemble.wcount-1;i_++)
+                {
+                    ensemble.weights[i_] = network.weights[i_+i1_];
+                }
+                i1_ = (0) - (k*pcount);
+                for(i_=k*pcount; i_<=(k+1)*pcount-1;i_++)
+                {
+                    ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
+                }
+                i1_ = (0) - (k*pcount);
+                for(i_=k*pcount; i_<=(k+1)*pcount-1;i_++)
+                {
+                    ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
+                }
+                rep.ngrad = rep.ngrad+tmprep.ngrad;
+                rep.nhess = rep.nhess+tmprep.nhess;
+                rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
+            }
+        }
+
+
+        /*************************************************************************
+        Calculation of all types of errors
+
+          -- ALGLIB --
+             Copyright 17.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        private static void mlpeallerrors(mlpensemble ensemble,
+            double[,] xy,
+            int npoints,
+            ref double relcls,
+            ref double avgce,
+            ref double rms,
+            ref double avg,
+            ref double avgrel)
+        {
+            int i = 0;
+            double[] buf = new double[0];
+            double[] workx = new double[0];
+            double[] y = new double[0];
+            double[] dy = new double[0];
+            int i_ = 0;
+            int i1_ = 0;
+
+            relcls = 0;
+            avgce = 0;
+            rms = 0;
+            avg = 0;
+            avgrel = 0;
+
+            workx = new double[ensemble.nin-1+1];
+            y = new double[ensemble.nout-1+1];
+            if( ensemble.issoftmax )
+            {
+                dy = new double[0+1];
+                bdss.dserrallocate(ensemble.nout, ref buf);
+            }
+            else
+            {
+                dy = new double[ensemble.nout-1+1];
+                bdss.dserrallocate(-ensemble.nout, ref buf);
+            }
+            for(i=0; i<=npoints-1; i++)
+            {
+                for(i_=0; i_<=ensemble.nin-1;i_++)
+                {
+                    workx[i_] = xy[i,i_];
+                }
+                mlpeprocess(ensemble, workx, ref y);
+                if( ensemble.issoftmax )
+                {
+                    dy[0] = xy[i,ensemble.nin];
+                }
+                else
+                {
+                    i1_ = (ensemble.nin) - (0);
+                    for(i_=0; i_<=ensemble.nout-1;i_++)
+                    {
+                        dy[i_] = xy[i,i_+i1_];
+                    }
+                }
+                bdss.dserraccumulate(ref buf, y, dy);
+            }
+            bdss.dserrfinish(ref buf);
+            relcls = buf[0];
+            avgce = buf[1];
+            rms = buf[2];
+            avg = buf[3];
+            avgrel = buf[4];
+        }
+
+
+        /*************************************************************************
+        Internal bagging subroutine.
+
+          -- ALGLIB --
+             Copyright 19.02.2009 by Bochkanov Sergey
+        *************************************************************************/
+        private static void mlpebagginginternal(mlpensemble ensemble,
+            double[,] xy,
+            int npoints,
+            double decay,
+            int restarts,
+            double wstep,
+            int maxits,
+            bool lmalgorithm,
+            ref int info,
+            mlptrain.mlpreport rep,
+            mlptrain.mlpcvreport ooberrors)
+        {
+            double[,] xys = new double[0,0];
+            bool[] s = new bool[0];
+            double[,] oobbuf = new double[0,0];
+            int[] oobcntbuf = new int[0];
+            double[] x = new double[0];
+            double[] y = new double[0];
+            double[] dy = new double[0];
+            double[] dsbuf = new double[0];
+            int nin = 0;
+            int nout = 0;
+            int ccnt = 0;
+            int pcnt = 0;
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            double v = 0;
+            mlptrain.mlpreport tmprep = new mlptrain.mlpreport();
+            mlpbase.multilayerperceptron network = new mlpbase.multilayerperceptron();
+            int i_ = 0;
+            int i1_ = 0;
+
+            info = 0;
+
+            
+            //
+            // Test for inputs
+            //
+            if( (!lmalgorithm & (double)(wstep)==(double)(0)) & maxits==0 )
+            {
+                info = -8;
+                return;
+            }
+            if( ((npoints<=0 | restarts<1) | (double)(wstep)<(double)(0)) | maxits<0 )
+            {
+                info = -1;
+                return;
+            }
+            if( ensemble.issoftmax )
+            {
+                for(i=0; i<=npoints-1; i++)
+                {
+                    if( (int)Math.Round(xy[i,ensemble.nin])<0 | (int)Math.Round(xy[i,ensemble.nin])>=ensemble.nout )
+                    {
+                        info = -2;
+                        return;
+                    }
+                }
+            }
+            
+            //
+            // allocate temporaries
+            //
+            info = 2;
+            rep.ngrad = 0;
+            rep.nhess = 0;
+            rep.ncholesky = 0;
+            ooberrors.relclserror = 0;
+            ooberrors.avgce = 0;
+            ooberrors.rmserror = 0;
+            ooberrors.avgerror = 0;
+            ooberrors.avgrelerror = 0;
+            nin = ensemble.nin;
+            nout = ensemble.nout;
+            if( ensemble.issoftmax )
+            {
+                ccnt = nin+1;
+                pcnt = nin;
+            }
+            else
+            {
+                ccnt = nin+nout;
+                pcnt = nin+nout;
+            }
+            xys = new double[npoints-1+1, ccnt-1+1];
+            s = new bool[npoints-1+1];
+            oobbuf = new double[npoints-1+1, nout-1+1];
+            oobcntbuf = new int[npoints-1+1];
+            x = new double[nin-1+1];
+            y = new double[nout-1+1];
+            if( ensemble.issoftmax )
+            {
+                dy = new double[0+1];
+            }
+            else
+            {
+                dy = new double[nout-1+1];
+            }
+            for(i=0; i<=npoints-1; i++)
+            {
+                for(j=0; j<=nout-1; j++)
+                {
+                    oobbuf[i,j] = 0;
+                }
+            }
+            for(i=0; i<=npoints-1; i++)
+            {
+                oobcntbuf[i] = 0;
+            }
+            mlpbase.mlpunserializeold(ensemble.serializedmlp, network);
+            
+            //
+            // main bagging cycle
+            //
+            for(k=0; k<=ensemble.ensemblesize-1; k++)
+            {
+                
+                //
+                // prepare dataset
+                //
+                for(i=0; i<=npoints-1; i++)
+                {
+                    s[i] = false;
+                }
+                for(i=0; i<=npoints-1; i++)
+                {
+                    j = math.randominteger(npoints);
+                    s[j] = true;
+                    for(i_=0; i_<=ccnt-1;i_++)
+                    {
+                        xys[i,i_] = xy[j,i_];
+                    }
+                }
+                
+                //
+                // train
+                //
+                if( lmalgorithm )
+                {
+                    mlptrain.mlptrainlm(network, xys, npoints, decay, restarts, ref info, tmprep);
+                }
+                else
+                {
+                    mlptrain.mlptrainlbfgs(network, xys, npoints, decay, restarts, wstep, maxits, ref info, tmprep);
+                }
+                if( info<0 )
+                {
+                    return;
+                }
+                
+                //
+                // save results
+                //
+                rep.ngrad = rep.ngrad+tmprep.ngrad;
+                rep.nhess = rep.nhess+tmprep.nhess;
+                rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
+                i1_ = (0) - (k*ensemble.wcount);
+                for(i_=k*ensemble.wcount; i_<=(k+1)*ensemble.wcount-1;i_++)
+                {
+                    ensemble.weights[i_] = network.weights[i_+i1_];
+                }
+                i1_ = (0) - (k*pcnt);
+                for(i_=k*pcnt; i_<=(k+1)*pcnt-1;i_++)
+                {
+                    ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
+                }
+                i1_ = (0) - (k*pcnt);
+                for(i_=k*pcnt; i_<=(k+1)*pcnt-1;i_++)
+                {
+                    ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
+                }
+                
+                //
+                // OOB estimates
+                //
+                for(i=0; i<=npoints-1; i++)
+                {
+                    if( !s[i] )
+                    {
+                        for(i_=0; i_<=nin-1;i_++)
+                        {
+                            x[i_] = xy[i,i_];
+                        }
+                        mlpbase.mlpprocess(network, x, ref y);
+                        for(i_=0; i_<=nout-1;i_++)
+                        {
+                            oobbuf[i,i_] = oobbuf[i,i_] + y[i_];
+                        }
+                        oobcntbuf[i] = oobcntbuf[i]+1;
+                    }
+                }
+            }
+            
+            //
+            // OOB estimates
+            //
+            if( ensemble.issoftmax )
+            {
+                bdss.dserrallocate(nout, ref dsbuf);
+            }
+            else
+            {
+                bdss.dserrallocate(-nout, ref dsbuf);
+            }
+            for(i=0; i<=npoints-1; i++)
+            {
+                if( oobcntbuf[i]!=0 )
+                {
+                    v = (double)1/(double)oobcntbuf[i];
+                    for(i_=0; i_<=nout-1;i_++)
+                    {
+                        y[i_] = v*oobbuf[i,i_];
+                    }
+                    if( ensemble.issoftmax )
+                    {
+                        dy[0] = xy[i,nin];
+                    }
+                    else
+                    {
+                        i1_ = (nin) - (0);
+                        for(i_=0; i_<=nout-1;i_++)
+                        {
+                            dy[i_] = v*xy[i,i_+i1_];
+                        }
+                    }
+                    bdss.dserraccumulate(ref dsbuf, y, dy);
+                }
+            }
+            bdss.dserrfinish(ref dsbuf);
+            ooberrors.relclserror = dsbuf[0];
+            ooberrors.avgce = dsbuf[1];
+            ooberrors.rmserror = dsbuf[2];
+            ooberrors.avgerror = dsbuf[3];
+            ooberrors.avgrelerror = dsbuf[4];
+        }
+
+
+    }
     public class pca
     {
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/diffequations.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/diffequations.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/diffequations.cs	(revision 7294)
@@ -168,19 +168,4 @@
         obj     -   optional object which is passed to diff; can be NULL
 
-    One iteration of ODE solver.
-
-    Called after inialization of State structure with OdeSolverXXX subroutine.
-    See HTML docs for examples.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state between subsequent
-                    calls and which is used for reverse communication. Must be
-                    initialized with OdeSolverXXX() call first.
-
-    If subroutine returned False, algorithm have finished its work.
-    If subroutine returned True, then user should:
-    * calculate F(State.X, State.Y)
-    * store it in State.DY
-    Here State.X is real, State.Y and State.DY are arrays[0..N-1] of reals.
 
       -- ALGLIB --
@@ -372,19 +357,4 @@
 
         /*************************************************************************
-        One iteration of ODE solver.
-
-        Called after inialization of State structure with OdeSolverXXX subroutine.
-        See HTML docs for examples.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state between subsequent
-                        calls and which is used for reverse communication. Must be
-                        initialized with OdeSolverXXX() call first.
-
-        If subroutine returned False, algorithm have finished its work.
-        If subroutine returned True, then user should:
-        * calculate F(State.X, State.Y)
-        * store it in State.DY
-        Here State.X is real, State.Y and State.DY are arrays[0..N-1] of reals.
 
           -- ALGLIB --
@@ -472,6 +442,4 @@
             m = state.m;
             h = state.h;
-            state.y = new double[n];
-            state.dy = new double[n];
             maxgrowpow = Math.Pow(odesolvermaxgrow, 5);
             state.repnfev = 0;
@@ -965,4 +933,10 @@
             state.solvertype = solvertype;
             state.repterminationtype = 0;
+            
+            //
+            // Allocate arrays
+            //
+            state.y = new double[n];
+            state.dy = new double[n];
         }
 
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/fasttransforms.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/fasttransforms.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/fasttransforms.cs	(revision 7294)
@@ -21,270 +21,4 @@
 using System;
 
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    1-dimensional complex convolution.
-
-    For given A/B returns conv(A,B) (non-circular). Subroutine can automatically
-    choose between three implementations: straightforward O(M*N)  formula  for
-    very small N (or M), overlap-add algorithm for  cases  where  max(M,N)  is
-    significantly larger than min(M,N), but O(M*N) algorithm is too slow,  and
-    general FFT-based formula for cases where two previois algorithms are  too
-    slow.
-
-    Algorithm has max(M,N)*log(max(M,N)) complexity for any M/N.
-
-    INPUT PARAMETERS
-        A   -   array[0..M-1] - complex function to be transformed
-        M   -   problem size
-        B   -   array[0..N-1] - complex function to be transformed
-        N   -   problem size
-
-    OUTPUT PARAMETERS
-        R   -   convolution: A*B. array[0..N+M-2].
-
-    NOTE:
-        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
-    functions have non-zero values at negative T's, you  can  still  use  this
-    subroutine - just shift its result correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convc1d(complex[] a, int m, complex[] b, int n, out complex[] r)
-    {
-        r = new complex[0];
-        conv.convc1d(a, m, b, n, ref r);
-        return;
-    }
-
-    /*************************************************************************
-    1-dimensional complex non-circular deconvolution (inverse of ConvC1D()).
-
-    Algorithm has M*log(M)) complexity for any M (composite or prime).
-
-    INPUT PARAMETERS
-        A   -   array[0..M-1] - convolved signal, A = conv(R, B)
-        M   -   convolved signal length
-        B   -   array[0..N-1] - response
-        N   -   response length, N<=M
-
-    OUTPUT PARAMETERS
-        R   -   deconvolved signal. array[0..M-N].
-
-    NOTE:
-        deconvolution is unstable process and may result in division  by  zero
-    (if your response function is degenerate, i.e. has zero Fourier coefficient).
-
-    NOTE:
-        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
-    functions have non-zero values at negative T's, you  can  still  use  this
-    subroutine - just shift its result correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convc1dinv(complex[] a, int m, complex[] b, int n, out complex[] r)
-    {
-        r = new complex[0];
-        conv.convc1dinv(a, m, b, n, ref r);
-        return;
-    }
-
-    /*************************************************************************
-    1-dimensional circular complex convolution.
-
-    For given S/R returns conv(S,R) (circular). Algorithm has linearithmic
-    complexity for any M/N.
-
-    IMPORTANT:  normal convolution is commutative,  i.e.   it  is symmetric  -
-    conv(A,B)=conv(B,A).  Cyclic convolution IS NOT.  One function - S - is  a
-    signal,  periodic function, and another - R - is a response,  non-periodic
-    function with limited length.
-
-    INPUT PARAMETERS
-        S   -   array[0..M-1] - complex periodic signal
-        M   -   problem size
-        B   -   array[0..N-1] - complex non-periodic response
-        N   -   problem size
-
-    OUTPUT PARAMETERS
-        R   -   convolution: A*B. array[0..M-1].
-
-    NOTE:
-        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
-    negative T's, you can still use this subroutine - just  shift  its  result
-    correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convc1dcircular(complex[] s, int m, complex[] r, int n, out complex[] c)
-    {
-        c = new complex[0];
-        conv.convc1dcircular(s, m, r, n, ref c);
-        return;
-    }
-
-    /*************************************************************************
-    1-dimensional circular complex deconvolution (inverse of ConvC1DCircular()).
-
-    Algorithm has M*log(M)) complexity for any M (composite or prime).
-
-    INPUT PARAMETERS
-        A   -   array[0..M-1] - convolved periodic signal, A = conv(R, B)
-        M   -   convolved signal length
-        B   -   array[0..N-1] - non-periodic response
-        N   -   response length
-
-    OUTPUT PARAMETERS
-        R   -   deconvolved signal. array[0..M-1].
-
-    NOTE:
-        deconvolution is unstable process and may result in division  by  zero
-    (if your response function is degenerate, i.e. has zero Fourier coefficient).
-
-    NOTE:
-        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
-    negative T's, you can still use this subroutine - just  shift  its  result
-    correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convc1dcircularinv(complex[] a, int m, complex[] b, int n, out complex[] r)
-    {
-        r = new complex[0];
-        conv.convc1dcircularinv(a, m, b, n, ref r);
-        return;
-    }
-
-    /*************************************************************************
-    1-dimensional real convolution.
-
-    Analogous to ConvC1D(), see ConvC1D() comments for more details.
-
-    INPUT PARAMETERS
-        A   -   array[0..M-1] - real function to be transformed
-        M   -   problem size
-        B   -   array[0..N-1] - real function to be transformed
-        N   -   problem size
-
-    OUTPUT PARAMETERS
-        R   -   convolution: A*B. array[0..N+M-2].
-
-    NOTE:
-        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
-    functions have non-zero values at negative T's, you  can  still  use  this
-    subroutine - just shift its result correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convr1d(double[] a, int m, double[] b, int n, out double[] r)
-    {
-        r = new double[0];
-        conv.convr1d(a, m, b, n, ref r);
-        return;
-    }
-
-    /*************************************************************************
-    1-dimensional real deconvolution (inverse of ConvC1D()).
-
-    Algorithm has M*log(M)) complexity for any M (composite or prime).
-
-    INPUT PARAMETERS
-        A   -   array[0..M-1] - convolved signal, A = conv(R, B)
-        M   -   convolved signal length
-        B   -   array[0..N-1] - response
-        N   -   response length, N<=M
-
-    OUTPUT PARAMETERS
-        R   -   deconvolved signal. array[0..M-N].
-
-    NOTE:
-        deconvolution is unstable process and may result in division  by  zero
-    (if your response function is degenerate, i.e. has zero Fourier coefficient).
-
-    NOTE:
-        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
-    functions have non-zero values at negative T's, you  can  still  use  this
-    subroutine - just shift its result correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convr1dinv(double[] a, int m, double[] b, int n, out double[] r)
-    {
-        r = new double[0];
-        conv.convr1dinv(a, m, b, n, ref r);
-        return;
-    }
-
-    /*************************************************************************
-    1-dimensional circular real convolution.
-
-    Analogous to ConvC1DCircular(), see ConvC1DCircular() comments for more details.
-
-    INPUT PARAMETERS
-        S   -   array[0..M-1] - real signal
-        M   -   problem size
-        B   -   array[0..N-1] - real response
-        N   -   problem size
-
-    OUTPUT PARAMETERS
-        R   -   convolution: A*B. array[0..M-1].
-
-    NOTE:
-        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
-    negative T's, you can still use this subroutine - just  shift  its  result
-    correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convr1dcircular(double[] s, int m, double[] r, int n, out double[] c)
-    {
-        c = new double[0];
-        conv.convr1dcircular(s, m, r, n, ref c);
-        return;
-    }
-
-    /*************************************************************************
-    1-dimensional complex deconvolution (inverse of ConvC1D()).
-
-    Algorithm has M*log(M)) complexity for any M (composite or prime).
-
-    INPUT PARAMETERS
-        A   -   array[0..M-1] - convolved signal, A = conv(R, B)
-        M   -   convolved signal length
-        B   -   array[0..N-1] - response
-        N   -   response length
-
-    OUTPUT PARAMETERS
-        R   -   deconvolved signal. array[0..M-N].
-
-    NOTE:
-        deconvolution is unstable process and may result in division  by  zero
-    (if your response function is degenerate, i.e. has zero Fourier coefficient).
-
-    NOTE:
-        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
-    negative T's, you can still use this subroutine - just  shift  its  result
-    correspondingly.
-
-      -- ALGLIB --
-         Copyright 21.07.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void convr1dcircularinv(double[] a, int m, double[] b, int n, out double[] r)
-    {
-        r = new double[0];
-        conv.convr1dcircularinv(a, m, b, n, ref r);
-        return;
-    }
-
-}
 public partial class alglib
 {
@@ -472,4 +206,270 @@
 
     /*************************************************************************
+    1-dimensional complex convolution.
+
+    For given A/B returns conv(A,B) (non-circular). Subroutine can automatically
+    choose between three implementations: straightforward O(M*N)  formula  for
+    very small N (or M), overlap-add algorithm for  cases  where  max(M,N)  is
+    significantly larger than min(M,N), but O(M*N) algorithm is too slow,  and
+    general FFT-based formula for cases where two previois algorithms are  too
+    slow.
+
+    Algorithm has max(M,N)*log(max(M,N)) complexity for any M/N.
+
+    INPUT PARAMETERS
+        A   -   array[0..M-1] - complex function to be transformed
+        M   -   problem size
+        B   -   array[0..N-1] - complex function to be transformed
+        N   -   problem size
+
+    OUTPUT PARAMETERS
+        R   -   convolution: A*B. array[0..N+M-2].
+
+    NOTE:
+        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
+    functions have non-zero values at negative T's, you  can  still  use  this
+    subroutine - just shift its result correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convc1d(complex[] a, int m, complex[] b, int n, out complex[] r)
+    {
+        r = new complex[0];
+        conv.convc1d(a, m, b, n, ref r);
+        return;
+    }
+
+    /*************************************************************************
+    1-dimensional complex non-circular deconvolution (inverse of ConvC1D()).
+
+    Algorithm has M*log(M)) complexity for any M (composite or prime).
+
+    INPUT PARAMETERS
+        A   -   array[0..M-1] - convolved signal, A = conv(R, B)
+        M   -   convolved signal length
+        B   -   array[0..N-1] - response
+        N   -   response length, N<=M
+
+    OUTPUT PARAMETERS
+        R   -   deconvolved signal. array[0..M-N].
+
+    NOTE:
+        deconvolution is unstable process and may result in division  by  zero
+    (if your response function is degenerate, i.e. has zero Fourier coefficient).
+
+    NOTE:
+        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
+    functions have non-zero values at negative T's, you  can  still  use  this
+    subroutine - just shift its result correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convc1dinv(complex[] a, int m, complex[] b, int n, out complex[] r)
+    {
+        r = new complex[0];
+        conv.convc1dinv(a, m, b, n, ref r);
+        return;
+    }
+
+    /*************************************************************************
+    1-dimensional circular complex convolution.
+
+    For given S/R returns conv(S,R) (circular). Algorithm has linearithmic
+    complexity for any M/N.
+
+    IMPORTANT:  normal convolution is commutative,  i.e.   it  is symmetric  -
+    conv(A,B)=conv(B,A).  Cyclic convolution IS NOT.  One function - S - is  a
+    signal,  periodic function, and another - R - is a response,  non-periodic
+    function with limited length.
+
+    INPUT PARAMETERS
+        S   -   array[0..M-1] - complex periodic signal
+        M   -   problem size
+        B   -   array[0..N-1] - complex non-periodic response
+        N   -   problem size
+
+    OUTPUT PARAMETERS
+        R   -   convolution: A*B. array[0..M-1].
+
+    NOTE:
+        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
+    negative T's, you can still use this subroutine - just  shift  its  result
+    correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convc1dcircular(complex[] s, int m, complex[] r, int n, out complex[] c)
+    {
+        c = new complex[0];
+        conv.convc1dcircular(s, m, r, n, ref c);
+        return;
+    }
+
+    /*************************************************************************
+    1-dimensional circular complex deconvolution (inverse of ConvC1DCircular()).
+
+    Algorithm has M*log(M)) complexity for any M (composite or prime).
+
+    INPUT PARAMETERS
+        A   -   array[0..M-1] - convolved periodic signal, A = conv(R, B)
+        M   -   convolved signal length
+        B   -   array[0..N-1] - non-periodic response
+        N   -   response length
+
+    OUTPUT PARAMETERS
+        R   -   deconvolved signal. array[0..M-1].
+
+    NOTE:
+        deconvolution is unstable process and may result in division  by  zero
+    (if your response function is degenerate, i.e. has zero Fourier coefficient).
+
+    NOTE:
+        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
+    negative T's, you can still use this subroutine - just  shift  its  result
+    correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convc1dcircularinv(complex[] a, int m, complex[] b, int n, out complex[] r)
+    {
+        r = new complex[0];
+        conv.convc1dcircularinv(a, m, b, n, ref r);
+        return;
+    }
+
+    /*************************************************************************
+    1-dimensional real convolution.
+
+    Analogous to ConvC1D(), see ConvC1D() comments for more details.
+
+    INPUT PARAMETERS
+        A   -   array[0..M-1] - real function to be transformed
+        M   -   problem size
+        B   -   array[0..N-1] - real function to be transformed
+        N   -   problem size
+
+    OUTPUT PARAMETERS
+        R   -   convolution: A*B. array[0..N+M-2].
+
+    NOTE:
+        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
+    functions have non-zero values at negative T's, you  can  still  use  this
+    subroutine - just shift its result correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convr1d(double[] a, int m, double[] b, int n, out double[] r)
+    {
+        r = new double[0];
+        conv.convr1d(a, m, b, n, ref r);
+        return;
+    }
+
+    /*************************************************************************
+    1-dimensional real deconvolution (inverse of ConvC1D()).
+
+    Algorithm has M*log(M)) complexity for any M (composite or prime).
+
+    INPUT PARAMETERS
+        A   -   array[0..M-1] - convolved signal, A = conv(R, B)
+        M   -   convolved signal length
+        B   -   array[0..N-1] - response
+        N   -   response length, N<=M
+
+    OUTPUT PARAMETERS
+        R   -   deconvolved signal. array[0..M-N].
+
+    NOTE:
+        deconvolution is unstable process and may result in division  by  zero
+    (if your response function is degenerate, i.e. has zero Fourier coefficient).
+
+    NOTE:
+        It is assumed that A is zero at T<0, B is zero too.  If  one  or  both
+    functions have non-zero values at negative T's, you  can  still  use  this
+    subroutine - just shift its result correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convr1dinv(double[] a, int m, double[] b, int n, out double[] r)
+    {
+        r = new double[0];
+        conv.convr1dinv(a, m, b, n, ref r);
+        return;
+    }
+
+    /*************************************************************************
+    1-dimensional circular real convolution.
+
+    Analogous to ConvC1DCircular(), see ConvC1DCircular() comments for more details.
+
+    INPUT PARAMETERS
+        S   -   array[0..M-1] - real signal
+        M   -   problem size
+        B   -   array[0..N-1] - real response
+        N   -   problem size
+
+    OUTPUT PARAMETERS
+        R   -   convolution: A*B. array[0..M-1].
+
+    NOTE:
+        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
+    negative T's, you can still use this subroutine - just  shift  its  result
+    correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convr1dcircular(double[] s, int m, double[] r, int n, out double[] c)
+    {
+        c = new double[0];
+        conv.convr1dcircular(s, m, r, n, ref c);
+        return;
+    }
+
+    /*************************************************************************
+    1-dimensional complex deconvolution (inverse of ConvC1D()).
+
+    Algorithm has M*log(M)) complexity for any M (composite or prime).
+
+    INPUT PARAMETERS
+        A   -   array[0..M-1] - convolved signal, A = conv(R, B)
+        M   -   convolved signal length
+        B   -   array[0..N-1] - response
+        N   -   response length
+
+    OUTPUT PARAMETERS
+        R   -   deconvolved signal. array[0..M-N].
+
+    NOTE:
+        deconvolution is unstable process and may result in division  by  zero
+    (if your response function is degenerate, i.e. has zero Fourier coefficient).
+
+    NOTE:
+        It is assumed that B is zero at T<0. If  it  has  non-zero  values  at
+    negative T's, you can still use this subroutine - just  shift  its  result
+    correspondingly.
+
+      -- ALGLIB --
+         Copyright 21.07.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void convr1dcircularinv(double[] a, int m, double[] b, int n, out double[] r)
+    {
+        r = new double[0];
+        conv.convr1dcircularinv(a, m, b, n, ref r);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
     1-dimensional complex cross-correlation.
 
@@ -680,4 +680,489 @@
 public partial class alglib
 {
+    public class fft
+    {
+        /*************************************************************************
+        1-dimensional complex FFT.
+
+        Array size N may be arbitrary number (composite or prime).  Composite  N's
+        are handled with cache-oblivious variation of  a  Cooley-Tukey  algorithm.
+        Small prime-factors are transformed using hard coded  codelets (similar to
+        FFTW codelets, but without low-level  optimization),  large  prime-factors
+        are handled with Bluestein's algorithm.
+
+        Fastests transforms are for smooth N's (prime factors are 2, 3,  5  only),
+        most fast for powers of 2. When N have prime factors  larger  than  these,
+        but orders of magnitude smaller than N, computations will be about 4 times
+        slower than for nearby highly composite N's. When N itself is prime, speed
+        will be 6 times lower.
+
+        Algorithm has O(N*logN) complexity for any N (composite or prime).
+
+        INPUT PARAMETERS
+            A   -   array[0..N-1] - complex function to be transformed
+            N   -   problem size
+            
+        OUTPUT PARAMETERS
+            A   -   DFT of a input array, array[0..N-1]
+                    A_out[j] = SUM(A_in[k]*exp(-2*pi*sqrt(-1)*j*k/N), k = 0..N-1)
+
+
+          -- ALGLIB --
+             Copyright 29.05.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void fftc1d(ref complex[] a,
+            int n)
+        {
+            ftbase.ftplan plan = new ftbase.ftplan();
+            int i = 0;
+            double[] buf = new double[0];
+
+            ap.assert(n>0, "FFTC1D: incorrect N!");
+            ap.assert(ap.len(a)>=n, "FFTC1D: Length(A)<N!");
+            ap.assert(apserv.isfinitecvector(a, n), "FFTC1D: A contains infinite or NAN values!");
+            
+            //
+            // Special case: N=1, FFT is just identity transform.
+            // After this block we assume that N is strictly greater than 1.
+            //
+            if( n==1 )
+            {
+                return;
+            }
+            
+            //
+            // convert input array to the more convinient format
+            //
+            buf = new double[2*n];
+            for(i=0; i<=n-1; i++)
+            {
+                buf[2*i+0] = a[i].x;
+                buf[2*i+1] = a[i].y;
+            }
+            
+            //
+            // Generate plan and execute it.
+            //
+            // Plan is a combination of a successive factorizations of N and
+            // precomputed data. It is much like a FFTW plan, but is not stored
+            // between subroutine calls and is much simpler.
+            //
+            ftbase.ftbasegeneratecomplexfftplan(n, plan);
+            ftbase.ftbaseexecuteplan(ref buf, 0, n, plan);
+            
+            //
+            // result
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                a[i].x = buf[2*i+0];
+                a[i].y = buf[2*i+1];
+            }
+        }
+
+
+        /*************************************************************************
+        1-dimensional complex inverse FFT.
+
+        Array size N may be arbitrary number (composite or prime).  Algorithm  has
+        O(N*logN) complexity for any N (composite or prime).
+
+        See FFTC1D() description for more information about algorithm performance.
+
+        INPUT PARAMETERS
+            A   -   array[0..N-1] - complex array to be transformed
+            N   -   problem size
+
+        OUTPUT PARAMETERS
+            A   -   inverse DFT of a input array, array[0..N-1]
+                    A_out[j] = SUM(A_in[k]/N*exp(+2*pi*sqrt(-1)*j*k/N), k = 0..N-1)
+
+
+          -- ALGLIB --
+             Copyright 29.05.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void fftc1dinv(ref complex[] a,
+            int n)
+        {
+            int i = 0;
+
+            ap.assert(n>0, "FFTC1DInv: incorrect N!");
+            ap.assert(ap.len(a)>=n, "FFTC1DInv: Length(A)<N!");
+            ap.assert(apserv.isfinitecvector(a, n), "FFTC1DInv: A contains infinite or NAN values!");
+            
+            //
+            // Inverse DFT can be expressed in terms of the DFT as
+            //
+            //     invfft(x) = fft(x')'/N
+            //
+            // here x' means conj(x).
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                a[i].y = -a[i].y;
+            }
+            fftc1d(ref a, n);
+            for(i=0; i<=n-1; i++)
+            {
+                a[i].x = a[i].x/n;
+                a[i].y = -(a[i].y/n);
+            }
+        }
+
+
+        /*************************************************************************
+        1-dimensional real FFT.
+
+        Algorithm has O(N*logN) complexity for any N (composite or prime).
+
+        INPUT PARAMETERS
+            A   -   array[0..N-1] - real function to be transformed
+            N   -   problem size
+
+        OUTPUT PARAMETERS
+            F   -   DFT of a input array, array[0..N-1]
+                    F[j] = SUM(A[k]*exp(-2*pi*sqrt(-1)*j*k/N), k = 0..N-1)
+
+        NOTE:
+            F[] satisfies symmetry property F[k] = conj(F[N-k]),  so just one half
+        of  array  is  usually needed. But for convinience subroutine returns full
+        complex array (with frequencies above N/2), so its result may be  used  by
+        other FFT-related subroutines.
+
+
+          -- ALGLIB --
+             Copyright 01.06.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void fftr1d(double[] a,
+            int n,
+            ref complex[] f)
+        {
+            int i = 0;
+            int n2 = 0;
+            int idx = 0;
+            complex hn = 0;
+            complex hmnc = 0;
+            complex v = 0;
+            double[] buf = new double[0];
+            ftbase.ftplan plan = new ftbase.ftplan();
+            int i_ = 0;
+
+            f = new complex[0];
+
+            ap.assert(n>0, "FFTR1D: incorrect N!");
+            ap.assert(ap.len(a)>=n, "FFTR1D: Length(A)<N!");
+            ap.assert(apserv.isfinitevector(a, n), "FFTR1D: A contains infinite or NAN values!");
+            
+            //
+            // Special cases:
+            // * N=1, FFT is just identity transform.
+            // * N=2, FFT is simple too
+            //
+            // After this block we assume that N is strictly greater than 2
+            //
+            if( n==1 )
+            {
+                f = new complex[1];
+                f[0] = a[0];
+                return;
+            }
+            if( n==2 )
+            {
+                f = new complex[2];
+                f[0].x = a[0]+a[1];
+                f[0].y = 0;
+                f[1].x = a[0]-a[1];
+                f[1].y = 0;
+                return;
+            }
+            
+            //
+            // Choose between odd-size and even-size FFTs
+            //
+            if( n%2==0 )
+            {
+                
+                //
+                // even-size real FFT, use reduction to the complex task
+                //
+                n2 = n/2;
+                buf = new double[n];
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    buf[i_] = a[i_];
+                }
+                ftbase.ftbasegeneratecomplexfftplan(n2, plan);
+                ftbase.ftbaseexecuteplan(ref buf, 0, n2, plan);
+                f = new complex[n];
+                for(i=0; i<=n2; i++)
+                {
+                    idx = 2*(i%n2);
+                    hn.x = buf[idx+0];
+                    hn.y = buf[idx+1];
+                    idx = 2*((n2-i)%n2);
+                    hmnc.x = buf[idx+0];
+                    hmnc.y = -buf[idx+1];
+                    v.x = -Math.Sin(-(2*Math.PI*i/n));
+                    v.y = Math.Cos(-(2*Math.PI*i/n));
+                    f[i] = hn+hmnc-v*(hn-hmnc);
+                    f[i].x = 0.5*f[i].x;
+                    f[i].y = 0.5*f[i].y;
+                }
+                for(i=n2+1; i<=n-1; i++)
+                {
+                    f[i] = math.conj(f[n-i]);
+                }
+            }
+            else
+            {
+                
+                //
+                // use complex FFT
+                //
+                f = new complex[n];
+                for(i=0; i<=n-1; i++)
+                {
+                    f[i] = a[i];
+                }
+                fftc1d(ref f, n);
+            }
+        }
+
+
+        /*************************************************************************
+        1-dimensional real inverse FFT.
+
+        Algorithm has O(N*logN) complexity for any N (composite or prime).
+
+        INPUT PARAMETERS
+            F   -   array[0..floor(N/2)] - frequencies from forward real FFT
+            N   -   problem size
+
+        OUTPUT PARAMETERS
+            A   -   inverse DFT of a input array, array[0..N-1]
+
+        NOTE:
+            F[] should satisfy symmetry property F[k] = conj(F[N-k]), so just  one
+        half of frequencies array is needed - elements from 0 to floor(N/2).  F[0]
+        is ALWAYS real. If N is even F[floor(N/2)] is real too. If N is odd,  then
+        F[floor(N/2)] has no special properties.
+
+        Relying on properties noted above, FFTR1DInv subroutine uses only elements
+        from 0th to floor(N/2)-th. It ignores imaginary part of F[0],  and in case
+        N is even it ignores imaginary part of F[floor(N/2)] too.
+
+        When you call this function using full arguments list - "FFTR1DInv(F,N,A)"
+        - you can pass either either frequencies array with N elements or  reduced
+        array with roughly N/2 elements - subroutine will  successfully  transform
+        both.
+
+        If you call this function using reduced arguments list -  "FFTR1DInv(F,A)"
+        - you must pass FULL array with N elements (although higher  N/2 are still
+        not used) because array size is used to automatically determine FFT length
+
+
+          -- ALGLIB --
+             Copyright 01.06.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void fftr1dinv(complex[] f,
+            int n,
+            ref double[] a)
+        {
+            int i = 0;
+            double[] h = new double[0];
+            complex[] fh = new complex[0];
+
+            a = new double[0];
+
+            ap.assert(n>0, "FFTR1DInv: incorrect N!");
+            ap.assert(ap.len(f)>=(int)Math.Floor((double)n/(double)2)+1, "FFTR1DInv: Length(F)<Floor(N/2)+1!");
+            ap.assert(math.isfinite(f[0].x), "FFTR1DInv: F contains infinite or NAN values!");
+            for(i=1; i<=(int)Math.Floor((double)n/(double)2)-1; i++)
+            {
+                ap.assert(math.isfinite(f[i].x) & math.isfinite(f[i].y), "FFTR1DInv: F contains infinite or NAN values!");
+            }
+            ap.assert(math.isfinite(f[(int)Math.Floor((double)n/(double)2)].x), "FFTR1DInv: F contains infinite or NAN values!");
+            if( n%2!=0 )
+            {
+                ap.assert(math.isfinite(f[(int)Math.Floor((double)n/(double)2)].y), "FFTR1DInv: F contains infinite or NAN values!");
+            }
+            
+            //
+            // Special case: N=1, FFT is just identity transform.
+            // After this block we assume that N is strictly greater than 1.
+            //
+            if( n==1 )
+            {
+                a = new double[1];
+                a[0] = f[0].x;
+                return;
+            }
+            
+            //
+            // inverse real FFT is reduced to the inverse real FHT,
+            // which is reduced to the forward real FHT,
+            // which is reduced to the forward real FFT.
+            //
+            // Don't worry, it is really compact and efficient reduction :)
+            //
+            h = new double[n];
+            a = new double[n];
+            h[0] = f[0].x;
+            for(i=1; i<=(int)Math.Floor((double)n/(double)2)-1; i++)
+            {
+                h[i] = f[i].x-f[i].y;
+                h[n-i] = f[i].x+f[i].y;
+            }
+            if( n%2==0 )
+            {
+                h[(int)Math.Floor((double)n/(double)2)] = f[(int)Math.Floor((double)n/(double)2)].x;
+            }
+            else
+            {
+                h[(int)Math.Floor((double)n/(double)2)] = f[(int)Math.Floor((double)n/(double)2)].x-f[(int)Math.Floor((double)n/(double)2)].y;
+                h[(int)Math.Floor((double)n/(double)2)+1] = f[(int)Math.Floor((double)n/(double)2)].x+f[(int)Math.Floor((double)n/(double)2)].y;
+            }
+            fftr1d(h, n, ref fh);
+            for(i=0; i<=n-1; i++)
+            {
+                a[i] = (fh[i].x-fh[i].y)/n;
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Never call it directly!
+
+
+          -- ALGLIB --
+             Copyright 01.06.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void fftr1dinternaleven(ref double[] a,
+            int n,
+            ref double[] buf,
+            ftbase.ftplan plan)
+        {
+            double x = 0;
+            double y = 0;
+            int i = 0;
+            int n2 = 0;
+            int idx = 0;
+            complex hn = 0;
+            complex hmnc = 0;
+            complex v = 0;
+            int i_ = 0;
+
+            ap.assert(n>0 & n%2==0, "FFTR1DEvenInplace: incorrect N!");
+            
+            //
+            // Special cases:
+            // * N=2
+            //
+            // After this block we assume that N is strictly greater than 2
+            //
+            if( n==2 )
+            {
+                x = a[0]+a[1];
+                y = a[0]-a[1];
+                a[0] = x;
+                a[1] = y;
+                return;
+            }
+            
+            //
+            // even-size real FFT, use reduction to the complex task
+            //
+            n2 = n/2;
+            for(i_=0; i_<=n-1;i_++)
+            {
+                buf[i_] = a[i_];
+            }
+            ftbase.ftbaseexecuteplan(ref buf, 0, n2, plan);
+            a[0] = buf[0]+buf[1];
+            for(i=1; i<=n2-1; i++)
+            {
+                idx = 2*(i%n2);
+                hn.x = buf[idx+0];
+                hn.y = buf[idx+1];
+                idx = 2*(n2-i);
+                hmnc.x = buf[idx+0];
+                hmnc.y = -buf[idx+1];
+                v.x = -Math.Sin(-(2*Math.PI*i/n));
+                v.y = Math.Cos(-(2*Math.PI*i/n));
+                v = hn+hmnc-v*(hn-hmnc);
+                a[2*i+0] = 0.5*v.x;
+                a[2*i+1] = 0.5*v.y;
+            }
+            a[1] = buf[0]-buf[1];
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Never call it directly!
+
+
+          -- ALGLIB --
+             Copyright 01.06.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void fftr1dinvinternaleven(ref double[] a,
+            int n,
+            ref double[] buf,
+            ftbase.ftplan plan)
+        {
+            double x = 0;
+            double y = 0;
+            double t = 0;
+            int i = 0;
+            int n2 = 0;
+
+            ap.assert(n>0 & n%2==0, "FFTR1DInvInternalEven: incorrect N!");
+            
+            //
+            // Special cases:
+            // * N=2
+            //
+            // After this block we assume that N is strictly greater than 2
+            //
+            if( n==2 )
+            {
+                x = 0.5*(a[0]+a[1]);
+                y = 0.5*(a[0]-a[1]);
+                a[0] = x;
+                a[1] = y;
+                return;
+            }
+            
+            //
+            // inverse real FFT is reduced to the inverse real FHT,
+            // which is reduced to the forward real FHT,
+            // which is reduced to the forward real FFT.
+            //
+            // Don't worry, it is really compact and efficient reduction :)
+            //
+            n2 = n/2;
+            buf[0] = a[0];
+            for(i=1; i<=n2-1; i++)
+            {
+                x = a[2*i+0];
+                y = a[2*i+1];
+                buf[i] = x-y;
+                buf[n-i] = x+y;
+            }
+            buf[n2] = a[1];
+            fftr1dinternaleven(ref buf, n, ref a, plan);
+            a[0] = buf[0]/n;
+            t = (double)1/(double)n;
+            for(i=1; i<=n2-1; i++)
+            {
+                x = buf[2*i+0];
+                y = buf[2*i+1];
+                a[i] = t*(x-y);
+                a[n-i] = t*(x+y);
+            }
+            a[n2] = buf[1]/n;
+        }
+
+
+    }
     public class conv
     {
@@ -2358,491 +2843,42 @@
 
     }
-    public class fft
+    public class corr
     {
         /*************************************************************************
-        1-dimensional complex FFT.
-
-        Array size N may be arbitrary number (composite or prime).  Composite  N's
-        are handled with cache-oblivious variation of  a  Cooley-Tukey  algorithm.
-        Small prime-factors are transformed using hard coded  codelets (similar to
-        FFTW codelets, but without low-level  optimization),  large  prime-factors
-        are handled with Bluestein's algorithm.
-
-        Fastests transforms are for smooth N's (prime factors are 2, 3,  5  only),
-        most fast for powers of 2. When N have prime factors  larger  than  these,
-        but orders of magnitude smaller than N, computations will be about 4 times
-        slower than for nearby highly composite N's. When N itself is prime, speed
-        will be 6 times lower.
-
-        Algorithm has O(N*logN) complexity for any N (composite or prime).
+        1-dimensional complex cross-correlation.
+
+        For given Pattern/Signal returns corr(Pattern,Signal) (non-circular).
+
+        Correlation is calculated using reduction to  convolution.  Algorithm with
+        max(N,N)*log(max(N,N)) complexity is used (see  ConvC1D()  for  more  info
+        about performance).
+
+        IMPORTANT:
+            for  historical reasons subroutine accepts its parameters in  reversed
+            order: CorrC1D(Signal, Pattern) = Pattern x Signal (using  traditional
+            definition of cross-correlation, denoting cross-correlation as "x").
 
         INPUT PARAMETERS
-            A   -   array[0..N-1] - complex function to be transformed
-            N   -   problem size
-            
+            Signal  -   array[0..N-1] - complex function to be transformed,
+                        signal containing pattern
+            N       -   problem size
+            Pattern -   array[0..M-1] - complex function to be transformed,
+                        pattern to search withing signal
+            M       -   problem size
+
         OUTPUT PARAMETERS
-            A   -   DFT of a input array, array[0..N-1]
-                    A_out[j] = SUM(A_in[k]*exp(-2*pi*sqrt(-1)*j*k/N), k = 0..N-1)
-
+            R       -   cross-correlation, array[0..N+M-2]:
+                        * positive lags are stored in R[0..N-1],
+                          R[i] = sum(conj(pattern[j])*signal[i+j]
+                        * negative lags are stored in R[N..N+M-2],
+                          R[N+M-1-i] = sum(conj(pattern[j])*signal[-i+j]
+
+        NOTE:
+            It is assumed that pattern domain is [0..M-1].  If Pattern is non-zero
+        on [-K..M-1],  you can still use this subroutine, just shift result by K.
 
           -- ALGLIB --
-             Copyright 29.05.2009 by Bochkanov Sergey
+             Copyright 21.07.2009 by Bochkanov Sergey
         *************************************************************************/
-        public static void fftc1d(ref complex[] a,
-            int n)
-        {
-            ftbase.ftplan plan = new ftbase.ftplan();
-            int i = 0;
-            double[] buf = new double[0];
-
-            ap.assert(n>0, "FFTC1D: incorrect N!");
-            ap.assert(ap.len(a)>=n, "FFTC1D: Length(A)<N!");
-            ap.assert(apserv.isfinitecvector(a, n), "FFTC1D: A contains infinite or NAN values!");
-            
-            //
-            // Special case: N=1, FFT is just identity transform.
-            // After this block we assume that N is strictly greater than 1.
-            //
-            if( n==1 )
-            {
-                return;
-            }
-            
-            //
-            // convert input array to the more convinient format
-            //
-            buf = new double[2*n];
-            for(i=0; i<=n-1; i++)
-            {
-                buf[2*i+0] = a[i].x;
-                buf[2*i+1] = a[i].y;
-            }
-            
-            //
-            // Generate plan and execute it.
-            //
-            // Plan is a combination of a successive factorizations of N and
-            // precomputed data. It is much like a FFTW plan, but is not stored
-            // between subroutine calls and is much simpler.
-            //
-            ftbase.ftbasegeneratecomplexfftplan(n, plan);
-            ftbase.ftbaseexecuteplan(ref buf, 0, n, plan);
-            
-            //
-            // result
-            //
-            for(i=0; i<=n-1; i++)
-            {
-                a[i].x = buf[2*i+0];
-                a[i].y = buf[2*i+1];
-            }
-        }
-
-
-        /*************************************************************************
-        1-dimensional complex inverse FFT.
-
-        Array size N may be arbitrary number (composite or prime).  Algorithm  has
-        O(N*logN) complexity for any N (composite or prime).
-
-        See FFTC1D() description for more information about algorithm performance.
-
-        INPUT PARAMETERS
-            A   -   array[0..N-1] - complex array to be transformed
-            N   -   problem size
-
-        OUTPUT PARAMETERS
-            A   -   inverse DFT of a input array, array[0..N-1]
-                    A_out[j] = SUM(A_in[k]/N*exp(+2*pi*sqrt(-1)*j*k/N), k = 0..N-1)
-
-
-          -- ALGLIB --
-             Copyright 29.05.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void fftc1dinv(ref complex[] a,
-            int n)
-        {
-            int i = 0;
-
-            ap.assert(n>0, "FFTC1DInv: incorrect N!");
-            ap.assert(ap.len(a)>=n, "FFTC1DInv: Length(A)<N!");
-            ap.assert(apserv.isfinitecvector(a, n), "FFTC1DInv: A contains infinite or NAN values!");
-            
-            //
-            // Inverse DFT can be expressed in terms of the DFT as
-            //
-            //     invfft(x) = fft(x')'/N
-            //
-            // here x' means conj(x).
-            //
-            for(i=0; i<=n-1; i++)
-            {
-                a[i].y = -a[i].y;
-            }
-            fftc1d(ref a, n);
-            for(i=0; i<=n-1; i++)
-            {
-                a[i].x = a[i].x/n;
-                a[i].y = -(a[i].y/n);
-            }
-        }
-
-
-        /*************************************************************************
-        1-dimensional real FFT.
-
-        Algorithm has O(N*logN) complexity for any N (composite or prime).
-
-        INPUT PARAMETERS
-            A   -   array[0..N-1] - real function to be transformed
-            N   -   problem size
-
-        OUTPUT PARAMETERS
-            F   -   DFT of a input array, array[0..N-1]
-                    F[j] = SUM(A[k]*exp(-2*pi*sqrt(-1)*j*k/N), k = 0..N-1)
-
-        NOTE:
-            F[] satisfies symmetry property F[k] = conj(F[N-k]),  so just one half
-        of  array  is  usually needed. But for convinience subroutine returns full
-        complex array (with frequencies above N/2), so its result may be  used  by
-        other FFT-related subroutines.
-
-
-          -- ALGLIB --
-             Copyright 01.06.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void fftr1d(double[] a,
-            int n,
-            ref complex[] f)
-        {
-            int i = 0;
-            int n2 = 0;
-            int idx = 0;
-            complex hn = 0;
-            complex hmnc = 0;
-            complex v = 0;
-            double[] buf = new double[0];
-            ftbase.ftplan plan = new ftbase.ftplan();
-            int i_ = 0;
-
-            f = new complex[0];
-
-            ap.assert(n>0, "FFTR1D: incorrect N!");
-            ap.assert(ap.len(a)>=n, "FFTR1D: Length(A)<N!");
-            ap.assert(apserv.isfinitevector(a, n), "FFTR1D: A contains infinite or NAN values!");
-            
-            //
-            // Special cases:
-            // * N=1, FFT is just identity transform.
-            // * N=2, FFT is simple too
-            //
-            // After this block we assume that N is strictly greater than 2
-            //
-            if( n==1 )
-            {
-                f = new complex[1];
-                f[0] = a[0];
-                return;
-            }
-            if( n==2 )
-            {
-                f = new complex[2];
-                f[0].x = a[0]+a[1];
-                f[0].y = 0;
-                f[1].x = a[0]-a[1];
-                f[1].y = 0;
-                return;
-            }
-            
-            //
-            // Choose between odd-size and even-size FFTs
-            //
-            if( n%2==0 )
-            {
-                
-                //
-                // even-size real FFT, use reduction to the complex task
-                //
-                n2 = n/2;
-                buf = new double[n];
-                for(i_=0; i_<=n-1;i_++)
-                {
-                    buf[i_] = a[i_];
-                }
-                ftbase.ftbasegeneratecomplexfftplan(n2, plan);
-                ftbase.ftbaseexecuteplan(ref buf, 0, n2, plan);
-                f = new complex[n];
-                for(i=0; i<=n2; i++)
-                {
-                    idx = 2*(i%n2);
-                    hn.x = buf[idx+0];
-                    hn.y = buf[idx+1];
-                    idx = 2*((n2-i)%n2);
-                    hmnc.x = buf[idx+0];
-                    hmnc.y = -buf[idx+1];
-                    v.x = -Math.Sin(-(2*Math.PI*i/n));
-                    v.y = Math.Cos(-(2*Math.PI*i/n));
-                    f[i] = hn+hmnc-v*(hn-hmnc);
-                    f[i].x = 0.5*f[i].x;
-                    f[i].y = 0.5*f[i].y;
-                }
-                for(i=n2+1; i<=n-1; i++)
-                {
-                    f[i] = math.conj(f[n-i]);
-                }
-            }
-            else
-            {
-                
-                //
-                // use complex FFT
-                //
-                f = new complex[n];
-                for(i=0; i<=n-1; i++)
-                {
-                    f[i] = a[i];
-                }
-                fftc1d(ref f, n);
-            }
-        }
-
-
-        /*************************************************************************
-        1-dimensional real inverse FFT.
-
-        Algorithm has O(N*logN) complexity for any N (composite or prime).
-
-        INPUT PARAMETERS
-            F   -   array[0..floor(N/2)] - frequencies from forward real FFT
-            N   -   problem size
-
-        OUTPUT PARAMETERS
-            A   -   inverse DFT of a input array, array[0..N-1]
-
-        NOTE:
-            F[] should satisfy symmetry property F[k] = conj(F[N-k]), so just  one
-        half of frequencies array is needed - elements from 0 to floor(N/2).  F[0]
-        is ALWAYS real. If N is even F[floor(N/2)] is real too. If N is odd,  then
-        F[floor(N/2)] has no special properties.
-
-        Relying on properties noted above, FFTR1DInv subroutine uses only elements
-        from 0th to floor(N/2)-th. It ignores imaginary part of F[0],  and in case
-        N is even it ignores imaginary part of F[floor(N/2)] too.
-
-        When you call this function using full arguments list - "FFTR1DInv(F,N,A)"
-        - you can pass either either frequencies array with N elements or  reduced
-        array with roughly N/2 elements - subroutine will  successfully  transform
-        both.
-
-        If you call this function using reduced arguments list -  "FFTR1DInv(F,A)"
-        - you must pass FULL array with N elements (although higher  N/2 are still
-        not used) because array size is used to automatically determine FFT length
-
-
-          -- ALGLIB --
-             Copyright 01.06.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void fftr1dinv(complex[] f,
-            int n,
-            ref double[] a)
-        {
-            int i = 0;
-            double[] h = new double[0];
-            complex[] fh = new complex[0];
-
-            a = new double[0];
-
-            ap.assert(n>0, "FFTR1DInv: incorrect N!");
-            ap.assert(ap.len(f)>=(int)Math.Floor((double)n/(double)2)+1, "FFTR1DInv: Length(F)<Floor(N/2)+1!");
-            ap.assert(math.isfinite(f[0].x), "FFTR1DInv: F contains infinite or NAN values!");
-            for(i=1; i<=(int)Math.Floor((double)n/(double)2)-1; i++)
-            {
-                ap.assert(math.isfinite(f[i].x) & math.isfinite(f[i].y), "FFTR1DInv: F contains infinite or NAN values!");
-            }
-            ap.assert(math.isfinite(f[(int)Math.Floor((double)n/(double)2)].x), "FFTR1DInv: F contains infinite or NAN values!");
-            if( n%2!=0 )
-            {
-                ap.assert(math.isfinite(f[(int)Math.Floor((double)n/(double)2)].y), "FFTR1DInv: F contains infinite or NAN values!");
-            }
-            
-            //
-            // Special case: N=1, FFT is just identity transform.
-            // After this block we assume that N is strictly greater than 1.
-            //
-            if( n==1 )
-            {
-                a = new double[1];
-                a[0] = f[0].x;
-                return;
-            }
-            
-            //
-            // inverse real FFT is reduced to the inverse real FHT,
-            // which is reduced to the forward real FHT,
-            // which is reduced to the forward real FFT.
-            //
-            // Don't worry, it is really compact and efficient reduction :)
-            //
-            h = new double[n];
-            a = new double[n];
-            h[0] = f[0].x;
-            for(i=1; i<=(int)Math.Floor((double)n/(double)2)-1; i++)
-            {
-                h[i] = f[i].x-f[i].y;
-                h[n-i] = f[i].x+f[i].y;
-            }
-            if( n%2==0 )
-            {
-                h[(int)Math.Floor((double)n/(double)2)] = f[(int)Math.Floor((double)n/(double)2)].x;
-            }
-            else
-            {
-                h[(int)Math.Floor((double)n/(double)2)] = f[(int)Math.Floor((double)n/(double)2)].x-f[(int)Math.Floor((double)n/(double)2)].y;
-                h[(int)Math.Floor((double)n/(double)2)+1] = f[(int)Math.Floor((double)n/(double)2)].x+f[(int)Math.Floor((double)n/(double)2)].y;
-            }
-            fftr1d(h, n, ref fh);
-            for(i=0; i<=n-1; i++)
-            {
-                a[i] = (fh[i].x-fh[i].y)/n;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Never call it directly!
-
-
-          -- ALGLIB --
-             Copyright 01.06.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void fftr1dinternaleven(ref double[] a,
-            int n,
-            ref double[] buf,
-            ftbase.ftplan plan)
-        {
-            double x = 0;
-            double y = 0;
-            int i = 0;
-            int n2 = 0;
-            int idx = 0;
-            complex hn = 0;
-            complex hmnc = 0;
-            complex v = 0;
-            int i_ = 0;
-
-            ap.assert(n>0 & n%2==0, "FFTR1DEvenInplace: incorrect N!");
-            
-            //
-            // Special cases:
-            // * N=2
-            //
-            // After this block we assume that N is strictly greater than 2
-            //
-            if( n==2 )
-            {
-                x = a[0]+a[1];
-                y = a[0]-a[1];
-                a[0] = x;
-                a[1] = y;
-                return;
-            }
-            
-            //
-            // even-size real FFT, use reduction to the complex task
-            //
-            n2 = n/2;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                buf[i_] = a[i_];
-            }
-            ftbase.ftbaseexecuteplan(ref buf, 0, n2, plan);
-            a[0] = buf[0]+buf[1];
-            for(i=1; i<=n2-1; i++)
-            {
-                idx = 2*(i%n2);
-                hn.x = buf[idx+0];
-                hn.y = buf[idx+1];
-                idx = 2*(n2-i);
-                hmnc.x = buf[idx+0];
-                hmnc.y = -buf[idx+1];
-                v.x = -Math.Sin(-(2*Math.PI*i/n));
-                v.y = Math.Cos(-(2*Math.PI*i/n));
-                v = hn+hmnc-v*(hn-hmnc);
-                a[2*i+0] = 0.5*v.x;
-                a[2*i+1] = 0.5*v.y;
-            }
-            a[1] = buf[0]-buf[1];
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Never call it directly!
-
-
-          -- ALGLIB --
-             Copyright 01.06.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void fftr1dinvinternaleven(ref double[] a,
-            int n,
-            ref double[] buf,
-            ftbase.ftplan plan)
-        {
-            double x = 0;
-            double y = 0;
-            double t = 0;
-            int i = 0;
-            int n2 = 0;
-
-            ap.assert(n>0 & n%2==0, "FFTR1DInvInternalEven: incorrect N!");
-            
-            //
-            // Special cases:
-            // * N=2
-            //
-            // After this block we assume that N is strictly greater than 2
-            //
-            if( n==2 )
-            {
-                x = 0.5*(a[0]+a[1]);
-                y = 0.5*(a[0]-a[1]);
-                a[0] = x;
-                a[1] = y;
-                return;
-            }
-            
-            //
-            // inverse real FFT is reduced to the inverse real FHT,
-            // which is reduced to the forward real FHT,
-            // which is reduced to the forward real FFT.
-            //
-            // Don't worry, it is really compact and efficient reduction :)
-            //
-            n2 = n/2;
-            buf[0] = a[0];
-            for(i=1; i<=n2-1; i++)
-            {
-                x = a[2*i+0];
-                y = a[2*i+1];
-                buf[i] = x-y;
-                buf[n-i] = x+y;
-            }
-            buf[n2] = a[1];
-            fftr1dinternaleven(ref buf, n, ref a, plan);
-            a[0] = buf[0]/n;
-            t = (double)1/(double)n;
-            for(i=1; i<=n2-1; i++)
-            {
-                x = buf[2*i+0];
-                y = buf[2*i+1];
-                a[i] = t*(x-y);
-                a[n-i] = t*(x+y);
-            }
-            a[n2] = buf[1]/n;
-        }
-
-
-    }
-    public class corr
-    {
         public static void corrc1d(complex[] signal,
             int n,
@@ -2883,4 +2919,31 @@
 
 
+        /*************************************************************************
+        1-dimensional circular complex cross-correlation.
+
+        For given Pattern/Signal returns corr(Pattern,Signal) (circular).
+        Algorithm has linearithmic complexity for any M/N.
+
+        IMPORTANT:
+            for  historical reasons subroutine accepts its parameters in  reversed
+            order:   CorrC1DCircular(Signal, Pattern) = Pattern x Signal    (using
+            traditional definition of cross-correlation, denoting cross-correlation
+            as "x").
+
+        INPUT PARAMETERS
+            Signal  -   array[0..N-1] - complex function to be transformed,
+                        periodic signal containing pattern
+            N       -   problem size
+            Pattern -   array[0..M-1] - complex function to be transformed,
+                        non-periodic pattern to search withing signal
+            M       -   problem size
+
+        OUTPUT PARAMETERS
+            R   -   convolution: A*B. array[0..M-1].
+
+
+          -- ALGLIB --
+             Copyright 21.07.2009 by Bochkanov Sergey
+        *************************************************************************/
         public static void corrc1dcircular(complex[] signal,
             int m,
@@ -2955,4 +3018,40 @@
 
 
+        /*************************************************************************
+        1-dimensional real cross-correlation.
+
+        For given Pattern/Signal returns corr(Pattern,Signal) (non-circular).
+
+        Correlation is calculated using reduction to  convolution.  Algorithm with
+        max(N,N)*log(max(N,N)) complexity is used (see  ConvC1D()  for  more  info
+        about performance).
+
+        IMPORTANT:
+            for  historical reasons subroutine accepts its parameters in  reversed
+            order: CorrR1D(Signal, Pattern) = Pattern x Signal (using  traditional
+            definition of cross-correlation, denoting cross-correlation as "x").
+
+        INPUT PARAMETERS
+            Signal  -   array[0..N-1] - real function to be transformed,
+                        signal containing pattern
+            N       -   problem size
+            Pattern -   array[0..M-1] - real function to be transformed,
+                        pattern to search withing signal
+            M       -   problem size
+
+        OUTPUT PARAMETERS
+            R       -   cross-correlation, array[0..N+M-2]:
+                        * positive lags are stored in R[0..N-1],
+                          R[i] = sum(pattern[j]*signal[i+j]
+                        * negative lags are stored in R[N..N+M-2],
+                          R[N+M-1-i] = sum(pattern[j]*signal[-i+j]
+
+        NOTE:
+            It is assumed that pattern domain is [0..M-1].  If Pattern is non-zero
+        on [-K..M-1],  you can still use this subroutine, just shift result by K.
+
+          -- ALGLIB --
+             Copyright 21.07.2009 by Bochkanov Sergey
+        *************************************************************************/
         public static void corrr1d(double[] signal,
             int n,
@@ -2993,4 +3092,31 @@
 
 
+        /*************************************************************************
+        1-dimensional circular real cross-correlation.
+
+        For given Pattern/Signal returns corr(Pattern,Signal) (circular).
+        Algorithm has linearithmic complexity for any M/N.
+
+        IMPORTANT:
+            for  historical reasons subroutine accepts its parameters in  reversed
+            order:   CorrR1DCircular(Signal, Pattern) = Pattern x Signal    (using
+            traditional definition of cross-correlation, denoting cross-correlation
+            as "x").
+
+        INPUT PARAMETERS
+            Signal  -   array[0..N-1] - real function to be transformed,
+                        periodic signal containing pattern
+            N       -   problem size
+            Pattern -   array[0..M-1] - real function to be transformed,
+                        non-periodic pattern to search withing signal
+            M       -   problem size
+
+        OUTPUT PARAMETERS
+            R   -   convolution: A*B. array[0..M-1].
+
+
+          -- ALGLIB --
+             Copyright 21.07.2009 by Bochkanov Sergey
+        *************************************************************************/
         public static void corrr1dcircular(double[] signal,
             int m,
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/integration.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/integration.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/integration.cs	(revision 7294)
@@ -21,249 +21,4 @@
 using System;
 
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Integration report:
-    * TerminationType = completetion code:
-        * -5    non-convergence of Gauss-Kronrod nodes
-                calculation subroutine.
-        * -1    incorrect parameters were specified
-        *  1    OK
-    * Rep.NFEV countains number of function calculations
-    * Rep.NIntervals contains number of intervals [a,b]
-      was partitioned into.
-    *************************************************************************/
-    public class autogkreport
-    {
-        //
-        // Public declarations
-        //
-        public int terminationtype { get { return _innerobj.terminationtype; } set { _innerobj.terminationtype = value; } }
-        public int nfev { get { return _innerobj.nfev; } set { _innerobj.nfev = value; } }
-        public int nintervals { get { return _innerobj.nintervals; } set { _innerobj.nintervals = value; } }
-
-        public autogkreport()
-        {
-            _innerobj = new autogk.autogkreport();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private autogk.autogkreport _innerobj;
-        public autogk.autogkreport innerobj { get { return _innerobj; } }
-        public autogkreport(autogk.autogkreport obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-
-    /*************************************************************************
-    This structure stores state of the integration algorithm.
-
-    Although this class has public fields,  they are not intended for external
-    use. You should use ALGLIB functions to work with this class:
-    * autogksmooth()/AutoGKSmoothW()/... to create objects
-    * autogkintegrate() to begin integration
-    * autogkresults() to get results
-    *************************************************************************/
-    public class autogkstate
-    {
-        //
-        // Public declarations
-        //
-        public bool needf { get { return _innerobj.needf; } set { _innerobj.needf = value; } }
-        public double x { get { return _innerobj.x; } set { _innerobj.x = value; } }
-        public double xminusa { get { return _innerobj.xminusa; } set { _innerobj.xminusa = value; } }
-        public double bminusx { get { return _innerobj.bminusx; } set { _innerobj.bminusx = value; } }
-        public double f { get { return _innerobj.f; } set { _innerobj.f = value; } }
-
-        public autogkstate()
-        {
-            _innerobj = new autogk.autogkstate();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private autogk.autogkstate _innerobj;
-        public autogk.autogkstate innerobj { get { return _innerobj; } }
-        public autogkstate(autogk.autogkstate obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-    /*************************************************************************
-    Integration of a smooth function F(x) on a finite interval [a,b].
-
-    Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
-    is calculated with accuracy close to the machine precision.
-
-    Algorithm works well only with smooth integrands.  It  may  be  used  with
-    continuous non-smooth integrands, but with  less  performance.
-
-    It should never be used with integrands which have integrable singularities
-    at lower or upper limits - algorithm may crash. Use AutoGKSingular in such
-    cases.
-
-    INPUT PARAMETERS:
-        A, B    -   interval boundaries (A<B, A=B or A>B)
-
-    OUTPUT PARAMETERS
-        State   -   structure which stores algorithm state
-
-    SEE ALSO
-        AutoGKSmoothW, AutoGKSingular, AutoGKResults.
-
-
-      -- ALGLIB --
-         Copyright 06.05.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void autogksmooth(double a, double b, out autogkstate state)
-    {
-        state = new autogkstate();
-        autogk.autogksmooth(a, b, state.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Integration of a smooth function F(x) on a finite interval [a,b].
-
-    This subroutine is same as AutoGKSmooth(), but it guarantees that interval
-    [a,b] is partitioned into subintervals which have width at most XWidth.
-
-    Subroutine  can  be  used  when  integrating nearly-constant function with
-    narrow "bumps" (about XWidth wide). If "bumps" are too narrow, AutoGKSmooth
-    subroutine can overlook them.
-
-    INPUT PARAMETERS:
-        A, B    -   interval boundaries (A<B, A=B or A>B)
-
-    OUTPUT PARAMETERS
-        State   -   structure which stores algorithm state
-
-    SEE ALSO
-        AutoGKSmooth, AutoGKSingular, AutoGKResults.
-
-
-      -- ALGLIB --
-         Copyright 06.05.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void autogksmoothw(double a, double b, double xwidth, out autogkstate state)
-    {
-        state = new autogkstate();
-        autogk.autogksmoothw(a, b, xwidth, state.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Integration on a finite interval [A,B].
-    Integrand have integrable singularities at A/B.
-
-    F(X) must diverge as "(x-A)^alpha" at A, as "(B-x)^beta" at B,  with known
-    alpha/beta (alpha>-1, beta>-1).  If alpha/beta  are  not known,  estimates
-    from below can be used (but these estimates should be greater than -1 too).
-
-    One  of  alpha/beta variables (or even both alpha/beta) may be equal to 0,
-    which means than function F(x) is non-singular at A/B. Anyway (singular at
-    bounds or not), function F(x) is supposed to be continuous on (A,B).
-
-    Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
-    is calculated with accuracy close to the machine precision.
-
-    INPUT PARAMETERS:
-        A, B    -   interval boundaries (A<B, A=B or A>B)
-        Alpha   -   power-law coefficient of the F(x) at A,
-                    Alpha>-1
-        Beta    -   power-law coefficient of the F(x) at B,
-                    Beta>-1
-
-    OUTPUT PARAMETERS
-        State   -   structure which stores algorithm state
-
-    SEE ALSO
-        AutoGKSmooth, AutoGKSmoothW, AutoGKResults.
-
-
-      -- ALGLIB --
-         Copyright 06.05.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void autogksingular(double a, double b, double alpha, double beta, out autogkstate state)
-    {
-        state = new autogkstate();
-        autogk.autogksingular(a, b, alpha, beta, state.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    This function provides reverse communication interface
-    Reverse communication interface is not documented or recommended to use.
-    See below for functions which provide better documented API
-    *************************************************************************/
-    public static bool autogkiteration(autogkstate state)
-    {
-
-        bool result = autogk.autogkiteration(state.innerobj);
-        return result;
-    }
-
-
-    /*************************************************************************
-    This function is used to launcn iterations of ODE solver
-
-    It accepts following parameters:
-        diff    -   callback which calculates dy/dx for given y and x
-        obj     -   optional object which is passed to diff; can be NULL
-
-
-      -- ALGLIB --
-         Copyright 07.05.2009 by Bochkanov Sergey
-
-    *************************************************************************/
-    public static void autogkintegrate(autogkstate state, integrator1_func func, object obj)
-    {
-        if( func==null )
-            throw new alglibexception("ALGLIB: error in 'autogkintegrate()' (func is null)");
-        while( alglib.autogkiteration(state) )
-        {
-            if( state.needf )
-            {
-                func(state.innerobj.x, state.innerobj.xminusa, state.innerobj.bminusx, ref state.innerobj.f, obj);
-                continue;
-            }
-            throw new alglibexception("ALGLIB: unexpected error in 'autogksolve'");
-        }
-    }
-
-    /*************************************************************************
-    Adaptive integration results
-
-    Called after AutoGKIteration returned False.
-
-    Input parameters:
-        State   -   algorithm state (used by AutoGKIteration).
-
-    Output parameters:
-        V       -   integral(f(x)dx,a,b)
-        Rep     -   optimization report (see AutoGKReport description)
-
-      -- ALGLIB --
-         Copyright 14.11.2007 by Bochkanov Sergey
-    *************************************************************************/
-    public static void autogkresults(autogkstate state, out double v, out autogkreport rep)
-    {
-        v = 0;
-        rep = new autogkreport();
-        autogk.autogkresults(state.innerobj, ref v, rep.innerobj);
-        return;
-    }
-
-}
 public partial class alglib
 {
@@ -764,1123 +519,249 @@
 public partial class alglib
 {
-    public class autogk
+
+
+    /*************************************************************************
+    Integration report:
+    * TerminationType = completetion code:
+        * -5    non-convergence of Gauss-Kronrod nodes
+                calculation subroutine.
+        * -1    incorrect parameters were specified
+        *  1    OK
+    * Rep.NFEV countains number of function calculations
+    * Rep.NIntervals contains number of intervals [a,b]
+      was partitioned into.
+    *************************************************************************/
+    public class autogkreport
     {
-        /*************************************************************************
-        Integration report:
-        * TerminationType = completetion code:
-            * -5    non-convergence of Gauss-Kronrod nodes
-                    calculation subroutine.
-            * -1    incorrect parameters were specified
-            *  1    OK
-        * Rep.NFEV countains number of function calculations
-        * Rep.NIntervals contains number of intervals [a,b]
-          was partitioned into.
-        *************************************************************************/
-        public class autogkreport
+        //
+        // Public declarations
+        //
+        public int terminationtype { get { return _innerobj.terminationtype; } set { _innerobj.terminationtype = value; } }
+        public int nfev { get { return _innerobj.nfev; } set { _innerobj.nfev = value; } }
+        public int nintervals { get { return _innerobj.nintervals; } set { _innerobj.nintervals = value; } }
+
+        public autogkreport()
         {
-            public int terminationtype;
-            public int nfev;
-            public int nintervals;
-        };
-
-
-        public class autogkinternalstate
+            _innerobj = new autogk.autogkreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private autogk.autogkreport _innerobj;
+        public autogk.autogkreport innerobj { get { return _innerobj; } }
+        public autogkreport(autogk.autogkreport obj)
         {
-            public double a;
-            public double b;
-            public double eps;
-            public double xwidth;
-            public double x;
-            public double f;
-            public int info;
-            public double r;
-            public double[,] heap;
-            public int heapsize;
-            public int heapwidth;
-            public int heapused;
-            public double sumerr;
-            public double sumabs;
-            public double[] qn;
-            public double[] wg;
-            public double[] wk;
-            public double[] wr;
-            public int n;
-            public rcommstate rstate;
-            public autogkinternalstate()
-            {
-                heap = new double[0,0];
-                qn = new double[0];
-                wg = new double[0];
-                wk = new double[0];
-                wr = new double[0];
-                rstate = new rcommstate();
-            }
-        };
-
-
-        /*************************************************************************
-        This structure stores state of the integration algorithm.
-
-        Although this class has public fields,  they are not intended for external
-        use. You should use ALGLIB functions to work with this class:
-        * autogksmooth()/AutoGKSmoothW()/... to create objects
-        * autogkintegrate() to begin integration
-        * autogkresults() to get results
-        *************************************************************************/
-        public class autogkstate
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    This structure stores state of the integration algorithm.
+
+    Although this class has public fields,  they are not intended for external
+    use. You should use ALGLIB functions to work with this class:
+    * autogksmooth()/AutoGKSmoothW()/... to create objects
+    * autogkintegrate() to begin integration
+    * autogkresults() to get results
+    *************************************************************************/
+    public class autogkstate
+    {
+        //
+        // Public declarations
+        //
+        public bool needf { get { return _innerobj.needf; } set { _innerobj.needf = value; } }
+        public double x { get { return _innerobj.x; } set { _innerobj.x = value; } }
+        public double xminusa { get { return _innerobj.xminusa; } set { _innerobj.xminusa = value; } }
+        public double bminusx { get { return _innerobj.bminusx; } set { _innerobj.bminusx = value; } }
+        public double f { get { return _innerobj.f; } set { _innerobj.f = value; } }
+
+        public autogkstate()
         {
-            public double a;
-            public double b;
-            public double alpha;
-            public double beta;
-            public double xwidth;
-            public double x;
-            public double xminusa;
-            public double bminusx;
-            public bool needf;
-            public double f;
-            public int wrappermode;
-            public autogkinternalstate internalstate;
-            public rcommstate rstate;
-            public double v;
-            public int terminationtype;
-            public int nfev;
-            public int nintervals;
-            public autogkstate()
-            {
-                internalstate = new autogkinternalstate();
-                rstate = new rcommstate();
-            }
-        };
-
-
-
-
-        /*************************************************************************
-        Integration of a smooth function F(x) on a finite interval [a,b].
-
-        Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
-        is calculated with accuracy close to the machine precision.
-
-        Algorithm works well only with smooth integrands.  It  may  be  used  with
-        continuous non-smooth integrands, but with  less  performance.
-
-        It should never be used with integrands which have integrable singularities
-        at lower or upper limits - algorithm may crash. Use AutoGKSingular in such
-        cases.
-
-        INPUT PARAMETERS:
-            A, B    -   interval boundaries (A<B, A=B or A>B)
-            
-        OUTPUT PARAMETERS
-            State   -   structure which stores algorithm state
-
-        SEE ALSO
-            AutoGKSmoothW, AutoGKSingular, AutoGKResults.
-            
-
-          -- ALGLIB --
-             Copyright 06.05.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void autogksmooth(double a,
-            double b,
-            autogkstate state)
+            _innerobj = new autogk.autogkstate();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private autogk.autogkstate _innerobj;
+        public autogk.autogkstate innerobj { get { return _innerobj; } }
+        public autogkstate(autogk.autogkstate obj)
         {
-            ap.assert(math.isfinite(a), "AutoGKSmooth: A is not finite!");
-            ap.assert(math.isfinite(b), "AutoGKSmooth: B is not finite!");
-            autogksmoothw(a, b, 0.0, state);
+            _innerobj = obj;
         }
-
-
-        /*************************************************************************
-        Integration of a smooth function F(x) on a finite interval [a,b].
-
-        This subroutine is same as AutoGKSmooth(), but it guarantees that interval
-        [a,b] is partitioned into subintervals which have width at most XWidth.
-
-        Subroutine  can  be  used  when  integrating nearly-constant function with
-        narrow "bumps" (about XWidth wide). If "bumps" are too narrow, AutoGKSmooth
-        subroutine can overlook them.
-
-        INPUT PARAMETERS:
-            A, B    -   interval boundaries (A<B, A=B or A>B)
-
-        OUTPUT PARAMETERS
-            State   -   structure which stores algorithm state
-
-        SEE ALSO
-            AutoGKSmooth, AutoGKSingular, AutoGKResults.
-
-
-          -- ALGLIB --
-             Copyright 06.05.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void autogksmoothw(double a,
-            double b,
-            double xwidth,
-            autogkstate state)
+    }
+
+    /*************************************************************************
+    Integration of a smooth function F(x) on a finite interval [a,b].
+
+    Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
+    is calculated with accuracy close to the machine precision.
+
+    Algorithm works well only with smooth integrands.  It  may  be  used  with
+    continuous non-smooth integrands, but with  less  performance.
+
+    It should never be used with integrands which have integrable singularities
+    at lower or upper limits - algorithm may crash. Use AutoGKSingular in such
+    cases.
+
+    INPUT PARAMETERS:
+        A, B    -   interval boundaries (A<B, A=B or A>B)
+
+    OUTPUT PARAMETERS
+        State   -   structure which stores algorithm state
+
+    SEE ALSO
+        AutoGKSmoothW, AutoGKSingular, AutoGKResults.
+
+
+      -- ALGLIB --
+         Copyright 06.05.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void autogksmooth(double a, double b, out autogkstate state)
+    {
+        state = new autogkstate();
+        autogk.autogksmooth(a, b, state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Integration of a smooth function F(x) on a finite interval [a,b].
+
+    This subroutine is same as AutoGKSmooth(), but it guarantees that interval
+    [a,b] is partitioned into subintervals which have width at most XWidth.
+
+    Subroutine  can  be  used  when  integrating nearly-constant function with
+    narrow "bumps" (about XWidth wide). If "bumps" are too narrow, AutoGKSmooth
+    subroutine can overlook them.
+
+    INPUT PARAMETERS:
+        A, B    -   interval boundaries (A<B, A=B or A>B)
+
+    OUTPUT PARAMETERS
+        State   -   structure which stores algorithm state
+
+    SEE ALSO
+        AutoGKSmooth, AutoGKSingular, AutoGKResults.
+
+
+      -- ALGLIB --
+         Copyright 06.05.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void autogksmoothw(double a, double b, double xwidth, out autogkstate state)
+    {
+        state = new autogkstate();
+        autogk.autogksmoothw(a, b, xwidth, state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Integration on a finite interval [A,B].
+    Integrand have integrable singularities at A/B.
+
+    F(X) must diverge as "(x-A)^alpha" at A, as "(B-x)^beta" at B,  with known
+    alpha/beta (alpha>-1, beta>-1).  If alpha/beta  are  not known,  estimates
+    from below can be used (but these estimates should be greater than -1 too).
+
+    One  of  alpha/beta variables (or even both alpha/beta) may be equal to 0,
+    which means than function F(x) is non-singular at A/B. Anyway (singular at
+    bounds or not), function F(x) is supposed to be continuous on (A,B).
+
+    Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
+    is calculated with accuracy close to the machine precision.
+
+    INPUT PARAMETERS:
+        A, B    -   interval boundaries (A<B, A=B or A>B)
+        Alpha   -   power-law coefficient of the F(x) at A,
+                    Alpha>-1
+        Beta    -   power-law coefficient of the F(x) at B,
+                    Beta>-1
+
+    OUTPUT PARAMETERS
+        State   -   structure which stores algorithm state
+
+    SEE ALSO
+        AutoGKSmooth, AutoGKSmoothW, AutoGKResults.
+
+
+      -- ALGLIB --
+         Copyright 06.05.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void autogksingular(double a, double b, double alpha, double beta, out autogkstate state)
+    {
+        state = new autogkstate();
+        autogk.autogksingular(a, b, alpha, beta, state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This function provides reverse communication interface
+    Reverse communication interface is not documented or recommended to use.
+    See below for functions which provide better documented API
+    *************************************************************************/
+    public static bool autogkiteration(autogkstate state)
+    {
+
+        bool result = autogk.autogkiteration(state.innerobj);
+        return result;
+    }
+
+
+    /*************************************************************************
+    This function is used to launcn iterations of ODE solver
+
+    It accepts following parameters:
+        diff    -   callback which calculates dy/dx for given y and x
+        obj     -   optional object which is passed to diff; can be NULL
+
+
+      -- ALGLIB --
+         Copyright 07.05.2009 by Bochkanov Sergey
+
+    *************************************************************************/
+    public static void autogkintegrate(autogkstate state, integrator1_func func, object obj)
+    {
+        if( func==null )
+            throw new alglibexception("ALGLIB: error in 'autogkintegrate()' (func is null)");
+        while( alglib.autogkiteration(state) )
         {
-            ap.assert(math.isfinite(a), "AutoGKSmoothW: A is not finite!");
-            ap.assert(math.isfinite(b), "AutoGKSmoothW: B is not finite!");
-            ap.assert(math.isfinite(xwidth), "AutoGKSmoothW: XWidth is not finite!");
-            state.wrappermode = 0;
-            state.a = a;
-            state.b = b;
-            state.xwidth = xwidth;
-            state.needf = false;
-            state.rstate.ra = new double[10+1];
-            state.rstate.stage = -1;
+            if( state.needf )
+            {
+                func(state.innerobj.x, state.innerobj.xminusa, state.innerobj.bminusx, ref state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: unexpected error in 'autogksolve'");
         }
-
-
-        /*************************************************************************
-        Integration on a finite interval [A,B].
-        Integrand have integrable singularities at A/B.
-
-        F(X) must diverge as "(x-A)^alpha" at A, as "(B-x)^beta" at B,  with known
-        alpha/beta (alpha>-1, beta>-1).  If alpha/beta  are  not known,  estimates
-        from below can be used (but these estimates should be greater than -1 too).
-
-        One  of  alpha/beta variables (or even both alpha/beta) may be equal to 0,
-        which means than function F(x) is non-singular at A/B. Anyway (singular at
-        bounds or not), function F(x) is supposed to be continuous on (A,B).
-
-        Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
-        is calculated with accuracy close to the machine precision.
-
-        INPUT PARAMETERS:
-            A, B    -   interval boundaries (A<B, A=B or A>B)
-            Alpha   -   power-law coefficient of the F(x) at A,
-                        Alpha>-1
-            Beta    -   power-law coefficient of the F(x) at B,
-                        Beta>-1
-
-        OUTPUT PARAMETERS
-            State   -   structure which stores algorithm state
-
-        SEE ALSO
-            AutoGKSmooth, AutoGKSmoothW, AutoGKResults.
-
-
-          -- ALGLIB --
-             Copyright 06.05.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void autogksingular(double a,
-            double b,
-            double alpha,
-            double beta,
-            autogkstate state)
-        {
-            ap.assert(math.isfinite(a), "AutoGKSingular: A is not finite!");
-            ap.assert(math.isfinite(b), "AutoGKSingular: B is not finite!");
-            ap.assert(math.isfinite(alpha), "AutoGKSingular: Alpha is not finite!");
-            ap.assert(math.isfinite(beta), "AutoGKSingular: Beta is not finite!");
-            state.wrappermode = 1;
-            state.a = a;
-            state.b = b;
-            state.alpha = alpha;
-            state.beta = beta;
-            state.xwidth = 0.0;
-            state.needf = false;
-            state.rstate.ra = new double[10+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-
-          -- ALGLIB --
-             Copyright 07.05.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool autogkiteration(autogkstate state)
-        {
-            bool result = new bool();
-            double s = 0;
-            double tmp = 0;
-            double eps = 0;
-            double a = 0;
-            double b = 0;
-            double x = 0;
-            double t = 0;
-            double alpha = 0;
-            double beta = 0;
-            double v1 = 0;
-            double v2 = 0;
-
-            
-            //
-            // Reverse communication preparations
-            // I know it looks ugly, but it works the same way
-            // anywhere from C++ to Python.
-            //
-            // This code initializes locals by:
-            // * random values determined during code
-            //   generation - on first subroutine call
-            // * values from previous call - on subsequent calls
-            //
-            if( state.rstate.stage>=0 )
-            {
-                s = state.rstate.ra[0];
-                tmp = state.rstate.ra[1];
-                eps = state.rstate.ra[2];
-                a = state.rstate.ra[3];
-                b = state.rstate.ra[4];
-                x = state.rstate.ra[5];
-                t = state.rstate.ra[6];
-                alpha = state.rstate.ra[7];
-                beta = state.rstate.ra[8];
-                v1 = state.rstate.ra[9];
-                v2 = state.rstate.ra[10];
-            }
-            else
-            {
-                s = -983;
-                tmp = -989;
-                eps = -834;
-                a = 900;
-                b = -287;
-                x = 364;
-                t = 214;
-                alpha = -338;
-                beta = -686;
-                v1 = 912;
-                v2 = 585;
-            }
-            if( state.rstate.stage==0 )
-            {
-                goto lbl_0;
-            }
-            if( state.rstate.stage==1 )
-            {
-                goto lbl_1;
-            }
-            if( state.rstate.stage==2 )
-            {
-                goto lbl_2;
-            }
-            
-            //
-            // Routine body
-            //
-            eps = 0;
-            a = state.a;
-            b = state.b;
-            alpha = state.alpha;
-            beta = state.beta;
-            state.terminationtype = -1;
-            state.nfev = 0;
-            state.nintervals = 0;
-            
-            //
-            // smooth function  at a finite interval
-            //
-            if( state.wrappermode!=0 )
-            {
-                goto lbl_3;
-            }
-            
-            //
-            // special case
-            //
-            if( (double)(a)==(double)(b) )
-            {
-                state.terminationtype = 1;
-                state.v = 0;
-                result = false;
-                return result;
-            }
-            
-            //
-            // general case
-            //
-            autogkinternalprepare(a, b, eps, state.xwidth, state.internalstate);
-        lbl_5:
-            if( !autogkinternaliteration(state.internalstate) )
-            {
-                goto lbl_6;
-            }
-            x = state.internalstate.x;
-            state.x = x;
-            state.xminusa = x-a;
-            state.bminusx = b-x;
-            state.needf = true;
-            state.rstate.stage = 0;
-            goto lbl_rcomm;
-        lbl_0:
-            state.needf = false;
-            state.nfev = state.nfev+1;
-            state.internalstate.f = state.f;
-            goto lbl_5;
-        lbl_6:
-            state.v = state.internalstate.r;
-            state.terminationtype = state.internalstate.info;
-            state.nintervals = state.internalstate.heapused;
-            result = false;
-            return result;
-        lbl_3:
-            
-            //
-            // function with power-law singularities at the ends of a finite interval
-            //
-            if( state.wrappermode!=1 )
-            {
-                goto lbl_7;
-            }
-            
-            //
-            // test coefficients
-            //
-            if( (double)(alpha)<=(double)(-1) | (double)(beta)<=(double)(-1) )
-            {
-                state.terminationtype = -1;
-                state.v = 0;
-                result = false;
-                return result;
-            }
-            
-            //
-            // special cases
-            //
-            if( (double)(a)==(double)(b) )
-            {
-                state.terminationtype = 1;
-                state.v = 0;
-                result = false;
-                return result;
-            }
-            
-            //
-            // reduction to general form
-            //
-            if( (double)(a)<(double)(b) )
-            {
-                s = 1;
-            }
-            else
-            {
-                s = -1;
-                tmp = a;
-                a = b;
-                b = tmp;
-                tmp = alpha;
-                alpha = beta;
-                beta = tmp;
-            }
-            alpha = Math.Min(alpha, 0);
-            beta = Math.Min(beta, 0);
-            
-            //
-            // first, integrate left half of [a,b]:
-            //     integral(f(x)dx, a, (b+a)/2) =
-            //     = 1/(1+alpha) * integral(t^(-alpha/(1+alpha))*f(a+t^(1/(1+alpha)))dt, 0, (0.5*(b-a))^(1+alpha))
-            //
-            autogkinternalprepare(0, Math.Pow(0.5*(b-a), 1+alpha), eps, state.xwidth, state.internalstate);
-        lbl_9:
-            if( !autogkinternaliteration(state.internalstate) )
-            {
-                goto lbl_10;
-            }
-            
-            //
-            // Fill State.X, State.XMinusA, State.BMinusX.
-            // Latter two are filled correctly even if B<A.
-            //
-            x = state.internalstate.x;
-            t = Math.Pow(x, 1/(1+alpha));
-            state.x = a+t;
-            if( (double)(s)>(double)(0) )
-            {
-                state.xminusa = t;
-                state.bminusx = b-(a+t);
-            }
-            else
-            {
-                state.xminusa = a+t-b;
-                state.bminusx = -t;
-            }
-            state.needf = true;
-            state.rstate.stage = 1;
-            goto lbl_rcomm;
-        lbl_1:
-            state.needf = false;
-            if( (double)(alpha)!=(double)(0) )
-            {
-                state.internalstate.f = state.f*Math.Pow(x, -(alpha/(1+alpha)))/(1+alpha);
-            }
-            else
-            {
-                state.internalstate.f = state.f;
-            }
-            state.nfev = state.nfev+1;
-            goto lbl_9;
-        lbl_10:
-            v1 = state.internalstate.r;
-            state.nintervals = state.nintervals+state.internalstate.heapused;
-            
-            //
-            // then, integrate right half of [a,b]:
-            //     integral(f(x)dx, (b+a)/2, b) =
-            //     = 1/(1+beta) * integral(t^(-beta/(1+beta))*f(b-t^(1/(1+beta)))dt, 0, (0.5*(b-a))^(1+beta))
-            //
-            autogkinternalprepare(0, Math.Pow(0.5*(b-a), 1+beta), eps, state.xwidth, state.internalstate);
-        lbl_11:
-            if( !autogkinternaliteration(state.internalstate) )
-            {
-                goto lbl_12;
-            }
-            
-            //
-            // Fill State.X, State.XMinusA, State.BMinusX.
-            // Latter two are filled correctly (X-A, B-X) even if B<A.
-            //
-            x = state.internalstate.x;
-            t = Math.Pow(x, 1/(1+beta));
-            state.x = b-t;
-            if( (double)(s)>(double)(0) )
-            {
-                state.xminusa = b-t-a;
-                state.bminusx = t;
-            }
-            else
-            {
-                state.xminusa = -t;
-                state.bminusx = a-(b-t);
-            }
-            state.needf = true;
-            state.rstate.stage = 2;
-            goto lbl_rcomm;
-        lbl_2:
-            state.needf = false;
-            if( (double)(beta)!=(double)(0) )
-            {
-                state.internalstate.f = state.f*Math.Pow(x, -(beta/(1+beta)))/(1+beta);
-            }
-            else
-            {
-                state.internalstate.f = state.f;
-            }
-            state.nfev = state.nfev+1;
-            goto lbl_11;
-        lbl_12:
-            v2 = state.internalstate.r;
-            state.nintervals = state.nintervals+state.internalstate.heapused;
-            
-            //
-            // final result
-            //
-            state.v = s*(v1+v2);
-            state.terminationtype = 1;
-            result = false;
-            return result;
-        lbl_7:
-            result = false;
-            return result;
-            
-            //
-            // Saving state
-            //
-        lbl_rcomm:
-            result = true;
-            state.rstate.ra[0] = s;
-            state.rstate.ra[1] = tmp;
-            state.rstate.ra[2] = eps;
-            state.rstate.ra[3] = a;
-            state.rstate.ra[4] = b;
-            state.rstate.ra[5] = x;
-            state.rstate.ra[6] = t;
-            state.rstate.ra[7] = alpha;
-            state.rstate.ra[8] = beta;
-            state.rstate.ra[9] = v1;
-            state.rstate.ra[10] = v2;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Adaptive integration results
-
-        Called after AutoGKIteration returned False.
-
-        Input parameters:
-            State   -   algorithm state (used by AutoGKIteration).
-
-        Output parameters:
-            V       -   integral(f(x)dx,a,b)
-            Rep     -   optimization report (see AutoGKReport description)
-
-          -- ALGLIB --
-             Copyright 14.11.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void autogkresults(autogkstate state,
-            ref double v,
-            autogkreport rep)
-        {
-            v = 0;
-
-            v = state.v;
-            rep.terminationtype = state.terminationtype;
-            rep.nfev = state.nfev;
-            rep.nintervals = state.nintervals;
-        }
-
-
-        /*************************************************************************
-        Internal AutoGK subroutine
-        eps<0   - error
-        eps=0   - automatic eps selection
-
-        width<0 -   error
-        width=0 -   no width requirements
-        *************************************************************************/
-        private static void autogkinternalprepare(double a,
-            double b,
-            double eps,
-            double xwidth,
-            autogkinternalstate state)
-        {
-            
-            //
-            // Save settings
-            //
-            state.a = a;
-            state.b = b;
-            state.eps = eps;
-            state.xwidth = xwidth;
-            
-            //
-            // Prepare RComm structure
-            //
-            state.rstate.ia = new int[3+1];
-            state.rstate.ra = new double[8+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-        Internal AutoGK subroutine
-        *************************************************************************/
-        private static bool autogkinternaliteration(autogkinternalstate state)
-        {
-            bool result = new bool();
-            double c1 = 0;
-            double c2 = 0;
-            int i = 0;
-            int j = 0;
-            double intg = 0;
-            double intk = 0;
-            double inta = 0;
-            double v = 0;
-            double ta = 0;
-            double tb = 0;
-            int ns = 0;
-            double qeps = 0;
-            int info = 0;
-
-            
-            //
-            // Reverse communication preparations
-            // I know it looks ugly, but it works the same way
-            // anywhere from C++ to Python.
-            //
-            // This code initializes locals by:
-            // * random values determined during code
-            //   generation - on first subroutine call
-            // * values from previous call - on subsequent calls
-            //
-            if( state.rstate.stage>=0 )
-            {
-                i = state.rstate.ia[0];
-                j = state.rstate.ia[1];
-                ns = state.rstate.ia[2];
-                info = state.rstate.ia[3];
-                c1 = state.rstate.ra[0];
-                c2 = state.rstate.ra[1];
-                intg = state.rstate.ra[2];
-                intk = state.rstate.ra[3];
-                inta = state.rstate.ra[4];
-                v = state.rstate.ra[5];
-                ta = state.rstate.ra[6];
-                tb = state.rstate.ra[7];
-                qeps = state.rstate.ra[8];
-            }
-            else
-            {
-                i = 497;
-                j = -271;
-                ns = -581;
-                info = 745;
-                c1 = -533;
-                c2 = -77;
-                intg = 678;
-                intk = -293;
-                inta = 316;
-                v = 647;
-                ta = -756;
-                tb = 830;
-                qeps = -871;
-            }
-            if( state.rstate.stage==0 )
-            {
-                goto lbl_0;
-            }
-            if( state.rstate.stage==1 )
-            {
-                goto lbl_1;
-            }
-            if( state.rstate.stage==2 )
-            {
-                goto lbl_2;
-            }
-            
-            //
-            // Routine body
-            //
-            
-            //
-            // initialize quadratures.
-            // use 15-point Gauss-Kronrod formula.
-            //
-            state.n = 15;
-            gkq.gkqgenerategausslegendre(state.n, ref info, ref state.qn, ref state.wk, ref state.wg);
-            if( info<0 )
-            {
-                state.info = -5;
-                state.r = 0;
-                result = false;
-                return result;
-            }
-            state.wr = new double[state.n];
-            for(i=0; i<=state.n-1; i++)
-            {
-                if( i==0 )
-                {
-                    state.wr[i] = 0.5*Math.Abs(state.qn[1]-state.qn[0]);
-                    continue;
-                }
-                if( i==state.n-1 )
-                {
-                    state.wr[state.n-1] = 0.5*Math.Abs(state.qn[state.n-1]-state.qn[state.n-2]);
-                    continue;
-                }
-                state.wr[i] = 0.5*Math.Abs(state.qn[i-1]-state.qn[i+1]);
-            }
-            
-            //
-            // special case
-            //
-            if( (double)(state.a)==(double)(state.b) )
-            {
-                state.info = 1;
-                state.r = 0;
-                result = false;
-                return result;
-            }
-            
-            //
-            // test parameters
-            //
-            if( (double)(state.eps)<(double)(0) | (double)(state.xwidth)<(double)(0) )
-            {
-                state.info = -1;
-                state.r = 0;
-                result = false;
-                return result;
-            }
-            state.info = 1;
-            if( (double)(state.eps)==(double)(0) )
-            {
-                state.eps = 1000*math.machineepsilon;
-            }
-            
-            //
-            // First, prepare heap
-            // * column 0   -   absolute error
-            // * column 1   -   integral of a F(x) (calculated using Kronrod extension nodes)
-            // * column 2   -   integral of a |F(x)| (calculated using modified rect. method)
-            // * column 3   -   left boundary of a subinterval
-            // * column 4   -   right boundary of a subinterval
-            //
-            if( (double)(state.xwidth)!=(double)(0) )
-            {
-                goto lbl_3;
-            }
-            
-            //
-            // no maximum width requirements
-            // start from one big subinterval
-            //
-            state.heapwidth = 5;
-            state.heapsize = 1;
-            state.heapused = 1;
-            state.heap = new double[state.heapsize, state.heapwidth];
-            c1 = 0.5*(state.b-state.a);
-            c2 = 0.5*(state.b+state.a);
-            intg = 0;
-            intk = 0;
-            inta = 0;
-            i = 0;
-        lbl_5:
-            if( i>state.n-1 )
-            {
-                goto lbl_7;
-            }
-            
-            //
-            // obtain F
-            //
-            state.x = c1*state.qn[i]+c2;
-            state.rstate.stage = 0;
-            goto lbl_rcomm;
-        lbl_0:
-            v = state.f;
-            
-            //
-            // Gauss-Kronrod formula
-            //
-            intk = intk+v*state.wk[i];
-            if( i%2==1 )
-            {
-                intg = intg+v*state.wg[i];
-            }
-            
-            //
-            // Integral |F(x)|
-            // Use rectangles method
-            //
-            inta = inta+Math.Abs(v)*state.wr[i];
-            i = i+1;
-            goto lbl_5;
-        lbl_7:
-            intk = intk*(state.b-state.a)*0.5;
-            intg = intg*(state.b-state.a)*0.5;
-            inta = inta*(state.b-state.a)*0.5;
-            state.heap[0,0] = Math.Abs(intg-intk);
-            state.heap[0,1] = intk;
-            state.heap[0,2] = inta;
-            state.heap[0,3] = state.a;
-            state.heap[0,4] = state.b;
-            state.sumerr = state.heap[0,0];
-            state.sumabs = Math.Abs(inta);
-            goto lbl_4;
-        lbl_3:
-            
-            //
-            // maximum subinterval should be no more than XWidth.
-            // so we create Ceil((B-A)/XWidth)+1 small subintervals
-            //
-            ns = (int)Math.Ceiling(Math.Abs(state.b-state.a)/state.xwidth)+1;
-            state.heapsize = ns;
-            state.heapused = ns;
-            state.heapwidth = 5;
-            state.heap = new double[state.heapsize, state.heapwidth];
-            state.sumerr = 0;
-            state.sumabs = 0;
-            j = 0;
-        lbl_8:
-            if( j>ns-1 )
-            {
-                goto lbl_10;
-            }
-            ta = state.a+j*(state.b-state.a)/ns;
-            tb = state.a+(j+1)*(state.b-state.a)/ns;
-            c1 = 0.5*(tb-ta);
-            c2 = 0.5*(tb+ta);
-            intg = 0;
-            intk = 0;
-            inta = 0;
-            i = 0;
-        lbl_11:
-            if( i>state.n-1 )
-            {
-                goto lbl_13;
-            }
-            
-            //
-            // obtain F
-            //
-            state.x = c1*state.qn[i]+c2;
-            state.rstate.stage = 1;
-            goto lbl_rcomm;
-        lbl_1:
-            v = state.f;
-            
-            //
-            // Gauss-Kronrod formula
-            //
-            intk = intk+v*state.wk[i];
-            if( i%2==1 )
-            {
-                intg = intg+v*state.wg[i];
-            }
-            
-            //
-            // Integral |F(x)|
-            // Use rectangles method
-            //
-            inta = inta+Math.Abs(v)*state.wr[i];
-            i = i+1;
-            goto lbl_11;
-        lbl_13:
-            intk = intk*(tb-ta)*0.5;
-            intg = intg*(tb-ta)*0.5;
-            inta = inta*(tb-ta)*0.5;
-            state.heap[j,0] = Math.Abs(intg-intk);
-            state.heap[j,1] = intk;
-            state.heap[j,2] = inta;
-            state.heap[j,3] = ta;
-            state.heap[j,4] = tb;
-            state.sumerr = state.sumerr+state.heap[j,0];
-            state.sumabs = state.sumabs+Math.Abs(inta);
-            j = j+1;
-            goto lbl_8;
-        lbl_10:
-        lbl_4:
-            
-            //
-            // method iterations
-            //
-        lbl_14:
-            if( false )
-            {
-                goto lbl_15;
-            }
-            
-            //
-            // additional memory if needed
-            //
-            if( state.heapused==state.heapsize )
-            {
-                mheapresize(ref state.heap, ref state.heapsize, 4*state.heapsize, state.heapwidth);
-            }
-            
-            //
-            // TODO: every 20 iterations recalculate errors/sums
-            // TODO: one more criterion to prevent infinite loops with too strict Eps
-            //
-            if( (double)(state.sumerr)<=(double)(state.eps*state.sumabs) )
-            {
-                state.r = 0;
-                for(j=0; j<=state.heapused-1; j++)
-                {
-                    state.r = state.r+state.heap[j,1];
-                }
-                result = false;
-                return result;
-            }
-            
-            //
-            // Exclude interval with maximum absolute error
-            //
-            mheappop(ref state.heap, state.heapused, state.heapwidth);
-            state.sumerr = state.sumerr-state.heap[state.heapused-1,0];
-            state.sumabs = state.sumabs-state.heap[state.heapused-1,2];
-            
-            //
-            // Divide interval, create subintervals
-            //
-            ta = state.heap[state.heapused-1,3];
-            tb = state.heap[state.heapused-1,4];
-            state.heap[state.heapused-1,3] = ta;
-            state.heap[state.heapused-1,4] = 0.5*(ta+tb);
-            state.heap[state.heapused,3] = 0.5*(ta+tb);
-            state.heap[state.heapused,4] = tb;
-            j = state.heapused-1;
-        lbl_16:
-            if( j>state.heapused )
-            {
-                goto lbl_18;
-            }
-            c1 = 0.5*(state.heap[j,4]-state.heap[j,3]);
-            c2 = 0.5*(state.heap[j,4]+state.heap[j,3]);
-            intg = 0;
-            intk = 0;
-            inta = 0;
-            i = 0;
-        lbl_19:
-            if( i>state.n-1 )
-            {
-                goto lbl_21;
-            }
-            
-            //
-            // F(x)
-            //
-            state.x = c1*state.qn[i]+c2;
-            state.rstate.stage = 2;
-            goto lbl_rcomm;
-        lbl_2:
-            v = state.f;
-            
-            //
-            // Gauss-Kronrod formula
-            //
-            intk = intk+v*state.wk[i];
-            if( i%2==1 )
-            {
-                intg = intg+v*state.wg[i];
-            }
-            
-            //
-            // Integral |F(x)|
-            // Use rectangles method
-            //
-            inta = inta+Math.Abs(v)*state.wr[i];
-            i = i+1;
-            goto lbl_19;
-        lbl_21:
-            intk = intk*(state.heap[j,4]-state.heap[j,3])*0.5;
-            intg = intg*(state.heap[j,4]-state.heap[j,3])*0.5;
-            inta = inta*(state.heap[j,4]-state.heap[j,3])*0.5;
-            state.heap[j,0] = Math.Abs(intg-intk);
-            state.heap[j,1] = intk;
-            state.heap[j,2] = inta;
-            state.sumerr = state.sumerr+state.heap[j,0];
-            state.sumabs = state.sumabs+state.heap[j,2];
-            j = j+1;
-            goto lbl_16;
-        lbl_18:
-            mheappush(ref state.heap, state.heapused-1, state.heapwidth);
-            mheappush(ref state.heap, state.heapused, state.heapwidth);
-            state.heapused = state.heapused+1;
-            goto lbl_14;
-        lbl_15:
-            result = false;
-            return result;
-            
-            //
-            // Saving state
-            //
-        lbl_rcomm:
-            result = true;
-            state.rstate.ia[0] = i;
-            state.rstate.ia[1] = j;
-            state.rstate.ia[2] = ns;
-            state.rstate.ia[3] = info;
-            state.rstate.ra[0] = c1;
-            state.rstate.ra[1] = c2;
-            state.rstate.ra[2] = intg;
-            state.rstate.ra[3] = intk;
-            state.rstate.ra[4] = inta;
-            state.rstate.ra[5] = v;
-            state.rstate.ra[6] = ta;
-            state.rstate.ra[7] = tb;
-            state.rstate.ra[8] = qeps;
-            return result;
-        }
-
-
-        private static void mheappop(ref double[,] heap,
-            int heapsize,
-            int heapwidth)
-        {
-            int i = 0;
-            int p = 0;
-            double t = 0;
-            int maxcp = 0;
-
-            if( heapsize==1 )
-            {
-                return;
-            }
-            for(i=0; i<=heapwidth-1; i++)
-            {
-                t = heap[heapsize-1,i];
-                heap[heapsize-1,i] = heap[0,i];
-                heap[0,i] = t;
-            }
-            p = 0;
-            while( 2*p+1<heapsize-1 )
-            {
-                maxcp = 2*p+1;
-                if( 2*p+2<heapsize-1 )
-                {
-                    if( (double)(heap[2*p+2,0])>(double)(heap[2*p+1,0]) )
-                    {
-                        maxcp = 2*p+2;
-                    }
-                }
-                if( (double)(heap[p,0])<(double)(heap[maxcp,0]) )
-                {
-                    for(i=0; i<=heapwidth-1; i++)
-                    {
-                        t = heap[p,i];
-                        heap[p,i] = heap[maxcp,i];
-                        heap[maxcp,i] = t;
-                    }
-                    p = maxcp;
-                }
-                else
-                {
-                    break;
-                }
-            }
-        }
-
-
-        private static void mheappush(ref double[,] heap,
-            int heapsize,
-            int heapwidth)
-        {
-            int i = 0;
-            int p = 0;
-            double t = 0;
-            int parent = 0;
-
-            if( heapsize==0 )
-            {
-                return;
-            }
-            p = heapsize;
-            while( p!=0 )
-            {
-                parent = (p-1)/2;
-                if( (double)(heap[p,0])>(double)(heap[parent,0]) )
-                {
-                    for(i=0; i<=heapwidth-1; i++)
-                    {
-                        t = heap[p,i];
-                        heap[p,i] = heap[parent,i];
-                        heap[parent,i] = t;
-                    }
-                    p = parent;
-                }
-                else
-                {
-                    break;
-                }
-            }
-        }
-
-
-        private static void mheapresize(ref double[,] heap,
-            ref int heapsize,
-            int newheapsize,
-            int heapwidth)
-        {
-            double[,] tmp = new double[0,0];
-            int i = 0;
-            int i_ = 0;
-
-            tmp = new double[heapsize, heapwidth];
-            for(i=0; i<=heapsize-1; i++)
-            {
-                for(i_=0; i_<=heapwidth-1;i_++)
-                {
-                    tmp[i,i_] = heap[i,i_];
-                }
-            }
-            heap = new double[newheapsize, heapwidth];
-            for(i=0; i<=heapsize-1; i++)
-            {
-                for(i_=0; i_<=heapwidth-1;i_++)
-                {
-                    heap[i,i_] = tmp[i,i_];
-                }
-            }
-            heapsize = newheapsize;
-        }
-
-
     }
+
+    /*************************************************************************
+    Adaptive integration results
+
+    Called after AutoGKIteration returned False.
+
+    Input parameters:
+        State   -   algorithm state (used by AutoGKIteration).
+
+    Output parameters:
+        V       -   integral(f(x)dx,a,b)
+        Rep     -   optimization report (see AutoGKReport description)
+
+      -- ALGLIB --
+         Copyright 14.11.2007 by Bochkanov Sergey
+    *************************************************************************/
+    public static void autogkresults(autogkstate state, out double v, out autogkreport rep)
+    {
+        v = 0;
+        rep = new autogkreport();
+        autogk.autogkresults(state.innerobj, ref v, rep.innerobj);
+        return;
+    }
+
+}
+public partial class alglib
+{
     public class gq
     {
@@ -3546,4 +2427,1125 @@
 
     }
+    public class autogk
+    {
+        /*************************************************************************
+        Integration report:
+        * TerminationType = completetion code:
+            * -5    non-convergence of Gauss-Kronrod nodes
+                    calculation subroutine.
+            * -1    incorrect parameters were specified
+            *  1    OK
+        * Rep.NFEV countains number of function calculations
+        * Rep.NIntervals contains number of intervals [a,b]
+          was partitioned into.
+        *************************************************************************/
+        public class autogkreport
+        {
+            public int terminationtype;
+            public int nfev;
+            public int nintervals;
+        };
+
+
+        public class autogkinternalstate
+        {
+            public double a;
+            public double b;
+            public double eps;
+            public double xwidth;
+            public double x;
+            public double f;
+            public int info;
+            public double r;
+            public double[,] heap;
+            public int heapsize;
+            public int heapwidth;
+            public int heapused;
+            public double sumerr;
+            public double sumabs;
+            public double[] qn;
+            public double[] wg;
+            public double[] wk;
+            public double[] wr;
+            public int n;
+            public rcommstate rstate;
+            public autogkinternalstate()
+            {
+                heap = new double[0,0];
+                qn = new double[0];
+                wg = new double[0];
+                wk = new double[0];
+                wr = new double[0];
+                rstate = new rcommstate();
+            }
+        };
+
+
+        /*************************************************************************
+        This structure stores state of the integration algorithm.
+
+        Although this class has public fields,  they are not intended for external
+        use. You should use ALGLIB functions to work with this class:
+        * autogksmooth()/AutoGKSmoothW()/... to create objects
+        * autogkintegrate() to begin integration
+        * autogkresults() to get results
+        *************************************************************************/
+        public class autogkstate
+        {
+            public double a;
+            public double b;
+            public double alpha;
+            public double beta;
+            public double xwidth;
+            public double x;
+            public double xminusa;
+            public double bminusx;
+            public bool needf;
+            public double f;
+            public int wrappermode;
+            public autogkinternalstate internalstate;
+            public rcommstate rstate;
+            public double v;
+            public int terminationtype;
+            public int nfev;
+            public int nintervals;
+            public autogkstate()
+            {
+                internalstate = new autogkinternalstate();
+                rstate = new rcommstate();
+            }
+        };
+
+
+
+
+        public const int maxsubintervals = 10000;
+
+
+        /*************************************************************************
+        Integration of a smooth function F(x) on a finite interval [a,b].
+
+        Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
+        is calculated with accuracy close to the machine precision.
+
+        Algorithm works well only with smooth integrands.  It  may  be  used  with
+        continuous non-smooth integrands, but with  less  performance.
+
+        It should never be used with integrands which have integrable singularities
+        at lower or upper limits - algorithm may crash. Use AutoGKSingular in such
+        cases.
+
+        INPUT PARAMETERS:
+            A, B    -   interval boundaries (A<B, A=B or A>B)
+            
+        OUTPUT PARAMETERS
+            State   -   structure which stores algorithm state
+
+        SEE ALSO
+            AutoGKSmoothW, AutoGKSingular, AutoGKResults.
+            
+
+          -- ALGLIB --
+             Copyright 06.05.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void autogksmooth(double a,
+            double b,
+            autogkstate state)
+        {
+            ap.assert(math.isfinite(a), "AutoGKSmooth: A is not finite!");
+            ap.assert(math.isfinite(b), "AutoGKSmooth: B is not finite!");
+            autogksmoothw(a, b, 0.0, state);
+        }
+
+
+        /*************************************************************************
+        Integration of a smooth function F(x) on a finite interval [a,b].
+
+        This subroutine is same as AutoGKSmooth(), but it guarantees that interval
+        [a,b] is partitioned into subintervals which have width at most XWidth.
+
+        Subroutine  can  be  used  when  integrating nearly-constant function with
+        narrow "bumps" (about XWidth wide). If "bumps" are too narrow, AutoGKSmooth
+        subroutine can overlook them.
+
+        INPUT PARAMETERS:
+            A, B    -   interval boundaries (A<B, A=B or A>B)
+
+        OUTPUT PARAMETERS
+            State   -   structure which stores algorithm state
+
+        SEE ALSO
+            AutoGKSmooth, AutoGKSingular, AutoGKResults.
+
+
+          -- ALGLIB --
+             Copyright 06.05.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void autogksmoothw(double a,
+            double b,
+            double xwidth,
+            autogkstate state)
+        {
+            ap.assert(math.isfinite(a), "AutoGKSmoothW: A is not finite!");
+            ap.assert(math.isfinite(b), "AutoGKSmoothW: B is not finite!");
+            ap.assert(math.isfinite(xwidth), "AutoGKSmoothW: XWidth is not finite!");
+            state.wrappermode = 0;
+            state.a = a;
+            state.b = b;
+            state.xwidth = xwidth;
+            state.needf = false;
+            state.rstate.ra = new double[10+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Integration on a finite interval [A,B].
+        Integrand have integrable singularities at A/B.
+
+        F(X) must diverge as "(x-A)^alpha" at A, as "(B-x)^beta" at B,  with known
+        alpha/beta (alpha>-1, beta>-1).  If alpha/beta  are  not known,  estimates
+        from below can be used (but these estimates should be greater than -1 too).
+
+        One  of  alpha/beta variables (or even both alpha/beta) may be equal to 0,
+        which means than function F(x) is non-singular at A/B. Anyway (singular at
+        bounds or not), function F(x) is supposed to be continuous on (A,B).
+
+        Fast-convergent algorithm based on a Gauss-Kronrod formula is used. Result
+        is calculated with accuracy close to the machine precision.
+
+        INPUT PARAMETERS:
+            A, B    -   interval boundaries (A<B, A=B or A>B)
+            Alpha   -   power-law coefficient of the F(x) at A,
+                        Alpha>-1
+            Beta    -   power-law coefficient of the F(x) at B,
+                        Beta>-1
+
+        OUTPUT PARAMETERS
+            State   -   structure which stores algorithm state
+
+        SEE ALSO
+            AutoGKSmooth, AutoGKSmoothW, AutoGKResults.
+
+
+          -- ALGLIB --
+             Copyright 06.05.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void autogksingular(double a,
+            double b,
+            double alpha,
+            double beta,
+            autogkstate state)
+        {
+            ap.assert(math.isfinite(a), "AutoGKSingular: A is not finite!");
+            ap.assert(math.isfinite(b), "AutoGKSingular: B is not finite!");
+            ap.assert(math.isfinite(alpha), "AutoGKSingular: Alpha is not finite!");
+            ap.assert(math.isfinite(beta), "AutoGKSingular: Beta is not finite!");
+            state.wrappermode = 1;
+            state.a = a;
+            state.b = b;
+            state.alpha = alpha;
+            state.beta = beta;
+            state.xwidth = 0.0;
+            state.needf = false;
+            state.rstate.ra = new double[10+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+
+          -- ALGLIB --
+             Copyright 07.05.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool autogkiteration(autogkstate state)
+        {
+            bool result = new bool();
+            double s = 0;
+            double tmp = 0;
+            double eps = 0;
+            double a = 0;
+            double b = 0;
+            double x = 0;
+            double t = 0;
+            double alpha = 0;
+            double beta = 0;
+            double v1 = 0;
+            double v2 = 0;
+
+            
+            //
+            // Reverse communication preparations
+            // I know it looks ugly, but it works the same way
+            // anywhere from C++ to Python.
+            //
+            // This code initializes locals by:
+            // * random values determined during code
+            //   generation - on first subroutine call
+            // * values from previous call - on subsequent calls
+            //
+            if( state.rstate.stage>=0 )
+            {
+                s = state.rstate.ra[0];
+                tmp = state.rstate.ra[1];
+                eps = state.rstate.ra[2];
+                a = state.rstate.ra[3];
+                b = state.rstate.ra[4];
+                x = state.rstate.ra[5];
+                t = state.rstate.ra[6];
+                alpha = state.rstate.ra[7];
+                beta = state.rstate.ra[8];
+                v1 = state.rstate.ra[9];
+                v2 = state.rstate.ra[10];
+            }
+            else
+            {
+                s = -983;
+                tmp = -989;
+                eps = -834;
+                a = 900;
+                b = -287;
+                x = 364;
+                t = 214;
+                alpha = -338;
+                beta = -686;
+                v1 = 912;
+                v2 = 585;
+            }
+            if( state.rstate.stage==0 )
+            {
+                goto lbl_0;
+            }
+            if( state.rstate.stage==1 )
+            {
+                goto lbl_1;
+            }
+            if( state.rstate.stage==2 )
+            {
+                goto lbl_2;
+            }
+            
+            //
+            // Routine body
+            //
+            eps = 0;
+            a = state.a;
+            b = state.b;
+            alpha = state.alpha;
+            beta = state.beta;
+            state.terminationtype = -1;
+            state.nfev = 0;
+            state.nintervals = 0;
+            
+            //
+            // smooth function  at a finite interval
+            //
+            if( state.wrappermode!=0 )
+            {
+                goto lbl_3;
+            }
+            
+            //
+            // special case
+            //
+            if( (double)(a)==(double)(b) )
+            {
+                state.terminationtype = 1;
+                state.v = 0;
+                result = false;
+                return result;
+            }
+            
+            //
+            // general case
+            //
+            autogkinternalprepare(a, b, eps, state.xwidth, state.internalstate);
+        lbl_5:
+            if( !autogkinternaliteration(state.internalstate) )
+            {
+                goto lbl_6;
+            }
+            x = state.internalstate.x;
+            state.x = x;
+            state.xminusa = x-a;
+            state.bminusx = b-x;
+            state.needf = true;
+            state.rstate.stage = 0;
+            goto lbl_rcomm;
+        lbl_0:
+            state.needf = false;
+            state.nfev = state.nfev+1;
+            state.internalstate.f = state.f;
+            goto lbl_5;
+        lbl_6:
+            state.v = state.internalstate.r;
+            state.terminationtype = state.internalstate.info;
+            state.nintervals = state.internalstate.heapused;
+            result = false;
+            return result;
+        lbl_3:
+            
+            //
+            // function with power-law singularities at the ends of a finite interval
+            //
+            if( state.wrappermode!=1 )
+            {
+                goto lbl_7;
+            }
+            
+            //
+            // test coefficients
+            //
+            if( (double)(alpha)<=(double)(-1) | (double)(beta)<=(double)(-1) )
+            {
+                state.terminationtype = -1;
+                state.v = 0;
+                result = false;
+                return result;
+            }
+            
+            //
+            // special cases
+            //
+            if( (double)(a)==(double)(b) )
+            {
+                state.terminationtype = 1;
+                state.v = 0;
+                result = false;
+                return result;
+            }
+            
+            //
+            // reduction to general form
+            //
+            if( (double)(a)<(double)(b) )
+            {
+                s = 1;
+            }
+            else
+            {
+                s = -1;
+                tmp = a;
+                a = b;
+                b = tmp;
+                tmp = alpha;
+                alpha = beta;
+                beta = tmp;
+            }
+            alpha = Math.Min(alpha, 0);
+            beta = Math.Min(beta, 0);
+            
+            //
+            // first, integrate left half of [a,b]:
+            //     integral(f(x)dx, a, (b+a)/2) =
+            //     = 1/(1+alpha) * integral(t^(-alpha/(1+alpha))*f(a+t^(1/(1+alpha)))dt, 0, (0.5*(b-a))^(1+alpha))
+            //
+            autogkinternalprepare(0, Math.Pow(0.5*(b-a), 1+alpha), eps, state.xwidth, state.internalstate);
+        lbl_9:
+            if( !autogkinternaliteration(state.internalstate) )
+            {
+                goto lbl_10;
+            }
+            
+            //
+            // Fill State.X, State.XMinusA, State.BMinusX.
+            // Latter two are filled correctly even if B<A.
+            //
+            x = state.internalstate.x;
+            t = Math.Pow(x, 1/(1+alpha));
+            state.x = a+t;
+            if( (double)(s)>(double)(0) )
+            {
+                state.xminusa = t;
+                state.bminusx = b-(a+t);
+            }
+            else
+            {
+                state.xminusa = a+t-b;
+                state.bminusx = -t;
+            }
+            state.needf = true;
+            state.rstate.stage = 1;
+            goto lbl_rcomm;
+        lbl_1:
+            state.needf = false;
+            if( (double)(alpha)!=(double)(0) )
+            {
+                state.internalstate.f = state.f*Math.Pow(x, -(alpha/(1+alpha)))/(1+alpha);
+            }
+            else
+            {
+                state.internalstate.f = state.f;
+            }
+            state.nfev = state.nfev+1;
+            goto lbl_9;
+        lbl_10:
+            v1 = state.internalstate.r;
+            state.nintervals = state.nintervals+state.internalstate.heapused;
+            
+            //
+            // then, integrate right half of [a,b]:
+            //     integral(f(x)dx, (b+a)/2, b) =
+            //     = 1/(1+beta) * integral(t^(-beta/(1+beta))*f(b-t^(1/(1+beta)))dt, 0, (0.5*(b-a))^(1+beta))
+            //
+            autogkinternalprepare(0, Math.Pow(0.5*(b-a), 1+beta), eps, state.xwidth, state.internalstate);
+        lbl_11:
+            if( !autogkinternaliteration(state.internalstate) )
+            {
+                goto lbl_12;
+            }
+            
+            //
+            // Fill State.X, State.XMinusA, State.BMinusX.
+            // Latter two are filled correctly (X-A, B-X) even if B<A.
+            //
+            x = state.internalstate.x;
+            t = Math.Pow(x, 1/(1+beta));
+            state.x = b-t;
+            if( (double)(s)>(double)(0) )
+            {
+                state.xminusa = b-t-a;
+                state.bminusx = t;
+            }
+            else
+            {
+                state.xminusa = -t;
+                state.bminusx = a-(b-t);
+            }
+            state.needf = true;
+            state.rstate.stage = 2;
+            goto lbl_rcomm;
+        lbl_2:
+            state.needf = false;
+            if( (double)(beta)!=(double)(0) )
+            {
+                state.internalstate.f = state.f*Math.Pow(x, -(beta/(1+beta)))/(1+beta);
+            }
+            else
+            {
+                state.internalstate.f = state.f;
+            }
+            state.nfev = state.nfev+1;
+            goto lbl_11;
+        lbl_12:
+            v2 = state.internalstate.r;
+            state.nintervals = state.nintervals+state.internalstate.heapused;
+            
+            //
+            // final result
+            //
+            state.v = s*(v1+v2);
+            state.terminationtype = 1;
+            result = false;
+            return result;
+        lbl_7:
+            result = false;
+            return result;
+            
+            //
+            // Saving state
+            //
+        lbl_rcomm:
+            result = true;
+            state.rstate.ra[0] = s;
+            state.rstate.ra[1] = tmp;
+            state.rstate.ra[2] = eps;
+            state.rstate.ra[3] = a;
+            state.rstate.ra[4] = b;
+            state.rstate.ra[5] = x;
+            state.rstate.ra[6] = t;
+            state.rstate.ra[7] = alpha;
+            state.rstate.ra[8] = beta;
+            state.rstate.ra[9] = v1;
+            state.rstate.ra[10] = v2;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Adaptive integration results
+
+        Called after AutoGKIteration returned False.
+
+        Input parameters:
+            State   -   algorithm state (used by AutoGKIteration).
+
+        Output parameters:
+            V       -   integral(f(x)dx,a,b)
+            Rep     -   optimization report (see AutoGKReport description)
+
+          -- ALGLIB --
+             Copyright 14.11.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void autogkresults(autogkstate state,
+            ref double v,
+            autogkreport rep)
+        {
+            v = 0;
+
+            v = state.v;
+            rep.terminationtype = state.terminationtype;
+            rep.nfev = state.nfev;
+            rep.nintervals = state.nintervals;
+        }
+
+
+        /*************************************************************************
+        Internal AutoGK subroutine
+        eps<0   - error
+        eps=0   - automatic eps selection
+
+        width<0 -   error
+        width=0 -   no width requirements
+        *************************************************************************/
+        private static void autogkinternalprepare(double a,
+            double b,
+            double eps,
+            double xwidth,
+            autogkinternalstate state)
+        {
+            
+            //
+            // Save settings
+            //
+            state.a = a;
+            state.b = b;
+            state.eps = eps;
+            state.xwidth = xwidth;
+            
+            //
+            // Prepare RComm structure
+            //
+            state.rstate.ia = new int[3+1];
+            state.rstate.ra = new double[8+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Internal AutoGK subroutine
+        *************************************************************************/
+        private static bool autogkinternaliteration(autogkinternalstate state)
+        {
+            bool result = new bool();
+            double c1 = 0;
+            double c2 = 0;
+            int i = 0;
+            int j = 0;
+            double intg = 0;
+            double intk = 0;
+            double inta = 0;
+            double v = 0;
+            double ta = 0;
+            double tb = 0;
+            int ns = 0;
+            double qeps = 0;
+            int info = 0;
+
+            
+            //
+            // Reverse communication preparations
+            // I know it looks ugly, but it works the same way
+            // anywhere from C++ to Python.
+            //
+            // This code initializes locals by:
+            // * random values determined during code
+            //   generation - on first subroutine call
+            // * values from previous call - on subsequent calls
+            //
+            if( state.rstate.stage>=0 )
+            {
+                i = state.rstate.ia[0];
+                j = state.rstate.ia[1];
+                ns = state.rstate.ia[2];
+                info = state.rstate.ia[3];
+                c1 = state.rstate.ra[0];
+                c2 = state.rstate.ra[1];
+                intg = state.rstate.ra[2];
+                intk = state.rstate.ra[3];
+                inta = state.rstate.ra[4];
+                v = state.rstate.ra[5];
+                ta = state.rstate.ra[6];
+                tb = state.rstate.ra[7];
+                qeps = state.rstate.ra[8];
+            }
+            else
+            {
+                i = 497;
+                j = -271;
+                ns = -581;
+                info = 745;
+                c1 = -533;
+                c2 = -77;
+                intg = 678;
+                intk = -293;
+                inta = 316;
+                v = 647;
+                ta = -756;
+                tb = 830;
+                qeps = -871;
+            }
+            if( state.rstate.stage==0 )
+            {
+                goto lbl_0;
+            }
+            if( state.rstate.stage==1 )
+            {
+                goto lbl_1;
+            }
+            if( state.rstate.stage==2 )
+            {
+                goto lbl_2;
+            }
+            
+            //
+            // Routine body
+            //
+            
+            //
+            // initialize quadratures.
+            // use 15-point Gauss-Kronrod formula.
+            //
+            state.n = 15;
+            gkq.gkqgenerategausslegendre(state.n, ref info, ref state.qn, ref state.wk, ref state.wg);
+            if( info<0 )
+            {
+                state.info = -5;
+                state.r = 0;
+                result = false;
+                return result;
+            }
+            state.wr = new double[state.n];
+            for(i=0; i<=state.n-1; i++)
+            {
+                if( i==0 )
+                {
+                    state.wr[i] = 0.5*Math.Abs(state.qn[1]-state.qn[0]);
+                    continue;
+                }
+                if( i==state.n-1 )
+                {
+                    state.wr[state.n-1] = 0.5*Math.Abs(state.qn[state.n-1]-state.qn[state.n-2]);
+                    continue;
+                }
+                state.wr[i] = 0.5*Math.Abs(state.qn[i-1]-state.qn[i+1]);
+            }
+            
+            //
+            // special case
+            //
+            if( (double)(state.a)==(double)(state.b) )
+            {
+                state.info = 1;
+                state.r = 0;
+                result = false;
+                return result;
+            }
+            
+            //
+            // test parameters
+            //
+            if( (double)(state.eps)<(double)(0) | (double)(state.xwidth)<(double)(0) )
+            {
+                state.info = -1;
+                state.r = 0;
+                result = false;
+                return result;
+            }
+            state.info = 1;
+            if( (double)(state.eps)==(double)(0) )
+            {
+                state.eps = 100000*math.machineepsilon;
+            }
+            
+            //
+            // First, prepare heap
+            // * column 0   -   absolute error
+            // * column 1   -   integral of a F(x) (calculated using Kronrod extension nodes)
+            // * column 2   -   integral of a |F(x)| (calculated using modified rect. method)
+            // * column 3   -   left boundary of a subinterval
+            // * column 4   -   right boundary of a subinterval
+            //
+            if( (double)(state.xwidth)!=(double)(0) )
+            {
+                goto lbl_3;
+            }
+            
+            //
+            // no maximum width requirements
+            // start from one big subinterval
+            //
+            state.heapwidth = 5;
+            state.heapsize = 1;
+            state.heapused = 1;
+            state.heap = new double[state.heapsize, state.heapwidth];
+            c1 = 0.5*(state.b-state.a);
+            c2 = 0.5*(state.b+state.a);
+            intg = 0;
+            intk = 0;
+            inta = 0;
+            i = 0;
+        lbl_5:
+            if( i>state.n-1 )
+            {
+                goto lbl_7;
+            }
+            
+            //
+            // obtain F
+            //
+            state.x = c1*state.qn[i]+c2;
+            state.rstate.stage = 0;
+            goto lbl_rcomm;
+        lbl_0:
+            v = state.f;
+            
+            //
+            // Gauss-Kronrod formula
+            //
+            intk = intk+v*state.wk[i];
+            if( i%2==1 )
+            {
+                intg = intg+v*state.wg[i];
+            }
+            
+            //
+            // Integral |F(x)|
+            // Use rectangles method
+            //
+            inta = inta+Math.Abs(v)*state.wr[i];
+            i = i+1;
+            goto lbl_5;
+        lbl_7:
+            intk = intk*(state.b-state.a)*0.5;
+            intg = intg*(state.b-state.a)*0.5;
+            inta = inta*(state.b-state.a)*0.5;
+            state.heap[0,0] = Math.Abs(intg-intk);
+            state.heap[0,1] = intk;
+            state.heap[0,2] = inta;
+            state.heap[0,3] = state.a;
+            state.heap[0,4] = state.b;
+            state.sumerr = state.heap[0,0];
+            state.sumabs = Math.Abs(inta);
+            goto lbl_4;
+        lbl_3:
+            
+            //
+            // maximum subinterval should be no more than XWidth.
+            // so we create Ceil((B-A)/XWidth)+1 small subintervals
+            //
+            ns = (int)Math.Ceiling(Math.Abs(state.b-state.a)/state.xwidth)+1;
+            state.heapsize = ns;
+            state.heapused = ns;
+            state.heapwidth = 5;
+            state.heap = new double[state.heapsize, state.heapwidth];
+            state.sumerr = 0;
+            state.sumabs = 0;
+            j = 0;
+        lbl_8:
+            if( j>ns-1 )
+            {
+                goto lbl_10;
+            }
+            ta = state.a+j*(state.b-state.a)/ns;
+            tb = state.a+(j+1)*(state.b-state.a)/ns;
+            c1 = 0.5*(tb-ta);
+            c2 = 0.5*(tb+ta);
+            intg = 0;
+            intk = 0;
+            inta = 0;
+            i = 0;
+        lbl_11:
+            if( i>state.n-1 )
+            {
+                goto lbl_13;
+            }
+            
+            //
+            // obtain F
+            //
+            state.x = c1*state.qn[i]+c2;
+            state.rstate.stage = 1;
+            goto lbl_rcomm;
+        lbl_1:
+            v = state.f;
+            
+            //
+            // Gauss-Kronrod formula
+            //
+            intk = intk+v*state.wk[i];
+            if( i%2==1 )
+            {
+                intg = intg+v*state.wg[i];
+            }
+            
+            //
+            // Integral |F(x)|
+            // Use rectangles method
+            //
+            inta = inta+Math.Abs(v)*state.wr[i];
+            i = i+1;
+            goto lbl_11;
+        lbl_13:
+            intk = intk*(tb-ta)*0.5;
+            intg = intg*(tb-ta)*0.5;
+            inta = inta*(tb-ta)*0.5;
+            state.heap[j,0] = Math.Abs(intg-intk);
+            state.heap[j,1] = intk;
+            state.heap[j,2] = inta;
+            state.heap[j,3] = ta;
+            state.heap[j,4] = tb;
+            state.sumerr = state.sumerr+state.heap[j,0];
+            state.sumabs = state.sumabs+Math.Abs(inta);
+            j = j+1;
+            goto lbl_8;
+        lbl_10:
+        lbl_4:
+            
+            //
+            // method iterations
+            //
+        lbl_14:
+            if( false )
+            {
+                goto lbl_15;
+            }
+            
+            //
+            // additional memory if needed
+            //
+            if( state.heapused==state.heapsize )
+            {
+                mheapresize(ref state.heap, ref state.heapsize, 4*state.heapsize, state.heapwidth);
+            }
+            
+            //
+            // TODO: every 20 iterations recalculate errors/sums
+            //
+            if( (double)(state.sumerr)<=(double)(state.eps*state.sumabs) | state.heapused>=maxsubintervals )
+            {
+                state.r = 0;
+                for(j=0; j<=state.heapused-1; j++)
+                {
+                    state.r = state.r+state.heap[j,1];
+                }
+                result = false;
+                return result;
+            }
+            
+            //
+            // Exclude interval with maximum absolute error
+            //
+            mheappop(ref state.heap, state.heapused, state.heapwidth);
+            state.sumerr = state.sumerr-state.heap[state.heapused-1,0];
+            state.sumabs = state.sumabs-state.heap[state.heapused-1,2];
+            
+            //
+            // Divide interval, create subintervals
+            //
+            ta = state.heap[state.heapused-1,3];
+            tb = state.heap[state.heapused-1,4];
+            state.heap[state.heapused-1,3] = ta;
+            state.heap[state.heapused-1,4] = 0.5*(ta+tb);
+            state.heap[state.heapused,3] = 0.5*(ta+tb);
+            state.heap[state.heapused,4] = tb;
+            j = state.heapused-1;
+        lbl_16:
+            if( j>state.heapused )
+            {
+                goto lbl_18;
+            }
+            c1 = 0.5*(state.heap[j,4]-state.heap[j,3]);
+            c2 = 0.5*(state.heap[j,4]+state.heap[j,3]);
+            intg = 0;
+            intk = 0;
+            inta = 0;
+            i = 0;
+        lbl_19:
+            if( i>state.n-1 )
+            {
+                goto lbl_21;
+            }
+            
+            //
+            // F(x)
+            //
+            state.x = c1*state.qn[i]+c2;
+            state.rstate.stage = 2;
+            goto lbl_rcomm;
+        lbl_2:
+            v = state.f;
+            
+            //
+            // Gauss-Kronrod formula
+            //
+            intk = intk+v*state.wk[i];
+            if( i%2==1 )
+            {
+                intg = intg+v*state.wg[i];
+            }
+            
+            //
+            // Integral |F(x)|
+            // Use rectangles method
+            //
+            inta = inta+Math.Abs(v)*state.wr[i];
+            i = i+1;
+            goto lbl_19;
+        lbl_21:
+            intk = intk*(state.heap[j,4]-state.heap[j,3])*0.5;
+            intg = intg*(state.heap[j,4]-state.heap[j,3])*0.5;
+            inta = inta*(state.heap[j,4]-state.heap[j,3])*0.5;
+            state.heap[j,0] = Math.Abs(intg-intk);
+            state.heap[j,1] = intk;
+            state.heap[j,2] = inta;
+            state.sumerr = state.sumerr+state.heap[j,0];
+            state.sumabs = state.sumabs+state.heap[j,2];
+            j = j+1;
+            goto lbl_16;
+        lbl_18:
+            mheappush(ref state.heap, state.heapused-1, state.heapwidth);
+            mheappush(ref state.heap, state.heapused, state.heapwidth);
+            state.heapused = state.heapused+1;
+            goto lbl_14;
+        lbl_15:
+            result = false;
+            return result;
+            
+            //
+            // Saving state
+            //
+        lbl_rcomm:
+            result = true;
+            state.rstate.ia[0] = i;
+            state.rstate.ia[1] = j;
+            state.rstate.ia[2] = ns;
+            state.rstate.ia[3] = info;
+            state.rstate.ra[0] = c1;
+            state.rstate.ra[1] = c2;
+            state.rstate.ra[2] = intg;
+            state.rstate.ra[3] = intk;
+            state.rstate.ra[4] = inta;
+            state.rstate.ra[5] = v;
+            state.rstate.ra[6] = ta;
+            state.rstate.ra[7] = tb;
+            state.rstate.ra[8] = qeps;
+            return result;
+        }
+
+
+        private static void mheappop(ref double[,] heap,
+            int heapsize,
+            int heapwidth)
+        {
+            int i = 0;
+            int p = 0;
+            double t = 0;
+            int maxcp = 0;
+
+            if( heapsize==1 )
+            {
+                return;
+            }
+            for(i=0; i<=heapwidth-1; i++)
+            {
+                t = heap[heapsize-1,i];
+                heap[heapsize-1,i] = heap[0,i];
+                heap[0,i] = t;
+            }
+            p = 0;
+            while( 2*p+1<heapsize-1 )
+            {
+                maxcp = 2*p+1;
+                if( 2*p+2<heapsize-1 )
+                {
+                    if( (double)(heap[2*p+2,0])>(double)(heap[2*p+1,0]) )
+                    {
+                        maxcp = 2*p+2;
+                    }
+                }
+                if( (double)(heap[p,0])<(double)(heap[maxcp,0]) )
+                {
+                    for(i=0; i<=heapwidth-1; i++)
+                    {
+                        t = heap[p,i];
+                        heap[p,i] = heap[maxcp,i];
+                        heap[maxcp,i] = t;
+                    }
+                    p = maxcp;
+                }
+                else
+                {
+                    break;
+                }
+            }
+        }
+
+
+        private static void mheappush(ref double[,] heap,
+            int heapsize,
+            int heapwidth)
+        {
+            int i = 0;
+            int p = 0;
+            double t = 0;
+            int parent = 0;
+
+            if( heapsize==0 )
+            {
+                return;
+            }
+            p = heapsize;
+            while( p!=0 )
+            {
+                parent = (p-1)/2;
+                if( (double)(heap[p,0])>(double)(heap[parent,0]) )
+                {
+                    for(i=0; i<=heapwidth-1; i++)
+                    {
+                        t = heap[p,i];
+                        heap[p,i] = heap[parent,i];
+                        heap[parent,i] = t;
+                    }
+                    p = parent;
+                }
+                else
+                {
+                    break;
+                }
+            }
+        }
+
+
+        private static void mheapresize(ref double[,] heap,
+            ref int heapsize,
+            int newheapsize,
+            int heapwidth)
+        {
+            double[,] tmp = new double[0,0];
+            int i = 0;
+            int i_ = 0;
+
+            tmp = new double[heapsize, heapwidth];
+            for(i=0; i<=heapsize-1; i++)
+            {
+                for(i_=0; i_<=heapwidth-1;i_++)
+                {
+                    tmp[i,i_] = heap[i,i_];
+                }
+            }
+            heap = new double[newheapsize, heapwidth];
+            for(i=0; i<=heapsize-1; i++)
+            {
+                for(i_=0; i_<=heapwidth-1;i_++)
+                {
+                    heap[i,i_] = tmp[i,i_];
+                }
+            }
+            heapsize = newheapsize;
+        }
+
+
+    }
 }
 
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/interpolation.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/interpolation.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/interpolation.cs	(revision 7294)
@@ -26,25 +26,15 @@
 
     /*************************************************************************
-    Polynomial fitting report:
-        TaskRCond       reciprocal of task's condition number
-        RMSError        RMS error
-        AvgError        average error
-        AvgRelError     average relative error (for non-zero Y[I])
-        MaxError        maximum error
+    IDW interpolant.
     *************************************************************************/
-    public class polynomialfitreport
+    public class idwinterpolant
     {
         //
         // Public declarations
         //
-        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
-        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
-        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
-        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
-        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
-
-        public polynomialfitreport()
+
+        public idwinterpolant()
         {
-            _innerobj = new lsfit.polynomialfitreport();
+            _innerobj = new idwint.idwinterpolant();
         }
 
@@ -53,7 +43,7 @@
         // They are intended for internal use only
         //
-        private lsfit.polynomialfitreport _innerobj;
-        public lsfit.polynomialfitreport innerobj { get { return _innerobj; } }
-        public polynomialfitreport(lsfit.polynomialfitreport obj)
+        private idwint.idwinterpolant _innerobj;
+        public idwint.idwinterpolant innerobj { get { return _innerobj; } }
+        public idwinterpolant(idwint.idwinterpolant obj)
         {
             _innerobj = obj;
@@ -61,1806 +51,188 @@
     }
 
-
     /*************************************************************************
-    Barycentric fitting report:
-        RMSError        RMS error
-        AvgError        average error
-        AvgRelError     average relative error (for non-zero Y[I])
-        MaxError        maximum error
-        TaskRCond       reciprocal of task's condition number
+    IDW interpolation
+
+    INPUT PARAMETERS:
+        Z   -   IDW interpolant built with one of model building
+                subroutines.
+        X   -   array[0..NX-1], interpolation point
+
+    Result:
+        IDW interpolant Z(X)
+
+      -- ALGLIB --
+         Copyright 02.03.2010 by Bochkanov Sergey
     *************************************************************************/
-    public class barycentricfitreport
+    public static double idwcalc(idwinterpolant z, double[] x)
     {
-        //
-        // Public declarations
-        //
-        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
-        public int dbest { get { return _innerobj.dbest; } set { _innerobj.dbest = value; } }
-        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
-        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
-        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
-        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
-
-        public barycentricfitreport()
-        {
-            _innerobj = new lsfit.barycentricfitreport();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private lsfit.barycentricfitreport _innerobj;
-        public lsfit.barycentricfitreport innerobj { get { return _innerobj; } }
-        public barycentricfitreport(lsfit.barycentricfitreport obj)
-        {
-            _innerobj = obj;
-        }
+
+        double result = idwint.idwcalc(z.innerobj, x);
+        return result;
     }
 
-
     /*************************************************************************
-    Spline fitting report:
-        RMSError        RMS error
-        AvgError        average error
-        AvgRelError     average relative error (for non-zero Y[I])
-        MaxError        maximum error
-
-    Fields  below are  filled  by   obsolete    functions   (Spline1DFitCubic,
-    Spline1DFitHermite). Modern fitting functions do NOT fill these fields:
-        TaskRCond       reciprocal of task's condition number
+    IDW interpolant using modified Shepard method for uniform point
+    distributions.
+
+    INPUT PARAMETERS:
+        XY  -   X and Y values, array[0..N-1,0..NX].
+                First NX columns contain X-values, last column contain
+                Y-values.
+        N   -   number of nodes, N>0.
+        NX  -   space dimension, NX>=1.
+        D   -   nodal function type, either:
+                * 0     constant  model.  Just  for  demonstration only, worst
+                        model ever.
+                * 1     linear model, least squares fitting. Simpe  model  for
+                        datasets too small for quadratic models
+                * 2     quadratic  model,  least  squares  fitting. Best model
+                        available (if your dataset is large enough).
+                * -1    "fast"  linear  model,  use  with  caution!!!   It  is
+                        significantly  faster than linear/quadratic and better
+                        than constant model. But it is less robust (especially
+                        in the presence of noise).
+        NQ  -   number of points used to calculate  nodal  functions  (ignored
+                for constant models). NQ should be LARGER than:
+                * max(1.5*(1+NX),2^NX+1) for linear model,
+                * max(3/4*(NX+2)*(NX+1),2^NX+1) for quadratic model.
+                Values less than this threshold will be silently increased.
+        NW  -   number of points used to calculate weights and to interpolate.
+                Required: >=2^NX+1, values less than this  threshold  will  be
+                silently increased.
+                Recommended value: about 2*NQ
+
+    OUTPUT PARAMETERS:
+        Z   -   IDW interpolant.
+
+    NOTES:
+      * best results are obtained with quadratic models, worst - with constant
+        models
+      * when N is large, NQ and NW must be significantly smaller than  N  both
+        to obtain optimal performance and to obtain optimal accuracy. In 2  or
+        3-dimensional tasks NQ=15 and NW=25 are good values to start with.
+      * NQ  and  NW  may  be  greater  than  N.  In  such  cases  they will be
+        automatically decreased.
+      * this subroutine is always succeeds (as long as correct parameters  are
+        passed).
+      * see  'Multivariate  Interpolation  of Large Sets of Scattered Data' by
+        Robert J. Renka for more information on this algorithm.
+      * this subroutine assumes that point distribution is uniform at the small
+        scales.  If  it  isn't  -  for  example,  points are concentrated along
+        "lines", but "lines" distribution is uniform at the larger scale - then
+        you should use IDWBuildModifiedShepardR()
+
+
+      -- ALGLIB PROJECT --
+         Copyright 02.03.2010 by Bochkanov Sergey
     *************************************************************************/
-    public class spline1dfitreport
+    public static void idwbuildmodifiedshepard(double[,] xy, int n, int nx, int d, int nq, int nw, out idwinterpolant z)
     {
-        //
-        // Public declarations
-        //
-        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
-        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
-        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
-        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
-        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
-
-        public spline1dfitreport()
-        {
-            _innerobj = new lsfit.spline1dfitreport();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private lsfit.spline1dfitreport _innerobj;
-        public lsfit.spline1dfitreport innerobj { get { return _innerobj; } }
-        public spline1dfitreport(lsfit.spline1dfitreport obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-
-    /*************************************************************************
-    Least squares fitting report:
-        TaskRCond       reciprocal of task's condition number
-        RMSError        RMS error
-        AvgError        average error
-        AvgRelError     average relative error (for non-zero Y[I])
-        MaxError        maximum error
-    *************************************************************************/
-    public class lsfitreport
-    {
-        //
-        // Public declarations
-        //
-        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
-        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
-        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
-        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
-        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
-
-        public lsfitreport()
-        {
-            _innerobj = new lsfit.lsfitreport();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private lsfit.lsfitreport _innerobj;
-        public lsfit.lsfitreport innerobj { get { return _innerobj; } }
-        public lsfitreport(lsfit.lsfitreport obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-
-    /*************************************************************************
-    Nonlinear fitter.
-
-    You should use ALGLIB functions to work with fitter.
-    Never try to access its fields directly!
-    *************************************************************************/
-    public class lsfitstate
-    {
-        //
-        // Public declarations
-        //
-        public bool needf { get { return _innerobj.needf; } set { _innerobj.needf = value; } }
-        public bool needfg { get { return _innerobj.needfg; } set { _innerobj.needfg = value; } }
-        public bool needfgh { get { return _innerobj.needfgh; } set { _innerobj.needfgh = value; } }
-        public bool xupdated { get { return _innerobj.xupdated; } set { _innerobj.xupdated = value; } }
-        public double[] c { get { return _innerobj.c; } }
-        public double f { get { return _innerobj.f; } set { _innerobj.f = value; } }
-        public double[] g { get { return _innerobj.g; } }
-        public double[,] h { get { return _innerobj.h; } }
-        public double[] x { get { return _innerobj.x; } }
-
-        public lsfitstate()
-        {
-            _innerobj = new lsfit.lsfitstate();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private lsfit.lsfitstate _innerobj;
-        public lsfit.lsfitstate innerobj { get { return _innerobj; } }
-        public lsfitstate(lsfit.lsfitstate obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-    /*************************************************************************
-    Fitting by polynomials in barycentric form. This function provides  simple
-    unterface for unconstrained unweighted fitting. See  PolynomialFitWC()  if
-    you need constrained fitting.
-
-    Task is linear, so linear least squares solver is used. Complexity of this
-    computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-    SEE ALSO:
-        PolynomialFitWC()
-
-    INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        N   -   number of points, N>0
-                * if given, only leading N elements of X/Y are used
-                * if not given, automatically determined from sizes of X/Y
-        M   -   number of basis functions (= polynomial_degree + 1), M>=1
-
-    OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearW() subroutine:
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD
-        P   -   interpolant in barycentric form.
-        Rep -   report, same format as in LSFitLinearW() subroutine.
-                Following fields are set:
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-    NOTES:
-        you can convert P from barycentric form  to  the  power  or  Chebyshev
-        basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
-        POLINT subpackage.
-
-      -- ALGLIB PROJECT --
-         Copyright 10.12.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void polynomialfit(double[] x, double[] y, int n, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
-    {
-        info = 0;
-        p = new barycentricinterpolant();
-        rep = new polynomialfitreport();
-        lsfit.polynomialfit(x, y, n, m, ref info, p.innerobj, rep.innerobj);
+        z = new idwinterpolant();
+        idwint.idwbuildmodifiedshepard(xy, n, nx, d, nq, nw, z.innerobj);
         return;
     }
-    public static void polynomialfit(double[] x, double[] y, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
+
+    /*************************************************************************
+    IDW interpolant using modified Shepard method for non-uniform datasets.
+
+    This type of model uses  constant  nodal  functions and interpolates using
+    all nodes which are closer than user-specified radius R. It  may  be  used
+    when points distribution is non-uniform at the small scale, but it  is  at
+    the distances as large as R.
+
+    INPUT PARAMETERS:
+        XY  -   X and Y values, array[0..N-1,0..NX].
+                First NX columns contain X-values, last column contain
+                Y-values.
+        N   -   number of nodes, N>0.
+        NX  -   space dimension, NX>=1.
+        R   -   radius, R>0
+
+    OUTPUT PARAMETERS:
+        Z   -   IDW interpolant.
+
+    NOTES:
+    * if there is less than IDWKMin points within  R-ball,  algorithm  selects
+      IDWKMin closest ones, so that continuity properties of  interpolant  are
+      preserved even far from points.
+
+      -- ALGLIB PROJECT --
+         Copyright 11.04.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void idwbuildmodifiedshepardr(double[,] xy, int n, int nx, double r, out idwinterpolant z)
     {
-        int n;
-        if( (ap.len(x)!=ap.len(y)))
-            throw new alglibexception("Error while calling 'polynomialfit': looks like one of arguments has wrong size");
-        info = 0;
-        p = new barycentricinterpolant();
-        rep = new polynomialfitreport();
-        n = ap.len(x);
-        lsfit.polynomialfit(x, y, n, m, ref info, p.innerobj, rep.innerobj);
-
+        z = new idwinterpolant();
+        idwint.idwbuildmodifiedshepardr(xy, n, nx, r, z.innerobj);
         return;
     }
 
     /*************************************************************************
-    Weighted  fitting by polynomials in barycentric form, with constraints  on
-    function values or first derivatives.
-
-    Small regularizing term is used when solving constrained tasks (to improve
-    stability).
-
-    Task is linear, so linear least squares solver is used. Complexity of this
-    computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-    SEE ALSO:
-        PolynomialFit()
+    IDW model for noisy data.
+
+    This subroutine may be used to handle noisy data, i.e. data with noise  in
+    OUTPUT values.  It differs from IDWBuildModifiedShepard() in the following
+    aspects:
+    * nodal functions are not constrained to pass through  nodes:  Qi(xi)<>yi,
+      i.e. we have fitting  instead  of  interpolation.
+    * weights which are used during least  squares fitting stage are all equal
+      to 1.0 (independently of distance)
+    * "fast"-linear or constant nodal functions are not supported (either  not
+      robust enough or too rigid)
+
+    This problem require far more complex tuning than interpolation  problems.
+    Below you can find some recommendations regarding this problem:
+    * focus on tuning NQ; it controls noise reduction. As for NW, you can just
+      make it equal to 2*NQ.
+    * you can use cross-validation to determine optimal NQ.
+    * optimal NQ is a result of complex tradeoff  between  noise  level  (more
+      noise = larger NQ required) and underlying  function  complexity  (given
+      fixed N, larger NQ means smoothing of compex features in the data).  For
+      example, NQ=N will reduce noise to the minimum level possible,  but  you
+      will end up with just constant/linear/quadratic (depending on  D)  least
+      squares model for the whole dataset.
 
     INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        W   -   weights, array[0..N-1]
-                Each summand in square  sum  of  approximation deviations from
-                given  values  is  multiplied  by  the square of corresponding
-                weight. Fill it by 1's if you don't  want  to  solve  weighted
-                task.
-        N   -   number of points, N>0.
-                * if given, only leading N elements of X/Y/W are used
-                * if not given, automatically determined from sizes of X/Y/W
-        XC  -   points where polynomial values/derivatives are constrained,
-                array[0..K-1].
-        YC  -   values of constraints, array[0..K-1]
-        DC  -   array[0..K-1], types of constraints:
-                * DC[i]=0   means that P(XC[i])=YC[i]
-                * DC[i]=1   means that P'(XC[i])=YC[i]
-                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-        K   -   number of constraints, 0<=K<M.
-                K=0 means no constraints (XC/YC/DC are not used in such cases)
-        M   -   number of basis functions (= polynomial_degree + 1), M>=1
+        XY  -   X and Y values, array[0..N-1,0..NX].
+                First NX columns contain X-values, last column contain
+                Y-values.
+        N   -   number of nodes, N>0.
+        NX  -   space dimension, NX>=1.
+        D   -   nodal function degree, either:
+                * 1     linear model, least squares fitting. Simpe  model  for
+                        datasets too small for quadratic models (or  for  very
+                        noisy problems).
+                * 2     quadratic  model,  least  squares  fitting. Best model
+                        available (if your dataset is large enough).
+        NQ  -   number of points used to calculate nodal functions.  NQ should
+                be  significantly   larger   than  1.5  times  the  number  of
+                coefficients in a nodal function to overcome effects of noise:
+                * larger than 1.5*(1+NX) for linear model,
+                * larger than 3/4*(NX+2)*(NX+1) for quadratic model.
+                Values less than this threshold will be silently increased.
+        NW  -   number of points used to calculate weights and to interpolate.
+                Required: >=2^NX+1, values less than this  threshold  will  be
+                silently increased.
+                Recommended value: about 2*NQ or larger
 
     OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearW() subroutine:
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD
-                            -3 means inconsistent constraints
-        P   -   interpolant in barycentric form.
-        Rep -   report, same format as in LSFitLinearW() subroutine.
-                Following fields are set:
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-    IMPORTANT:
-        this subroitine doesn't calculate task's condition number for K<>0.
+        Z   -   IDW interpolant.
 
     NOTES:
-        you can convert P from barycentric form  to  the  power  or  Chebyshev
-        basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
-        POLINT subpackage.
-
-    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-    Setting constraints can lead  to undesired  results,  like ill-conditioned
-    behavior, or inconsistency being detected. From the other side,  it allows
-    us to improve quality of the fit. Here we summarize  our  experience  with
-    constrained regression splines:
-    * even simple constraints can be inconsistent, see  Wikipedia  article  on
-      this subject: http://en.wikipedia.org/wiki/Birkhoff_interpolation
-    * the  greater  is  M (given  fixed  constraints),  the  more chances that
-      constraints will be consistent
-    * in the general case, consistency of constraints is NOT GUARANTEED.
-    * in the one special cases, however, we can  guarantee  consistency.  This
-      case  is:  M>1  and constraints on the function values (NOT DERIVATIVES)
-
-    Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
-    can't solve your task without them. Anything beyond  special  cases  given
-    above is not guaranteed and may result in inconsistency.
+      * best results are obtained with quadratic models, linear models are not
+        recommended to use unless you are pretty sure that it is what you want
+      * this subroutine is always succeeds (as long as correct parameters  are
+        passed).
+      * see  'Multivariate  Interpolation  of Large Sets of Scattered Data' by
+        Robert J. Renka for more information on this algorithm.
+
 
       -- ALGLIB PROJECT --
-         Copyright 10.12.2009 by Bochkanov Sergey
+         Copyright 02.03.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void polynomialfitwc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
+    public static void idwbuildnoisy(double[,] xy, int n, int nx, int d, int nq, int nw, out idwinterpolant z)
     {
-        info = 0;
-        p = new barycentricinterpolant();
-        rep = new polynomialfitreport();
-        lsfit.polynomialfitwc(x, y, w, n, xc, yc, dc, k, m, ref info, p.innerobj, rep.innerobj);
-        return;
-    }
-    public static void polynomialfitwc(double[] x, double[] y, double[] w, double[] xc, double[] yc, int[] dc, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
-    {
-        int n;
-        int k;
-        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
-            throw new alglibexception("Error while calling 'polynomialfitwc': looks like one of arguments has wrong size");
-        if( (ap.len(xc)!=ap.len(yc)) || (ap.len(xc)!=ap.len(dc)))
-            throw new alglibexception("Error while calling 'polynomialfitwc': looks like one of arguments has wrong size");
-        info = 0;
-        p = new barycentricinterpolant();
-        rep = new polynomialfitreport();
-        n = ap.len(x);
-        k = ap.len(xc);
-        lsfit.polynomialfitwc(x, y, w, n, xc, yc, dc, k, m, ref info, p.innerobj, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weghted rational least  squares  fitting  using  Floater-Hormann  rational
-    functions  with  optimal  D  chosen  from  [0,9],  with  constraints   and
-    individual weights.
-
-    Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
-    functions. Different values of D are tried, optimal D (least WEIGHTED root
-    mean square error) is chosen.  Task  is  linear,  so  linear least squares
-    solver  is  used.  Complexity  of  this  computational  scheme is O(N*M^2)
-    (mostly dominated by the least squares solver).
-
-    SEE ALSO
-    * BarycentricFitFloaterHormann(), "lightweight" fitting without invididual
-      weights and constraints.
-
-    INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        W   -   weights, array[0..N-1]
-                Each summand in square  sum  of  approximation deviations from
-                given  values  is  multiplied  by  the square of corresponding
-                weight. Fill it by 1's if you don't  want  to  solve  weighted
-                task.
-        N   -   number of points, N>0.
-        XC  -   points where function values/derivatives are constrained,
-                array[0..K-1].
-        YC  -   values of constraints, array[0..K-1]
-        DC  -   array[0..K-1], types of constraints:
-                * DC[i]=0   means that S(XC[i])=YC[i]
-                * DC[i]=1   means that S'(XC[i])=YC[i]
-                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-        K   -   number of constraints, 0<=K<M.
-                K=0 means no constraints (XC/YC/DC are not used in such cases)
-        M   -   number of basis functions ( = number_of_nodes), M>=2.
-
-    OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearWC() subroutine.
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD
-                            -3 means inconsistent constraints
-                            -1 means another errors in parameters passed
-                               (N<=0, for example)
-        B   -   barycentric interpolant.
-        Rep -   report, same format as in LSFitLinearWC() subroutine.
-                Following fields are set:
-                * DBest         best value of the D parameter
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-    IMPORTANT:
-        this subroutine doesn't calculate task's condition number for K<>0.
-
-    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-    Setting constraints can lead  to undesired  results,  like ill-conditioned
-    behavior, or inconsistency being detected. From the other side,  it allows
-    us to improve quality of the fit. Here we summarize  our  experience  with
-    constrained barycentric interpolants:
-    * excessive  constraints  can  be  inconsistent.   Floater-Hormann   basis
-      functions aren't as flexible as splines (although they are very smooth).
-    * the more evenly constraints are spread across [min(x),max(x)],  the more
-      chances that they will be consistent
-    * the  greater  is  M (given  fixed  constraints),  the  more chances that
-      constraints will be consistent
-    * in the general case, consistency of constraints IS NOT GUARANTEED.
-    * in the several special cases, however, we CAN guarantee consistency.
-    * one of this cases is constraints on the function  VALUES at the interval
-      boundaries. Note that consustency of the  constraints  on  the  function
-      DERIVATIVES is NOT guaranteed (you can use in such cases  cubic  splines
-      which are more flexible).
-    * another  special  case  is ONE constraint on the function value (OR, but
-      not AND, derivative) anywhere in the interval
-
-    Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
-    can't solve your task without them. Anything beyond  special  cases  given
-    above is not guaranteed and may result in inconsistency.
-
-      -- ALGLIB PROJECT --
-         Copyright 18.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void barycentricfitfloaterhormannwc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out barycentricinterpolant b, out barycentricfitreport rep)
-    {
-        info = 0;
-        b = new barycentricinterpolant();
-        rep = new barycentricfitreport();
-        lsfit.barycentricfitfloaterhormannwc(x, y, w, n, xc, yc, dc, k, m, ref info, b.innerobj, rep.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Rational least squares fitting using  Floater-Hormann  rational  functions
-    with optimal D chosen from [0,9].
-
-    Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
-    functions. Different values of D are tried, optimal  D  (least  root  mean
-    square error) is chosen.  Task  is  linear, so linear least squares solver
-    is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
-    dominated by the least squares solver).
-
-    INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        N   -   number of points, N>0.
-        M   -   number of basis functions ( = number_of_nodes), M>=2.
-
-    OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearWC() subroutine.
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD
-                            -3 means inconsistent constraints
-        B   -   barycentric interpolant.
-        Rep -   report, same format as in LSFitLinearWC() subroutine.
-                Following fields are set:
-                * DBest         best value of the D parameter
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-      -- ALGLIB PROJECT --
-         Copyright 18.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void barycentricfitfloaterhormann(double[] x, double[] y, int n, int m, out int info, out barycentricinterpolant b, out barycentricfitreport rep)
-    {
-        info = 0;
-        b = new barycentricinterpolant();
-        rep = new barycentricfitreport();
-        lsfit.barycentricfitfloaterhormann(x, y, n, m, ref info, b.innerobj, rep.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Rational least squares fitting using  Floater-Hormann  rational  functions
-    with optimal D chosen from [0,9].
-
-    Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
-    functions. Different values of D are tried, optimal  D  (least  root  mean
-    square error) is chosen.  Task  is  linear, so linear least squares solver
-    is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
-    dominated by the least squares solver).
-
-    INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        N   -   number of points, N>0.
-        M   -   number of basis functions ( = number_of_nodes), M>=2.
-
-    OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearWC() subroutine.
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD
-                            -3 means inconsistent constraints
-        B   -   barycentric interpolant.
-        Rep -   report, same format as in LSFitLinearWC() subroutine.
-                Following fields are set:
-                * DBest         best value of the D parameter
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-      -- ALGLIB PROJECT --
-         Copyright 18.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void spline1dfitpenalized(double[] x, double[] y, int n, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        lsfit.spline1dfitpenalized(x, y, n, m, rho, ref info, s.innerobj, rep.innerobj);
-        return;
-    }
-    public static void spline1dfitpenalized(double[] x, double[] y, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        int n;
-        if( (ap.len(x)!=ap.len(y)))
-            throw new alglibexception("Error while calling 'spline1dfitpenalized': looks like one of arguments has wrong size");
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        n = ap.len(x);
-        lsfit.spline1dfitpenalized(x, y, n, m, rho, ref info, s.innerobj, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted fitting by penalized cubic spline.
-
-    Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
-    basis functions. Basis functions are cubic splines with  natural  boundary
-    conditions. Problem is regularized by  adding non-linearity penalty to the
-    usual least squares penalty function:
-
-        S(x) = arg min { LS + P }, where
-        LS   = SUM { w[i]^2*(y[i] - S(x[i]))^2 } - least squares penalty
-        P    = C*10^rho*integral{ S''(x)^2*dx } - non-linearity penalty
-        rho  - tunable constant given by user
-        C    - automatically determined scale parameter,
-               makes penalty invariant with respect to scaling of X, Y, W.
-
-    INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        W   -   weights, array[0..N-1]
-                Each summand in square  sum  of  approximation deviations from
-                given  values  is  multiplied  by  the square of corresponding
-                weight. Fill it by 1's if you don't  want  to  solve  weighted
-                problem.
-        N   -   number of points (optional):
-                * N>0
-                * if given, only first N elements of X/Y/W are processed
-                * if not given, automatically determined from X/Y/W sizes
-        M   -   number of basis functions ( = number_of_nodes), M>=4.
-        Rho -   regularization  constant  passed   by   user.   It   penalizes
-                nonlinearity in the regression spline. It  is  logarithmically
-                scaled,  i.e.  actual  value  of  regularization  constant  is
-                calculated as 10^Rho. It is automatically scaled so that:
-                * Rho=2.0 corresponds to moderate amount of nonlinearity
-                * generally, it should be somewhere in the [-8.0,+8.0]
-                If you do not want to penalize nonlineary,
-                pass small Rho. Values as low as -15 should work.
-
-    OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearWC() subroutine.
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD or
-                               Cholesky decomposition; problem may be
-                               too ill-conditioned (very rare)
-        S   -   spline interpolant.
-        Rep -   Following fields are set:
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-    IMPORTANT:
-        this subroitine doesn't calculate task's condition number for K<>0.
-
-    NOTE 1: additional nodes are added to the spline outside  of  the  fitting
-    interval to force linearity when x<min(x,xc) or x>max(x,xc).  It  is  done
-    for consistency - we penalize non-linearity  at [min(x,xc),max(x,xc)],  so
-    it is natural to force linearity outside of this interval.
-
-    NOTE 2: function automatically sorts points,  so  caller may pass unsorted
-    array.
-
-      -- ALGLIB PROJECT --
-         Copyright 19.10.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void spline1dfitpenalizedw(double[] x, double[] y, double[] w, int n, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        lsfit.spline1dfitpenalizedw(x, y, w, n, m, rho, ref info, s.innerobj, rep.innerobj);
-        return;
-    }
-    public static void spline1dfitpenalizedw(double[] x, double[] y, double[] w, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        int n;
-        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
-            throw new alglibexception("Error while calling 'spline1dfitpenalizedw': looks like one of arguments has wrong size");
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        n = ap.len(x);
-        lsfit.spline1dfitpenalizedw(x, y, w, n, m, rho, ref info, s.innerobj, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted fitting by cubic  spline,  with constraints on function values or
-    derivatives.
-
-    Equidistant grid with M-2 nodes on [min(x,xc),max(x,xc)] is  used to build
-    basis functions. Basis functions are cubic splines with continuous  second
-    derivatives  and  non-fixed first  derivatives  at  interval  ends.  Small
-    regularizing term is used  when  solving  constrained  tasks  (to  improve
-    stability).
-
-    Task is linear, so linear least squares solver is used. Complexity of this
-    computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-    SEE ALSO
-        Spline1DFitHermiteWC()  -   fitting by Hermite splines (more flexible,
-                                    less smooth)
-        Spline1DFitCubic()      -   "lightweight" fitting  by  cubic  splines,
-                                    without invididual weights and constraints
-
-    INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        W   -   weights, array[0..N-1]
-                Each summand in square  sum  of  approximation deviations from
-                given  values  is  multiplied  by  the square of corresponding
-                weight. Fill it by 1's if you don't  want  to  solve  weighted
-                task.
-        N   -   number of points (optional):
-                * N>0
-                * if given, only first N elements of X/Y/W are processed
-                * if not given, automatically determined from X/Y/W sizes
-        XC  -   points where spline values/derivatives are constrained,
-                array[0..K-1].
-        YC  -   values of constraints, array[0..K-1]
-        DC  -   array[0..K-1], types of constraints:
-                * DC[i]=0   means that S(XC[i])=YC[i]
-                * DC[i]=1   means that S'(XC[i])=YC[i]
-                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-        K   -   number of constraints (optional):
-                * 0<=K<M.
-                * K=0 means no constraints (XC/YC/DC are not used)
-                * if given, only first K elements of XC/YC/DC are used
-                * if not given, automatically determined from XC/YC/DC
-        M   -   number of basis functions ( = number_of_nodes+2), M>=4.
-
-    OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearWC() subroutine.
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD
-                            -3 means inconsistent constraints
-        S   -   spline interpolant.
-        Rep -   report, same format as in LSFitLinearWC() subroutine.
-                Following fields are set:
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-    IMPORTANT:
-        this subroitine doesn't calculate task's condition number for K<>0.
-
-
-    ORDER OF POINTS
-
-    Subroutine automatically sorts points, so caller may pass unsorted array.
-
-    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-    Setting constraints can lead  to undesired  results,  like ill-conditioned
-    behavior, or inconsistency being detected. From the other side,  it allows
-    us to improve quality of the fit. Here we summarize  our  experience  with
-    constrained regression splines:
-    * excessive constraints can be inconsistent. Splines are  piecewise  cubic
-      functions, and it is easy to create an example, where  large  number  of
-      constraints  concentrated  in  small  area will result in inconsistency.
-      Just because spline is not flexible enough to satisfy all of  them.  And
-      same constraints spread across the  [min(x),max(x)]  will  be  perfectly
-      consistent.
-    * the more evenly constraints are spread across [min(x),max(x)],  the more
-      chances that they will be consistent
-    * the  greater  is  M (given  fixed  constraints),  the  more chances that
-      constraints will be consistent
-    * in the general case, consistency of constraints IS NOT GUARANTEED.
-    * in the several special cases, however, we CAN guarantee consistency.
-    * one of this cases is constraints  on  the  function  values  AND/OR  its
-      derivatives at the interval boundaries.
-    * another  special  case  is ONE constraint on the function value (OR, but
-      not AND, derivative) anywhere in the interval
-
-    Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
-    can't solve your task without them. Anything beyond  special  cases  given
-    above is not guaranteed and may result in inconsistency.
-
-
-      -- ALGLIB PROJECT --
-         Copyright 18.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void spline1dfitcubicwc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        lsfit.spline1dfitcubicwc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
-        return;
-    }
-    public static void spline1dfitcubicwc(double[] x, double[] y, double[] w, double[] xc, double[] yc, int[] dc, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        int n;
-        int k;
-        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
-            throw new alglibexception("Error while calling 'spline1dfitcubicwc': looks like one of arguments has wrong size");
-        if( (ap.len(xc)!=ap.len(yc)) || (ap.len(xc)!=ap.len(dc)))
-            throw new alglibexception("Error while calling 'spline1dfitcubicwc': looks like one of arguments has wrong size");
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        n = ap.len(x);
-        k = ap.len(xc);
-        lsfit.spline1dfitcubicwc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted  fitting  by Hermite spline,  with constraints on function values
-    or first derivatives.
-
-    Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
-    basis functions. Basis functions are Hermite splines.  Small  regularizing
-    term is used when solving constrained tasks (to improve stability).
-
-    Task is linear, so linear least squares solver is used. Complexity of this
-    computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-    SEE ALSO
-        Spline1DFitCubicWC()    -   fitting by Cubic splines (less flexible,
-                                    more smooth)
-        Spline1DFitHermite()    -   "lightweight" Hermite fitting, without
-                                    invididual weights and constraints
-
-    INPUT PARAMETERS:
-        X   -   points, array[0..N-1].
-        Y   -   function values, array[0..N-1].
-        W   -   weights, array[0..N-1]
-                Each summand in square  sum  of  approximation deviations from
-                given  values  is  multiplied  by  the square of corresponding
-                weight. Fill it by 1's if you don't  want  to  solve  weighted
-                task.
-        N   -   number of points (optional):
-                * N>0
-                * if given, only first N elements of X/Y/W are processed
-                * if not given, automatically determined from X/Y/W sizes
-        XC  -   points where spline values/derivatives are constrained,
-                array[0..K-1].
-        YC  -   values of constraints, array[0..K-1]
-        DC  -   array[0..K-1], types of constraints:
-                * DC[i]=0   means that S(XC[i])=YC[i]
-                * DC[i]=1   means that S'(XC[i])=YC[i]
-                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-        K   -   number of constraints (optional):
-                * 0<=K<M.
-                * K=0 means no constraints (XC/YC/DC are not used)
-                * if given, only first K elements of XC/YC/DC are used
-                * if not given, automatically determined from XC/YC/DC
-        M   -   number of basis functions (= 2 * number of nodes),
-                M>=4,
-                M IS EVEN!
-
-    OUTPUT PARAMETERS:
-        Info-   same format as in LSFitLinearW() subroutine:
-                * Info>0    task is solved
-                * Info<=0   an error occured:
-                            -4 means inconvergence of internal SVD
-                            -3 means inconsistent constraints
-                            -2 means odd M was passed (which is not supported)
-                            -1 means another errors in parameters passed
-                               (N<=0, for example)
-        S   -   spline interpolant.
-        Rep -   report, same format as in LSFitLinearW() subroutine.
-                Following fields are set:
-                * RMSError      rms error on the (X,Y).
-                * AvgError      average error on the (X,Y).
-                * AvgRelError   average relative error on the non-zero Y
-                * MaxError      maximum error
-                                NON-WEIGHTED ERRORS ARE CALCULATED
-
-    IMPORTANT:
-        this subroitine doesn't calculate task's condition number for K<>0.
-
-    IMPORTANT:
-        this subroitine supports only even M's
-
-
-    ORDER OF POINTS
-
-    Subroutine automatically sorts points, so caller may pass unsorted array.
-
-    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-    Setting constraints can lead  to undesired  results,  like ill-conditioned
-    behavior, or inconsistency being detected. From the other side,  it allows
-    us to improve quality of the fit. Here we summarize  our  experience  with
-    constrained regression splines:
-    * excessive constraints can be inconsistent. Splines are  piecewise  cubic
-      functions, and it is easy to create an example, where  large  number  of
-      constraints  concentrated  in  small  area will result in inconsistency.
-      Just because spline is not flexible enough to satisfy all of  them.  And
-      same constraints spread across the  [min(x),max(x)]  will  be  perfectly
-      consistent.
-    * the more evenly constraints are spread across [min(x),max(x)],  the more
-      chances that they will be consistent
-    * the  greater  is  M (given  fixed  constraints),  the  more chances that
-      constraints will be consistent
-    * in the general case, consistency of constraints is NOT GUARANTEED.
-    * in the several special cases, however, we can guarantee consistency.
-    * one of this cases is  M>=4  and   constraints  on   the  function  value
-      (AND/OR its derivative) at the interval boundaries.
-    * another special case is M>=4  and  ONE  constraint on the function value
-      (OR, BUT NOT AND, derivative) anywhere in [min(x),max(x)]
-
-    Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
-    can't solve your task without them. Anything beyond  special  cases  given
-    above is not guaranteed and may result in inconsistency.
-
-      -- ALGLIB PROJECT --
-         Copyright 18.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void spline1dfithermitewc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        lsfit.spline1dfithermitewc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
-        return;
-    }
-    public static void spline1dfithermitewc(double[] x, double[] y, double[] w, double[] xc, double[] yc, int[] dc, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        int n;
-        int k;
-        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
-            throw new alglibexception("Error while calling 'spline1dfithermitewc': looks like one of arguments has wrong size");
-        if( (ap.len(xc)!=ap.len(yc)) || (ap.len(xc)!=ap.len(dc)))
-            throw new alglibexception("Error while calling 'spline1dfithermitewc': looks like one of arguments has wrong size");
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        n = ap.len(x);
-        k = ap.len(xc);
-        lsfit.spline1dfithermitewc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Least squares fitting by cubic spline.
-
-    This subroutine is "lightweight" alternative for more complex and feature-
-    rich Spline1DFitCubicWC().  See  Spline1DFitCubicWC() for more information
-    about subroutine parameters (we don't duplicate it here because of length)
-
-      -- ALGLIB PROJECT --
-         Copyright 18.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void spline1dfitcubic(double[] x, double[] y, int n, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        lsfit.spline1dfitcubic(x, y, n, m, ref info, s.innerobj, rep.innerobj);
-        return;
-    }
-    public static void spline1dfitcubic(double[] x, double[] y, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        int n;
-        if( (ap.len(x)!=ap.len(y)))
-            throw new alglibexception("Error while calling 'spline1dfitcubic': looks like one of arguments has wrong size");
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        n = ap.len(x);
-        lsfit.spline1dfitcubic(x, y, n, m, ref info, s.innerobj, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Least squares fitting by Hermite spline.
-
-    This subroutine is "lightweight" alternative for more complex and feature-
-    rich Spline1DFitHermiteWC().  See Spline1DFitHermiteWC()  description  for
-    more information about subroutine parameters (we don't duplicate  it  here
-    because of length).
-
-      -- ALGLIB PROJECT --
-         Copyright 18.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void spline1dfithermite(double[] x, double[] y, int n, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        lsfit.spline1dfithermite(x, y, n, m, ref info, s.innerobj, rep.innerobj);
-        return;
-    }
-    public static void spline1dfithermite(double[] x, double[] y, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
-    {
-        int n;
-        if( (ap.len(x)!=ap.len(y)))
-            throw new alglibexception("Error while calling 'spline1dfithermite': looks like one of arguments has wrong size");
-        info = 0;
-        s = new spline1dinterpolant();
-        rep = new spline1dfitreport();
-        n = ap.len(x);
-        lsfit.spline1dfithermite(x, y, n, m, ref info, s.innerobj, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted linear least squares fitting.
-
-    QR decomposition is used to reduce task to MxM, then triangular solver  or
-    SVD-based solver is used depending on condition number of the  system.  It
-    allows to maximize speed and retain decent accuracy.
-
-    INPUT PARAMETERS:
-        Y       -   array[0..N-1] Function values in  N  points.
-        W       -   array[0..N-1]  Weights  corresponding to function  values.
-                    Each summand in square  sum  of  approximation  deviations
-                    from  given  values  is  multiplied  by  the   square   of
-                    corresponding weight.
-        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                    FMatrix[I, J] - value of J-th basis function in I-th point.
-        N       -   number of points used. N>=1.
-        M       -   number of basis functions, M>=1.
-
-    OUTPUT PARAMETERS:
-        Info    -   error code:
-                    * -4    internal SVD decomposition subroutine failed (very
-                            rare and for degenerate systems only)
-                    * -1    incorrect N/M were specified
-                    *  1    task is solved
-        C       -   decomposition coefficients, array[0..M-1]
-        Rep     -   fitting report. Following fields are set:
-                    * Rep.TaskRCond     reciprocal of condition number
-                    * RMSError          rms error on the (X,Y).
-                    * AvgError          average error on the (X,Y).
-                    * AvgRelError       average relative error on the non-zero Y
-                    * MaxError          maximum error
-                                        NON-WEIGHTED ERRORS ARE CALCULATED
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitlinearw(double[] y, double[] w, double[,] fmatrix, int n, int m, out int info, out double[] c, out lsfitreport rep)
-    {
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        lsfit.lsfitlinearw(y, w, fmatrix, n, m, ref info, ref c, rep.innerobj);
-        return;
-    }
-    public static void lsfitlinearw(double[] y, double[] w, double[,] fmatrix, out int info, out double[] c, out lsfitreport rep)
-    {
-        int n;
-        int m;
-        if( (ap.len(y)!=ap.len(w)) || (ap.len(y)!=ap.rows(fmatrix)))
-            throw new alglibexception("Error while calling 'lsfitlinearw': looks like one of arguments has wrong size");
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        n = ap.len(y);
-        m = ap.cols(fmatrix);
-        lsfit.lsfitlinearw(y, w, fmatrix, n, m, ref info, ref c, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted constained linear least squares fitting.
-
-    This  is  variation  of LSFitLinearW(), which searchs for min|A*x=b| given
-    that  K  additional  constaints  C*x=bc are satisfied. It reduces original
-    task to modified one: min|B*y-d| WITHOUT constraints,  then LSFitLinearW()
-    is called.
-
-    INPUT PARAMETERS:
-        Y       -   array[0..N-1] Function values in  N  points.
-        W       -   array[0..N-1]  Weights  corresponding to function  values.
-                    Each summand in square  sum  of  approximation  deviations
-                    from  given  values  is  multiplied  by  the   square   of
-                    corresponding weight.
-        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                    FMatrix[I,J] - value of J-th basis function in I-th point.
-        CMatrix -   a table of constaints, array[0..K-1,0..M].
-                    I-th row of CMatrix corresponds to I-th linear constraint:
-                    CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
-        N       -   number of points used. N>=1.
-        M       -   number of basis functions, M>=1.
-        K       -   number of constraints, 0 <= K < M
-                    K=0 corresponds to absence of constraints.
-
-    OUTPUT PARAMETERS:
-        Info    -   error code:
-                    * -4    internal SVD decomposition subroutine failed (very
-                            rare and for degenerate systems only)
-                    * -3    either   too   many  constraints  (M   or   more),
-                            degenerate  constraints   (some   constraints  are
-                            repetead twice) or inconsistent  constraints  were
-                            specified.
-                    *  1    task is solved
-        C       -   decomposition coefficients, array[0..M-1]
-        Rep     -   fitting report. Following fields are set:
-                    * RMSError          rms error on the (X,Y).
-                    * AvgError          average error on the (X,Y).
-                    * AvgRelError       average relative error on the non-zero Y
-                    * MaxError          maximum error
-                                        NON-WEIGHTED ERRORS ARE CALCULATED
-
-    IMPORTANT:
-        this subroitine doesn't calculate task's condition number for K<>0.
-
-      -- ALGLIB --
-         Copyright 07.09.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitlinearwc(double[] y, double[] w, double[,] fmatrix, double[,] cmatrix, int n, int m, int k, out int info, out double[] c, out lsfitreport rep)
-    {
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        lsfit.lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
-        return;
-    }
-    public static void lsfitlinearwc(double[] y, double[] w, double[,] fmatrix, double[,] cmatrix, out int info, out double[] c, out lsfitreport rep)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.len(y)!=ap.len(w)) || (ap.len(y)!=ap.rows(fmatrix)))
-            throw new alglibexception("Error while calling 'lsfitlinearwc': looks like one of arguments has wrong size");
-        if( (ap.cols(fmatrix)!=ap.cols(cmatrix)-1))
-            throw new alglibexception("Error while calling 'lsfitlinearwc': looks like one of arguments has wrong size");
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        n = ap.len(y);
-        m = ap.cols(fmatrix);
-        k = ap.rows(cmatrix);
-        lsfit.lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Linear least squares fitting.
-
-    QR decomposition is used to reduce task to MxM, then triangular solver  or
-    SVD-based solver is used depending on condition number of the  system.  It
-    allows to maximize speed and retain decent accuracy.
-
-    INPUT PARAMETERS:
-        Y       -   array[0..N-1] Function values in  N  points.
-        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                    FMatrix[I, J] - value of J-th basis function in I-th point.
-        N       -   number of points used. N>=1.
-        M       -   number of basis functions, M>=1.
-
-    OUTPUT PARAMETERS:
-        Info    -   error code:
-                    * -4    internal SVD decomposition subroutine failed (very
-                            rare and for degenerate systems only)
-                    *  1    task is solved
-        C       -   decomposition coefficients, array[0..M-1]
-        Rep     -   fitting report. Following fields are set:
-                    * Rep.TaskRCond     reciprocal of condition number
-                    * RMSError          rms error on the (X,Y).
-                    * AvgError          average error on the (X,Y).
-                    * AvgRelError       average relative error on the non-zero Y
-                    * MaxError          maximum error
-                                        NON-WEIGHTED ERRORS ARE CALCULATED
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitlinear(double[] y, double[,] fmatrix, int n, int m, out int info, out double[] c, out lsfitreport rep)
-    {
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        lsfit.lsfitlinear(y, fmatrix, n, m, ref info, ref c, rep.innerobj);
-        return;
-    }
-    public static void lsfitlinear(double[] y, double[,] fmatrix, out int info, out double[] c, out lsfitreport rep)
-    {
-        int n;
-        int m;
-        if( (ap.len(y)!=ap.rows(fmatrix)))
-            throw new alglibexception("Error while calling 'lsfitlinear': looks like one of arguments has wrong size");
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        n = ap.len(y);
-        m = ap.cols(fmatrix);
-        lsfit.lsfitlinear(y, fmatrix, n, m, ref info, ref c, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Constained linear least squares fitting.
-
-    This  is  variation  of LSFitLinear(),  which searchs for min|A*x=b| given
-    that  K  additional  constaints  C*x=bc are satisfied. It reduces original
-    task to modified one: min|B*y-d| WITHOUT constraints,  then  LSFitLinear()
-    is called.
-
-    INPUT PARAMETERS:
-        Y       -   array[0..N-1] Function values in  N  points.
-        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                    FMatrix[I,J] - value of J-th basis function in I-th point.
-        CMatrix -   a table of constaints, array[0..K-1,0..M].
-                    I-th row of CMatrix corresponds to I-th linear constraint:
-                    CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
-        N       -   number of points used. N>=1.
-        M       -   number of basis functions, M>=1.
-        K       -   number of constraints, 0 <= K < M
-                    K=0 corresponds to absence of constraints.
-
-    OUTPUT PARAMETERS:
-        Info    -   error code:
-                    * -4    internal SVD decomposition subroutine failed (very
-                            rare and for degenerate systems only)
-                    * -3    either   too   many  constraints  (M   or   more),
-                            degenerate  constraints   (some   constraints  are
-                            repetead twice) or inconsistent  constraints  were
-                            specified.
-                    *  1    task is solved
-        C       -   decomposition coefficients, array[0..M-1]
-        Rep     -   fitting report. Following fields are set:
-                    * RMSError          rms error on the (X,Y).
-                    * AvgError          average error on the (X,Y).
-                    * AvgRelError       average relative error on the non-zero Y
-                    * MaxError          maximum error
-                                        NON-WEIGHTED ERRORS ARE CALCULATED
-
-    IMPORTANT:
-        this subroitine doesn't calculate task's condition number for K<>0.
-
-      -- ALGLIB --
-         Copyright 07.09.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitlinearc(double[] y, double[,] fmatrix, double[,] cmatrix, int n, int m, int k, out int info, out double[] c, out lsfitreport rep)
-    {
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        lsfit.lsfitlinearc(y, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
-        return;
-    }
-    public static void lsfitlinearc(double[] y, double[,] fmatrix, double[,] cmatrix, out int info, out double[] c, out lsfitreport rep)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.len(y)!=ap.rows(fmatrix)))
-            throw new alglibexception("Error while calling 'lsfitlinearc': looks like one of arguments has wrong size");
-        if( (ap.cols(fmatrix)!=ap.cols(cmatrix)-1))
-            throw new alglibexception("Error while calling 'lsfitlinearc': looks like one of arguments has wrong size");
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        n = ap.len(y);
-        m = ap.cols(fmatrix);
-        k = ap.rows(cmatrix);
-        lsfit.lsfitlinearc(y, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted nonlinear least squares fitting using function values only.
-
-    Combination of numerical differentiation and secant updates is used to
-    obtain function Jacobian.
-
-    Nonlinear task min(F(c)) is solved, where
-
-        F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
-
-        * N is a number of points,
-        * M is a dimension of a space points belong to,
-        * K is a dimension of a space of parameters being fitted,
-        * w is an N-dimensional vector of weight coefficients,
-        * x is a set of N points, each of them is an M-dimensional vector,
-        * c is a K-dimensional vector of parameters being fitted
-
-    This subroutine uses only f(c,x[i]).
-
-    INPUT PARAMETERS:
-        X       -   array[0..N-1,0..M-1], points (one row = one point)
-        Y       -   array[0..N-1], function values.
-        W       -   weights, array[0..N-1]
-        C       -   array[0..K-1], initial approximation to the solution,
-        N       -   number of points, N>1
-        M       -   dimension of space
-        K       -   number of parameters being fitted
-        DiffStep-   numerical differentiation step;
-                    should not be very small or large;
-                    large = loss of accuracy
-                    small = growth of round-off errors
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-      -- ALGLIB --
-         Copyright 18.10.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitcreatewf(double[,] x, double[] y, double[] w, double[] c, int n, int m, int k, double diffstep, out lsfitstate state)
-    {
-        state = new lsfitstate();
-        lsfit.lsfitcreatewf(x, y, w, c, n, m, k, diffstep, state.innerobj);
-        return;
-    }
-    public static void lsfitcreatewf(double[,] x, double[] y, double[] w, double[] c, double diffstep, out lsfitstate state)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.rows(x)!=ap.len(y)) || (ap.rows(x)!=ap.len(w)))
-            throw new alglibexception("Error while calling 'lsfitcreatewf': looks like one of arguments has wrong size");
-        state = new lsfitstate();
-        n = ap.rows(x);
-        m = ap.cols(x);
-        k = ap.len(c);
-        lsfit.lsfitcreatewf(x, y, w, c, n, m, k, diffstep, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Nonlinear least squares fitting using function values only.
-
-    Combination of numerical differentiation and secant updates is used to
-    obtain function Jacobian.
-
-    Nonlinear task min(F(c)) is solved, where
-
-        F(c) = (f(c,x[0])-y[0])^2 + ... + (f(c,x[n-1])-y[n-1])^2,
-
-        * N is a number of points,
-        * M is a dimension of a space points belong to,
-        * K is a dimension of a space of parameters being fitted,
-        * w is an N-dimensional vector of weight coefficients,
-        * x is a set of N points, each of them is an M-dimensional vector,
-        * c is a K-dimensional vector of parameters being fitted
-
-    This subroutine uses only f(c,x[i]).
-
-    INPUT PARAMETERS:
-        X       -   array[0..N-1,0..M-1], points (one row = one point)
-        Y       -   array[0..N-1], function values.
-        C       -   array[0..K-1], initial approximation to the solution,
-        N       -   number of points, N>1
-        M       -   dimension of space
-        K       -   number of parameters being fitted
-        DiffStep-   numerical differentiation step;
-                    should not be very small or large;
-                    large = loss of accuracy
-                    small = growth of round-off errors
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-      -- ALGLIB --
-         Copyright 18.10.2008 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitcreatef(double[,] x, double[] y, double[] c, int n, int m, int k, double diffstep, out lsfitstate state)
-    {
-        state = new lsfitstate();
-        lsfit.lsfitcreatef(x, y, c, n, m, k, diffstep, state.innerobj);
-        return;
-    }
-    public static void lsfitcreatef(double[,] x, double[] y, double[] c, double diffstep, out lsfitstate state)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.rows(x)!=ap.len(y)))
-            throw new alglibexception("Error while calling 'lsfitcreatef': looks like one of arguments has wrong size");
-        state = new lsfitstate();
-        n = ap.rows(x);
-        m = ap.cols(x);
-        k = ap.len(c);
-        lsfit.lsfitcreatef(x, y, c, n, m, k, diffstep, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted nonlinear least squares fitting using gradient only.
-
-    Nonlinear task min(F(c)) is solved, where
-
-        F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
-
-        * N is a number of points,
-        * M is a dimension of a space points belong to,
-        * K is a dimension of a space of parameters being fitted,
-        * w is an N-dimensional vector of weight coefficients,
-        * x is a set of N points, each of them is an M-dimensional vector,
-        * c is a K-dimensional vector of parameters being fitted
-
-    This subroutine uses only f(c,x[i]) and its gradient.
-
-    INPUT PARAMETERS:
-        X       -   array[0..N-1,0..M-1], points (one row = one point)
-        Y       -   array[0..N-1], function values.
-        W       -   weights, array[0..N-1]
-        C       -   array[0..K-1], initial approximation to the solution,
-        N       -   number of points, N>1
-        M       -   dimension of space
-        K       -   number of parameters being fitted
-        CheapFG -   boolean flag, which is:
-                    * True  if both function and gradient calculation complexity
-                            are less than O(M^2).  An improved  algorithm  can
-                            be  used  which corresponds  to  FGJ  scheme  from
-                            MINLM unit.
-                    * False otherwise.
-                            Standard Jacibian-bases  Levenberg-Marquardt  algo
-                            will be used (FJ scheme).
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-    See also:
-        LSFitResults
-        LSFitCreateFG (fitting without weights)
-        LSFitCreateWFGH (fitting using Hessian)
-        LSFitCreateFGH (fitting using Hessian, without weights)
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitcreatewfg(double[,] x, double[] y, double[] w, double[] c, int n, int m, int k, bool cheapfg, out lsfitstate state)
-    {
-        state = new lsfitstate();
-        lsfit.lsfitcreatewfg(x, y, w, c, n, m, k, cheapfg, state.innerobj);
-        return;
-    }
-    public static void lsfitcreatewfg(double[,] x, double[] y, double[] w, double[] c, bool cheapfg, out lsfitstate state)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.rows(x)!=ap.len(y)) || (ap.rows(x)!=ap.len(w)))
-            throw new alglibexception("Error while calling 'lsfitcreatewfg': looks like one of arguments has wrong size");
-        state = new lsfitstate();
-        n = ap.rows(x);
-        m = ap.cols(x);
-        k = ap.len(c);
-        lsfit.lsfitcreatewfg(x, y, w, c, n, m, k, cheapfg, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Nonlinear least squares fitting using gradient only, without individual
-    weights.
-
-    Nonlinear task min(F(c)) is solved, where
-
-        F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
-
-        * N is a number of points,
-        * M is a dimension of a space points belong to,
-        * K is a dimension of a space of parameters being fitted,
-        * x is a set of N points, each of them is an M-dimensional vector,
-        * c is a K-dimensional vector of parameters being fitted
-
-    This subroutine uses only f(c,x[i]) and its gradient.
-
-    INPUT PARAMETERS:
-        X       -   array[0..N-1,0..M-1], points (one row = one point)
-        Y       -   array[0..N-1], function values.
-        C       -   array[0..K-1], initial approximation to the solution,
-        N       -   number of points, N>1
-        M       -   dimension of space
-        K       -   number of parameters being fitted
-        CheapFG -   boolean flag, which is:
-                    * True  if both function and gradient calculation complexity
-                            are less than O(M^2).  An improved  algorithm  can
-                            be  used  which corresponds  to  FGJ  scheme  from
-                            MINLM unit.
-                    * False otherwise.
-                            Standard Jacibian-bases  Levenberg-Marquardt  algo
-                            will be used (FJ scheme).
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitcreatefg(double[,] x, double[] y, double[] c, int n, int m, int k, bool cheapfg, out lsfitstate state)
-    {
-        state = new lsfitstate();
-        lsfit.lsfitcreatefg(x, y, c, n, m, k, cheapfg, state.innerobj);
-        return;
-    }
-    public static void lsfitcreatefg(double[,] x, double[] y, double[] c, bool cheapfg, out lsfitstate state)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.rows(x)!=ap.len(y)))
-            throw new alglibexception("Error while calling 'lsfitcreatefg': looks like one of arguments has wrong size");
-        state = new lsfitstate();
-        n = ap.rows(x);
-        m = ap.cols(x);
-        k = ap.len(c);
-        lsfit.lsfitcreatefg(x, y, c, n, m, k, cheapfg, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Weighted nonlinear least squares fitting using gradient/Hessian.
-
-    Nonlinear task min(F(c)) is solved, where
-
-        F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
-
-        * N is a number of points,
-        * M is a dimension of a space points belong to,
-        * K is a dimension of a space of parameters being fitted,
-        * w is an N-dimensional vector of weight coefficients,
-        * x is a set of N points, each of them is an M-dimensional vector,
-        * c is a K-dimensional vector of parameters being fitted
-
-    This subroutine uses f(c,x[i]), its gradient and its Hessian.
-
-    INPUT PARAMETERS:
-        X       -   array[0..N-1,0..M-1], points (one row = one point)
-        Y       -   array[0..N-1], function values.
-        W       -   weights, array[0..N-1]
-        C       -   array[0..K-1], initial approximation to the solution,
-        N       -   number of points, N>1
-        M       -   dimension of space
-        K       -   number of parameters being fitted
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitcreatewfgh(double[,] x, double[] y, double[] w, double[] c, int n, int m, int k, out lsfitstate state)
-    {
-        state = new lsfitstate();
-        lsfit.lsfitcreatewfgh(x, y, w, c, n, m, k, state.innerobj);
-        return;
-    }
-    public static void lsfitcreatewfgh(double[,] x, double[] y, double[] w, double[] c, out lsfitstate state)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.rows(x)!=ap.len(y)) || (ap.rows(x)!=ap.len(w)))
-            throw new alglibexception("Error while calling 'lsfitcreatewfgh': looks like one of arguments has wrong size");
-        state = new lsfitstate();
-        n = ap.rows(x);
-        m = ap.cols(x);
-        k = ap.len(c);
-        lsfit.lsfitcreatewfgh(x, y, w, c, n, m, k, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Nonlinear least squares fitting using gradient/Hessian, without individial
-    weights.
-
-    Nonlinear task min(F(c)) is solved, where
-
-        F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
-
-        * N is a number of points,
-        * M is a dimension of a space points belong to,
-        * K is a dimension of a space of parameters being fitted,
-        * x is a set of N points, each of them is an M-dimensional vector,
-        * c is a K-dimensional vector of parameters being fitted
-
-    This subroutine uses f(c,x[i]), its gradient and its Hessian.
-
-    INPUT PARAMETERS:
-        X       -   array[0..N-1,0..M-1], points (one row = one point)
-        Y       -   array[0..N-1], function values.
-        C       -   array[0..K-1], initial approximation to the solution,
-        N       -   number of points, N>1
-        M       -   dimension of space
-        K       -   number of parameters being fitted
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitcreatefgh(double[,] x, double[] y, double[] c, int n, int m, int k, out lsfitstate state)
-    {
-        state = new lsfitstate();
-        lsfit.lsfitcreatefgh(x, y, c, n, m, k, state.innerobj);
-        return;
-    }
-    public static void lsfitcreatefgh(double[,] x, double[] y, double[] c, out lsfitstate state)
-    {
-        int n;
-        int m;
-        int k;
-        if( (ap.rows(x)!=ap.len(y)))
-            throw new alglibexception("Error while calling 'lsfitcreatefgh': looks like one of arguments has wrong size");
-        state = new lsfitstate();
-        n = ap.rows(x);
-        m = ap.cols(x);
-        k = ap.len(c);
-        lsfit.lsfitcreatefgh(x, y, c, n, m, k, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-    Stopping conditions for nonlinear least squares fitting.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        EpsF    -   stopping criterion. Algorithm stops if
-                    |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
-        EpsX    -   stopping criterion. Algorithm stops if
-                    |X(k+1)-X(k)| <= EpsX*(1+|X(k)|)
-        MaxIts  -   stopping criterion. Algorithm stops after MaxIts iterations.
-                    MaxIts=0 means no stopping criterion.
-
-    NOTE
-
-    Passing EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to automatic
-    stopping criterion selection (according to the scheme used by MINLM unit).
-
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitsetcond(lsfitstate state, double epsf, double epsx, int maxits)
-    {
-
-        lsfit.lsfitsetcond(state.innerobj, epsf, epsx, maxits);
-        return;
-    }
-
-    /*************************************************************************
-    This function sets maximum step length
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
-                    want to limit step length.
-
-    Use this subroutine when you optimize target function which contains exp()
-    or  other  fast  growing  functions,  and optimization algorithm makes too
-    large  steps  which  leads  to overflow. This function allows us to reject
-    steps  that  are  too  large  (and  therefore  expose  us  to the possible
-    overflow) without actually calculating function value at the x+stp*d.
-
-    NOTE: non-zero StpMax leads to moderate  performance  degradation  because
-    intermediate  step  of  preconditioned L-BFGS optimization is incompatible
-    with limits on step size.
-
-      -- ALGLIB --
-         Copyright 02.04.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitsetstpmax(lsfitstate state, double stpmax)
-    {
-
-        lsfit.lsfitsetstpmax(state.innerobj, stpmax);
-        return;
-    }
-
-    /*************************************************************************
-    This function turns on/off reporting.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        NeedXRep-   whether iteration reports are needed or not
-
-    When reports are needed, State.C (current parameters) and State.F (current
-    value of fitting function) are reported.
-
-
-      -- ALGLIB --
-         Copyright 15.08.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitsetxrep(lsfitstate state, bool needxrep)
-    {
-
-        lsfit.lsfitsetxrep(state.innerobj, needxrep);
-        return;
-    }
-
-    /*************************************************************************
-    This function provides reverse communication interface
-    Reverse communication interface is not documented or recommended to use.
-    See below for functions which provide better documented API
-    *************************************************************************/
-    public static bool lsfititeration(lsfitstate state)
-    {
-
-        bool result = lsfit.lsfititeration(state.innerobj);
-        return result;
-    }
-    /*************************************************************************
-    This family of functions is used to launcn iterations of nonlinear fitter
-
-    These functions accept following parameters:
-        func    -   callback which calculates function (or merit function)
-                    value func at given point x
-        grad    -   callback which calculates function (or merit function)
-                    value func and gradient grad at given point x
-        hess    -   callback which calculates function (or merit function)
-                    value func, gradient grad and Hessian hess at given point x
-        rep     -   optional callback which is called after each iteration
-                    can be null
-        obj     -   optional object which is passed to func/grad/hess/jac/rep
-                    can be null
-
-    NOTES:
-
-    1. this algorithm is somewhat unusual because it works with  parameterized
-       function f(C,X), where X is a function argument (we  have  many  points
-       which are characterized by different  argument  values),  and  C  is  a
-       parameter to fit.
-
-       For example, if we want to do linear fit by f(c0,c1,x) = c0*x+c1,  then
-       x will be argument, and {c0,c1} will be parameters.
-
-       It is important to understand that this algorithm finds minimum in  the
-       space of function PARAMETERS (not arguments), so it  needs  derivatives
-       of f() with respect to C, not X.
-
-       In the example above it will need f=c0*x+c1 and {df/dc0,df/dc1} = {x,1}
-       instead of {df/dx} = {c0}.
-
-    2. Callback functions accept C as the first parameter, and X as the second
-
-    3. If  state  was  created  with  LSFitCreateFG(),  algorithm  needs  just
-       function   and   its   gradient,   but   if   state   was  created with
-       LSFitCreateFGH(), algorithm will need function, gradient and Hessian.
-
-       According  to  the  said  above,  there  ase  several  versions of this
-       function, which accept different sets of callbacks.
-
-       This flexibility opens way to subtle errors - you may create state with
-       LSFitCreateFGH() (optimization using Hessian), but call function  which
-       does not accept Hessian. So when algorithm will request Hessian,  there
-       will be no callback to call. In this case exception will be thrown.
-
-       Be careful to avoid such errors because there is no way to find them at
-       compile time - you can see them at runtime only.
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-
-    *************************************************************************/
-    public static void lsfitfit(lsfitstate state, ndimensional_pfunc func, ndimensional_rep rep, object obj)
-    {
-        if( func==null )
-            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (func is null)");
-        while( alglib.lsfititeration(state) )
-        {
-            if( state.needf )
-            {
-                func(state.c, state.x, ref state.innerobj.f, obj);
-                continue;
-            }
-            if( state.innerobj.xupdated )
-            {
-                if( rep!=null )
-                    rep(state.innerobj.x, state.innerobj.f, obj);
-                continue;
-            }
-            throw new alglibexception("ALGLIB: error in 'lsfitfit' (some derivatives were not provided?)");
-        }
-    }
-
-
-    public static void lsfitfit(lsfitstate state, ndimensional_pfunc func, ndimensional_pgrad grad, ndimensional_rep rep, object obj)
-    {
-        if( func==null )
-            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (func is null)");
-        if( grad==null )
-            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (grad is null)");
-        while( alglib.lsfititeration(state) )
-        {
-            if( state.needf )
-            {
-                func(state.c, state.x, ref state.innerobj.f, obj);
-                continue;
-            }
-            if( state.needfg )
-            {
-                grad(state.c, state.x, ref state.innerobj.f, state.innerobj.g, obj);
-                continue;
-            }
-            if( state.innerobj.xupdated )
-            {
-                if( rep!=null )
-                    rep(state.innerobj.x, state.innerobj.f, obj);
-                continue;
-            }
-            throw new alglibexception("ALGLIB: error in 'lsfitfit' (some derivatives were not provided?)");
-        }
-    }
-
-
-    public static void lsfitfit(lsfitstate state, ndimensional_pfunc func, ndimensional_pgrad grad, ndimensional_phess hess, ndimensional_rep rep, object obj)
-    {
-        if( func==null )
-            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (func is null)");
-        if( grad==null )
-            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (grad is null)");
-        if( hess==null )
-            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (hess is null)");
-        while( alglib.lsfititeration(state) )
-        {
-            if( state.needf )
-            {
-                func(state.c, state.x, ref state.innerobj.f, obj);
-                continue;
-            }
-            if( state.needfg )
-            {
-                grad(state.c, state.x, ref state.innerobj.f, state.innerobj.g, obj);
-                continue;
-            }
-            if( state.needfgh )
-            {
-                hess(state.c, state.x, ref state.innerobj.f, state.innerobj.g, state.innerobj.h, obj);
-                continue;
-            }
-            if( state.innerobj.xupdated )
-            {
-                if( rep!=null )
-                    rep(state.innerobj.x, state.innerobj.f, obj);
-                continue;
-            }
-            throw new alglibexception("ALGLIB: error in 'lsfitfit' (some derivatives were not provided?)");
-        }
-    }
-
-
-
-    /*************************************************************************
-    Nonlinear least squares fitting results.
-
-    Called after return from LSFitFit().
-
-    INPUT PARAMETERS:
-        State   -   algorithm state
-
-    OUTPUT PARAMETERS:
-        Info    -   completetion code:
-                        *  1    relative function improvement is no more than
-                                EpsF.
-                        *  2    relative step is no more than EpsX.
-                        *  4    gradient norm is no more than EpsG
-                        *  5    MaxIts steps was taken
-                        *  7    stopping conditions are too stringent,
-                                further improvement is impossible
-        C       -   array[0..K-1], solution
-        Rep     -   optimization report. Following fields are set:
-                    * Rep.TerminationType completetion code:
-                    * RMSError          rms error on the (X,Y).
-                    * AvgError          average error on the (X,Y).
-                    * AvgRelError       average relative error on the non-zero Y
-                    * MaxError          maximum error
-                                        NON-WEIGHTED ERRORS ARE CALCULATED
-
-
-      -- ALGLIB --
-         Copyright 17.08.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void lsfitresults(lsfitstate state, out int info, out double[] c, out lsfitreport rep)
-    {
-        info = 0;
-        c = new double[0];
-        rep = new lsfitreport();
-        lsfit.lsfitresults(state.innerobj, ref info, ref c, rep.innerobj);
+        z = new idwinterpolant();
+        idwint.idwbuildnoisy(xy, n, nx, d, nq, nw, z.innerobj);
         return;
     }
@@ -3469,4 +1841,2480 @@
 
     /*************************************************************************
+    Polynomial fitting report:
+        TaskRCond       reciprocal of task's condition number
+        RMSError        RMS error
+        AvgError        average error
+        AvgRelError     average relative error (for non-zero Y[I])
+        MaxError        maximum error
+    *************************************************************************/
+    public class polynomialfitreport
+    {
+        //
+        // Public declarations
+        //
+        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
+        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
+        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
+        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
+        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
+
+        public polynomialfitreport()
+        {
+            _innerobj = new lsfit.polynomialfitreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private lsfit.polynomialfitreport _innerobj;
+        public lsfit.polynomialfitreport innerobj { get { return _innerobj; } }
+        public polynomialfitreport(lsfit.polynomialfitreport obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    Barycentric fitting report:
+        RMSError        RMS error
+        AvgError        average error
+        AvgRelError     average relative error (for non-zero Y[I])
+        MaxError        maximum error
+        TaskRCond       reciprocal of task's condition number
+    *************************************************************************/
+    public class barycentricfitreport
+    {
+        //
+        // Public declarations
+        //
+        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
+        public int dbest { get { return _innerobj.dbest; } set { _innerobj.dbest = value; } }
+        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
+        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
+        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
+        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
+
+        public barycentricfitreport()
+        {
+            _innerobj = new lsfit.barycentricfitreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private lsfit.barycentricfitreport _innerobj;
+        public lsfit.barycentricfitreport innerobj { get { return _innerobj; } }
+        public barycentricfitreport(lsfit.barycentricfitreport obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    Spline fitting report:
+        RMSError        RMS error
+        AvgError        average error
+        AvgRelError     average relative error (for non-zero Y[I])
+        MaxError        maximum error
+
+    Fields  below are  filled  by   obsolete    functions   (Spline1DFitCubic,
+    Spline1DFitHermite). Modern fitting functions do NOT fill these fields:
+        TaskRCond       reciprocal of task's condition number
+    *************************************************************************/
+    public class spline1dfitreport
+    {
+        //
+        // Public declarations
+        //
+        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
+        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
+        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
+        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
+        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
+
+        public spline1dfitreport()
+        {
+            _innerobj = new lsfit.spline1dfitreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private lsfit.spline1dfitreport _innerobj;
+        public lsfit.spline1dfitreport innerobj { get { return _innerobj; } }
+        public spline1dfitreport(lsfit.spline1dfitreport obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    Least squares fitting report:
+        TaskRCond       reciprocal of task's condition number
+        IterationsCount number of internal iterations
+
+        RMSError        RMS error
+        AvgError        average error
+        AvgRelError     average relative error (for non-zero Y[I])
+        MaxError        maximum error
+
+        WRMSError       weighted RMS error
+    *************************************************************************/
+    public class lsfitreport
+    {
+        //
+        // Public declarations
+        //
+        public double taskrcond { get { return _innerobj.taskrcond; } set { _innerobj.taskrcond = value; } }
+        public int iterationscount { get { return _innerobj.iterationscount; } set { _innerobj.iterationscount = value; } }
+        public double rmserror { get { return _innerobj.rmserror; } set { _innerobj.rmserror = value; } }
+        public double avgerror { get { return _innerobj.avgerror; } set { _innerobj.avgerror = value; } }
+        public double avgrelerror { get { return _innerobj.avgrelerror; } set { _innerobj.avgrelerror = value; } }
+        public double maxerror { get { return _innerobj.maxerror; } set { _innerobj.maxerror = value; } }
+        public double wrmserror { get { return _innerobj.wrmserror; } set { _innerobj.wrmserror = value; } }
+
+        public lsfitreport()
+        {
+            _innerobj = new lsfit.lsfitreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private lsfit.lsfitreport _innerobj;
+        public lsfit.lsfitreport innerobj { get { return _innerobj; } }
+        public lsfitreport(lsfit.lsfitreport obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    Nonlinear fitter.
+
+    You should use ALGLIB functions to work with fitter.
+    Never try to access its fields directly!
+    *************************************************************************/
+    public class lsfitstate
+    {
+        //
+        // Public declarations
+        //
+        public bool needf { get { return _innerobj.needf; } set { _innerobj.needf = value; } }
+        public bool needfg { get { return _innerobj.needfg; } set { _innerobj.needfg = value; } }
+        public bool needfgh { get { return _innerobj.needfgh; } set { _innerobj.needfgh = value; } }
+        public bool xupdated { get { return _innerobj.xupdated; } set { _innerobj.xupdated = value; } }
+        public double[] c { get { return _innerobj.c; } }
+        public double f { get { return _innerobj.f; } set { _innerobj.f = value; } }
+        public double[] g { get { return _innerobj.g; } }
+        public double[,] h { get { return _innerobj.h; } }
+        public double[] x { get { return _innerobj.x; } }
+
+        public lsfitstate()
+        {
+            _innerobj = new lsfit.lsfitstate();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private lsfit.lsfitstate _innerobj;
+        public lsfit.lsfitstate innerobj { get { return _innerobj; } }
+        public lsfitstate(lsfit.lsfitstate obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+    /*************************************************************************
+    Fitting by polynomials in barycentric form. This function provides  simple
+    unterface for unconstrained unweighted fitting. See  PolynomialFitWC()  if
+    you need constrained fitting.
+
+    Task is linear, so linear least squares solver is used. Complexity of this
+    computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+    SEE ALSO:
+        PolynomialFitWC()
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        N   -   number of points, N>0
+                * if given, only leading N elements of X/Y are used
+                * if not given, automatically determined from sizes of X/Y
+        M   -   number of basis functions (= polynomial_degree + 1), M>=1
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearW() subroutine:
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD
+        P   -   interpolant in barycentric form.
+        Rep -   report, same format as in LSFitLinearW() subroutine.
+                Following fields are set:
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+    NOTES:
+        you can convert P from barycentric form  to  the  power  or  Chebyshev
+        basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
+        POLINT subpackage.
+
+      -- ALGLIB PROJECT --
+         Copyright 10.12.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void polynomialfit(double[] x, double[] y, int n, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
+    {
+        info = 0;
+        p = new barycentricinterpolant();
+        rep = new polynomialfitreport();
+        lsfit.polynomialfit(x, y, n, m, ref info, p.innerobj, rep.innerobj);
+        return;
+    }
+    public static void polynomialfit(double[] x, double[] y, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
+    {
+        int n;
+        if( (ap.len(x)!=ap.len(y)))
+            throw new alglibexception("Error while calling 'polynomialfit': looks like one of arguments has wrong size");
+        info = 0;
+        p = new barycentricinterpolant();
+        rep = new polynomialfitreport();
+        n = ap.len(x);
+        lsfit.polynomialfit(x, y, n, m, ref info, p.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted  fitting by polynomials in barycentric form, with constraints  on
+    function values or first derivatives.
+
+    Small regularizing term is used when solving constrained tasks (to improve
+    stability).
+
+    Task is linear, so linear least squares solver is used. Complexity of this
+    computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+    SEE ALSO:
+        PolynomialFit()
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        W   -   weights, array[0..N-1]
+                Each summand in square  sum  of  approximation deviations from
+                given  values  is  multiplied  by  the square of corresponding
+                weight. Fill it by 1's if you don't  want  to  solve  weighted
+                task.
+        N   -   number of points, N>0.
+                * if given, only leading N elements of X/Y/W are used
+                * if not given, automatically determined from sizes of X/Y/W
+        XC  -   points where polynomial values/derivatives are constrained,
+                array[0..K-1].
+        YC  -   values of constraints, array[0..K-1]
+        DC  -   array[0..K-1], types of constraints:
+                * DC[i]=0   means that P(XC[i])=YC[i]
+                * DC[i]=1   means that P'(XC[i])=YC[i]
+                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+        K   -   number of constraints, 0<=K<M.
+                K=0 means no constraints (XC/YC/DC are not used in such cases)
+        M   -   number of basis functions (= polynomial_degree + 1), M>=1
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearW() subroutine:
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD
+                            -3 means inconsistent constraints
+        P   -   interpolant in barycentric form.
+        Rep -   report, same format as in LSFitLinearW() subroutine.
+                Following fields are set:
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+    IMPORTANT:
+        this subroitine doesn't calculate task's condition number for K<>0.
+
+    NOTES:
+        you can convert P from barycentric form  to  the  power  or  Chebyshev
+        basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
+        POLINT subpackage.
+
+    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+    Setting constraints can lead  to undesired  results,  like ill-conditioned
+    behavior, or inconsistency being detected. From the other side,  it allows
+    us to improve quality of the fit. Here we summarize  our  experience  with
+    constrained regression splines:
+    * even simple constraints can be inconsistent, see  Wikipedia  article  on
+      this subject: http://en.wikipedia.org/wiki/Birkhoff_interpolation
+    * the  greater  is  M (given  fixed  constraints),  the  more chances that
+      constraints will be consistent
+    * in the general case, consistency of constraints is NOT GUARANTEED.
+    * in the one special cases, however, we can  guarantee  consistency.  This
+      case  is:  M>1  and constraints on the function values (NOT DERIVATIVES)
+
+    Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
+    can't solve your task without them. Anything beyond  special  cases  given
+    above is not guaranteed and may result in inconsistency.
+
+      -- ALGLIB PROJECT --
+         Copyright 10.12.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void polynomialfitwc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
+    {
+        info = 0;
+        p = new barycentricinterpolant();
+        rep = new polynomialfitreport();
+        lsfit.polynomialfitwc(x, y, w, n, xc, yc, dc, k, m, ref info, p.innerobj, rep.innerobj);
+        return;
+    }
+    public static void polynomialfitwc(double[] x, double[] y, double[] w, double[] xc, double[] yc, int[] dc, int m, out int info, out barycentricinterpolant p, out polynomialfitreport rep)
+    {
+        int n;
+        int k;
+        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
+            throw new alglibexception("Error while calling 'polynomialfitwc': looks like one of arguments has wrong size");
+        if( (ap.len(xc)!=ap.len(yc)) || (ap.len(xc)!=ap.len(dc)))
+            throw new alglibexception("Error while calling 'polynomialfitwc': looks like one of arguments has wrong size");
+        info = 0;
+        p = new barycentricinterpolant();
+        rep = new polynomialfitreport();
+        n = ap.len(x);
+        k = ap.len(xc);
+        lsfit.polynomialfitwc(x, y, w, n, xc, yc, dc, k, m, ref info, p.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weghted rational least  squares  fitting  using  Floater-Hormann  rational
+    functions  with  optimal  D  chosen  from  [0,9],  with  constraints   and
+    individual weights.
+
+    Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
+    functions. Different values of D are tried, optimal D (least WEIGHTED root
+    mean square error) is chosen.  Task  is  linear,  so  linear least squares
+    solver  is  used.  Complexity  of  this  computational  scheme is O(N*M^2)
+    (mostly dominated by the least squares solver).
+
+    SEE ALSO
+    * BarycentricFitFloaterHormann(), "lightweight" fitting without invididual
+      weights and constraints.
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        W   -   weights, array[0..N-1]
+                Each summand in square  sum  of  approximation deviations from
+                given  values  is  multiplied  by  the square of corresponding
+                weight. Fill it by 1's if you don't  want  to  solve  weighted
+                task.
+        N   -   number of points, N>0.
+        XC  -   points where function values/derivatives are constrained,
+                array[0..K-1].
+        YC  -   values of constraints, array[0..K-1]
+        DC  -   array[0..K-1], types of constraints:
+                * DC[i]=0   means that S(XC[i])=YC[i]
+                * DC[i]=1   means that S'(XC[i])=YC[i]
+                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+        K   -   number of constraints, 0<=K<M.
+                K=0 means no constraints (XC/YC/DC are not used in such cases)
+        M   -   number of basis functions ( = number_of_nodes), M>=2.
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearWC() subroutine.
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD
+                            -3 means inconsistent constraints
+                            -1 means another errors in parameters passed
+                               (N<=0, for example)
+        B   -   barycentric interpolant.
+        Rep -   report, same format as in LSFitLinearWC() subroutine.
+                Following fields are set:
+                * DBest         best value of the D parameter
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+    IMPORTANT:
+        this subroutine doesn't calculate task's condition number for K<>0.
+
+    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+    Setting constraints can lead  to undesired  results,  like ill-conditioned
+    behavior, or inconsistency being detected. From the other side,  it allows
+    us to improve quality of the fit. Here we summarize  our  experience  with
+    constrained barycentric interpolants:
+    * excessive  constraints  can  be  inconsistent.   Floater-Hormann   basis
+      functions aren't as flexible as splines (although they are very smooth).
+    * the more evenly constraints are spread across [min(x),max(x)],  the more
+      chances that they will be consistent
+    * the  greater  is  M (given  fixed  constraints),  the  more chances that
+      constraints will be consistent
+    * in the general case, consistency of constraints IS NOT GUARANTEED.
+    * in the several special cases, however, we CAN guarantee consistency.
+    * one of this cases is constraints on the function  VALUES at the interval
+      boundaries. Note that consustency of the  constraints  on  the  function
+      DERIVATIVES is NOT guaranteed (you can use in such cases  cubic  splines
+      which are more flexible).
+    * another  special  case  is ONE constraint on the function value (OR, but
+      not AND, derivative) anywhere in the interval
+
+    Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
+    can't solve your task without them. Anything beyond  special  cases  given
+    above is not guaranteed and may result in inconsistency.
+
+      -- ALGLIB PROJECT --
+         Copyright 18.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void barycentricfitfloaterhormannwc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out barycentricinterpolant b, out barycentricfitreport rep)
+    {
+        info = 0;
+        b = new barycentricinterpolant();
+        rep = new barycentricfitreport();
+        lsfit.barycentricfitfloaterhormannwc(x, y, w, n, xc, yc, dc, k, m, ref info, b.innerobj, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Rational least squares fitting using  Floater-Hormann  rational  functions
+    with optimal D chosen from [0,9].
+
+    Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
+    functions. Different values of D are tried, optimal  D  (least  root  mean
+    square error) is chosen.  Task  is  linear, so linear least squares solver
+    is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
+    dominated by the least squares solver).
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        N   -   number of points, N>0.
+        M   -   number of basis functions ( = number_of_nodes), M>=2.
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearWC() subroutine.
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD
+                            -3 means inconsistent constraints
+        B   -   barycentric interpolant.
+        Rep -   report, same format as in LSFitLinearWC() subroutine.
+                Following fields are set:
+                * DBest         best value of the D parameter
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+      -- ALGLIB PROJECT --
+         Copyright 18.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void barycentricfitfloaterhormann(double[] x, double[] y, int n, int m, out int info, out barycentricinterpolant b, out barycentricfitreport rep)
+    {
+        info = 0;
+        b = new barycentricinterpolant();
+        rep = new barycentricfitreport();
+        lsfit.barycentricfitfloaterhormann(x, y, n, m, ref info, b.innerobj, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Rational least squares fitting using  Floater-Hormann  rational  functions
+    with optimal D chosen from [0,9].
+
+    Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
+    functions. Different values of D are tried, optimal  D  (least  root  mean
+    square error) is chosen.  Task  is  linear, so linear least squares solver
+    is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
+    dominated by the least squares solver).
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        N   -   number of points, N>0.
+        M   -   number of basis functions ( = number_of_nodes), M>=2.
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearWC() subroutine.
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD
+                            -3 means inconsistent constraints
+        B   -   barycentric interpolant.
+        Rep -   report, same format as in LSFitLinearWC() subroutine.
+                Following fields are set:
+                * DBest         best value of the D parameter
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+      -- ALGLIB PROJECT --
+         Copyright 18.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void spline1dfitpenalized(double[] x, double[] y, int n, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        lsfit.spline1dfitpenalized(x, y, n, m, rho, ref info, s.innerobj, rep.innerobj);
+        return;
+    }
+    public static void spline1dfitpenalized(double[] x, double[] y, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        int n;
+        if( (ap.len(x)!=ap.len(y)))
+            throw new alglibexception("Error while calling 'spline1dfitpenalized': looks like one of arguments has wrong size");
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        n = ap.len(x);
+        lsfit.spline1dfitpenalized(x, y, n, m, rho, ref info, s.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted fitting by penalized cubic spline.
+
+    Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
+    basis functions. Basis functions are cubic splines with  natural  boundary
+    conditions. Problem is regularized by  adding non-linearity penalty to the
+    usual least squares penalty function:
+
+        S(x) = arg min { LS + P }, where
+        LS   = SUM { w[i]^2*(y[i] - S(x[i]))^2 } - least squares penalty
+        P    = C*10^rho*integral{ S''(x)^2*dx } - non-linearity penalty
+        rho  - tunable constant given by user
+        C    - automatically determined scale parameter,
+               makes penalty invariant with respect to scaling of X, Y, W.
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        W   -   weights, array[0..N-1]
+                Each summand in square  sum  of  approximation deviations from
+                given  values  is  multiplied  by  the square of corresponding
+                weight. Fill it by 1's if you don't  want  to  solve  weighted
+                problem.
+        N   -   number of points (optional):
+                * N>0
+                * if given, only first N elements of X/Y/W are processed
+                * if not given, automatically determined from X/Y/W sizes
+        M   -   number of basis functions ( = number_of_nodes), M>=4.
+        Rho -   regularization  constant  passed   by   user.   It   penalizes
+                nonlinearity in the regression spline. It  is  logarithmically
+                scaled,  i.e.  actual  value  of  regularization  constant  is
+                calculated as 10^Rho. It is automatically scaled so that:
+                * Rho=2.0 corresponds to moderate amount of nonlinearity
+                * generally, it should be somewhere in the [-8.0,+8.0]
+                If you do not want to penalize nonlineary,
+                pass small Rho. Values as low as -15 should work.
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearWC() subroutine.
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD or
+                               Cholesky decomposition; problem may be
+                               too ill-conditioned (very rare)
+        S   -   spline interpolant.
+        Rep -   Following fields are set:
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+    IMPORTANT:
+        this subroitine doesn't calculate task's condition number for K<>0.
+
+    NOTE 1: additional nodes are added to the spline outside  of  the  fitting
+    interval to force linearity when x<min(x,xc) or x>max(x,xc).  It  is  done
+    for consistency - we penalize non-linearity  at [min(x,xc),max(x,xc)],  so
+    it is natural to force linearity outside of this interval.
+
+    NOTE 2: function automatically sorts points,  so  caller may pass unsorted
+    array.
+
+      -- ALGLIB PROJECT --
+         Copyright 19.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void spline1dfitpenalizedw(double[] x, double[] y, double[] w, int n, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        lsfit.spline1dfitpenalizedw(x, y, w, n, m, rho, ref info, s.innerobj, rep.innerobj);
+        return;
+    }
+    public static void spline1dfitpenalizedw(double[] x, double[] y, double[] w, int m, double rho, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        int n;
+        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
+            throw new alglibexception("Error while calling 'spline1dfitpenalizedw': looks like one of arguments has wrong size");
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        n = ap.len(x);
+        lsfit.spline1dfitpenalizedw(x, y, w, n, m, rho, ref info, s.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted fitting by cubic  spline,  with constraints on function values or
+    derivatives.
+
+    Equidistant grid with M-2 nodes on [min(x,xc),max(x,xc)] is  used to build
+    basis functions. Basis functions are cubic splines with continuous  second
+    derivatives  and  non-fixed first  derivatives  at  interval  ends.  Small
+    regularizing term is used  when  solving  constrained  tasks  (to  improve
+    stability).
+
+    Task is linear, so linear least squares solver is used. Complexity of this
+    computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+    SEE ALSO
+        Spline1DFitHermiteWC()  -   fitting by Hermite splines (more flexible,
+                                    less smooth)
+        Spline1DFitCubic()      -   "lightweight" fitting  by  cubic  splines,
+                                    without invididual weights and constraints
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        W   -   weights, array[0..N-1]
+                Each summand in square  sum  of  approximation deviations from
+                given  values  is  multiplied  by  the square of corresponding
+                weight. Fill it by 1's if you don't  want  to  solve  weighted
+                task.
+        N   -   number of points (optional):
+                * N>0
+                * if given, only first N elements of X/Y/W are processed
+                * if not given, automatically determined from X/Y/W sizes
+        XC  -   points where spline values/derivatives are constrained,
+                array[0..K-1].
+        YC  -   values of constraints, array[0..K-1]
+        DC  -   array[0..K-1], types of constraints:
+                * DC[i]=0   means that S(XC[i])=YC[i]
+                * DC[i]=1   means that S'(XC[i])=YC[i]
+                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+        K   -   number of constraints (optional):
+                * 0<=K<M.
+                * K=0 means no constraints (XC/YC/DC are not used)
+                * if given, only first K elements of XC/YC/DC are used
+                * if not given, automatically determined from XC/YC/DC
+        M   -   number of basis functions ( = number_of_nodes+2), M>=4.
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearWC() subroutine.
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD
+                            -3 means inconsistent constraints
+        S   -   spline interpolant.
+        Rep -   report, same format as in LSFitLinearWC() subroutine.
+                Following fields are set:
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+    IMPORTANT:
+        this subroitine doesn't calculate task's condition number for K<>0.
+
+
+    ORDER OF POINTS
+
+    Subroutine automatically sorts points, so caller may pass unsorted array.
+
+    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+    Setting constraints can lead  to undesired  results,  like ill-conditioned
+    behavior, or inconsistency being detected. From the other side,  it allows
+    us to improve quality of the fit. Here we summarize  our  experience  with
+    constrained regression splines:
+    * excessive constraints can be inconsistent. Splines are  piecewise  cubic
+      functions, and it is easy to create an example, where  large  number  of
+      constraints  concentrated  in  small  area will result in inconsistency.
+      Just because spline is not flexible enough to satisfy all of  them.  And
+      same constraints spread across the  [min(x),max(x)]  will  be  perfectly
+      consistent.
+    * the more evenly constraints are spread across [min(x),max(x)],  the more
+      chances that they will be consistent
+    * the  greater  is  M (given  fixed  constraints),  the  more chances that
+      constraints will be consistent
+    * in the general case, consistency of constraints IS NOT GUARANTEED.
+    * in the several special cases, however, we CAN guarantee consistency.
+    * one of this cases is constraints  on  the  function  values  AND/OR  its
+      derivatives at the interval boundaries.
+    * another  special  case  is ONE constraint on the function value (OR, but
+      not AND, derivative) anywhere in the interval
+
+    Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
+    can't solve your task without them. Anything beyond  special  cases  given
+    above is not guaranteed and may result in inconsistency.
+
+
+      -- ALGLIB PROJECT --
+         Copyright 18.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void spline1dfitcubicwc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        lsfit.spline1dfitcubicwc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
+        return;
+    }
+    public static void spline1dfitcubicwc(double[] x, double[] y, double[] w, double[] xc, double[] yc, int[] dc, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        int n;
+        int k;
+        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
+            throw new alglibexception("Error while calling 'spline1dfitcubicwc': looks like one of arguments has wrong size");
+        if( (ap.len(xc)!=ap.len(yc)) || (ap.len(xc)!=ap.len(dc)))
+            throw new alglibexception("Error while calling 'spline1dfitcubicwc': looks like one of arguments has wrong size");
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        n = ap.len(x);
+        k = ap.len(xc);
+        lsfit.spline1dfitcubicwc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted  fitting  by Hermite spline,  with constraints on function values
+    or first derivatives.
+
+    Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
+    basis functions. Basis functions are Hermite splines.  Small  regularizing
+    term is used when solving constrained tasks (to improve stability).
+
+    Task is linear, so linear least squares solver is used. Complexity of this
+    computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+    SEE ALSO
+        Spline1DFitCubicWC()    -   fitting by Cubic splines (less flexible,
+                                    more smooth)
+        Spline1DFitHermite()    -   "lightweight" Hermite fitting, without
+                                    invididual weights and constraints
+
+    INPUT PARAMETERS:
+        X   -   points, array[0..N-1].
+        Y   -   function values, array[0..N-1].
+        W   -   weights, array[0..N-1]
+                Each summand in square  sum  of  approximation deviations from
+                given  values  is  multiplied  by  the square of corresponding
+                weight. Fill it by 1's if you don't  want  to  solve  weighted
+                task.
+        N   -   number of points (optional):
+                * N>0
+                * if given, only first N elements of X/Y/W are processed
+                * if not given, automatically determined from X/Y/W sizes
+        XC  -   points where spline values/derivatives are constrained,
+                array[0..K-1].
+        YC  -   values of constraints, array[0..K-1]
+        DC  -   array[0..K-1], types of constraints:
+                * DC[i]=0   means that S(XC[i])=YC[i]
+                * DC[i]=1   means that S'(XC[i])=YC[i]
+                SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+        K   -   number of constraints (optional):
+                * 0<=K<M.
+                * K=0 means no constraints (XC/YC/DC are not used)
+                * if given, only first K elements of XC/YC/DC are used
+                * if not given, automatically determined from XC/YC/DC
+        M   -   number of basis functions (= 2 * number of nodes),
+                M>=4,
+                M IS EVEN!
+
+    OUTPUT PARAMETERS:
+        Info-   same format as in LSFitLinearW() subroutine:
+                * Info>0    task is solved
+                * Info<=0   an error occured:
+                            -4 means inconvergence of internal SVD
+                            -3 means inconsistent constraints
+                            -2 means odd M was passed (which is not supported)
+                            -1 means another errors in parameters passed
+                               (N<=0, for example)
+        S   -   spline interpolant.
+        Rep -   report, same format as in LSFitLinearW() subroutine.
+                Following fields are set:
+                * RMSError      rms error on the (X,Y).
+                * AvgError      average error on the (X,Y).
+                * AvgRelError   average relative error on the non-zero Y
+                * MaxError      maximum error
+                                NON-WEIGHTED ERRORS ARE CALCULATED
+
+    IMPORTANT:
+        this subroitine doesn't calculate task's condition number for K<>0.
+
+    IMPORTANT:
+        this subroitine supports only even M's
+
+
+    ORDER OF POINTS
+
+    Subroutine automatically sorts points, so caller may pass unsorted array.
+
+    SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+    Setting constraints can lead  to undesired  results,  like ill-conditioned
+    behavior, or inconsistency being detected. From the other side,  it allows
+    us to improve quality of the fit. Here we summarize  our  experience  with
+    constrained regression splines:
+    * excessive constraints can be inconsistent. Splines are  piecewise  cubic
+      functions, and it is easy to create an example, where  large  number  of
+      constraints  concentrated  in  small  area will result in inconsistency.
+      Just because spline is not flexible enough to satisfy all of  them.  And
+      same constraints spread across the  [min(x),max(x)]  will  be  perfectly
+      consistent.
+    * the more evenly constraints are spread across [min(x),max(x)],  the more
+      chances that they will be consistent
+    * the  greater  is  M (given  fixed  constraints),  the  more chances that
+      constraints will be consistent
+    * in the general case, consistency of constraints is NOT GUARANTEED.
+    * in the several special cases, however, we can guarantee consistency.
+    * one of this cases is  M>=4  and   constraints  on   the  function  value
+      (AND/OR its derivative) at the interval boundaries.
+    * another special case is M>=4  and  ONE  constraint on the function value
+      (OR, BUT NOT AND, derivative) anywhere in [min(x),max(x)]
+
+    Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
+    can't solve your task without them. Anything beyond  special  cases  given
+    above is not guaranteed and may result in inconsistency.
+
+      -- ALGLIB PROJECT --
+         Copyright 18.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void spline1dfithermitewc(double[] x, double[] y, double[] w, int n, double[] xc, double[] yc, int[] dc, int k, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        lsfit.spline1dfithermitewc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
+        return;
+    }
+    public static void spline1dfithermitewc(double[] x, double[] y, double[] w, double[] xc, double[] yc, int[] dc, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        int n;
+        int k;
+        if( (ap.len(x)!=ap.len(y)) || (ap.len(x)!=ap.len(w)))
+            throw new alglibexception("Error while calling 'spline1dfithermitewc': looks like one of arguments has wrong size");
+        if( (ap.len(xc)!=ap.len(yc)) || (ap.len(xc)!=ap.len(dc)))
+            throw new alglibexception("Error while calling 'spline1dfithermitewc': looks like one of arguments has wrong size");
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        n = ap.len(x);
+        k = ap.len(xc);
+        lsfit.spline1dfithermitewc(x, y, w, n, xc, yc, dc, k, m, ref info, s.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Least squares fitting by cubic spline.
+
+    This subroutine is "lightweight" alternative for more complex and feature-
+    rich Spline1DFitCubicWC().  See  Spline1DFitCubicWC() for more information
+    about subroutine parameters (we don't duplicate it here because of length)
+
+      -- ALGLIB PROJECT --
+         Copyright 18.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void spline1dfitcubic(double[] x, double[] y, int n, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        lsfit.spline1dfitcubic(x, y, n, m, ref info, s.innerobj, rep.innerobj);
+        return;
+    }
+    public static void spline1dfitcubic(double[] x, double[] y, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        int n;
+        if( (ap.len(x)!=ap.len(y)))
+            throw new alglibexception("Error while calling 'spline1dfitcubic': looks like one of arguments has wrong size");
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        n = ap.len(x);
+        lsfit.spline1dfitcubic(x, y, n, m, ref info, s.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Least squares fitting by Hermite spline.
+
+    This subroutine is "lightweight" alternative for more complex and feature-
+    rich Spline1DFitHermiteWC().  See Spline1DFitHermiteWC()  description  for
+    more information about subroutine parameters (we don't duplicate  it  here
+    because of length).
+
+      -- ALGLIB PROJECT --
+         Copyright 18.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void spline1dfithermite(double[] x, double[] y, int n, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        lsfit.spline1dfithermite(x, y, n, m, ref info, s.innerobj, rep.innerobj);
+        return;
+    }
+    public static void spline1dfithermite(double[] x, double[] y, int m, out int info, out spline1dinterpolant s, out spline1dfitreport rep)
+    {
+        int n;
+        if( (ap.len(x)!=ap.len(y)))
+            throw new alglibexception("Error while calling 'spline1dfithermite': looks like one of arguments has wrong size");
+        info = 0;
+        s = new spline1dinterpolant();
+        rep = new spline1dfitreport();
+        n = ap.len(x);
+        lsfit.spline1dfithermite(x, y, n, m, ref info, s.innerobj, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted linear least squares fitting.
+
+    QR decomposition is used to reduce task to MxM, then triangular solver  or
+    SVD-based solver is used depending on condition number of the  system.  It
+    allows to maximize speed and retain decent accuracy.
+
+    INPUT PARAMETERS:
+        Y       -   array[0..N-1] Function values in  N  points.
+        W       -   array[0..N-1]  Weights  corresponding to function  values.
+                    Each summand in square  sum  of  approximation  deviations
+                    from  given  values  is  multiplied  by  the   square   of
+                    corresponding weight.
+        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                    FMatrix[I, J] - value of J-th basis function in I-th point.
+        N       -   number of points used. N>=1.
+        M       -   number of basis functions, M>=1.
+
+    OUTPUT PARAMETERS:
+        Info    -   error code:
+                    * -4    internal SVD decomposition subroutine failed (very
+                            rare and for degenerate systems only)
+                    * -1    incorrect N/M were specified
+                    *  1    task is solved
+        C       -   decomposition coefficients, array[0..M-1]
+        Rep     -   fitting report. Following fields are set:
+                    * Rep.TaskRCond     reciprocal of condition number
+                    * RMSError          rms error on the (X,Y).
+                    * AvgError          average error on the (X,Y).
+                    * AvgRelError       average relative error on the non-zero Y
+                    * MaxError          maximum error
+                                        NON-WEIGHTED ERRORS ARE CALCULATED
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitlinearw(double[] y, double[] w, double[,] fmatrix, int n, int m, out int info, out double[] c, out lsfitreport rep)
+    {
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        lsfit.lsfitlinearw(y, w, fmatrix, n, m, ref info, ref c, rep.innerobj);
+        return;
+    }
+    public static void lsfitlinearw(double[] y, double[] w, double[,] fmatrix, out int info, out double[] c, out lsfitreport rep)
+    {
+        int n;
+        int m;
+        if( (ap.len(y)!=ap.len(w)) || (ap.len(y)!=ap.rows(fmatrix)))
+            throw new alglibexception("Error while calling 'lsfitlinearw': looks like one of arguments has wrong size");
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        n = ap.len(y);
+        m = ap.cols(fmatrix);
+        lsfit.lsfitlinearw(y, w, fmatrix, n, m, ref info, ref c, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted constained linear least squares fitting.
+
+    This  is  variation  of LSFitLinearW(), which searchs for min|A*x=b| given
+    that  K  additional  constaints  C*x=bc are satisfied. It reduces original
+    task to modified one: min|B*y-d| WITHOUT constraints,  then LSFitLinearW()
+    is called.
+
+    INPUT PARAMETERS:
+        Y       -   array[0..N-1] Function values in  N  points.
+        W       -   array[0..N-1]  Weights  corresponding to function  values.
+                    Each summand in square  sum  of  approximation  deviations
+                    from  given  values  is  multiplied  by  the   square   of
+                    corresponding weight.
+        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                    FMatrix[I,J] - value of J-th basis function in I-th point.
+        CMatrix -   a table of constaints, array[0..K-1,0..M].
+                    I-th row of CMatrix corresponds to I-th linear constraint:
+                    CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
+        N       -   number of points used. N>=1.
+        M       -   number of basis functions, M>=1.
+        K       -   number of constraints, 0 <= K < M
+                    K=0 corresponds to absence of constraints.
+
+    OUTPUT PARAMETERS:
+        Info    -   error code:
+                    * -4    internal SVD decomposition subroutine failed (very
+                            rare and for degenerate systems only)
+                    * -3    either   too   many  constraints  (M   or   more),
+                            degenerate  constraints   (some   constraints  are
+                            repetead twice) or inconsistent  constraints  were
+                            specified.
+                    *  1    task is solved
+        C       -   decomposition coefficients, array[0..M-1]
+        Rep     -   fitting report. Following fields are set:
+                    * RMSError          rms error on the (X,Y).
+                    * AvgError          average error on the (X,Y).
+                    * AvgRelError       average relative error on the non-zero Y
+                    * MaxError          maximum error
+                                        NON-WEIGHTED ERRORS ARE CALCULATED
+
+    IMPORTANT:
+        this subroitine doesn't calculate task's condition number for K<>0.
+
+      -- ALGLIB --
+         Copyright 07.09.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitlinearwc(double[] y, double[] w, double[,] fmatrix, double[,] cmatrix, int n, int m, int k, out int info, out double[] c, out lsfitreport rep)
+    {
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        lsfit.lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
+        return;
+    }
+    public static void lsfitlinearwc(double[] y, double[] w, double[,] fmatrix, double[,] cmatrix, out int info, out double[] c, out lsfitreport rep)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.len(y)!=ap.len(w)) || (ap.len(y)!=ap.rows(fmatrix)))
+            throw new alglibexception("Error while calling 'lsfitlinearwc': looks like one of arguments has wrong size");
+        if( (ap.cols(fmatrix)!=ap.cols(cmatrix)-1))
+            throw new alglibexception("Error while calling 'lsfitlinearwc': looks like one of arguments has wrong size");
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        n = ap.len(y);
+        m = ap.cols(fmatrix);
+        k = ap.rows(cmatrix);
+        lsfit.lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Linear least squares fitting.
+
+    QR decomposition is used to reduce task to MxM, then triangular solver  or
+    SVD-based solver is used depending on condition number of the  system.  It
+    allows to maximize speed and retain decent accuracy.
+
+    INPUT PARAMETERS:
+        Y       -   array[0..N-1] Function values in  N  points.
+        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                    FMatrix[I, J] - value of J-th basis function in I-th point.
+        N       -   number of points used. N>=1.
+        M       -   number of basis functions, M>=1.
+
+    OUTPUT PARAMETERS:
+        Info    -   error code:
+                    * -4    internal SVD decomposition subroutine failed (very
+                            rare and for degenerate systems only)
+                    *  1    task is solved
+        C       -   decomposition coefficients, array[0..M-1]
+        Rep     -   fitting report. Following fields are set:
+                    * Rep.TaskRCond     reciprocal of condition number
+                    * RMSError          rms error on the (X,Y).
+                    * AvgError          average error on the (X,Y).
+                    * AvgRelError       average relative error on the non-zero Y
+                    * MaxError          maximum error
+                                        NON-WEIGHTED ERRORS ARE CALCULATED
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitlinear(double[] y, double[,] fmatrix, int n, int m, out int info, out double[] c, out lsfitreport rep)
+    {
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        lsfit.lsfitlinear(y, fmatrix, n, m, ref info, ref c, rep.innerobj);
+        return;
+    }
+    public static void lsfitlinear(double[] y, double[,] fmatrix, out int info, out double[] c, out lsfitreport rep)
+    {
+        int n;
+        int m;
+        if( (ap.len(y)!=ap.rows(fmatrix)))
+            throw new alglibexception("Error while calling 'lsfitlinear': looks like one of arguments has wrong size");
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        n = ap.len(y);
+        m = ap.cols(fmatrix);
+        lsfit.lsfitlinear(y, fmatrix, n, m, ref info, ref c, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Constained linear least squares fitting.
+
+    This  is  variation  of LSFitLinear(),  which searchs for min|A*x=b| given
+    that  K  additional  constaints  C*x=bc are satisfied. It reduces original
+    task to modified one: min|B*y-d| WITHOUT constraints,  then  LSFitLinear()
+    is called.
+
+    INPUT PARAMETERS:
+        Y       -   array[0..N-1] Function values in  N  points.
+        FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                    FMatrix[I,J] - value of J-th basis function in I-th point.
+        CMatrix -   a table of constaints, array[0..K-1,0..M].
+                    I-th row of CMatrix corresponds to I-th linear constraint:
+                    CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
+        N       -   number of points used. N>=1.
+        M       -   number of basis functions, M>=1.
+        K       -   number of constraints, 0 <= K < M
+                    K=0 corresponds to absence of constraints.
+
+    OUTPUT PARAMETERS:
+        Info    -   error code:
+                    * -4    internal SVD decomposition subroutine failed (very
+                            rare and for degenerate systems only)
+                    * -3    either   too   many  constraints  (M   or   more),
+                            degenerate  constraints   (some   constraints  are
+                            repetead twice) or inconsistent  constraints  were
+                            specified.
+                    *  1    task is solved
+        C       -   decomposition coefficients, array[0..M-1]
+        Rep     -   fitting report. Following fields are set:
+                    * RMSError          rms error on the (X,Y).
+                    * AvgError          average error on the (X,Y).
+                    * AvgRelError       average relative error on the non-zero Y
+                    * MaxError          maximum error
+                                        NON-WEIGHTED ERRORS ARE CALCULATED
+
+    IMPORTANT:
+        this subroitine doesn't calculate task's condition number for K<>0.
+
+      -- ALGLIB --
+         Copyright 07.09.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitlinearc(double[] y, double[,] fmatrix, double[,] cmatrix, int n, int m, int k, out int info, out double[] c, out lsfitreport rep)
+    {
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        lsfit.lsfitlinearc(y, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
+        return;
+    }
+    public static void lsfitlinearc(double[] y, double[,] fmatrix, double[,] cmatrix, out int info, out double[] c, out lsfitreport rep)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.len(y)!=ap.rows(fmatrix)))
+            throw new alglibexception("Error while calling 'lsfitlinearc': looks like one of arguments has wrong size");
+        if( (ap.cols(fmatrix)!=ap.cols(cmatrix)-1))
+            throw new alglibexception("Error while calling 'lsfitlinearc': looks like one of arguments has wrong size");
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        n = ap.len(y);
+        m = ap.cols(fmatrix);
+        k = ap.rows(cmatrix);
+        lsfit.lsfitlinearc(y, fmatrix, cmatrix, n, m, k, ref info, ref c, rep.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted nonlinear least squares fitting using function values only.
+
+    Combination of numerical differentiation and secant updates is used to
+    obtain function Jacobian.
+
+    Nonlinear task min(F(c)) is solved, where
+
+        F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
+
+        * N is a number of points,
+        * M is a dimension of a space points belong to,
+        * K is a dimension of a space of parameters being fitted,
+        * w is an N-dimensional vector of weight coefficients,
+        * x is a set of N points, each of them is an M-dimensional vector,
+        * c is a K-dimensional vector of parameters being fitted
+
+    This subroutine uses only f(c,x[i]).
+
+    INPUT PARAMETERS:
+        X       -   array[0..N-1,0..M-1], points (one row = one point)
+        Y       -   array[0..N-1], function values.
+        W       -   weights, array[0..N-1]
+        C       -   array[0..K-1], initial approximation to the solution,
+        N       -   number of points, N>1
+        M       -   dimension of space
+        K       -   number of parameters being fitted
+        DiffStep-   numerical differentiation step;
+                    should not be very small or large;
+                    large = loss of accuracy
+                    small = growth of round-off errors
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 18.10.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitcreatewf(double[,] x, double[] y, double[] w, double[] c, int n, int m, int k, double diffstep, out lsfitstate state)
+    {
+        state = new lsfitstate();
+        lsfit.lsfitcreatewf(x, y, w, c, n, m, k, diffstep, state.innerobj);
+        return;
+    }
+    public static void lsfitcreatewf(double[,] x, double[] y, double[] w, double[] c, double diffstep, out lsfitstate state)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.rows(x)!=ap.len(y)) || (ap.rows(x)!=ap.len(w)))
+            throw new alglibexception("Error while calling 'lsfitcreatewf': looks like one of arguments has wrong size");
+        state = new lsfitstate();
+        n = ap.rows(x);
+        m = ap.cols(x);
+        k = ap.len(c);
+        lsfit.lsfitcreatewf(x, y, w, c, n, m, k, diffstep, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Nonlinear least squares fitting using function values only.
+
+    Combination of numerical differentiation and secant updates is used to
+    obtain function Jacobian.
+
+    Nonlinear task min(F(c)) is solved, where
+
+        F(c) = (f(c,x[0])-y[0])^2 + ... + (f(c,x[n-1])-y[n-1])^2,
+
+        * N is a number of points,
+        * M is a dimension of a space points belong to,
+        * K is a dimension of a space of parameters being fitted,
+        * w is an N-dimensional vector of weight coefficients,
+        * x is a set of N points, each of them is an M-dimensional vector,
+        * c is a K-dimensional vector of parameters being fitted
+
+    This subroutine uses only f(c,x[i]).
+
+    INPUT PARAMETERS:
+        X       -   array[0..N-1,0..M-1], points (one row = one point)
+        Y       -   array[0..N-1], function values.
+        C       -   array[0..K-1], initial approximation to the solution,
+        N       -   number of points, N>1
+        M       -   dimension of space
+        K       -   number of parameters being fitted
+        DiffStep-   numerical differentiation step;
+                    should not be very small or large;
+                    large = loss of accuracy
+                    small = growth of round-off errors
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 18.10.2008 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitcreatef(double[,] x, double[] y, double[] c, int n, int m, int k, double diffstep, out lsfitstate state)
+    {
+        state = new lsfitstate();
+        lsfit.lsfitcreatef(x, y, c, n, m, k, diffstep, state.innerobj);
+        return;
+    }
+    public static void lsfitcreatef(double[,] x, double[] y, double[] c, double diffstep, out lsfitstate state)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.rows(x)!=ap.len(y)))
+            throw new alglibexception("Error while calling 'lsfitcreatef': looks like one of arguments has wrong size");
+        state = new lsfitstate();
+        n = ap.rows(x);
+        m = ap.cols(x);
+        k = ap.len(c);
+        lsfit.lsfitcreatef(x, y, c, n, m, k, diffstep, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted nonlinear least squares fitting using gradient only.
+
+    Nonlinear task min(F(c)) is solved, where
+
+        F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
+
+        * N is a number of points,
+        * M is a dimension of a space points belong to,
+        * K is a dimension of a space of parameters being fitted,
+        * w is an N-dimensional vector of weight coefficients,
+        * x is a set of N points, each of them is an M-dimensional vector,
+        * c is a K-dimensional vector of parameters being fitted
+
+    This subroutine uses only f(c,x[i]) and its gradient.
+
+    INPUT PARAMETERS:
+        X       -   array[0..N-1,0..M-1], points (one row = one point)
+        Y       -   array[0..N-1], function values.
+        W       -   weights, array[0..N-1]
+        C       -   array[0..K-1], initial approximation to the solution,
+        N       -   number of points, N>1
+        M       -   dimension of space
+        K       -   number of parameters being fitted
+        CheapFG -   boolean flag, which is:
+                    * True  if both function and gradient calculation complexity
+                            are less than O(M^2).  An improved  algorithm  can
+                            be  used  which corresponds  to  FGJ  scheme  from
+                            MINLM unit.
+                    * False otherwise.
+                            Standard Jacibian-bases  Levenberg-Marquardt  algo
+                            will be used (FJ scheme).
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+    See also:
+        LSFitResults
+        LSFitCreateFG (fitting without weights)
+        LSFitCreateWFGH (fitting using Hessian)
+        LSFitCreateFGH (fitting using Hessian, without weights)
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitcreatewfg(double[,] x, double[] y, double[] w, double[] c, int n, int m, int k, bool cheapfg, out lsfitstate state)
+    {
+        state = new lsfitstate();
+        lsfit.lsfitcreatewfg(x, y, w, c, n, m, k, cheapfg, state.innerobj);
+        return;
+    }
+    public static void lsfitcreatewfg(double[,] x, double[] y, double[] w, double[] c, bool cheapfg, out lsfitstate state)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.rows(x)!=ap.len(y)) || (ap.rows(x)!=ap.len(w)))
+            throw new alglibexception("Error while calling 'lsfitcreatewfg': looks like one of arguments has wrong size");
+        state = new lsfitstate();
+        n = ap.rows(x);
+        m = ap.cols(x);
+        k = ap.len(c);
+        lsfit.lsfitcreatewfg(x, y, w, c, n, m, k, cheapfg, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Nonlinear least squares fitting using gradient only, without individual
+    weights.
+
+    Nonlinear task min(F(c)) is solved, where
+
+        F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
+
+        * N is a number of points,
+        * M is a dimension of a space points belong to,
+        * K is a dimension of a space of parameters being fitted,
+        * x is a set of N points, each of them is an M-dimensional vector,
+        * c is a K-dimensional vector of parameters being fitted
+
+    This subroutine uses only f(c,x[i]) and its gradient.
+
+    INPUT PARAMETERS:
+        X       -   array[0..N-1,0..M-1], points (one row = one point)
+        Y       -   array[0..N-1], function values.
+        C       -   array[0..K-1], initial approximation to the solution,
+        N       -   number of points, N>1
+        M       -   dimension of space
+        K       -   number of parameters being fitted
+        CheapFG -   boolean flag, which is:
+                    * True  if both function and gradient calculation complexity
+                            are less than O(M^2).  An improved  algorithm  can
+                            be  used  which corresponds  to  FGJ  scheme  from
+                            MINLM unit.
+                    * False otherwise.
+                            Standard Jacibian-bases  Levenberg-Marquardt  algo
+                            will be used (FJ scheme).
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitcreatefg(double[,] x, double[] y, double[] c, int n, int m, int k, bool cheapfg, out lsfitstate state)
+    {
+        state = new lsfitstate();
+        lsfit.lsfitcreatefg(x, y, c, n, m, k, cheapfg, state.innerobj);
+        return;
+    }
+    public static void lsfitcreatefg(double[,] x, double[] y, double[] c, bool cheapfg, out lsfitstate state)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.rows(x)!=ap.len(y)))
+            throw new alglibexception("Error while calling 'lsfitcreatefg': looks like one of arguments has wrong size");
+        state = new lsfitstate();
+        n = ap.rows(x);
+        m = ap.cols(x);
+        k = ap.len(c);
+        lsfit.lsfitcreatefg(x, y, c, n, m, k, cheapfg, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Weighted nonlinear least squares fitting using gradient/Hessian.
+
+    Nonlinear task min(F(c)) is solved, where
+
+        F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
+
+        * N is a number of points,
+        * M is a dimension of a space points belong to,
+        * K is a dimension of a space of parameters being fitted,
+        * w is an N-dimensional vector of weight coefficients,
+        * x is a set of N points, each of them is an M-dimensional vector,
+        * c is a K-dimensional vector of parameters being fitted
+
+    This subroutine uses f(c,x[i]), its gradient and its Hessian.
+
+    INPUT PARAMETERS:
+        X       -   array[0..N-1,0..M-1], points (one row = one point)
+        Y       -   array[0..N-1], function values.
+        W       -   weights, array[0..N-1]
+        C       -   array[0..K-1], initial approximation to the solution,
+        N       -   number of points, N>1
+        M       -   dimension of space
+        K       -   number of parameters being fitted
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitcreatewfgh(double[,] x, double[] y, double[] w, double[] c, int n, int m, int k, out lsfitstate state)
+    {
+        state = new lsfitstate();
+        lsfit.lsfitcreatewfgh(x, y, w, c, n, m, k, state.innerobj);
+        return;
+    }
+    public static void lsfitcreatewfgh(double[,] x, double[] y, double[] w, double[] c, out lsfitstate state)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.rows(x)!=ap.len(y)) || (ap.rows(x)!=ap.len(w)))
+            throw new alglibexception("Error while calling 'lsfitcreatewfgh': looks like one of arguments has wrong size");
+        state = new lsfitstate();
+        n = ap.rows(x);
+        m = ap.cols(x);
+        k = ap.len(c);
+        lsfit.lsfitcreatewfgh(x, y, w, c, n, m, k, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Nonlinear least squares fitting using gradient/Hessian, without individial
+    weights.
+
+    Nonlinear task min(F(c)) is solved, where
+
+        F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
+
+        * N is a number of points,
+        * M is a dimension of a space points belong to,
+        * K is a dimension of a space of parameters being fitted,
+        * x is a set of N points, each of them is an M-dimensional vector,
+        * c is a K-dimensional vector of parameters being fitted
+
+    This subroutine uses f(c,x[i]), its gradient and its Hessian.
+
+    INPUT PARAMETERS:
+        X       -   array[0..N-1,0..M-1], points (one row = one point)
+        Y       -   array[0..N-1], function values.
+        C       -   array[0..K-1], initial approximation to the solution,
+        N       -   number of points, N>1
+        M       -   dimension of space
+        K       -   number of parameters being fitted
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitcreatefgh(double[,] x, double[] y, double[] c, int n, int m, int k, out lsfitstate state)
+    {
+        state = new lsfitstate();
+        lsfit.lsfitcreatefgh(x, y, c, n, m, k, state.innerobj);
+        return;
+    }
+    public static void lsfitcreatefgh(double[,] x, double[] y, double[] c, out lsfitstate state)
+    {
+        int n;
+        int m;
+        int k;
+        if( (ap.rows(x)!=ap.len(y)))
+            throw new alglibexception("Error while calling 'lsfitcreatefgh': looks like one of arguments has wrong size");
+        state = new lsfitstate();
+        n = ap.rows(x);
+        m = ap.cols(x);
+        k = ap.len(c);
+        lsfit.lsfitcreatefgh(x, y, c, n, m, k, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    Stopping conditions for nonlinear least squares fitting.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        EpsF    -   stopping criterion. Algorithm stops if
+                    |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
+        EpsX    -   >=0
+                    The subroutine finishes its work if  on  k+1-th  iteration
+                    the condition |v|<=EpsX is fulfilled, where:
+                    * |.| means Euclidian norm
+                    * v - scaled step vector, v[i]=dx[i]/s[i]
+                    * dx - ste pvector, dx=X(k+1)-X(k)
+                    * s - scaling coefficients set by LSFitSetScale()
+        MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
+                    iterations   is    unlimited.   Only   Levenberg-Marquardt
+                    iterations  are  counted  (L-BFGS/CG  iterations  are  NOT
+                    counted because their cost is very low compared to that of
+                    LM).
+
+    NOTE
+
+    Passing EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to automatic
+    stopping criterion selection (according to the scheme used by MINLM unit).
+
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitsetcond(lsfitstate state, double epsf, double epsx, int maxits)
+    {
+
+        lsfit.lsfitsetcond(state.innerobj, epsf, epsx, maxits);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets maximum step length
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
+                    want to limit step length.
+
+    Use this subroutine when you optimize target function which contains exp()
+    or  other  fast  growing  functions,  and optimization algorithm makes too
+    large  steps  which  leads  to overflow. This function allows us to reject
+    steps  that  are  too  large  (and  therefore  expose  us  to the possible
+    overflow) without actually calculating function value at the x+stp*d.
+
+    NOTE: non-zero StpMax leads to moderate  performance  degradation  because
+    intermediate  step  of  preconditioned L-BFGS optimization is incompatible
+    with limits on step size.
+
+      -- ALGLIB --
+         Copyright 02.04.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitsetstpmax(lsfitstate state, double stpmax)
+    {
+
+        lsfit.lsfitsetstpmax(state.innerobj, stpmax);
+        return;
+    }
+
+    /*************************************************************************
+    This function turns on/off reporting.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        NeedXRep-   whether iteration reports are needed or not
+
+    When reports are needed, State.C (current parameters) and State.F (current
+    value of fitting function) are reported.
+
+
+      -- ALGLIB --
+         Copyright 15.08.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitsetxrep(lsfitstate state, bool needxrep)
+    {
+
+        lsfit.lsfitsetxrep(state.innerobj, needxrep);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets scaling coefficients for underlying optimizer.
+
+    ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+    size and gradient are scaled before comparison with tolerances).  Scale of
+    the I-th variable is a translation invariant measure of:
+    a) "how large" the variable is
+    b) how large the step should be to make significant changes in the function
+
+    Generally, scale is NOT considered to be a form of preconditioner.  But LM
+    optimizer is unique in that it uses scaling matrix both  in  the  stopping
+    condition tests and as Marquardt damping factor.
+
+    Proper scaling is very important for the algorithm performance. It is less
+    important for the quality of results, but still has some influence (it  is
+    easier  to  converge  when  variables  are  properly  scaled, so premature
+    stopping is possible when very badly scalled variables are  combined  with
+    relaxed stopping conditions).
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        S       -   array[N], non-zero scaling coefficients
+                    S[i] may be negative, sign doesn't matter.
+
+      -- ALGLIB --
+         Copyright 14.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitsetscale(lsfitstate state, double[] s)
+    {
+
+        lsfit.lsfitsetscale(state.innerobj, s);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets boundary constraints for underlying optimizer
+
+    Boundary constraints are inactive by default (after initial creation).
+    They are preserved until explicitly turned off with another SetBC() call.
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        BndL    -   lower bounds, array[K].
+                    If some (all) variables are unbounded, you may specify
+                    very small number or -INF (latter is recommended because
+                    it will allow solver to use better algorithm).
+        BndU    -   upper bounds, array[K].
+                    If some (all) variables are unbounded, you may specify
+                    very large number or +INF (latter is recommended because
+                    it will allow solver to use better algorithm).
+
+    NOTE 1: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+    variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+    NOTE 2: unlike other constrained optimization algorithms, this solver  has
+    following useful properties:
+    * bound constraints are always satisfied exactly
+    * function is evaluated only INSIDE area specified by bound constraints
+
+      -- ALGLIB --
+         Copyright 14.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitsetbc(lsfitstate state, double[] bndl, double[] bndu)
+    {
+
+        lsfit.lsfitsetbc(state.innerobj, bndl, bndu);
+        return;
+    }
+
+    /*************************************************************************
+    This function provides reverse communication interface
+    Reverse communication interface is not documented or recommended to use.
+    See below for functions which provide better documented API
+    *************************************************************************/
+    public static bool lsfititeration(lsfitstate state)
+    {
+
+        bool result = lsfit.lsfititeration(state.innerobj);
+        return result;
+    }
+    /*************************************************************************
+    This family of functions is used to launcn iterations of nonlinear fitter
+
+    These functions accept following parameters:
+        func    -   callback which calculates function (or merit function)
+                    value func at given point x
+        grad    -   callback which calculates function (or merit function)
+                    value func and gradient grad at given point x
+        hess    -   callback which calculates function (or merit function)
+                    value func, gradient grad and Hessian hess at given point x
+        rep     -   optional callback which is called after each iteration
+                    can be null
+        obj     -   optional object which is passed to func/grad/hess/jac/rep
+                    can be null
+
+    NOTES:
+
+    1. this algorithm is somewhat unusual because it works with  parameterized
+       function f(C,X), where X is a function argument (we  have  many  points
+       which are characterized by different  argument  values),  and  C  is  a
+       parameter to fit.
+
+       For example, if we want to do linear fit by f(c0,c1,x) = c0*x+c1,  then
+       x will be argument, and {c0,c1} will be parameters.
+
+       It is important to understand that this algorithm finds minimum in  the
+       space of function PARAMETERS (not arguments), so it  needs  derivatives
+       of f() with respect to C, not X.
+
+       In the example above it will need f=c0*x+c1 and {df/dc0,df/dc1} = {x,1}
+       instead of {df/dx} = {c0}.
+
+    2. Callback functions accept C as the first parameter, and X as the second
+
+    3. If  state  was  created  with  LSFitCreateFG(),  algorithm  needs  just
+       function   and   its   gradient,   but   if   state   was  created with
+       LSFitCreateFGH(), algorithm will need function, gradient and Hessian.
+
+       According  to  the  said  above,  there  ase  several  versions of this
+       function, which accept different sets of callbacks.
+
+       This flexibility opens way to subtle errors - you may create state with
+       LSFitCreateFGH() (optimization using Hessian), but call function  which
+       does not accept Hessian. So when algorithm will request Hessian,  there
+       will be no callback to call. In this case exception will be thrown.
+
+       Be careful to avoid such errors because there is no way to find them at
+       compile time - you can see them at runtime only.
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+
+    *************************************************************************/
+    public static void lsfitfit(lsfitstate state, ndimensional_pfunc func, ndimensional_rep rep, object obj)
+    {
+        if( func==null )
+            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (func is null)");
+        while( alglib.lsfititeration(state) )
+        {
+            if( state.needf )
+            {
+                func(state.c, state.x, ref state.innerobj.f, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.c, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'lsfitfit' (some derivatives were not provided?)");
+        }
+    }
+
+
+    public static void lsfitfit(lsfitstate state, ndimensional_pfunc func, ndimensional_pgrad grad, ndimensional_rep rep, object obj)
+    {
+        if( func==null )
+            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (func is null)");
+        if( grad==null )
+            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (grad is null)");
+        while( alglib.lsfititeration(state) )
+        {
+            if( state.needf )
+            {
+                func(state.c, state.x, ref state.innerobj.f, obj);
+                continue;
+            }
+            if( state.needfg )
+            {
+                grad(state.c, state.x, ref state.innerobj.f, state.innerobj.g, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.c, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'lsfitfit' (some derivatives were not provided?)");
+        }
+    }
+
+
+    public static void lsfitfit(lsfitstate state, ndimensional_pfunc func, ndimensional_pgrad grad, ndimensional_phess hess, ndimensional_rep rep, object obj)
+    {
+        if( func==null )
+            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (func is null)");
+        if( grad==null )
+            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (grad is null)");
+        if( hess==null )
+            throw new alglibexception("ALGLIB: error in 'lsfitfit()' (hess is null)");
+        while( alglib.lsfititeration(state) )
+        {
+            if( state.needf )
+            {
+                func(state.c, state.x, ref state.innerobj.f, obj);
+                continue;
+            }
+            if( state.needfg )
+            {
+                grad(state.c, state.x, ref state.innerobj.f, state.innerobj.g, obj);
+                continue;
+            }
+            if( state.needfgh )
+            {
+                hess(state.c, state.x, ref state.innerobj.f, state.innerobj.g, state.innerobj.h, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.c, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'lsfitfit' (some derivatives were not provided?)");
+        }
+    }
+
+
+
+    /*************************************************************************
+    Nonlinear least squares fitting results.
+
+    Called after return from LSFitFit().
+
+    INPUT PARAMETERS:
+        State   -   algorithm state
+
+    OUTPUT PARAMETERS:
+        Info    -   completetion code:
+                        *  1    relative function improvement is no more than
+                                EpsF.
+                        *  2    relative step is no more than EpsX.
+                        *  4    gradient norm is no more than EpsG
+                        *  5    MaxIts steps was taken
+                        *  7    stopping conditions are too stringent,
+                                further improvement is impossible
+        C       -   array[0..K-1], solution
+        Rep     -   optimization report. Following fields are set:
+                    * Rep.TerminationType completetion code:
+                    * RMSError          rms error on the (X,Y).
+                    * AvgError          average error on the (X,Y).
+                    * AvgRelError       average relative error on the non-zero Y
+                    * MaxError          maximum error
+                                        NON-WEIGHTED ERRORS ARE CALCULATED
+                    * WRMSError         weighted rms error on the (X,Y).
+
+
+      -- ALGLIB --
+         Copyright 17.08.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void lsfitresults(lsfitstate state, out int info, out double[] c, out lsfitreport rep)
+    {
+        info = 0;
+        c = new double[0];
+        rep = new lsfitreport();
+        lsfit.lsfitresults(state.innerobj, ref info, ref c, rep.innerobj);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Parametric spline inteprolant: 2-dimensional curve.
+
+    You should not try to access its members directly - use PSpline2XXXXXXXX()
+    functions instead.
+    *************************************************************************/
+    public class pspline2interpolant
+    {
+        //
+        // Public declarations
+        //
+
+        public pspline2interpolant()
+        {
+            _innerobj = new pspline.pspline2interpolant();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private pspline.pspline2interpolant _innerobj;
+        public pspline.pspline2interpolant innerobj { get { return _innerobj; } }
+        public pspline2interpolant(pspline.pspline2interpolant obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    Parametric spline inteprolant: 3-dimensional curve.
+
+    You should not try to access its members directly - use PSpline3XXXXXXXX()
+    functions instead.
+    *************************************************************************/
+    public class pspline3interpolant
+    {
+        //
+        // Public declarations
+        //
+
+        public pspline3interpolant()
+        {
+            _innerobj = new pspline.pspline3interpolant();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private pspline.pspline3interpolant _innerobj;
+        public pspline.pspline3interpolant innerobj { get { return _innerobj; } }
+        public pspline3interpolant(pspline.pspline3interpolant obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+    /*************************************************************************
+    This function  builds  non-periodic 2-dimensional parametric spline  which
+    starts at (X[0],Y[0]) and ends at (X[N-1],Y[N-1]).
+
+    INPUT PARAMETERS:
+        XY  -   points, array[0..N-1,0..1].
+                XY[I,0:1] corresponds to the Ith point.
+                Order of points is important!
+        N   -   points count, N>=5 for Akima splines, N>=2 for other types  of
+                splines.
+        ST  -   spline type:
+                * 0     Akima spline
+                * 1     parabolically terminated Catmull-Rom spline (Tension=0)
+                * 2     parabolically terminated cubic spline
+        PT  -   parameterization type:
+                * 0     uniform
+                * 1     chord length
+                * 2     centripetal
+
+    OUTPUT PARAMETERS:
+        P   -   parametric spline interpolant
+
+
+    NOTES:
+    * this function  assumes  that  there all consequent points  are distinct.
+      I.e. (x0,y0)<>(x1,y1),  (x1,y1)<>(x2,y2),  (x2,y2)<>(x3,y3)  and  so on.
+      However, non-consequent points may coincide, i.e. we can  have  (x0,y0)=
+      =(x2,y2).
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline2build(double[,] xy, int n, int st, int pt, out pspline2interpolant p)
+    {
+        p = new pspline2interpolant();
+        pspline.pspline2build(xy, n, st, pt, p.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This function  builds  non-periodic 3-dimensional parametric spline  which
+    starts at (X[0],Y[0],Z[0]) and ends at (X[N-1],Y[N-1],Z[N-1]).
+
+    Same as PSpline2Build() function, but for 3D, so we  won't  duplicate  its
+    description here.
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline3build(double[,] xy, int n, int st, int pt, out pspline3interpolant p)
+    {
+        p = new pspline3interpolant();
+        pspline.pspline3build(xy, n, st, pt, p.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This  function  builds  periodic  2-dimensional  parametric  spline  which
+    starts at (X[0],Y[0]), goes through all points to (X[N-1],Y[N-1]) and then
+    back to (X[0],Y[0]).
+
+    INPUT PARAMETERS:
+        XY  -   points, array[0..N-1,0..1].
+                XY[I,0:1] corresponds to the Ith point.
+                XY[N-1,0:1] must be different from XY[0,0:1].
+                Order of points is important!
+        N   -   points count, N>=3 for other types of splines.
+        ST  -   spline type:
+                * 1     Catmull-Rom spline (Tension=0) with cyclic boundary conditions
+                * 2     cubic spline with cyclic boundary conditions
+        PT  -   parameterization type:
+                * 0     uniform
+                * 1     chord length
+                * 2     centripetal
+
+    OUTPUT PARAMETERS:
+        P   -   parametric spline interpolant
+
+
+    NOTES:
+    * this function  assumes  that there all consequent points  are  distinct.
+      I.e. (x0,y0)<>(x1,y1), (x1,y1)<>(x2,y2),  (x2,y2)<>(x3,y3)  and  so  on.
+      However, non-consequent points may coincide, i.e. we can  have  (x0,y0)=
+      =(x2,y2).
+    * last point of sequence is NOT equal to the first  point.  You  shouldn't
+      make curve "explicitly periodic" by making them equal.
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline2buildperiodic(double[,] xy, int n, int st, int pt, out pspline2interpolant p)
+    {
+        p = new pspline2interpolant();
+        pspline.pspline2buildperiodic(xy, n, st, pt, p.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This  function  builds  periodic  3-dimensional  parametric  spline  which
+    starts at (X[0],Y[0],Z[0]), goes through all points to (X[N-1],Y[N-1],Z[N-1])
+    and then back to (X[0],Y[0],Z[0]).
+
+    Same as PSpline2Build() function, but for 3D, so we  won't  duplicate  its
+    description here.
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline3buildperiodic(double[,] xy, int n, int st, int pt, out pspline3interpolant p)
+    {
+        p = new pspline3interpolant();
+        pspline.pspline3buildperiodic(xy, n, st, pt, p.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This function returns vector of parameter values correspoding to points.
+
+    I.e. for P created from (X[0],Y[0])...(X[N-1],Y[N-1]) and U=TValues(P)  we
+    have
+        (X[0],Y[0]) = PSpline2Calc(P,U[0]),
+        (X[1],Y[1]) = PSpline2Calc(P,U[1]),
+        (X[2],Y[2]) = PSpline2Calc(P,U[2]),
+        ...
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+
+    OUTPUT PARAMETERS:
+        N   -   array size
+        T   -   array[0..N-1]
+
+
+    NOTES:
+    * for non-periodic splines U[0]=0, U[0]<U[1]<...<U[N-1], U[N-1]=1
+    * for periodic splines     U[0]=0, U[0]<U[1]<...<U[N-1], U[N-1]<1
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline2parametervalues(pspline2interpolant p, out int n, out double[] t)
+    {
+        n = 0;
+        t = new double[0];
+        pspline.pspline2parametervalues(p.innerobj, ref n, ref t);
+        return;
+    }
+
+    /*************************************************************************
+    This function returns vector of parameter values correspoding to points.
+
+    Same as PSpline2ParameterValues(), but for 3D.
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline3parametervalues(pspline3interpolant p, out int n, out double[] t)
+    {
+        n = 0;
+        t = new double[0];
+        pspline.pspline3parametervalues(p.innerobj, ref n, ref t);
+        return;
+    }
+
+    /*************************************************************************
+    This function  calculates  the value of the parametric spline for a  given
+    value of parameter T
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X   -   X-position
+        Y   -   Y-position
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline2calc(pspline2interpolant p, double t, out double x, out double y)
+    {
+        x = 0;
+        y = 0;
+        pspline.pspline2calc(p.innerobj, t, ref x, ref y);
+        return;
+    }
+
+    /*************************************************************************
+    This function  calculates  the value of the parametric spline for a  given
+    value of parameter T.
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X   -   X-position
+        Y   -   Y-position
+        Z   -   Z-position
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline3calc(pspline3interpolant p, double t, out double x, out double y, out double z)
+    {
+        x = 0;
+        y = 0;
+        z = 0;
+        pspline.pspline3calc(p.innerobj, t, ref x, ref y, ref z);
+        return;
+    }
+
+    /*************************************************************************
+    This function  calculates  tangent vector for a given value of parameter T
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X    -   X-component of tangent vector (normalized)
+        Y    -   Y-component of tangent vector (normalized)
+
+    NOTE:
+        X^2+Y^2 is either 1 (for non-zero tangent vector) or 0.
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline2tangent(pspline2interpolant p, double t, out double x, out double y)
+    {
+        x = 0;
+        y = 0;
+        pspline.pspline2tangent(p.innerobj, t, ref x, ref y);
+        return;
+    }
+
+    /*************************************************************************
+    This function  calculates  tangent vector for a given value of parameter T
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X    -   X-component of tangent vector (normalized)
+        Y    -   Y-component of tangent vector (normalized)
+        Z    -   Z-component of tangent vector (normalized)
+
+    NOTE:
+        X^2+Y^2+Z^2 is either 1 (for non-zero tangent vector) or 0.
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline3tangent(pspline3interpolant p, double t, out double x, out double y, out double z)
+    {
+        x = 0;
+        y = 0;
+        z = 0;
+        pspline.pspline3tangent(p.innerobj, t, ref x, ref y, ref z);
+        return;
+    }
+
+    /*************************************************************************
+    This function calculates derivative, i.e. it returns (dX/dT,dY/dT).
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X   -   X-value
+        DX  -   X-derivative
+        Y   -   Y-value
+        DY  -   Y-derivative
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline2diff(pspline2interpolant p, double t, out double x, out double dx, out double y, out double dy)
+    {
+        x = 0;
+        dx = 0;
+        y = 0;
+        dy = 0;
+        pspline.pspline2diff(p.innerobj, t, ref x, ref dx, ref y, ref dy);
+        return;
+    }
+
+    /*************************************************************************
+    This function calculates derivative, i.e. it returns (dX/dT,dY/dT,dZ/dT).
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X   -   X-value
+        DX  -   X-derivative
+        Y   -   Y-value
+        DY  -   Y-derivative
+        Z   -   Z-value
+        DZ  -   Z-derivative
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline3diff(pspline3interpolant p, double t, out double x, out double dx, out double y, out double dy, out double z, out double dz)
+    {
+        x = 0;
+        dx = 0;
+        y = 0;
+        dy = 0;
+        z = 0;
+        dz = 0;
+        pspline.pspline3diff(p.innerobj, t, ref x, ref dx, ref y, ref dy, ref z, ref dz);
+        return;
+    }
+
+    /*************************************************************************
+    This function calculates first and second derivative with respect to T.
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X   -   X-value
+        DX  -   derivative
+        D2X -   second derivative
+        Y   -   Y-value
+        DY  -   derivative
+        D2Y -   second derivative
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline2diff2(pspline2interpolant p, double t, out double x, out double dx, out double d2x, out double y, out double dy, out double d2y)
+    {
+        x = 0;
+        dx = 0;
+        d2x = 0;
+        y = 0;
+        dy = 0;
+        d2y = 0;
+        pspline.pspline2diff2(p.innerobj, t, ref x, ref dx, ref d2x, ref y, ref dy, ref d2y);
+        return;
+    }
+
+    /*************************************************************************
+    This function calculates first and second derivative with respect to T.
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        T   -   point:
+                * T in [0,1] corresponds to interval spanned by points
+                * for non-periodic splines T<0 (or T>1) correspond to parts of
+                  the curve before the first (after the last) point
+                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
+                  by making T=T-floor(T).
+
+    OUTPUT PARAMETERS:
+        X   -   X-value
+        DX  -   derivative
+        D2X -   second derivative
+        Y   -   Y-value
+        DY  -   derivative
+        D2Y -   second derivative
+        Z   -   Z-value
+        DZ  -   derivative
+        D2Z -   second derivative
+
+
+      -- ALGLIB PROJECT --
+         Copyright 28.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void pspline3diff2(pspline3interpolant p, double t, out double x, out double dx, out double d2x, out double y, out double dy, out double d2y, out double z, out double dz, out double d2z)
+    {
+        x = 0;
+        dx = 0;
+        d2x = 0;
+        y = 0;
+        dy = 0;
+        d2y = 0;
+        z = 0;
+        dz = 0;
+        d2z = 0;
+        pspline.pspline3diff2(p.innerobj, t, ref x, ref dx, ref d2x, ref y, ref dy, ref d2y, ref z, ref dz, ref d2z);
+        return;
+    }
+
+    /*************************************************************************
+    This function  calculates  arc length, i.e. length of  curve  between  t=a
+    and t=b.
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        A,B -   parameter values corresponding to arc ends:
+                * B>A will result in positive length returned
+                * B<A will result in negative length returned
+
+    RESULT:
+        length of arc starting at T=A and ending at T=B.
+
+
+      -- ALGLIB PROJECT --
+         Copyright 30.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static double pspline2arclength(pspline2interpolant p, double a, double b)
+    {
+
+        double result = pspline.pspline2arclength(p.innerobj, a, b);
+        return result;
+    }
+
+    /*************************************************************************
+    This function  calculates  arc length, i.e. length of  curve  between  t=a
+    and t=b.
+
+    INPUT PARAMETERS:
+        P   -   parametric spline interpolant
+        A,B -   parameter values corresponding to arc ends:
+                * B>A will result in positive length returned
+                * B<A will result in negative length returned
+
+    RESULT:
+        length of arc starting at T=A and ending at T=B.
+
+
+      -- ALGLIB PROJECT --
+         Copyright 30.05.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static double pspline3arclength(pspline3interpolant p, double a, double b)
+    {
+
+        double result = pspline.pspline3arclength(p.innerobj, a, b);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
     2-dimensional spline inteprolant
     *************************************************************************/
@@ -3723,10945 +4571,4 @@
 public partial class alglib
 {
-
-
-    /*************************************************************************
-    IDW interpolant.
-    *************************************************************************/
-    public class idwinterpolant
-    {
-        //
-        // Public declarations
-        //
-
-        public idwinterpolant()
-        {
-            _innerobj = new idwint.idwinterpolant();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private idwint.idwinterpolant _innerobj;
-        public idwint.idwinterpolant innerobj { get { return _innerobj; } }
-        public idwinterpolant(idwint.idwinterpolant obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-    /*************************************************************************
-    IDW interpolation
-
-    INPUT PARAMETERS:
-        Z   -   IDW interpolant built with one of model building
-                subroutines.
-        X   -   array[0..NX-1], interpolation point
-
-    Result:
-        IDW interpolant Z(X)
-
-      -- ALGLIB --
-         Copyright 02.03.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static double idwcalc(idwinterpolant z, double[] x)
-    {
-
-        double result = idwint.idwcalc(z.innerobj, x);
-        return result;
-    }
-
-    /*************************************************************************
-    IDW interpolant using modified Shepard method for uniform point
-    distributions.
-
-    INPUT PARAMETERS:
-        XY  -   X and Y values, array[0..N-1,0..NX].
-                First NX columns contain X-values, last column contain
-                Y-values.
-        N   -   number of nodes, N>0.
-        NX  -   space dimension, NX>=1.
-        D   -   nodal function type, either:
-                * 0     constant  model.  Just  for  demonstration only, worst
-                        model ever.
-                * 1     linear model, least squares fitting. Simpe  model  for
-                        datasets too small for quadratic models
-                * 2     quadratic  model,  least  squares  fitting. Best model
-                        available (if your dataset is large enough).
-                * -1    "fast"  linear  model,  use  with  caution!!!   It  is
-                        significantly  faster than linear/quadratic and better
-                        than constant model. But it is less robust (especially
-                        in the presence of noise).
-        NQ  -   number of points used to calculate  nodal  functions  (ignored
-                for constant models). NQ should be LARGER than:
-                * max(1.5*(1+NX),2^NX+1) for linear model,
-                * max(3/4*(NX+2)*(NX+1),2^NX+1) for quadratic model.
-                Values less than this threshold will be silently increased.
-        NW  -   number of points used to calculate weights and to interpolate.
-                Required: >=2^NX+1, values less than this  threshold  will  be
-                silently increased.
-                Recommended value: about 2*NQ
-
-    OUTPUT PARAMETERS:
-        Z   -   IDW interpolant.
-
-    NOTES:
-      * best results are obtained with quadratic models, worst - with constant
-        models
-      * when N is large, NQ and NW must be significantly smaller than  N  both
-        to obtain optimal performance and to obtain optimal accuracy. In 2  or
-        3-dimensional tasks NQ=15 and NW=25 are good values to start with.
-      * NQ  and  NW  may  be  greater  than  N.  In  such  cases  they will be
-        automatically decreased.
-      * this subroutine is always succeeds (as long as correct parameters  are
-        passed).
-      * see  'Multivariate  Interpolation  of Large Sets of Scattered Data' by
-        Robert J. Renka for more information on this algorithm.
-      * this subroutine assumes that point distribution is uniform at the small
-        scales.  If  it  isn't  -  for  example,  points are concentrated along
-        "lines", but "lines" distribution is uniform at the larger scale - then
-        you should use IDWBuildModifiedShepardR()
-
-
-      -- ALGLIB PROJECT --
-         Copyright 02.03.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void idwbuildmodifiedshepard(double[,] xy, int n, int nx, int d, int nq, int nw, out idwinterpolant z)
-    {
-        z = new idwinterpolant();
-        idwint.idwbuildmodifiedshepard(xy, n, nx, d, nq, nw, z.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    IDW interpolant using modified Shepard method for non-uniform datasets.
-
-    This type of model uses  constant  nodal  functions and interpolates using
-    all nodes which are closer than user-specified radius R. It  may  be  used
-    when points distribution is non-uniform at the small scale, but it  is  at
-    the distances as large as R.
-
-    INPUT PARAMETERS:
-        XY  -   X and Y values, array[0..N-1,0..NX].
-                First NX columns contain X-values, last column contain
-                Y-values.
-        N   -   number of nodes, N>0.
-        NX  -   space dimension, NX>=1.
-        R   -   radius, R>0
-
-    OUTPUT PARAMETERS:
-        Z   -   IDW interpolant.
-
-    NOTES:
-    * if there is less than IDWKMin points within  R-ball,  algorithm  selects
-      IDWKMin closest ones, so that continuity properties of  interpolant  are
-      preserved even far from points.
-
-      -- ALGLIB PROJECT --
-         Copyright 11.04.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void idwbuildmodifiedshepardr(double[,] xy, int n, int nx, double r, out idwinterpolant z)
-    {
-        z = new idwinterpolant();
-        idwint.idwbuildmodifiedshepardr(xy, n, nx, r, z.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    IDW model for noisy data.
-
-    This subroutine may be used to handle noisy data, i.e. data with noise  in
-    OUTPUT values.  It differs from IDWBuildModifiedShepard() in the following
-    aspects:
-    * nodal functions are not constrained to pass through  nodes:  Qi(xi)<>yi,
-      i.e. we have fitting  instead  of  interpolation.
-    * weights which are used during least  squares fitting stage are all equal
-      to 1.0 (independently of distance)
-    * "fast"-linear or constant nodal functions are not supported (either  not
-      robust enough or too rigid)
-
-    This problem require far more complex tuning than interpolation  problems.
-    Below you can find some recommendations regarding this problem:
-    * focus on tuning NQ; it controls noise reduction. As for NW, you can just
-      make it equal to 2*NQ.
-    * you can use cross-validation to determine optimal NQ.
-    * optimal NQ is a result of complex tradeoff  between  noise  level  (more
-      noise = larger NQ required) and underlying  function  complexity  (given
-      fixed N, larger NQ means smoothing of compex features in the data).  For
-      example, NQ=N will reduce noise to the minimum level possible,  but  you
-      will end up with just constant/linear/quadratic (depending on  D)  least
-      squares model for the whole dataset.
-
-    INPUT PARAMETERS:
-        XY  -   X and Y values, array[0..N-1,0..NX].
-                First NX columns contain X-values, last column contain
-                Y-values.
-        N   -   number of nodes, N>0.
-        NX  -   space dimension, NX>=1.
-        D   -   nodal function degree, either:
-                * 1     linear model, least squares fitting. Simpe  model  for
-                        datasets too small for quadratic models (or  for  very
-                        noisy problems).
-                * 2     quadratic  model,  least  squares  fitting. Best model
-                        available (if your dataset is large enough).
-        NQ  -   number of points used to calculate nodal functions.  NQ should
-                be  significantly   larger   than  1.5  times  the  number  of
-                coefficients in a nodal function to overcome effects of noise:
-                * larger than 1.5*(1+NX) for linear model,
-                * larger than 3/4*(NX+2)*(NX+1) for quadratic model.
-                Values less than this threshold will be silently increased.
-        NW  -   number of points used to calculate weights and to interpolate.
-                Required: >=2^NX+1, values less than this  threshold  will  be
-                silently increased.
-                Recommended value: about 2*NQ or larger
-
-    OUTPUT PARAMETERS:
-        Z   -   IDW interpolant.
-
-    NOTES:
-      * best results are obtained with quadratic models, linear models are not
-        recommended to use unless you are pretty sure that it is what you want
-      * this subroutine is always succeeds (as long as correct parameters  are
-        passed).
-      * see  'Multivariate  Interpolation  of Large Sets of Scattered Data' by
-        Robert J. Renka for more information on this algorithm.
-
-
-      -- ALGLIB PROJECT --
-         Copyright 02.03.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void idwbuildnoisy(double[,] xy, int n, int nx, int d, int nq, int nw, out idwinterpolant z)
-    {
-        z = new idwinterpolant();
-        idwint.idwbuildnoisy(xy, n, nx, d, nq, nw, z.innerobj);
-        return;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Parametric spline inteprolant: 2-dimensional curve.
-
-    You should not try to access its members directly - use PSpline2XXXXXXXX()
-    functions instead.
-    *************************************************************************/
-    public class pspline2interpolant
-    {
-        //
-        // Public declarations
-        //
-
-        public pspline2interpolant()
-        {
-            _innerobj = new pspline.pspline2interpolant();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private pspline.pspline2interpolant _innerobj;
-        public pspline.pspline2interpolant innerobj { get { return _innerobj; } }
-        public pspline2interpolant(pspline.pspline2interpolant obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-
-    /*************************************************************************
-    Parametric spline inteprolant: 3-dimensional curve.
-
-    You should not try to access its members directly - use PSpline3XXXXXXXX()
-    functions instead.
-    *************************************************************************/
-    public class pspline3interpolant
-    {
-        //
-        // Public declarations
-        //
-
-        public pspline3interpolant()
-        {
-            _innerobj = new pspline.pspline3interpolant();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private pspline.pspline3interpolant _innerobj;
-        public pspline.pspline3interpolant innerobj { get { return _innerobj; } }
-        public pspline3interpolant(pspline.pspline3interpolant obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-    /*************************************************************************
-    This function  builds  non-periodic 2-dimensional parametric spline  which
-    starts at (X[0],Y[0]) and ends at (X[N-1],Y[N-1]).
-
-    INPUT PARAMETERS:
-        XY  -   points, array[0..N-1,0..1].
-                XY[I,0:1] corresponds to the Ith point.
-                Order of points is important!
-        N   -   points count, N>=5 for Akima splines, N>=2 for other types  of
-                splines.
-        ST  -   spline type:
-                * 0     Akima spline
-                * 1     parabolically terminated Catmull-Rom spline (Tension=0)
-                * 2     parabolically terminated cubic spline
-        PT  -   parameterization type:
-                * 0     uniform
-                * 1     chord length
-                * 2     centripetal
-
-    OUTPUT PARAMETERS:
-        P   -   parametric spline interpolant
-
-
-    NOTES:
-    * this function  assumes  that  there all consequent points  are distinct.
-      I.e. (x0,y0)<>(x1,y1),  (x1,y1)<>(x2,y2),  (x2,y2)<>(x3,y3)  and  so on.
-      However, non-consequent points may coincide, i.e. we can  have  (x0,y0)=
-      =(x2,y2).
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline2build(double[,] xy, int n, int st, int pt, out pspline2interpolant p)
-    {
-        p = new pspline2interpolant();
-        pspline.pspline2build(xy, n, st, pt, p.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    This function  builds  non-periodic 3-dimensional parametric spline  which
-    starts at (X[0],Y[0],Z[0]) and ends at (X[N-1],Y[N-1],Z[N-1]).
-
-    Same as PSpline2Build() function, but for 3D, so we  won't  duplicate  its
-    description here.
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline3build(double[,] xy, int n, int st, int pt, out pspline3interpolant p)
-    {
-        p = new pspline3interpolant();
-        pspline.pspline3build(xy, n, st, pt, p.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    This  function  builds  periodic  2-dimensional  parametric  spline  which
-    starts at (X[0],Y[0]), goes through all points to (X[N-1],Y[N-1]) and then
-    back to (X[0],Y[0]).
-
-    INPUT PARAMETERS:
-        XY  -   points, array[0..N-1,0..1].
-                XY[I,0:1] corresponds to the Ith point.
-                XY[N-1,0:1] must be different from XY[0,0:1].
-                Order of points is important!
-        N   -   points count, N>=3 for other types of splines.
-        ST  -   spline type:
-                * 1     Catmull-Rom spline (Tension=0) with cyclic boundary conditions
-                * 2     cubic spline with cyclic boundary conditions
-        PT  -   parameterization type:
-                * 0     uniform
-                * 1     chord length
-                * 2     centripetal
-
-    OUTPUT PARAMETERS:
-        P   -   parametric spline interpolant
-
-
-    NOTES:
-    * this function  assumes  that there all consequent points  are  distinct.
-      I.e. (x0,y0)<>(x1,y1), (x1,y1)<>(x2,y2),  (x2,y2)<>(x3,y3)  and  so  on.
-      However, non-consequent points may coincide, i.e. we can  have  (x0,y0)=
-      =(x2,y2).
-    * last point of sequence is NOT equal to the first  point.  You  shouldn't
-      make curve "explicitly periodic" by making them equal.
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline2buildperiodic(double[,] xy, int n, int st, int pt, out pspline2interpolant p)
-    {
-        p = new pspline2interpolant();
-        pspline.pspline2buildperiodic(xy, n, st, pt, p.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    This  function  builds  periodic  3-dimensional  parametric  spline  which
-    starts at (X[0],Y[0],Z[0]), goes through all points to (X[N-1],Y[N-1],Z[N-1])
-    and then back to (X[0],Y[0],Z[0]).
-
-    Same as PSpline2Build() function, but for 3D, so we  won't  duplicate  its
-    description here.
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline3buildperiodic(double[,] xy, int n, int st, int pt, out pspline3interpolant p)
-    {
-        p = new pspline3interpolant();
-        pspline.pspline3buildperiodic(xy, n, st, pt, p.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    This function returns vector of parameter values correspoding to points.
-
-    I.e. for P created from (X[0],Y[0])...(X[N-1],Y[N-1]) and U=TValues(P)  we
-    have
-        (X[0],Y[0]) = PSpline2Calc(P,U[0]),
-        (X[1],Y[1]) = PSpline2Calc(P,U[1]),
-        (X[2],Y[2]) = PSpline2Calc(P,U[2]),
-        ...
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-
-    OUTPUT PARAMETERS:
-        N   -   array size
-        T   -   array[0..N-1]
-
-
-    NOTES:
-    * for non-periodic splines U[0]=0, U[0]<U[1]<...<U[N-1], U[N-1]=1
-    * for periodic splines     U[0]=0, U[0]<U[1]<...<U[N-1], U[N-1]<1
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline2parametervalues(pspline2interpolant p, out int n, out double[] t)
-    {
-        n = 0;
-        t = new double[0];
-        pspline.pspline2parametervalues(p.innerobj, ref n, ref t);
-        return;
-    }
-
-    /*************************************************************************
-    This function returns vector of parameter values correspoding to points.
-
-    Same as PSpline2ParameterValues(), but for 3D.
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline3parametervalues(pspline3interpolant p, out int n, out double[] t)
-    {
-        n = 0;
-        t = new double[0];
-        pspline.pspline3parametervalues(p.innerobj, ref n, ref t);
-        return;
-    }
-
-    /*************************************************************************
-    This function  calculates  the value of the parametric spline for a  given
-    value of parameter T
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X   -   X-position
-        Y   -   Y-position
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline2calc(pspline2interpolant p, double t, out double x, out double y)
-    {
-        x = 0;
-        y = 0;
-        pspline.pspline2calc(p.innerobj, t, ref x, ref y);
-        return;
-    }
-
-    /*************************************************************************
-    This function  calculates  the value of the parametric spline for a  given
-    value of parameter T.
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X   -   X-position
-        Y   -   Y-position
-        Z   -   Z-position
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline3calc(pspline3interpolant p, double t, out double x, out double y, out double z)
-    {
-        x = 0;
-        y = 0;
-        z = 0;
-        pspline.pspline3calc(p.innerobj, t, ref x, ref y, ref z);
-        return;
-    }
-
-    /*************************************************************************
-    This function  calculates  tangent vector for a given value of parameter T
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X    -   X-component of tangent vector (normalized)
-        Y    -   Y-component of tangent vector (normalized)
-
-    NOTE:
-        X^2+Y^2 is either 1 (for non-zero tangent vector) or 0.
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline2tangent(pspline2interpolant p, double t, out double x, out double y)
-    {
-        x = 0;
-        y = 0;
-        pspline.pspline2tangent(p.innerobj, t, ref x, ref y);
-        return;
-    }
-
-    /*************************************************************************
-    This function  calculates  tangent vector for a given value of parameter T
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X    -   X-component of tangent vector (normalized)
-        Y    -   Y-component of tangent vector (normalized)
-        Z    -   Z-component of tangent vector (normalized)
-
-    NOTE:
-        X^2+Y^2+Z^2 is either 1 (for non-zero tangent vector) or 0.
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline3tangent(pspline3interpolant p, double t, out double x, out double y, out double z)
-    {
-        x = 0;
-        y = 0;
-        z = 0;
-        pspline.pspline3tangent(p.innerobj, t, ref x, ref y, ref z);
-        return;
-    }
-
-    /*************************************************************************
-    This function calculates derivative, i.e. it returns (dX/dT,dY/dT).
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X   -   X-value
-        DX  -   X-derivative
-        Y   -   Y-value
-        DY  -   Y-derivative
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline2diff(pspline2interpolant p, double t, out double x, out double dx, out double y, out double dy)
-    {
-        x = 0;
-        dx = 0;
-        y = 0;
-        dy = 0;
-        pspline.pspline2diff(p.innerobj, t, ref x, ref dx, ref y, ref dy);
-        return;
-    }
-
-    /*************************************************************************
-    This function calculates derivative, i.e. it returns (dX/dT,dY/dT,dZ/dT).
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X   -   X-value
-        DX  -   X-derivative
-        Y   -   Y-value
-        DY  -   Y-derivative
-        Z   -   Z-value
-        DZ  -   Z-derivative
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline3diff(pspline3interpolant p, double t, out double x, out double dx, out double y, out double dy, out double z, out double dz)
-    {
-        x = 0;
-        dx = 0;
-        y = 0;
-        dy = 0;
-        z = 0;
-        dz = 0;
-        pspline.pspline3diff(p.innerobj, t, ref x, ref dx, ref y, ref dy, ref z, ref dz);
-        return;
-    }
-
-    /*************************************************************************
-    This function calculates first and second derivative with respect to T.
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X   -   X-value
-        DX  -   derivative
-        D2X -   second derivative
-        Y   -   Y-value
-        DY  -   derivative
-        D2Y -   second derivative
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline2diff2(pspline2interpolant p, double t, out double x, out double dx, out double d2x, out double y, out double dy, out double d2y)
-    {
-        x = 0;
-        dx = 0;
-        d2x = 0;
-        y = 0;
-        dy = 0;
-        d2y = 0;
-        pspline.pspline2diff2(p.innerobj, t, ref x, ref dx, ref d2x, ref y, ref dy, ref d2y);
-        return;
-    }
-
-    /*************************************************************************
-    This function calculates first and second derivative with respect to T.
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        T   -   point:
-                * T in [0,1] corresponds to interval spanned by points
-                * for non-periodic splines T<0 (or T>1) correspond to parts of
-                  the curve before the first (after the last) point
-                * for periodic splines T<0 (or T>1) are projected  into  [0,1]
-                  by making T=T-floor(T).
-
-    OUTPUT PARAMETERS:
-        X   -   X-value
-        DX  -   derivative
-        D2X -   second derivative
-        Y   -   Y-value
-        DY  -   derivative
-        D2Y -   second derivative
-        Z   -   Z-value
-        DZ  -   derivative
-        D2Z -   second derivative
-
-
-      -- ALGLIB PROJECT --
-         Copyright 28.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void pspline3diff2(pspline3interpolant p, double t, out double x, out double dx, out double d2x, out double y, out double dy, out double d2y, out double z, out double dz, out double d2z)
-    {
-        x = 0;
-        dx = 0;
-        d2x = 0;
-        y = 0;
-        dy = 0;
-        d2y = 0;
-        z = 0;
-        dz = 0;
-        d2z = 0;
-        pspline.pspline3diff2(p.innerobj, t, ref x, ref dx, ref d2x, ref y, ref dy, ref d2y, ref z, ref dz, ref d2z);
-        return;
-    }
-
-    /*************************************************************************
-    This function  calculates  arc length, i.e. length of  curve  between  t=a
-    and t=b.
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        A,B -   parameter values corresponding to arc ends:
-                * B>A will result in positive length returned
-                * B<A will result in negative length returned
-
-    RESULT:
-        length of arc starting at T=A and ending at T=B.
-
-
-      -- ALGLIB PROJECT --
-         Copyright 30.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static double pspline2arclength(pspline2interpolant p, double a, double b)
-    {
-
-        double result = pspline.pspline2arclength(p.innerobj, a, b);
-        return result;
-    }
-
-    /*************************************************************************
-    This function  calculates  arc length, i.e. length of  curve  between  t=a
-    and t=b.
-
-    INPUT PARAMETERS:
-        P   -   parametric spline interpolant
-        A,B -   parameter values corresponding to arc ends:
-                * B>A will result in positive length returned
-                * B<A will result in negative length returned
-
-    RESULT:
-        length of arc starting at T=A and ending at T=B.
-
-
-      -- ALGLIB PROJECT --
-         Copyright 30.05.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static double pspline3arclength(pspline3interpolant p, double a, double b)
-    {
-
-        double result = pspline.pspline3arclength(p.innerobj, a, b);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-    public class lsfit
-    {
-        /*************************************************************************
-        Polynomial fitting report:
-            TaskRCond       reciprocal of task's condition number
-            RMSError        RMS error
-            AvgError        average error
-            AvgRelError     average relative error (for non-zero Y[I])
-            MaxError        maximum error
-        *************************************************************************/
-        public class polynomialfitreport
-        {
-            public double taskrcond;
-            public double rmserror;
-            public double avgerror;
-            public double avgrelerror;
-            public double maxerror;
-        };
-
-
-        /*************************************************************************
-        Barycentric fitting report:
-            RMSError        RMS error
-            AvgError        average error
-            AvgRelError     average relative error (for non-zero Y[I])
-            MaxError        maximum error
-            TaskRCond       reciprocal of task's condition number
-        *************************************************************************/
-        public class barycentricfitreport
-        {
-            public double taskrcond;
-            public int dbest;
-            public double rmserror;
-            public double avgerror;
-            public double avgrelerror;
-            public double maxerror;
-        };
-
-
-        /*************************************************************************
-        Spline fitting report:
-            RMSError        RMS error
-            AvgError        average error
-            AvgRelError     average relative error (for non-zero Y[I])
-            MaxError        maximum error
-            
-        Fields  below are  filled  by   obsolete    functions   (Spline1DFitCubic,
-        Spline1DFitHermite). Modern fitting functions do NOT fill these fields:
-            TaskRCond       reciprocal of task's condition number
-        *************************************************************************/
-        public class spline1dfitreport
-        {
-            public double taskrcond;
-            public double rmserror;
-            public double avgerror;
-            public double avgrelerror;
-            public double maxerror;
-        };
-
-
-        /*************************************************************************
-        Least squares fitting report:
-            TaskRCond       reciprocal of task's condition number
-            RMSError        RMS error
-            AvgError        average error
-            AvgRelError     average relative error (for non-zero Y[I])
-            MaxError        maximum error
-        *************************************************************************/
-        public class lsfitreport
-        {
-            public double taskrcond;
-            public double rmserror;
-            public double avgerror;
-            public double avgrelerror;
-            public double maxerror;
-        };
-
-
-        /*************************************************************************
-        Nonlinear fitter.
-
-        You should use ALGLIB functions to work with fitter.
-        Never try to access its fields directly!
-        *************************************************************************/
-        public class lsfitstate
-        {
-            public int n;
-            public int m;
-            public int k;
-            public double epsf;
-            public double epsx;
-            public int maxits;
-            public double stpmax;
-            public bool xrep;
-            public double[,] taskx;
-            public double[] tasky;
-            public double[] w;
-            public bool xupdated;
-            public bool needf;
-            public bool needfg;
-            public bool needfgh;
-            public int pointindex;
-            public double[] x;
-            public double[] c;
-            public double f;
-            public double[] g;
-            public double[,] h;
-            public int repterminationtype;
-            public double reprmserror;
-            public double repavgerror;
-            public double repavgrelerror;
-            public double repmaxerror;
-            public minlm.minlmstate optstate;
-            public minlm.minlmreport optrep;
-            public rcommstate rstate;
-            public lsfitstate()
-            {
-                taskx = new double[0,0];
-                tasky = new double[0];
-                w = new double[0];
-                x = new double[0];
-                c = new double[0];
-                g = new double[0];
-                h = new double[0,0];
-                optstate = new minlm.minlmstate();
-                optrep = new minlm.minlmreport();
-                rstate = new rcommstate();
-            }
-        };
-
-
-
-
-        public const int rfsmax = 10;
-
-
-        /*************************************************************************
-        Fitting by polynomials in barycentric form. This function provides  simple
-        unterface for unconstrained unweighted fitting. See  PolynomialFitWC()  if
-        you need constrained fitting.
-
-        Task is linear, so linear least squares solver is used. Complexity of this
-        computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-        SEE ALSO:
-            PolynomialFitWC()
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            N   -   number of points, N>0
-                    * if given, only leading N elements of X/Y are used
-                    * if not given, automatically determined from sizes of X/Y
-            M   -   number of basis functions (= polynomial_degree + 1), M>=1
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearW() subroutine:
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-            P   -   interpolant in barycentric form.
-            Rep -   report, same format as in LSFitLinearW() subroutine.
-                    Following fields are set:
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-        NOTES:
-            you can convert P from barycentric form  to  the  power  or  Chebyshev
-            basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
-            POLINT subpackage.
-
-          -- ALGLIB PROJECT --
-             Copyright 10.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialfit(double[] x,
-            double[] y,
-            int n,
-            int m,
-            ref int info,
-            ratint.barycentricinterpolant p,
-            polynomialfitreport rep)
-        {
-            int i = 0;
-            double[] w = new double[0];
-            double[] xc = new double[0];
-            double[] yc = new double[0];
-            int[] dc = new int[0];
-
-            info = 0;
-
-            ap.assert(n>0, "PolynomialFit: N<=0!");
-            ap.assert(m>0, "PolynomialFit: M<=0!");
-            ap.assert(ap.len(x)>=n, "PolynomialFit: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "PolynomialFit: Length(Y)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "PolynomialFit: X contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(y, n), "PolynomialFit: Y contains infinite or NaN values!");
-            w = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = 1;
-            }
-            polynomialfitwc(x, y, w, n, xc, yc, dc, 0, m, ref info, p, rep);
-        }
-
-
-        /*************************************************************************
-        Weighted  fitting by polynomials in barycentric form, with constraints  on
-        function values or first derivatives.
-
-        Small regularizing term is used when solving constrained tasks (to improve
-        stability).
-
-        Task is linear, so linear least squares solver is used. Complexity of this
-        computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-        SEE ALSO:
-            PolynomialFit()
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            W   -   weights, array[0..N-1]
-                    Each summand in square  sum  of  approximation deviations from
-                    given  values  is  multiplied  by  the square of corresponding
-                    weight. Fill it by 1's if you don't  want  to  solve  weighted
-                    task.
-            N   -   number of points, N>0.
-                    * if given, only leading N elements of X/Y/W are used
-                    * if not given, automatically determined from sizes of X/Y/W
-            XC  -   points where polynomial values/derivatives are constrained,
-                    array[0..K-1].
-            YC  -   values of constraints, array[0..K-1]
-            DC  -   array[0..K-1], types of constraints:
-                    * DC[i]=0   means that P(XC[i])=YC[i]
-                    * DC[i]=1   means that P'(XC[i])=YC[i]
-                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-            K   -   number of constraints, 0<=K<M.
-                    K=0 means no constraints (XC/YC/DC are not used in such cases)
-            M   -   number of basis functions (= polynomial_degree + 1), M>=1
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearW() subroutine:
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-                                -3 means inconsistent constraints
-            P   -   interpolant in barycentric form.
-            Rep -   report, same format as in LSFitLinearW() subroutine.
-                    Following fields are set:
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroitine doesn't calculate task's condition number for K<>0.
-
-        NOTES:
-            you can convert P from barycentric form  to  the  power  or  Chebyshev
-            basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
-            POLINT subpackage.
-
-        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-        Setting constraints can lead  to undesired  results,  like ill-conditioned
-        behavior, or inconsistency being detected. From the other side,  it allows
-        us to improve quality of the fit. Here we summarize  our  experience  with
-        constrained regression splines:
-        * even simple constraints can be inconsistent, see  Wikipedia  article  on
-          this subject: http://en.wikipedia.org/wiki/Birkhoff_interpolation
-        * the  greater  is  M (given  fixed  constraints),  the  more chances that
-          constraints will be consistent
-        * in the general case, consistency of constraints is NOT GUARANTEED.
-        * in the one special cases, however, we can  guarantee  consistency.  This
-          case  is:  M>1  and constraints on the function values (NOT DERIVATIVES)
-
-        Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
-        can't solve your task without them. Anything beyond  special  cases  given
-        above is not guaranteed and may result in inconsistency.
-
-          -- ALGLIB PROJECT --
-             Copyright 10.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialfitwc(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            double[] xc,
-            double[] yc,
-            int[] dc,
-            int k,
-            int m,
-            ref int info,
-            ratint.barycentricinterpolant p,
-            polynomialfitreport rep)
-        {
-            double xa = 0;
-            double xb = 0;
-            double sa = 0;
-            double sb = 0;
-            double[] xoriginal = new double[0];
-            double[] yoriginal = new double[0];
-            double[] y2 = new double[0];
-            double[] w2 = new double[0];
-            double[] tmp = new double[0];
-            double[] tmp2 = new double[0];
-            double[] bx = new double[0];
-            double[] by = new double[0];
-            double[] bw = new double[0];
-            int i = 0;
-            int j = 0;
-            double u = 0;
-            double v = 0;
-            double s = 0;
-            int relcnt = 0;
-            lsfitreport lrep = new lsfitreport();
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            w = (double[])w.Clone();
-            xc = (double[])xc.Clone();
-            yc = (double[])yc.Clone();
-            info = 0;
-
-            ap.assert(n>0, "PolynomialFitWC: N<=0!");
-            ap.assert(m>0, "PolynomialFitWC: M<=0!");
-            ap.assert(k>=0, "PolynomialFitWC: K<0!");
-            ap.assert(k<m, "PolynomialFitWC: K>=M!");
-            ap.assert(ap.len(x)>=n, "PolynomialFitWC: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "PolynomialFitWC: Length(Y)<N!");
-            ap.assert(ap.len(w)>=n, "PolynomialFitWC: Length(W)<N!");
-            ap.assert(ap.len(xc)>=k, "PolynomialFitWC: Length(XC)<K!");
-            ap.assert(ap.len(yc)>=k, "PolynomialFitWC: Length(YC)<K!");
-            ap.assert(ap.len(dc)>=k, "PolynomialFitWC: Length(DC)<K!");
-            ap.assert(apserv.isfinitevector(x, n), "PolynomialFitWC: X contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(y, n), "PolynomialFitWC: Y contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(w, n), "PolynomialFitWC: X contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(xc, k), "PolynomialFitWC: XC contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(yc, k), "PolynomialFitWC: YC contains infinite or NaN values!");
-            for(i=0; i<=k-1; i++)
-            {
-                ap.assert(dc[i]==0 | dc[i]==1, "PolynomialFitWC: one of DC[] is not 0 or 1!");
-            }
-            
-            //
-            // Scale X, Y, XC, YC.
-            // Solve scaled problem using internal Chebyshev fitting function.
-            //
-            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
-            internalchebyshevfit(x, y, w, n, xc, yc, dc, k, m, ref info, ref tmp, lrep);
-            if( info<0 )
-            {
-                return;
-            }
-            
-            //
-            // Generate barycentric model and scale it
-            // * BX, BY store barycentric model nodes
-            // * FMatrix is reused (remember - it is at least MxM, what we need)
-            //
-            // Model intialization is done in O(M^2). In principle, it can be
-            // done in O(M*log(M)), but before it we solved task with O(N*M^2)
-            // complexity, so it is only a small amount of total time spent.
-            //
-            bx = new double[m];
-            by = new double[m];
-            bw = new double[m];
-            tmp2 = new double[m];
-            s = 1;
-            for(i=0; i<=m-1; i++)
-            {
-                if( m!=1 )
-                {
-                    u = Math.Cos(Math.PI*i/(m-1));
-                }
-                else
-                {
-                    u = 0;
-                }
-                v = 0;
-                for(j=0; j<=m-1; j++)
-                {
-                    if( j==0 )
-                    {
-                        tmp2[j] = 1;
-                    }
-                    else
-                    {
-                        if( j==1 )
-                        {
-                            tmp2[j] = u;
-                        }
-                        else
-                        {
-                            tmp2[j] = 2*u*tmp2[j-1]-tmp2[j-2];
-                        }
-                    }
-                    v = v+tmp[j]*tmp2[j];
-                }
-                bx[i] = u;
-                by[i] = v;
-                bw[i] = s;
-                if( i==0 | i==m-1 )
-                {
-                    bw[i] = 0.5*bw[i];
-                }
-                s = -s;
-            }
-            ratint.barycentricbuildxyw(bx, by, bw, m, p);
-            ratint.barycentriclintransx(p, 2/(xb-xa), -((xa+xb)/(xb-xa)));
-            ratint.barycentriclintransy(p, sb-sa, sa);
-            
-            //
-            // Scale absolute errors obtained from LSFitLinearW.
-            // Relative error should be calculated separately
-            // (because of shifting/scaling of the task)
-            //
-            rep.taskrcond = lrep.taskrcond;
-            rep.rmserror = lrep.rmserror*(sb-sa);
-            rep.avgerror = lrep.avgerror*(sb-sa);
-            rep.maxerror = lrep.maxerror*(sb-sa);
-            rep.avgrelerror = 0;
-            relcnt = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                if( (double)(yoriginal[i])!=(double)(0) )
-                {
-                    rep.avgrelerror = rep.avgrelerror+Math.Abs(ratint.barycentriccalc(p, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
-                    relcnt = relcnt+1;
-                }
-            }
-            if( relcnt!=0 )
-            {
-                rep.avgrelerror = rep.avgrelerror/relcnt;
-            }
-        }
-
-
-        /*************************************************************************
-        Weghted rational least  squares  fitting  using  Floater-Hormann  rational
-        functions  with  optimal  D  chosen  from  [0,9],  with  constraints   and
-        individual weights.
-
-        Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
-        functions. Different values of D are tried, optimal D (least WEIGHTED root
-        mean square error) is chosen.  Task  is  linear,  so  linear least squares
-        solver  is  used.  Complexity  of  this  computational  scheme is O(N*M^2)
-        (mostly dominated by the least squares solver).
-
-        SEE ALSO
-        * BarycentricFitFloaterHormann(), "lightweight" fitting without invididual
-          weights and constraints.
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            W   -   weights, array[0..N-1]
-                    Each summand in square  sum  of  approximation deviations from
-                    given  values  is  multiplied  by  the square of corresponding
-                    weight. Fill it by 1's if you don't  want  to  solve  weighted
-                    task.
-            N   -   number of points, N>0.
-            XC  -   points where function values/derivatives are constrained,
-                    array[0..K-1].
-            YC  -   values of constraints, array[0..K-1]
-            DC  -   array[0..K-1], types of constraints:
-                    * DC[i]=0   means that S(XC[i])=YC[i]
-                    * DC[i]=1   means that S'(XC[i])=YC[i]
-                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-            K   -   number of constraints, 0<=K<M.
-                    K=0 means no constraints (XC/YC/DC are not used in such cases)
-            M   -   number of basis functions ( = number_of_nodes), M>=2.
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearWC() subroutine.
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-                                -3 means inconsistent constraints
-                                -1 means another errors in parameters passed
-                                   (N<=0, for example)
-            B   -   barycentric interpolant.
-            Rep -   report, same format as in LSFitLinearWC() subroutine.
-                    Following fields are set:
-                    * DBest         best value of the D parameter
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroutine doesn't calculate task's condition number for K<>0.
-
-        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-        Setting constraints can lead  to undesired  results,  like ill-conditioned
-        behavior, or inconsistency being detected. From the other side,  it allows
-        us to improve quality of the fit. Here we summarize  our  experience  with
-        constrained barycentric interpolants:
-        * excessive  constraints  can  be  inconsistent.   Floater-Hormann   basis
-          functions aren't as flexible as splines (although they are very smooth).
-        * the more evenly constraints are spread across [min(x),max(x)],  the more
-          chances that they will be consistent
-        * the  greater  is  M (given  fixed  constraints),  the  more chances that
-          constraints will be consistent
-        * in the general case, consistency of constraints IS NOT GUARANTEED.
-        * in the several special cases, however, we CAN guarantee consistency.
-        * one of this cases is constraints on the function  VALUES at the interval
-          boundaries. Note that consustency of the  constraints  on  the  function
-          DERIVATIVES is NOT guaranteed (you can use in such cases  cubic  splines
-          which are more flexible).
-        * another  special  case  is ONE constraint on the function value (OR, but
-          not AND, derivative) anywhere in the interval
-
-        Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
-        can't solve your task without them. Anything beyond  special  cases  given
-        above is not guaranteed and may result in inconsistency.
-
-          -- ALGLIB PROJECT --
-             Copyright 18.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentricfitfloaterhormannwc(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            double[] xc,
-            double[] yc,
-            int[] dc,
-            int k,
-            int m,
-            ref int info,
-            ratint.barycentricinterpolant b,
-            barycentricfitreport rep)
-        {
-            int d = 0;
-            int i = 0;
-            double wrmscur = 0;
-            double wrmsbest = 0;
-            ratint.barycentricinterpolant locb = new ratint.barycentricinterpolant();
-            barycentricfitreport locrep = new barycentricfitreport();
-            int locinfo = 0;
-
-            info = 0;
-
-            ap.assert(n>0, "BarycentricFitFloaterHormannWC: N<=0!");
-            ap.assert(m>0, "BarycentricFitFloaterHormannWC: M<=0!");
-            ap.assert(k>=0, "BarycentricFitFloaterHormannWC: K<0!");
-            ap.assert(k<m, "BarycentricFitFloaterHormannWC: K>=M!");
-            ap.assert(ap.len(x)>=n, "BarycentricFitFloaterHormannWC: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "BarycentricFitFloaterHormannWC: Length(Y)<N!");
-            ap.assert(ap.len(w)>=n, "BarycentricFitFloaterHormannWC: Length(W)<N!");
-            ap.assert(ap.len(xc)>=k, "BarycentricFitFloaterHormannWC: Length(XC)<K!");
-            ap.assert(ap.len(yc)>=k, "BarycentricFitFloaterHormannWC: Length(YC)<K!");
-            ap.assert(ap.len(dc)>=k, "BarycentricFitFloaterHormannWC: Length(DC)<K!");
-            ap.assert(apserv.isfinitevector(x, n), "BarycentricFitFloaterHormannWC: X contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(y, n), "BarycentricFitFloaterHormannWC: Y contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(w, n), "BarycentricFitFloaterHormannWC: X contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(xc, k), "BarycentricFitFloaterHormannWC: XC contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(yc, k), "BarycentricFitFloaterHormannWC: YC contains infinite or NaN values!");
-            for(i=0; i<=k-1; i++)
-            {
-                ap.assert(dc[i]==0 | dc[i]==1, "BarycentricFitFloaterHormannWC: one of DC[] is not 0 or 1!");
-            }
-            
-            //
-            // Find optimal D
-            //
-            // Info is -3 by default (degenerate constraints).
-            // If LocInfo will always be equal to -3, Info will remain equal to -3.
-            // If at least once LocInfo will be -4, Info will be -4.
-            //
-            wrmsbest = math.maxrealnumber;
-            rep.dbest = -1;
-            info = -3;
-            for(d=0; d<=Math.Min(9, n-1); d++)
-            {
-                barycentricfitwcfixedd(x, y, w, n, xc, yc, dc, k, m, d, ref locinfo, locb, locrep);
-                ap.assert((locinfo==-4 | locinfo==-3) | locinfo>0, "BarycentricFitFloaterHormannWC: unexpected result from BarycentricFitWCFixedD!");
-                if( locinfo>0 )
-                {
-                    
-                    //
-                    // Calculate weghted RMS
-                    //
-                    wrmscur = 0;
-                    for(i=0; i<=n-1; i++)
-                    {
-                        wrmscur = wrmscur+math.sqr(w[i]*(y[i]-ratint.barycentriccalc(locb, x[i])));
-                    }
-                    wrmscur = Math.Sqrt(wrmscur/n);
-                    if( (double)(wrmscur)<(double)(wrmsbest) | rep.dbest<0 )
-                    {
-                        ratint.barycentriccopy(locb, b);
-                        rep.dbest = d;
-                        info = 1;
-                        rep.rmserror = locrep.rmserror;
-                        rep.avgerror = locrep.avgerror;
-                        rep.avgrelerror = locrep.avgrelerror;
-                        rep.maxerror = locrep.maxerror;
-                        rep.taskrcond = locrep.taskrcond;
-                        wrmsbest = wrmscur;
-                    }
-                }
-                else
-                {
-                    if( locinfo!=-3 & info<0 )
-                    {
-                        info = locinfo;
-                    }
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Rational least squares fitting using  Floater-Hormann  rational  functions
-        with optimal D chosen from [0,9].
-
-        Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
-        functions. Different values of D are tried, optimal  D  (least  root  mean
-        square error) is chosen.  Task  is  linear, so linear least squares solver
-        is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
-        dominated by the least squares solver).
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            N   -   number of points, N>0.
-            M   -   number of basis functions ( = number_of_nodes), M>=2.
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearWC() subroutine.
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-                                -3 means inconsistent constraints
-            B   -   barycentric interpolant.
-            Rep -   report, same format as in LSFitLinearWC() subroutine.
-                    Following fields are set:
-                    * DBest         best value of the D parameter
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-          -- ALGLIB PROJECT --
-             Copyright 18.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentricfitfloaterhormann(double[] x,
-            double[] y,
-            int n,
-            int m,
-            ref int info,
-            ratint.barycentricinterpolant b,
-            barycentricfitreport rep)
-        {
-            double[] w = new double[0];
-            double[] xc = new double[0];
-            double[] yc = new double[0];
-            int[] dc = new int[0];
-            int i = 0;
-
-            info = 0;
-
-            ap.assert(n>0, "BarycentricFitFloaterHormann: N<=0!");
-            ap.assert(m>0, "BarycentricFitFloaterHormann: M<=0!");
-            ap.assert(ap.len(x)>=n, "BarycentricFitFloaterHormann: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "BarycentricFitFloaterHormann: Length(Y)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "BarycentricFitFloaterHormann: X contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(y, n), "BarycentricFitFloaterHormann: Y contains infinite or NaN values!");
-            w = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = 1;
-            }
-            barycentricfitfloaterhormannwc(x, y, w, n, xc, yc, dc, 0, m, ref info, b, rep);
-        }
-
-
-        /*************************************************************************
-        Rational least squares fitting using  Floater-Hormann  rational  functions
-        with optimal D chosen from [0,9].
-
-        Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
-        functions. Different values of D are tried, optimal  D  (least  root  mean
-        square error) is chosen.  Task  is  linear, so linear least squares solver
-        is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
-        dominated by the least squares solver).
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            N   -   number of points, N>0.
-            M   -   number of basis functions ( = number_of_nodes), M>=2.
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearWC() subroutine.
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-                                -3 means inconsistent constraints
-            B   -   barycentric interpolant.
-            Rep -   report, same format as in LSFitLinearWC() subroutine.
-                    Following fields are set:
-                    * DBest         best value of the D parameter
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-          -- ALGLIB PROJECT --
-             Copyright 18.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dfitpenalized(double[] x,
-            double[] y,
-            int n,
-            int m,
-            double rho,
-            ref int info,
-            spline1d.spline1dinterpolant s,
-            spline1dfitreport rep)
-        {
-            double[] w = new double[0];
-            int i = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            info = 0;
-
-            ap.assert(n>=1, "Spline1DFitPenalized: N<1!");
-            ap.assert(m>=4, "Spline1DFitPenalized: M<4!");
-            ap.assert(ap.len(x)>=n, "Spline1DFitPenalized: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DFitPenalized: Length(Y)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitPenalized: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitPenalized: Y contains infinite or NAN values!");
-            ap.assert(math.isfinite(rho), "Spline1DFitPenalized: Rho is infinite!");
-            w = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = 1;
-            }
-            spline1dfitpenalizedw(x, y, w, n, m, rho, ref info, s, rep);
-        }
-
-
-        /*************************************************************************
-        Weighted fitting by penalized cubic spline.
-
-        Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
-        basis functions. Basis functions are cubic splines with  natural  boundary
-        conditions. Problem is regularized by  adding non-linearity penalty to the
-        usual least squares penalty function:
-
-            S(x) = arg min { LS + P }, where
-            LS   = SUM { w[i]^2*(y[i] - S(x[i]))^2 } - least squares penalty
-            P    = C*10^rho*integral{ S''(x)^2*dx } - non-linearity penalty
-            rho  - tunable constant given by user
-            C    - automatically determined scale parameter,
-                   makes penalty invariant with respect to scaling of X, Y, W.
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            W   -   weights, array[0..N-1]
-                    Each summand in square  sum  of  approximation deviations from
-                    given  values  is  multiplied  by  the square of corresponding
-                    weight. Fill it by 1's if you don't  want  to  solve  weighted
-                    problem.
-            N   -   number of points (optional):
-                    * N>0
-                    * if given, only first N elements of X/Y/W are processed
-                    * if not given, automatically determined from X/Y/W sizes
-            M   -   number of basis functions ( = number_of_nodes), M>=4.
-            Rho -   regularization  constant  passed   by   user.   It   penalizes
-                    nonlinearity in the regression spline. It  is  logarithmically
-                    scaled,  i.e.  actual  value  of  regularization  constant  is
-                    calculated as 10^Rho. It is automatically scaled so that:
-                    * Rho=2.0 corresponds to moderate amount of nonlinearity
-                    * generally, it should be somewhere in the [-8.0,+8.0]
-                    If you do not want to penalize nonlineary,
-                    pass small Rho. Values as low as -15 should work.
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearWC() subroutine.
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD or
-                                   Cholesky decomposition; problem may be
-                                   too ill-conditioned (very rare)
-            S   -   spline interpolant.
-            Rep -   Following fields are set:
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroitine doesn't calculate task's condition number for K<>0.
-
-        NOTE 1: additional nodes are added to the spline outside  of  the  fitting
-        interval to force linearity when x<min(x,xc) or x>max(x,xc).  It  is  done
-        for consistency - we penalize non-linearity  at [min(x,xc),max(x,xc)],  so
-        it is natural to force linearity outside of this interval.
-
-        NOTE 2: function automatically sorts points,  so  caller may pass unsorted
-        array.
-
-          -- ALGLIB PROJECT --
-             Copyright 19.10.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dfitpenalizedw(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            int m,
-            double rho,
-            ref int info,
-            spline1d.spline1dinterpolant s,
-            spline1dfitreport rep)
-        {
-            int i = 0;
-            int j = 0;
-            int b = 0;
-            double v = 0;
-            double relcnt = 0;
-            double xa = 0;
-            double xb = 0;
-            double sa = 0;
-            double sb = 0;
-            double[] xoriginal = new double[0];
-            double[] yoriginal = new double[0];
-            double pdecay = 0;
-            double tdecay = 0;
-            double[,] fmatrix = new double[0,0];
-            double[] fcolumn = new double[0];
-            double[] y2 = new double[0];
-            double[] w2 = new double[0];
-            double[] xc = new double[0];
-            double[] yc = new double[0];
-            int[] dc = new int[0];
-            double fdmax = 0;
-            double admax = 0;
-            double[,] amatrix = new double[0,0];
-            double[,] d2matrix = new double[0,0];
-            double fa = 0;
-            double ga = 0;
-            double fb = 0;
-            double gb = 0;
-            double lambdav = 0;
-            double[] bx = new double[0];
-            double[] by = new double[0];
-            double[] bd1 = new double[0];
-            double[] bd2 = new double[0];
-            double[] tx = new double[0];
-            double[] ty = new double[0];
-            double[] td = new double[0];
-            spline1d.spline1dinterpolant bs = new spline1d.spline1dinterpolant();
-            double[,] nmatrix = new double[0,0];
-            double[] rightpart = new double[0];
-            fbls.fblslincgstate cgstate = new fbls.fblslincgstate();
-            double[] c = new double[0];
-            double[] tmp0 = new double[0];
-            int i_ = 0;
-            int i1_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            w = (double[])w.Clone();
-            info = 0;
-
-            ap.assert(n>=1, "Spline1DFitPenalizedW: N<1!");
-            ap.assert(m>=4, "Spline1DFitPenalizedW: M<4!");
-            ap.assert(ap.len(x)>=n, "Spline1DFitPenalizedW: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DFitPenalizedW: Length(Y)<N!");
-            ap.assert(ap.len(w)>=n, "Spline1DFitPenalizedW: Length(W)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitPenalizedW: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitPenalizedW: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(w, n), "Spline1DFitPenalizedW: Y contains infinite or NAN values!");
-            ap.assert(math.isfinite(rho), "Spline1DFitPenalizedW: Rho is infinite!");
-            
-            //
-            // Prepare LambdaV
-            //
-            v = -(Math.Log(math.machineepsilon)/Math.Log(10));
-            if( (double)(rho)<(double)(-v) )
-            {
-                rho = -v;
-            }
-            if( (double)(rho)>(double)(v) )
-            {
-                rho = v;
-            }
-            lambdav = Math.Pow(10, rho);
-            
-            //
-            // Sort X, Y, W
-            //
-            spline1d.heapsortdpoints(ref x, ref y, ref w, n);
-            
-            //
-            // Scale X, Y, XC, YC
-            //
-            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, 0, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
-            
-            //
-            // Allocate space
-            //
-            fmatrix = new double[n, m];
-            amatrix = new double[m, m];
-            d2matrix = new double[m, m];
-            bx = new double[m];
-            by = new double[m];
-            fcolumn = new double[n];
-            nmatrix = new double[m, m];
-            rightpart = new double[m];
-            tmp0 = new double[Math.Max(m, n)];
-            c = new double[m];
-            
-            //
-            // Fill:
-            // * FMatrix by values of basis functions
-            // * TmpAMatrix by second derivatives of I-th function at J-th point
-            // * CMatrix by constraints
-            //
-            fdmax = 0;
-            for(b=0; b<=m-1; b++)
-            {
-                
-                //
-                // Prepare I-th basis function
-                //
-                for(j=0; j<=m-1; j++)
-                {
-                    bx[j] = (double)(2*j)/(double)(m-1)-1;
-                    by[j] = 0;
-                }
-                by[b] = 1;
-                spline1d.spline1dgriddiff2cubic(bx, by, m, 2, 0.0, 2, 0.0, ref bd1, ref bd2);
-                spline1d.spline1dbuildcubic(bx, by, m, 2, 0.0, 2, 0.0, bs);
-                
-                //
-                // Calculate B-th column of FMatrix
-                // Update FDMax (maximum column norm)
-                //
-                spline1d.spline1dconvcubic(bx, by, m, 2, 0.0, 2, 0.0, x, n, ref fcolumn);
-                for(i_=0; i_<=n-1;i_++)
-                {
-                    fmatrix[i_,b] = fcolumn[i_];
-                }
-                v = 0;
-                for(i=0; i<=n-1; i++)
-                {
-                    v = v+math.sqr(w[i]*fcolumn[i]);
-                }
-                fdmax = Math.Max(fdmax, v);
-                
-                //
-                // Fill temporary with second derivatives of basis function
-                //
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    d2matrix[b,i_] = bd2[i_];
-                }
-            }
-            
-            //
-            // * calculate penalty matrix A
-            // * calculate max of diagonal elements of A
-            // * calculate PDecay - coefficient before penalty matrix
-            //
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=i; j<=m-1; j++)
-                {
-                    
-                    //
-                    // calculate integral(B_i''*B_j'') where B_i and B_j are
-                    // i-th and j-th basis splines.
-                    // B_i and B_j are piecewise linear functions.
-                    //
-                    v = 0;
-                    for(b=0; b<=m-2; b++)
-                    {
-                        fa = d2matrix[i,b];
-                        fb = d2matrix[i,b+1];
-                        ga = d2matrix[j,b];
-                        gb = d2matrix[j,b+1];
-                        v = v+(bx[b+1]-bx[b])*(fa*ga+(fa*(gb-ga)+ga*(fb-fa))/2+(fb-fa)*(gb-ga)/3);
-                    }
-                    amatrix[i,j] = v;
-                    amatrix[j,i] = v;
-                }
-            }
-            admax = 0;
-            for(i=0; i<=m-1; i++)
-            {
-                admax = Math.Max(admax, Math.Abs(amatrix[i,i]));
-            }
-            pdecay = lambdav*fdmax/admax;
-            
-            //
-            // Calculate TDecay for Tikhonov regularization
-            //
-            tdecay = fdmax*(1+pdecay)*10*math.machineepsilon;
-            
-            //
-            // Prepare system
-            //
-            // NOTE: FMatrix is spoiled during this process
-            //
-            for(i=0; i<=n-1; i++)
-            {
-                v = w[i];
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    fmatrix[i,i_] = v*fmatrix[i,i_];
-                }
-            }
-            ablas.rmatrixgemm(m, m, n, 1.0, fmatrix, 0, 0, 1, fmatrix, 0, 0, 0, 0.0, ref nmatrix, 0, 0);
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=m-1; j++)
-                {
-                    nmatrix[i,j] = nmatrix[i,j]+pdecay*amatrix[i,j];
-                }
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                nmatrix[i,i] = nmatrix[i,i]+tdecay;
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                rightpart[i] = 0;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                v = y[i]*w[i];
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    rightpart[i_] = rightpart[i_] + v*fmatrix[i,i_];
-                }
-            }
-            
-            //
-            // Solve system
-            //
-            if( !trfac.spdmatrixcholesky(ref nmatrix, m, true) )
-            {
-                info = -4;
-                return;
-            }
-            fbls.fblscholeskysolve(nmatrix, 1.0, m, true, ref rightpart, ref tmp0);
-            for(i_=0; i_<=m-1;i_++)
-            {
-                c[i_] = rightpart[i_];
-            }
-            
-            //
-            // add nodes to force linearity outside of the fitting interval
-            //
-            spline1d.spline1dgriddiffcubic(bx, c, m, 2, 0.0, 2, 0.0, ref bd1);
-            tx = new double[m+2];
-            ty = new double[m+2];
-            td = new double[m+2];
-            i1_ = (0) - (1);
-            for(i_=1; i_<=m;i_++)
-            {
-                tx[i_] = bx[i_+i1_];
-            }
-            i1_ = (0) - (1);
-            for(i_=1; i_<=m;i_++)
-            {
-                ty[i_] = rightpart[i_+i1_];
-            }
-            i1_ = (0) - (1);
-            for(i_=1; i_<=m;i_++)
-            {
-                td[i_] = bd1[i_+i1_];
-            }
-            tx[0] = tx[1]-(tx[2]-tx[1]);
-            ty[0] = ty[1]-td[1]*(tx[2]-tx[1]);
-            td[0] = td[1];
-            tx[m+1] = tx[m]+(tx[m]-tx[m-1]);
-            ty[m+1] = ty[m]+td[m]*(tx[m]-tx[m-1]);
-            td[m+1] = td[m];
-            spline1d.spline1dbuildhermite(tx, ty, td, m+2, s);
-            spline1d.spline1dlintransx(s, 2/(xb-xa), -((xa+xb)/(xb-xa)));
-            spline1d.spline1dlintransy(s, sb-sa, sa);
-            info = 1;
-            
-            //
-            // Fill report
-            //
-            rep.rmserror = 0;
-            rep.avgerror = 0;
-            rep.avgrelerror = 0;
-            rep.maxerror = 0;
-            relcnt = 0;
-            spline1d.spline1dconvcubic(bx, rightpart, m, 2, 0.0, 2, 0.0, x, n, ref fcolumn);
-            for(i=0; i<=n-1; i++)
-            {
-                v = (sb-sa)*fcolumn[i]+sa;
-                rep.rmserror = rep.rmserror+math.sqr(v-yoriginal[i]);
-                rep.avgerror = rep.avgerror+Math.Abs(v-yoriginal[i]);
-                if( (double)(yoriginal[i])!=(double)(0) )
-                {
-                    rep.avgrelerror = rep.avgrelerror+Math.Abs(v-yoriginal[i])/Math.Abs(yoriginal[i]);
-                    relcnt = relcnt+1;
-                }
-                rep.maxerror = Math.Max(rep.maxerror, Math.Abs(v-yoriginal[i]));
-            }
-            rep.rmserror = Math.Sqrt(rep.rmserror/n);
-            rep.avgerror = rep.avgerror/n;
-            if( (double)(relcnt)!=(double)(0) )
-            {
-                rep.avgrelerror = rep.avgrelerror/relcnt;
-            }
-        }
-
-
-        /*************************************************************************
-        Weighted fitting by cubic  spline,  with constraints on function values or
-        derivatives.
-
-        Equidistant grid with M-2 nodes on [min(x,xc),max(x,xc)] is  used to build
-        basis functions. Basis functions are cubic splines with continuous  second
-        derivatives  and  non-fixed first  derivatives  at  interval  ends.  Small
-        regularizing term is used  when  solving  constrained  tasks  (to  improve
-        stability).
-
-        Task is linear, so linear least squares solver is used. Complexity of this
-        computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-        SEE ALSO
-            Spline1DFitHermiteWC()  -   fitting by Hermite splines (more flexible,
-                                        less smooth)
-            Spline1DFitCubic()      -   "lightweight" fitting  by  cubic  splines,
-                                        without invididual weights and constraints
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            W   -   weights, array[0..N-1]
-                    Each summand in square  sum  of  approximation deviations from
-                    given  values  is  multiplied  by  the square of corresponding
-                    weight. Fill it by 1's if you don't  want  to  solve  weighted
-                    task.
-            N   -   number of points (optional):
-                    * N>0
-                    * if given, only first N elements of X/Y/W are processed
-                    * if not given, automatically determined from X/Y/W sizes
-            XC  -   points where spline values/derivatives are constrained,
-                    array[0..K-1].
-            YC  -   values of constraints, array[0..K-1]
-            DC  -   array[0..K-1], types of constraints:
-                    * DC[i]=0   means that S(XC[i])=YC[i]
-                    * DC[i]=1   means that S'(XC[i])=YC[i]
-                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-            K   -   number of constraints (optional):
-                    * 0<=K<M.
-                    * K=0 means no constraints (XC/YC/DC are not used)
-                    * if given, only first K elements of XC/YC/DC are used
-                    * if not given, automatically determined from XC/YC/DC
-            M   -   number of basis functions ( = number_of_nodes+2), M>=4.
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearWC() subroutine.
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-                                -3 means inconsistent constraints
-            S   -   spline interpolant.
-            Rep -   report, same format as in LSFitLinearWC() subroutine.
-                    Following fields are set:
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroitine doesn't calculate task's condition number for K<>0.
-
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-
-        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-        Setting constraints can lead  to undesired  results,  like ill-conditioned
-        behavior, or inconsistency being detected. From the other side,  it allows
-        us to improve quality of the fit. Here we summarize  our  experience  with
-        constrained regression splines:
-        * excessive constraints can be inconsistent. Splines are  piecewise  cubic
-          functions, and it is easy to create an example, where  large  number  of
-          constraints  concentrated  in  small  area will result in inconsistency.
-          Just because spline is not flexible enough to satisfy all of  them.  And
-          same constraints spread across the  [min(x),max(x)]  will  be  perfectly
-          consistent.
-        * the more evenly constraints are spread across [min(x),max(x)],  the more
-          chances that they will be consistent
-        * the  greater  is  M (given  fixed  constraints),  the  more chances that
-          constraints will be consistent
-        * in the general case, consistency of constraints IS NOT GUARANTEED.
-        * in the several special cases, however, we CAN guarantee consistency.
-        * one of this cases is constraints  on  the  function  values  AND/OR  its
-          derivatives at the interval boundaries.
-        * another  special  case  is ONE constraint on the function value (OR, but
-          not AND, derivative) anywhere in the interval
-
-        Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
-        can't solve your task without them. Anything beyond  special  cases  given
-        above is not guaranteed and may result in inconsistency.
-
-
-          -- ALGLIB PROJECT --
-             Copyright 18.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dfitcubicwc(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            double[] xc,
-            double[] yc,
-            int[] dc,
-            int k,
-            int m,
-            ref int info,
-            spline1d.spline1dinterpolant s,
-            spline1dfitreport rep)
-        {
-            int i = 0;
-
-            info = 0;
-
-            ap.assert(n>=1, "Spline1DFitCubicWC: N<1!");
-            ap.assert(m>=4, "Spline1DFitCubicWC: M<4!");
-            ap.assert(k>=0, "Spline1DFitCubicWC: K<0!");
-            ap.assert(k<m, "Spline1DFitCubicWC: K>=M!");
-            ap.assert(ap.len(x)>=n, "Spline1DFitCubicWC: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DFitCubicWC: Length(Y)<N!");
-            ap.assert(ap.len(w)>=n, "Spline1DFitCubicWC: Length(W)<N!");
-            ap.assert(ap.len(xc)>=k, "Spline1DFitCubicWC: Length(XC)<K!");
-            ap.assert(ap.len(yc)>=k, "Spline1DFitCubicWC: Length(YC)<K!");
-            ap.assert(ap.len(dc)>=k, "Spline1DFitCubicWC: Length(DC)<K!");
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitCubicWC: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitCubicWC: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(w, n), "Spline1DFitCubicWC: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(xc, k), "Spline1DFitCubicWC: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(yc, k), "Spline1DFitCubicWC: Y contains infinite or NAN values!");
-            for(i=0; i<=k-1; i++)
-            {
-                ap.assert(dc[i]==0 | dc[i]==1, "Spline1DFitCubicWC: DC[i] is neither 0 or 1!");
-            }
-            spline1dfitinternal(0, x, y, w, n, xc, yc, dc, k, m, ref info, s, rep);
-        }
-
-
-        /*************************************************************************
-        Weighted  fitting  by Hermite spline,  with constraints on function values
-        or first derivatives.
-
-        Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
-        basis functions. Basis functions are Hermite splines.  Small  regularizing
-        term is used when solving constrained tasks (to improve stability).
-
-        Task is linear, so linear least squares solver is used. Complexity of this
-        computational scheme is O(N*M^2), mostly dominated by least squares solver
-
-        SEE ALSO
-            Spline1DFitCubicWC()    -   fitting by Cubic splines (less flexible,
-                                        more smooth)
-            Spline1DFitHermite()    -   "lightweight" Hermite fitting, without
-                                        invididual weights and constraints
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            W   -   weights, array[0..N-1]
-                    Each summand in square  sum  of  approximation deviations from
-                    given  values  is  multiplied  by  the square of corresponding
-                    weight. Fill it by 1's if you don't  want  to  solve  weighted
-                    task.
-            N   -   number of points (optional):
-                    * N>0
-                    * if given, only first N elements of X/Y/W are processed
-                    * if not given, automatically determined from X/Y/W sizes
-            XC  -   points where spline values/derivatives are constrained,
-                    array[0..K-1].
-            YC  -   values of constraints, array[0..K-1]
-            DC  -   array[0..K-1], types of constraints:
-                    * DC[i]=0   means that S(XC[i])=YC[i]
-                    * DC[i]=1   means that S'(XC[i])=YC[i]
-                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
-            K   -   number of constraints (optional):
-                    * 0<=K<M.
-                    * K=0 means no constraints (XC/YC/DC are not used)
-                    * if given, only first K elements of XC/YC/DC are used
-                    * if not given, automatically determined from XC/YC/DC
-            M   -   number of basis functions (= 2 * number of nodes),
-                    M>=4,
-                    M IS EVEN!
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearW() subroutine:
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-                                -3 means inconsistent constraints
-                                -2 means odd M was passed (which is not supported)
-                                -1 means another errors in parameters passed
-                                   (N<=0, for example)
-            S   -   spline interpolant.
-            Rep -   report, same format as in LSFitLinearW() subroutine.
-                    Following fields are set:
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroitine doesn't calculate task's condition number for K<>0.
-
-        IMPORTANT:
-            this subroitine supports only even M's
-
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-
-        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
-
-        Setting constraints can lead  to undesired  results,  like ill-conditioned
-        behavior, or inconsistency being detected. From the other side,  it allows
-        us to improve quality of the fit. Here we summarize  our  experience  with
-        constrained regression splines:
-        * excessive constraints can be inconsistent. Splines are  piecewise  cubic
-          functions, and it is easy to create an example, where  large  number  of
-          constraints  concentrated  in  small  area will result in inconsistency.
-          Just because spline is not flexible enough to satisfy all of  them.  And
-          same constraints spread across the  [min(x),max(x)]  will  be  perfectly
-          consistent.
-        * the more evenly constraints are spread across [min(x),max(x)],  the more
-          chances that they will be consistent
-        * the  greater  is  M (given  fixed  constraints),  the  more chances that
-          constraints will be consistent
-        * in the general case, consistency of constraints is NOT GUARANTEED.
-        * in the several special cases, however, we can guarantee consistency.
-        * one of this cases is  M>=4  and   constraints  on   the  function  value
-          (AND/OR its derivative) at the interval boundaries.
-        * another special case is M>=4  and  ONE  constraint on the function value
-          (OR, BUT NOT AND, derivative) anywhere in [min(x),max(x)]
-
-        Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
-        can't solve your task without them. Anything beyond  special  cases  given
-        above is not guaranteed and may result in inconsistency.
-
-          -- ALGLIB PROJECT --
-             Copyright 18.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dfithermitewc(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            double[] xc,
-            double[] yc,
-            int[] dc,
-            int k,
-            int m,
-            ref int info,
-            spline1d.spline1dinterpolant s,
-            spline1dfitreport rep)
-        {
-            int i = 0;
-
-            info = 0;
-
-            ap.assert(n>=1, "Spline1DFitHermiteWC: N<1!");
-            ap.assert(m>=4, "Spline1DFitHermiteWC: M<4!");
-            ap.assert(m%2==0, "Spline1DFitHermiteWC: M is odd!");
-            ap.assert(k>=0, "Spline1DFitHermiteWC: K<0!");
-            ap.assert(k<m, "Spline1DFitHermiteWC: K>=M!");
-            ap.assert(ap.len(x)>=n, "Spline1DFitHermiteWC: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DFitHermiteWC: Length(Y)<N!");
-            ap.assert(ap.len(w)>=n, "Spline1DFitHermiteWC: Length(W)<N!");
-            ap.assert(ap.len(xc)>=k, "Spline1DFitHermiteWC: Length(XC)<K!");
-            ap.assert(ap.len(yc)>=k, "Spline1DFitHermiteWC: Length(YC)<K!");
-            ap.assert(ap.len(dc)>=k, "Spline1DFitHermiteWC: Length(DC)<K!");
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitHermiteWC: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitHermiteWC: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(w, n), "Spline1DFitHermiteWC: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(xc, k), "Spline1DFitHermiteWC: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(yc, k), "Spline1DFitHermiteWC: Y contains infinite or NAN values!");
-            for(i=0; i<=k-1; i++)
-            {
-                ap.assert(dc[i]==0 | dc[i]==1, "Spline1DFitHermiteWC: DC[i] is neither 0 or 1!");
-            }
-            spline1dfitinternal(1, x, y, w, n, xc, yc, dc, k, m, ref info, s, rep);
-        }
-
-
-        /*************************************************************************
-        Least squares fitting by cubic spline.
-
-        This subroutine is "lightweight" alternative for more complex and feature-
-        rich Spline1DFitCubicWC().  See  Spline1DFitCubicWC() for more information
-        about subroutine parameters (we don't duplicate it here because of length)
-
-          -- ALGLIB PROJECT --
-             Copyright 18.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dfitcubic(double[] x,
-            double[] y,
-            int n,
-            int m,
-            ref int info,
-            spline1d.spline1dinterpolant s,
-            spline1dfitreport rep)
-        {
-            int i = 0;
-            double[] w = new double[0];
-            double[] xc = new double[0];
-            double[] yc = new double[0];
-            int[] dc = new int[0];
-
-            info = 0;
-
-            ap.assert(n>=1, "Spline1DFitCubic: N<1!");
-            ap.assert(m>=4, "Spline1DFitCubic: M<4!");
-            ap.assert(ap.len(x)>=n, "Spline1DFitCubic: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DFitCubic: Length(Y)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitCubic: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitCubic: Y contains infinite or NAN values!");
-            w = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = 1;
-            }
-            spline1dfitcubicwc(x, y, w, n, xc, yc, dc, 0, m, ref info, s, rep);
-        }
-
-
-        /*************************************************************************
-        Least squares fitting by Hermite spline.
-
-        This subroutine is "lightweight" alternative for more complex and feature-
-        rich Spline1DFitHermiteWC().  See Spline1DFitHermiteWC()  description  for
-        more information about subroutine parameters (we don't duplicate  it  here
-        because of length).
-
-          -- ALGLIB PROJECT --
-             Copyright 18.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dfithermite(double[] x,
-            double[] y,
-            int n,
-            int m,
-            ref int info,
-            spline1d.spline1dinterpolant s,
-            spline1dfitreport rep)
-        {
-            int i = 0;
-            double[] w = new double[0];
-            double[] xc = new double[0];
-            double[] yc = new double[0];
-            int[] dc = new int[0];
-
-            info = 0;
-
-            ap.assert(n>=1, "Spline1DFitHermite: N<1!");
-            ap.assert(m>=4, "Spline1DFitHermite: M<4!");
-            ap.assert(m%2==0, "Spline1DFitHermite: M is odd!");
-            ap.assert(ap.len(x)>=n, "Spline1DFitHermite: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DFitHermite: Length(Y)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitHermite: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitHermite: Y contains infinite or NAN values!");
-            w = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = 1;
-            }
-            spline1dfithermitewc(x, y, w, n, xc, yc, dc, 0, m, ref info, s, rep);
-        }
-
-
-        /*************************************************************************
-        Weighted linear least squares fitting.
-
-        QR decomposition is used to reduce task to MxM, then triangular solver  or
-        SVD-based solver is used depending on condition number of the  system.  It
-        allows to maximize speed and retain decent accuracy.
-
-        INPUT PARAMETERS:
-            Y       -   array[0..N-1] Function values in  N  points.
-            W       -   array[0..N-1]  Weights  corresponding to function  values.
-                        Each summand in square  sum  of  approximation  deviations
-                        from  given  values  is  multiplied  by  the   square   of
-                        corresponding weight.
-            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                        FMatrix[I, J] - value of J-th basis function in I-th point.
-            N       -   number of points used. N>=1.
-            M       -   number of basis functions, M>=1.
-
-        OUTPUT PARAMETERS:
-            Info    -   error code:
-                        * -4    internal SVD decomposition subroutine failed (very
-                                rare and for degenerate systems only)
-                        * -1    incorrect N/M were specified
-                        *  1    task is solved
-            C       -   decomposition coefficients, array[0..M-1]
-            Rep     -   fitting report. Following fields are set:
-                        * Rep.TaskRCond     reciprocal of condition number
-                        * RMSError          rms error on the (X,Y).
-                        * AvgError          average error on the (X,Y).
-                        * AvgRelError       average relative error on the non-zero Y
-                        * MaxError          maximum error
-                                            NON-WEIGHTED ERRORS ARE CALCULATED
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitlinearw(double[] y,
-            double[] w,
-            double[,] fmatrix,
-            int n,
-            int m,
-            ref int info,
-            ref double[] c,
-            lsfitreport rep)
-        {
-            info = 0;
-            c = new double[0];
-
-            ap.assert(n>=1, "LSFitLinearW: N<1!");
-            ap.assert(m>=1, "LSFitLinearW: M<1!");
-            ap.assert(ap.len(y)>=n, "LSFitLinearW: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitLinearW: Y contains infinite or NaN values!");
-            ap.assert(ap.len(w)>=n, "LSFitLinearW: length(W)<N!");
-            ap.assert(apserv.isfinitevector(w, n), "LSFitLinearW: W contains infinite or NaN values!");
-            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinearW: rows(FMatrix)<N!");
-            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinearW: cols(FMatrix)<M!");
-            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinearW: FMatrix contains infinite or NaN values!");
-            lsfitlinearinternal(y, w, fmatrix, n, m, ref info, ref c, rep);
-        }
-
-
-        /*************************************************************************
-        Weighted constained linear least squares fitting.
-
-        This  is  variation  of LSFitLinearW(), which searchs for min|A*x=b| given
-        that  K  additional  constaints  C*x=bc are satisfied. It reduces original
-        task to modified one: min|B*y-d| WITHOUT constraints,  then LSFitLinearW()
-        is called.
-
-        INPUT PARAMETERS:
-            Y       -   array[0..N-1] Function values in  N  points.
-            W       -   array[0..N-1]  Weights  corresponding to function  values.
-                        Each summand in square  sum  of  approximation  deviations
-                        from  given  values  is  multiplied  by  the   square   of
-                        corresponding weight.
-            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                        FMatrix[I,J] - value of J-th basis function in I-th point.
-            CMatrix -   a table of constaints, array[0..K-1,0..M].
-                        I-th row of CMatrix corresponds to I-th linear constraint:
-                        CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
-            N       -   number of points used. N>=1.
-            M       -   number of basis functions, M>=1.
-            K       -   number of constraints, 0 <= K < M
-                        K=0 corresponds to absence of constraints.
-
-        OUTPUT PARAMETERS:
-            Info    -   error code:
-                        * -4    internal SVD decomposition subroutine failed (very
-                                rare and for degenerate systems only)
-                        * -3    either   too   many  constraints  (M   or   more),
-                                degenerate  constraints   (some   constraints  are
-                                repetead twice) or inconsistent  constraints  were
-                                specified.
-                        *  1    task is solved
-            C       -   decomposition coefficients, array[0..M-1]
-            Rep     -   fitting report. Following fields are set:
-                        * RMSError          rms error on the (X,Y).
-                        * AvgError          average error on the (X,Y).
-                        * AvgRelError       average relative error on the non-zero Y
-                        * MaxError          maximum error
-                                            NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroitine doesn't calculate task's condition number for K<>0.
-
-          -- ALGLIB --
-             Copyright 07.09.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitlinearwc(double[] y,
-            double[] w,
-            double[,] fmatrix,
-            double[,] cmatrix,
-            int n,
-            int m,
-            int k,
-            ref int info,
-            ref double[] c,
-            lsfitreport rep)
-        {
-            int i = 0;
-            int j = 0;
-            double[] tau = new double[0];
-            double[,] q = new double[0,0];
-            double[,] f2 = new double[0,0];
-            double[] tmp = new double[0];
-            double[] c0 = new double[0];
-            double v = 0;
-            int i_ = 0;
-
-            y = (double[])y.Clone();
-            cmatrix = (double[,])cmatrix.Clone();
-            info = 0;
-            c = new double[0];
-
-            ap.assert(n>=1, "LSFitLinearWC: N<1!");
-            ap.assert(m>=1, "LSFitLinearWC: M<1!");
-            ap.assert(k>=0, "LSFitLinearWC: K<0!");
-            ap.assert(ap.len(y)>=n, "LSFitLinearWC: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitLinearWC: Y contains infinite or NaN values!");
-            ap.assert(ap.len(w)>=n, "LSFitLinearWC: length(W)<N!");
-            ap.assert(apserv.isfinitevector(w, n), "LSFitLinearWC: W contains infinite or NaN values!");
-            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinearWC: rows(FMatrix)<N!");
-            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinearWC: cols(FMatrix)<M!");
-            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinearWC: FMatrix contains infinite or NaN values!");
-            ap.assert(ap.rows(cmatrix)>=k, "LSFitLinearWC: rows(CMatrix)<K!");
-            ap.assert(ap.cols(cmatrix)>=m+1 | k==0, "LSFitLinearWC: cols(CMatrix)<M+1!");
-            ap.assert(apserv.apservisfinitematrix(cmatrix, k, m+1), "LSFitLinearWC: CMatrix contains infinite or NaN values!");
-            if( k>=m )
-            {
-                info = -3;
-                return;
-            }
-            
-            //
-            // Solve
-            //
-            if( k==0 )
-            {
-                
-                //
-                // no constraints
-                //
-                lsfitlinearinternal(y, w, fmatrix, n, m, ref info, ref c, rep);
-            }
-            else
-            {
-                
-                //
-                // First, find general form solution of constraints system:
-                // * factorize C = L*Q
-                // * unpack Q
-                // * fill upper part of C with zeros (for RCond)
-                //
-                // We got C=C0+Q2'*y where Q2 is lower M-K rows of Q.
-                //
-                ortfac.rmatrixlq(ref cmatrix, k, m, ref tau);
-                ortfac.rmatrixlqunpackq(cmatrix, k, m, tau, m, ref q);
-                for(i=0; i<=k-1; i++)
-                {
-                    for(j=i+1; j<=m-1; j++)
-                    {
-                        cmatrix[i,j] = 0.0;
-                    }
-                }
-                if( (double)(rcond.rmatrixlurcondinf(cmatrix, k))<(double)(1000*math.machineepsilon) )
-                {
-                    info = -3;
-                    return;
-                }
-                tmp = new double[k];
-                for(i=0; i<=k-1; i++)
-                {
-                    if( i>0 )
-                    {
-                        v = 0.0;
-                        for(i_=0; i_<=i-1;i_++)
-                        {
-                            v += cmatrix[i,i_]*tmp[i_];
-                        }
-                    }
-                    else
-                    {
-                        v = 0;
-                    }
-                    tmp[i] = (cmatrix[i,m]-v)/cmatrix[i,i];
-                }
-                c0 = new double[m];
-                for(i=0; i<=m-1; i++)
-                {
-                    c0[i] = 0;
-                }
-                for(i=0; i<=k-1; i++)
-                {
-                    v = tmp[i];
-                    for(i_=0; i_<=m-1;i_++)
-                    {
-                        c0[i_] = c0[i_] + v*q[i,i_];
-                    }
-                }
-                
-                //
-                // Second, prepare modified matrix F2 = F*Q2' and solve modified task
-                //
-                tmp = new double[Math.Max(n, m)+1];
-                f2 = new double[n, m-k];
-                blas.matrixvectormultiply(fmatrix, 0, n-1, 0, m-1, false, c0, 0, m-1, -1.0, ref y, 0, n-1, 1.0);
-                blas.matrixmatrixmultiply(fmatrix, 0, n-1, 0, m-1, false, q, k, m-1, 0, m-1, true, 1.0, ref f2, 0, n-1, 0, m-k-1, 0.0, ref tmp);
-                lsfitlinearinternal(y, w, f2, n, m-k, ref info, ref tmp, rep);
-                rep.taskrcond = -1;
-                if( info<=0 )
-                {
-                    return;
-                }
-                
-                //
-                // then, convert back to original answer: C = C0 + Q2'*Y0
-                //
-                c = new double[m];
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    c[i_] = c0[i_];
-                }
-                blas.matrixvectormultiply(q, k, m-1, 0, m-1, true, tmp, 0, m-k-1, 1.0, ref c, 0, m-1, 1.0);
-            }
-        }
-
-
-        /*************************************************************************
-        Linear least squares fitting.
-
-        QR decomposition is used to reduce task to MxM, then triangular solver  or
-        SVD-based solver is used depending on condition number of the  system.  It
-        allows to maximize speed and retain decent accuracy.
-
-        INPUT PARAMETERS:
-            Y       -   array[0..N-1] Function values in  N  points.
-            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                        FMatrix[I, J] - value of J-th basis function in I-th point.
-            N       -   number of points used. N>=1.
-            M       -   number of basis functions, M>=1.
-
-        OUTPUT PARAMETERS:
-            Info    -   error code:
-                        * -4    internal SVD decomposition subroutine failed (very
-                                rare and for degenerate systems only)
-                        *  1    task is solved
-            C       -   decomposition coefficients, array[0..M-1]
-            Rep     -   fitting report. Following fields are set:
-                        * Rep.TaskRCond     reciprocal of condition number
-                        * RMSError          rms error on the (X,Y).
-                        * AvgError          average error on the (X,Y).
-                        * AvgRelError       average relative error on the non-zero Y
-                        * MaxError          maximum error
-                                            NON-WEIGHTED ERRORS ARE CALCULATED
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitlinear(double[] y,
-            double[,] fmatrix,
-            int n,
-            int m,
-            ref int info,
-            ref double[] c,
-            lsfitreport rep)
-        {
-            double[] w = new double[0];
-            int i = 0;
-
-            info = 0;
-            c = new double[0];
-
-            ap.assert(n>=1, "LSFitLinear: N<1!");
-            ap.assert(m>=1, "LSFitLinear: M<1!");
-            ap.assert(ap.len(y)>=n, "LSFitLinear: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitLinear: Y contains infinite or NaN values!");
-            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinear: rows(FMatrix)<N!");
-            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinear: cols(FMatrix)<M!");
-            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinear: FMatrix contains infinite or NaN values!");
-            w = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = 1;
-            }
-            lsfitlinearinternal(y, w, fmatrix, n, m, ref info, ref c, rep);
-        }
-
-
-        /*************************************************************************
-        Constained linear least squares fitting.
-
-        This  is  variation  of LSFitLinear(),  which searchs for min|A*x=b| given
-        that  K  additional  constaints  C*x=bc are satisfied. It reduces original
-        task to modified one: min|B*y-d| WITHOUT constraints,  then  LSFitLinear()
-        is called.
-
-        INPUT PARAMETERS:
-            Y       -   array[0..N-1] Function values in  N  points.
-            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
-                        FMatrix[I,J] - value of J-th basis function in I-th point.
-            CMatrix -   a table of constaints, array[0..K-1,0..M].
-                        I-th row of CMatrix corresponds to I-th linear constraint:
-                        CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
-            N       -   number of points used. N>=1.
-            M       -   number of basis functions, M>=1.
-            K       -   number of constraints, 0 <= K < M
-                        K=0 corresponds to absence of constraints.
-
-        OUTPUT PARAMETERS:
-            Info    -   error code:
-                        * -4    internal SVD decomposition subroutine failed (very
-                                rare and for degenerate systems only)
-                        * -3    either   too   many  constraints  (M   or   more),
-                                degenerate  constraints   (some   constraints  are
-                                repetead twice) or inconsistent  constraints  were
-                                specified.
-                        *  1    task is solved
-            C       -   decomposition coefficients, array[0..M-1]
-            Rep     -   fitting report. Following fields are set:
-                        * RMSError          rms error on the (X,Y).
-                        * AvgError          average error on the (X,Y).
-                        * AvgRelError       average relative error on the non-zero Y
-                        * MaxError          maximum error
-                                            NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroitine doesn't calculate task's condition number for K<>0.
-
-          -- ALGLIB --
-             Copyright 07.09.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitlinearc(double[] y,
-            double[,] fmatrix,
-            double[,] cmatrix,
-            int n,
-            int m,
-            int k,
-            ref int info,
-            ref double[] c,
-            lsfitreport rep)
-        {
-            double[] w = new double[0];
-            int i = 0;
-
-            y = (double[])y.Clone();
-            info = 0;
-            c = new double[0];
-
-            ap.assert(n>=1, "LSFitLinearC: N<1!");
-            ap.assert(m>=1, "LSFitLinearC: M<1!");
-            ap.assert(k>=0, "LSFitLinearC: K<0!");
-            ap.assert(ap.len(y)>=n, "LSFitLinearC: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitLinearC: Y contains infinite or NaN values!");
-            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinearC: rows(FMatrix)<N!");
-            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinearC: cols(FMatrix)<M!");
-            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinearC: FMatrix contains infinite or NaN values!");
-            ap.assert(ap.rows(cmatrix)>=k, "LSFitLinearC: rows(CMatrix)<K!");
-            ap.assert(ap.cols(cmatrix)>=m+1 | k==0, "LSFitLinearC: cols(CMatrix)<M+1!");
-            ap.assert(apserv.apservisfinitematrix(cmatrix, k, m+1), "LSFitLinearC: CMatrix contains infinite or NaN values!");
-            w = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = 1;
-            }
-            lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref c, rep);
-        }
-
-
-        /*************************************************************************
-        Weighted nonlinear least squares fitting using function values only.
-
-        Combination of numerical differentiation and secant updates is used to
-        obtain function Jacobian.
-
-        Nonlinear task min(F(c)) is solved, where
-
-            F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
-
-            * N is a number of points,
-            * M is a dimension of a space points belong to,
-            * K is a dimension of a space of parameters being fitted,
-            * w is an N-dimensional vector of weight coefficients,
-            * x is a set of N points, each of them is an M-dimensional vector,
-            * c is a K-dimensional vector of parameters being fitted
-
-        This subroutine uses only f(c,x[i]).
-
-        INPUT PARAMETERS:
-            X       -   array[0..N-1,0..M-1], points (one row = one point)
-            Y       -   array[0..N-1], function values.
-            W       -   weights, array[0..N-1]
-            C       -   array[0..K-1], initial approximation to the solution,
-            N       -   number of points, N>1
-            M       -   dimension of space
-            K       -   number of parameters being fitted
-            DiffStep-   numerical differentiation step;
-                        should not be very small or large;
-                        large = loss of accuracy
-                        small = growth of round-off errors
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-
-          -- ALGLIB --
-             Copyright 18.10.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitcreatewf(double[,] x,
-            double[] y,
-            double[] w,
-            double[] c,
-            int n,
-            int m,
-            int k,
-            double diffstep,
-            lsfitstate state)
-        {
-            int i = 0;
-            int i_ = 0;
-
-            ap.assert(n>=1, "LSFitCreateWF: N<1!");
-            ap.assert(m>=1, "LSFitCreateWF: M<1!");
-            ap.assert(k>=1, "LSFitCreateWF: K<1!");
-            ap.assert(ap.len(c)>=k, "LSFitCreateWF: length(C)<K!");
-            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateWF: C contains infinite or NaN values!");
-            ap.assert(ap.len(y)>=n, "LSFitCreateWF: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateWF: Y contains infinite or NaN values!");
-            ap.assert(ap.len(w)>=n, "LSFitCreateWF: length(W)<N!");
-            ap.assert(apserv.isfinitevector(w, n), "LSFitCreateWF: W contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateWF: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateWF: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateWF: X contains infinite or NaN values!");
-            ap.assert(math.isfinite(diffstep), "LSFitCreateWF: DiffStep is not finite!");
-            ap.assert((double)(diffstep)>(double)(0), "LSFitCreateWF: DiffStep<=0!");
-            state.n = n;
-            state.m = m;
-            state.k = k;
-            lsfitsetcond(state, 0.0, 0.0, 0);
-            lsfitsetstpmax(state, 0.0);
-            lsfitsetxrep(state, false);
-            state.taskx = new double[n, m];
-            state.tasky = new double[n];
-            state.w = new double[n];
-            state.c = new double[k];
-            state.x = new double[m];
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = c[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.w[i_] = w[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    state.taskx[i,i_] = x[i,i_];
-                }
-                state.tasky[i] = y[i];
-            }
-            minlm.minlmcreatev(k, n, state.c, diffstep, state.optstate);
-            lsfitclearrequestfields(state);
-            state.rstate.ia = new int[4+1];
-            state.rstate.ra = new double[1+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-        Nonlinear least squares fitting using function values only.
-
-        Combination of numerical differentiation and secant updates is used to
-        obtain function Jacobian.
-
-        Nonlinear task min(F(c)) is solved, where
-
-            F(c) = (f(c,x[0])-y[0])^2 + ... + (f(c,x[n-1])-y[n-1])^2,
-
-            * N is a number of points,
-            * M is a dimension of a space points belong to,
-            * K is a dimension of a space of parameters being fitted,
-            * w is an N-dimensional vector of weight coefficients,
-            * x is a set of N points, each of them is an M-dimensional vector,
-            * c is a K-dimensional vector of parameters being fitted
-
-        This subroutine uses only f(c,x[i]).
-
-        INPUT PARAMETERS:
-            X       -   array[0..N-1,0..M-1], points (one row = one point)
-            Y       -   array[0..N-1], function values.
-            C       -   array[0..K-1], initial approximation to the solution,
-            N       -   number of points, N>1
-            M       -   dimension of space
-            K       -   number of parameters being fitted
-            DiffStep-   numerical differentiation step;
-                        should not be very small or large;
-                        large = loss of accuracy
-                        small = growth of round-off errors
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-
-          -- ALGLIB --
-             Copyright 18.10.2008 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitcreatef(double[,] x,
-            double[] y,
-            double[] c,
-            int n,
-            int m,
-            int k,
-            double diffstep,
-            lsfitstate state)
-        {
-            int i = 0;
-            int i_ = 0;
-
-            ap.assert(n>=1, "LSFitCreateFG: N<1!");
-            ap.assert(m>=1, "LSFitCreateFG: M<1!");
-            ap.assert(k>=1, "LSFitCreateFG: K<1!");
-            ap.assert(ap.len(c)>=k, "LSFitCreateFG: length(C)<K!");
-            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateFG: C contains infinite or NaN values!");
-            ap.assert(ap.len(y)>=n, "LSFitCreateFG: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateFG: Y contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateFG: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateFG: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFG: X contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateFG: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateFG: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFG: X contains infinite or NaN values!");
-            ap.assert(math.isfinite(diffstep), "LSFitCreateWF: DiffStep is not finite!");
-            ap.assert((double)(diffstep)>(double)(0), "LSFitCreateWF: DiffStep<=0!");
-            state.n = n;
-            state.m = m;
-            state.k = k;
-            lsfitsetcond(state, 0.0, 0.0, 0);
-            lsfitsetstpmax(state, 0.0);
-            lsfitsetxrep(state, false);
-            state.taskx = new double[n, m];
-            state.tasky = new double[n];
-            state.w = new double[n];
-            state.c = new double[k];
-            state.x = new double[m];
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = c[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    state.taskx[i,i_] = x[i,i_];
-                }
-                state.tasky[i] = y[i];
-                state.w[i] = 1;
-            }
-            minlm.minlmcreatev(k, n, state.c, diffstep, state.optstate);
-            lsfitclearrequestfields(state);
-            state.rstate.ia = new int[4+1];
-            state.rstate.ra = new double[1+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-        Weighted nonlinear least squares fitting using gradient only.
-
-        Nonlinear task min(F(c)) is solved, where
-
-            F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
-            
-            * N is a number of points,
-            * M is a dimension of a space points belong to,
-            * K is a dimension of a space of parameters being fitted,
-            * w is an N-dimensional vector of weight coefficients,
-            * x is a set of N points, each of them is an M-dimensional vector,
-            * c is a K-dimensional vector of parameters being fitted
-            
-        This subroutine uses only f(c,x[i]) and its gradient.
-            
-        INPUT PARAMETERS:
-            X       -   array[0..N-1,0..M-1], points (one row = one point)
-            Y       -   array[0..N-1], function values.
-            W       -   weights, array[0..N-1]
-            C       -   array[0..K-1], initial approximation to the solution,
-            N       -   number of points, N>1
-            M       -   dimension of space
-            K       -   number of parameters being fitted
-            CheapFG -   boolean flag, which is:
-                        * True  if both function and gradient calculation complexity
-                                are less than O(M^2).  An improved  algorithm  can
-                                be  used  which corresponds  to  FGJ  scheme  from
-                                MINLM unit.
-                        * False otherwise.
-                                Standard Jacibian-bases  Levenberg-Marquardt  algo
-                                will be used (FJ scheme).
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-
-        See also:
-            LSFitResults
-            LSFitCreateFG (fitting without weights)
-            LSFitCreateWFGH (fitting using Hessian)
-            LSFitCreateFGH (fitting using Hessian, without weights)
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitcreatewfg(double[,] x,
-            double[] y,
-            double[] w,
-            double[] c,
-            int n,
-            int m,
-            int k,
-            bool cheapfg,
-            lsfitstate state)
-        {
-            int i = 0;
-            int i_ = 0;
-
-            ap.assert(n>=1, "LSFitCreateWFG: N<1!");
-            ap.assert(m>=1, "LSFitCreateWFG: M<1!");
-            ap.assert(k>=1, "LSFitCreateWFG: K<1!");
-            ap.assert(ap.len(c)>=k, "LSFitCreateWFG: length(C)<K!");
-            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateWFG: C contains infinite or NaN values!");
-            ap.assert(ap.len(y)>=n, "LSFitCreateWFG: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateWFG: Y contains infinite or NaN values!");
-            ap.assert(ap.len(w)>=n, "LSFitCreateWFG: length(W)<N!");
-            ap.assert(apserv.isfinitevector(w, n), "LSFitCreateWFG: W contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateWFG: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateWFG: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateWFG: X contains infinite or NaN values!");
-            state.n = n;
-            state.m = m;
-            state.k = k;
-            lsfitsetcond(state, 0.0, 0.0, 0);
-            lsfitsetstpmax(state, 0.0);
-            lsfitsetxrep(state, false);
-            state.taskx = new double[n, m];
-            state.tasky = new double[n];
-            state.w = new double[n];
-            state.c = new double[k];
-            state.x = new double[m];
-            state.g = new double[k];
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = c[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.w[i_] = w[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    state.taskx[i,i_] = x[i,i_];
-                }
-                state.tasky[i] = y[i];
-            }
-            if( cheapfg )
-            {
-                minlm.minlmcreatevgj(k, n, state.c, state.optstate);
-            }
-            else
-            {
-                minlm.minlmcreatevj(k, n, state.c, state.optstate);
-            }
-            lsfitclearrequestfields(state);
-            state.rstate.ia = new int[4+1];
-            state.rstate.ra = new double[1+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-        Nonlinear least squares fitting using gradient only, without individual
-        weights.
-
-        Nonlinear task min(F(c)) is solved, where
-
-            F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
-
-            * N is a number of points,
-            * M is a dimension of a space points belong to,
-            * K is a dimension of a space of parameters being fitted,
-            * x is a set of N points, each of them is an M-dimensional vector,
-            * c is a K-dimensional vector of parameters being fitted
-
-        This subroutine uses only f(c,x[i]) and its gradient.
-
-        INPUT PARAMETERS:
-            X       -   array[0..N-1,0..M-1], points (one row = one point)
-            Y       -   array[0..N-1], function values.
-            C       -   array[0..K-1], initial approximation to the solution,
-            N       -   number of points, N>1
-            M       -   dimension of space
-            K       -   number of parameters being fitted
-            CheapFG -   boolean flag, which is:
-                        * True  if both function and gradient calculation complexity
-                                are less than O(M^2).  An improved  algorithm  can
-                                be  used  which corresponds  to  FGJ  scheme  from
-                                MINLM unit.
-                        * False otherwise.
-                                Standard Jacibian-bases  Levenberg-Marquardt  algo
-                                will be used (FJ scheme).
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitcreatefg(double[,] x,
-            double[] y,
-            double[] c,
-            int n,
-            int m,
-            int k,
-            bool cheapfg,
-            lsfitstate state)
-        {
-            int i = 0;
-            int i_ = 0;
-
-            ap.assert(n>=1, "LSFitCreateFG: N<1!");
-            ap.assert(m>=1, "LSFitCreateFG: M<1!");
-            ap.assert(k>=1, "LSFitCreateFG: K<1!");
-            ap.assert(ap.len(c)>=k, "LSFitCreateFG: length(C)<K!");
-            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateFG: C contains infinite or NaN values!");
-            ap.assert(ap.len(y)>=n, "LSFitCreateFG: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateFG: Y contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateFG: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateFG: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFG: X contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateFG: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateFG: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFG: X contains infinite or NaN values!");
-            state.n = n;
-            state.m = m;
-            state.k = k;
-            lsfitsetcond(state, 0.0, 0.0, 0);
-            lsfitsetstpmax(state, 0.0);
-            lsfitsetxrep(state, false);
-            state.taskx = new double[n, m];
-            state.tasky = new double[n];
-            state.w = new double[n];
-            state.c = new double[k];
-            state.x = new double[m];
-            state.g = new double[k];
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = c[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    state.taskx[i,i_] = x[i,i_];
-                }
-                state.tasky[i] = y[i];
-                state.w[i] = 1;
-            }
-            if( cheapfg )
-            {
-                minlm.minlmcreatevgj(k, n, state.c, state.optstate);
-            }
-            else
-            {
-                minlm.minlmcreatevj(k, n, state.c, state.optstate);
-            }
-            lsfitclearrequestfields(state);
-            state.rstate.ia = new int[4+1];
-            state.rstate.ra = new double[1+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-        Weighted nonlinear least squares fitting using gradient/Hessian.
-
-        Nonlinear task min(F(c)) is solved, where
-
-            F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
-
-            * N is a number of points,
-            * M is a dimension of a space points belong to,
-            * K is a dimension of a space of parameters being fitted,
-            * w is an N-dimensional vector of weight coefficients,
-            * x is a set of N points, each of them is an M-dimensional vector,
-            * c is a K-dimensional vector of parameters being fitted
-
-        This subroutine uses f(c,x[i]), its gradient and its Hessian.
-
-        INPUT PARAMETERS:
-            X       -   array[0..N-1,0..M-1], points (one row = one point)
-            Y       -   array[0..N-1], function values.
-            W       -   weights, array[0..N-1]
-            C       -   array[0..K-1], initial approximation to the solution,
-            N       -   number of points, N>1
-            M       -   dimension of space
-            K       -   number of parameters being fitted
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitcreatewfgh(double[,] x,
-            double[] y,
-            double[] w,
-            double[] c,
-            int n,
-            int m,
-            int k,
-            lsfitstate state)
-        {
-            int i = 0;
-            int i_ = 0;
-
-            ap.assert(n>=1, "LSFitCreateWFGH: N<1!");
-            ap.assert(m>=1, "LSFitCreateWFGH: M<1!");
-            ap.assert(k>=1, "LSFitCreateWFGH: K<1!");
-            ap.assert(ap.len(c)>=k, "LSFitCreateWFGH: length(C)<K!");
-            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateWFGH: C contains infinite or NaN values!");
-            ap.assert(ap.len(y)>=n, "LSFitCreateWFGH: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateWFGH: Y contains infinite or NaN values!");
-            ap.assert(ap.len(w)>=n, "LSFitCreateWFGH: length(W)<N!");
-            ap.assert(apserv.isfinitevector(w, n), "LSFitCreateWFGH: W contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateWFGH: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateWFGH: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateWFGH: X contains infinite or NaN values!");
-            state.n = n;
-            state.m = m;
-            state.k = k;
-            lsfitsetcond(state, 0.0, 0.0, 0);
-            lsfitsetstpmax(state, 0.0);
-            lsfitsetxrep(state, false);
-            state.taskx = new double[n, m];
-            state.tasky = new double[n];
-            state.w = new double[n];
-            state.c = new double[k];
-            state.h = new double[k, k];
-            state.x = new double[m];
-            state.g = new double[k];
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = c[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.w[i_] = w[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    state.taskx[i,i_] = x[i,i_];
-                }
-                state.tasky[i] = y[i];
-            }
-            minlm.minlmcreatefgh(k, state.c, state.optstate);
-            lsfitclearrequestfields(state);
-            state.rstate.ia = new int[4+1];
-            state.rstate.ra = new double[1+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-        Nonlinear least squares fitting using gradient/Hessian, without individial
-        weights.
-
-        Nonlinear task min(F(c)) is solved, where
-
-            F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
-
-            * N is a number of points,
-            * M is a dimension of a space points belong to,
-            * K is a dimension of a space of parameters being fitted,
-            * x is a set of N points, each of them is an M-dimensional vector,
-            * c is a K-dimensional vector of parameters being fitted
-
-        This subroutine uses f(c,x[i]), its gradient and its Hessian.
-
-        INPUT PARAMETERS:
-            X       -   array[0..N-1,0..M-1], points (one row = one point)
-            Y       -   array[0..N-1], function values.
-            C       -   array[0..K-1], initial approximation to the solution,
-            N       -   number of points, N>1
-            M       -   dimension of space
-            K       -   number of parameters being fitted
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitcreatefgh(double[,] x,
-            double[] y,
-            double[] c,
-            int n,
-            int m,
-            int k,
-            lsfitstate state)
-        {
-            int i = 0;
-            int i_ = 0;
-
-            ap.assert(n>=1, "LSFitCreateFGH: N<1!");
-            ap.assert(m>=1, "LSFitCreateFGH: M<1!");
-            ap.assert(k>=1, "LSFitCreateFGH: K<1!");
-            ap.assert(ap.len(c)>=k, "LSFitCreateFGH: length(C)<K!");
-            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateFGH: C contains infinite or NaN values!");
-            ap.assert(ap.len(y)>=n, "LSFitCreateFGH: length(Y)<N!");
-            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateFGH: Y contains infinite or NaN values!");
-            ap.assert(ap.rows(x)>=n, "LSFitCreateFGH: rows(X)<N!");
-            ap.assert(ap.cols(x)>=m, "LSFitCreateFGH: cols(X)<M!");
-            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFGH: X contains infinite or NaN values!");
-            state.n = n;
-            state.m = m;
-            state.k = k;
-            lsfitsetcond(state, 0.0, 0.0, 0);
-            lsfitsetstpmax(state, 0.0);
-            lsfitsetxrep(state, false);
-            state.taskx = new double[n, m];
-            state.tasky = new double[n];
-            state.w = new double[n];
-            state.c = new double[k];
-            state.h = new double[k, k];
-            state.x = new double[m];
-            state.g = new double[k];
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = c[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    state.taskx[i,i_] = x[i,i_];
-                }
-                state.tasky[i] = y[i];
-                state.w[i] = 1;
-            }
-            minlm.minlmcreatefgh(k, state.c, state.optstate);
-            lsfitclearrequestfields(state);
-            state.rstate.ia = new int[4+1];
-            state.rstate.ra = new double[1+1];
-            state.rstate.stage = -1;
-        }
-
-
-        /*************************************************************************
-        Stopping conditions for nonlinear least squares fitting.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            EpsF    -   stopping criterion. Algorithm stops if
-                        |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
-            EpsX    -   stopping criterion. Algorithm stops if
-                        |X(k+1)-X(k)| <= EpsX*(1+|X(k)|)
-            MaxIts  -   stopping criterion. Algorithm stops after MaxIts iterations.
-                        MaxIts=0 means no stopping criterion.
-
-        NOTE
-
-        Passing EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to automatic
-        stopping criterion selection (according to the scheme used by MINLM unit).
-
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitsetcond(lsfitstate state,
-            double epsf,
-            double epsx,
-            int maxits)
-        {
-            ap.assert(math.isfinite(epsf), "LSFitSetCond: EpsF is not finite!");
-            ap.assert((double)(epsf)>=(double)(0), "LSFitSetCond: negative EpsF!");
-            ap.assert(math.isfinite(epsx), "LSFitSetCond: EpsX is not finite!");
-            ap.assert((double)(epsx)>=(double)(0), "LSFitSetCond: negative EpsX!");
-            ap.assert(maxits>=0, "LSFitSetCond: negative MaxIts!");
-            state.epsf = epsf;
-            state.epsx = epsx;
-            state.maxits = maxits;
-        }
-
-
-        /*************************************************************************
-        This function sets maximum step length
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
-                        want to limit step length.
-
-        Use this subroutine when you optimize target function which contains exp()
-        or  other  fast  growing  functions,  and optimization algorithm makes too
-        large  steps  which  leads  to overflow. This function allows us to reject
-        steps  that  are  too  large  (and  therefore  expose  us  to the possible
-        overflow) without actually calculating function value at the x+stp*d.
-
-        NOTE: non-zero StpMax leads to moderate  performance  degradation  because
-        intermediate  step  of  preconditioned L-BFGS optimization is incompatible
-        with limits on step size.
-
-          -- ALGLIB --
-             Copyright 02.04.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitsetstpmax(lsfitstate state,
-            double stpmax)
-        {
-            ap.assert((double)(stpmax)>=(double)(0), "LSFitSetStpMax: StpMax<0!");
-            state.stpmax = stpmax;
-        }
-
-
-        /*************************************************************************
-        This function turns on/off reporting.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            NeedXRep-   whether iteration reports are needed or not
-            
-        When reports are needed, State.C (current parameters) and State.F (current
-        value of fitting function) are reported.
-
-
-          -- ALGLIB --
-             Copyright 15.08.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitsetxrep(lsfitstate state,
-            bool needxrep)
-        {
-            state.xrep = needxrep;
-        }
-
-
-        /*************************************************************************
-        NOTES:
-
-        1. this algorithm is somewhat unusual because it works with  parameterized
-           function f(C,X), where X is a function argument (we  have  many  points
-           which are characterized by different  argument  values),  and  C  is  a
-           parameter to fit.
-
-           For example, if we want to do linear fit by f(c0,c1,x) = c0*x+c1,  then
-           x will be argument, and {c0,c1} will be parameters.
-           
-           It is important to understand that this algorithm finds minimum in  the
-           space of function PARAMETERS (not arguments), so it  needs  derivatives
-           of f() with respect to C, not X.
-           
-           In the example above it will need f=c0*x+c1 and {df/dc0,df/dc1} = {x,1}
-           instead of {df/dx} = {c0}.
-
-        2. Callback functions accept C as the first parameter, and X as the second
-
-        3. If  state  was  created  with  LSFitCreateFG(),  algorithm  needs  just
-           function   and   its   gradient,   but   if   state   was  created with
-           LSFitCreateFGH(), algorithm will need function, gradient and Hessian.
-           
-           According  to  the  said  above,  there  ase  several  versions of this
-           function, which accept different sets of callbacks.
-           
-           This flexibility opens way to subtle errors - you may create state with
-           LSFitCreateFGH() (optimization using Hessian), but call function  which
-           does not accept Hessian. So when algorithm will request Hessian,  there
-           will be no callback to call. In this case exception will be thrown.
-           
-           Be careful to avoid such errors because there is no way to find them at
-           compile time - you can see them at runtime only.
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool lsfititeration(lsfitstate state)
-        {
-            bool result = new bool();
-            int n = 0;
-            int m = 0;
-            int k = 0;
-            int i = 0;
-            int j = 0;
-            double v = 0;
-            double relcnt = 0;
-            int i_ = 0;
-
-            
-            //
-            // Reverse communication preparations
-            // I know it looks ugly, but it works the same way
-            // anywhere from C++ to Python.
-            //
-            // This code initializes locals by:
-            // * random values determined during code
-            //   generation - on first subroutine call
-            // * values from previous call - on subsequent calls
-            //
-            if( state.rstate.stage>=0 )
-            {
-                n = state.rstate.ia[0];
-                m = state.rstate.ia[1];
-                k = state.rstate.ia[2];
-                i = state.rstate.ia[3];
-                j = state.rstate.ia[4];
-                v = state.rstate.ra[0];
-                relcnt = state.rstate.ra[1];
-            }
-            else
-            {
-                n = -983;
-                m = -989;
-                k = -834;
-                i = 900;
-                j = -287;
-                v = 364;
-                relcnt = 214;
-            }
-            if( state.rstate.stage==0 )
-            {
-                goto lbl_0;
-            }
-            if( state.rstate.stage==1 )
-            {
-                goto lbl_1;
-            }
-            if( state.rstate.stage==2 )
-            {
-                goto lbl_2;
-            }
-            if( state.rstate.stage==3 )
-            {
-                goto lbl_3;
-            }
-            if( state.rstate.stage==4 )
-            {
-                goto lbl_4;
-            }
-            if( state.rstate.stage==5 )
-            {
-                goto lbl_5;
-            }
-            if( state.rstate.stage==6 )
-            {
-                goto lbl_6;
-            }
-            
-            //
-            // Routine body
-            //
-            
-            //
-            // init
-            //
-            n = state.n;
-            m = state.m;
-            k = state.k;
-            minlm.minlmsetcond(state.optstate, 0.0, state.epsf, state.epsx, state.maxits);
-            minlm.minlmsetstpmax(state.optstate, state.stpmax);
-            minlm.minlmsetxrep(state.optstate, state.xrep);
-            
-            //
-            // Optimize
-            //
-        lbl_7:
-            if( !minlm.minlmiteration(state.optstate) )
-            {
-                goto lbl_8;
-            }
-            if( !state.optstate.needfi )
-            {
-                goto lbl_9;
-            }
-            
-            //
-            // calculate f[] = wi*(f(xi,c)-yi)
-            //
-            i = 0;
-        lbl_11:
-            if( i>n-1 )
-            {
-                goto lbl_13;
-            }
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = state.optstate.x[i_];
-            }
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.x[i_] = state.taskx[i,i_];
-            }
-            state.pointindex = i;
-            lsfitclearrequestfields(state);
-            state.needf = true;
-            state.rstate.stage = 0;
-            goto lbl_rcomm;
-        lbl_0:
-            state.needf = false;
-            state.optstate.fi[i] = state.w[i]*(state.f-state.tasky[i]);
-            i = i+1;
-            goto lbl_11;
-        lbl_13:
-            goto lbl_7;
-        lbl_9:
-            if( !state.optstate.needf )
-            {
-                goto lbl_14;
-            }
-            
-            //
-            // calculate F = sum (wi*(f(xi,c)-yi))^2
-            //
-            state.optstate.f = 0;
-            i = 0;
-        lbl_16:
-            if( i>n-1 )
-            {
-                goto lbl_18;
-            }
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = state.optstate.x[i_];
-            }
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.x[i_] = state.taskx[i,i_];
-            }
-            state.pointindex = i;
-            lsfitclearrequestfields(state);
-            state.needf = true;
-            state.rstate.stage = 1;
-            goto lbl_rcomm;
-        lbl_1:
-            state.needf = false;
-            state.optstate.f = state.optstate.f+math.sqr(state.w[i]*(state.f-state.tasky[i]));
-            i = i+1;
-            goto lbl_16;
-        lbl_18:
-            goto lbl_7;
-        lbl_14:
-            if( !state.optstate.needfg )
-            {
-                goto lbl_19;
-            }
-            
-            //
-            // calculate F/gradF
-            //
-            state.optstate.f = 0;
-            for(i=0; i<=k-1; i++)
-            {
-                state.optstate.g[i] = 0;
-            }
-            i = 0;
-        lbl_21:
-            if( i>n-1 )
-            {
-                goto lbl_23;
-            }
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = state.optstate.x[i_];
-            }
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.x[i_] = state.taskx[i,i_];
-            }
-            state.pointindex = i;
-            lsfitclearrequestfields(state);
-            state.needfg = true;
-            state.rstate.stage = 2;
-            goto lbl_rcomm;
-        lbl_2:
-            state.needfg = false;
-            state.optstate.f = state.optstate.f+math.sqr(state.w[i]*(state.f-state.tasky[i]));
-            v = math.sqr(state.w[i])*2*(state.f-state.tasky[i]);
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.optstate.g[i_] = state.optstate.g[i_] + v*state.g[i_];
-            }
-            i = i+1;
-            goto lbl_21;
-        lbl_23:
-            goto lbl_7;
-        lbl_19:
-            if( !state.optstate.needfij )
-            {
-                goto lbl_24;
-            }
-            
-            //
-            // calculate Fi/jac(Fi)
-            //
-            i = 0;
-        lbl_26:
-            if( i>n-1 )
-            {
-                goto lbl_28;
-            }
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = state.optstate.x[i_];
-            }
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.x[i_] = state.taskx[i,i_];
-            }
-            state.pointindex = i;
-            lsfitclearrequestfields(state);
-            state.needfg = true;
-            state.rstate.stage = 3;
-            goto lbl_rcomm;
-        lbl_3:
-            state.needfg = false;
-            state.optstate.fi[i] = state.w[i]*(state.f-state.tasky[i]);
-            v = state.w[i];
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.optstate.j[i,i_] = v*state.g[i_];
-            }
-            i = i+1;
-            goto lbl_26;
-        lbl_28:
-            goto lbl_7;
-        lbl_24:
-            if( !state.optstate.needfgh )
-            {
-                goto lbl_29;
-            }
-            
-            //
-            // calculate F/grad(F)/hess(F)
-            //
-            state.optstate.f = 0;
-            for(i=0; i<=k-1; i++)
-            {
-                state.optstate.g[i] = 0;
-            }
-            for(i=0; i<=k-1; i++)
-            {
-                for(j=0; j<=k-1; j++)
-                {
-                    state.optstate.h[i,j] = 0;
-                }
-            }
-            i = 0;
-        lbl_31:
-            if( i>n-1 )
-            {
-                goto lbl_33;
-            }
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = state.optstate.x[i_];
-            }
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.x[i_] = state.taskx[i,i_];
-            }
-            state.pointindex = i;
-            lsfitclearrequestfields(state);
-            state.needfgh = true;
-            state.rstate.stage = 4;
-            goto lbl_rcomm;
-        lbl_4:
-            state.needfgh = false;
-            state.optstate.f = state.optstate.f+math.sqr(state.w[i]*(state.f-state.tasky[i]));
-            v = math.sqr(state.w[i])*2*(state.f-state.tasky[i]);
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.optstate.g[i_] = state.optstate.g[i_] + v*state.g[i_];
-            }
-            for(j=0; j<=k-1; j++)
-            {
-                v = 2*math.sqr(state.w[i])*state.g[j];
-                for(i_=0; i_<=k-1;i_++)
-                {
-                    state.optstate.h[j,i_] = state.optstate.h[j,i_] + v*state.g[i_];
-                }
-                v = 2*math.sqr(state.w[i])*(state.f-state.tasky[i]);
-                for(i_=0; i_<=k-1;i_++)
-                {
-                    state.optstate.h[j,i_] = state.optstate.h[j,i_] + v*state.h[j,i_];
-                }
-            }
-            i = i+1;
-            goto lbl_31;
-        lbl_33:
-            goto lbl_7;
-        lbl_29:
-            if( !state.optstate.xupdated )
-            {
-                goto lbl_34;
-            }
-            
-            //
-            // Report new iteration
-            //
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = state.optstate.x[i_];
-            }
-            state.f = state.optstate.f;
-            lsfitclearrequestfields(state);
-            state.xupdated = true;
-            state.rstate.stage = 5;
-            goto lbl_rcomm;
-        lbl_5:
-            state.xupdated = false;
-            goto lbl_7;
-        lbl_34:
-            goto lbl_7;
-        lbl_8:
-            minlm.minlmresults(state.optstate, ref state.c, state.optrep);
-            state.repterminationtype = state.optrep.terminationtype;
-            
-            //
-            // calculate errors
-            //
-            if( state.repterminationtype<=0 )
-            {
-                goto lbl_36;
-            }
-            state.reprmserror = 0;
-            state.repavgerror = 0;
-            state.repavgrelerror = 0;
-            state.repmaxerror = 0;
-            relcnt = 0;
-            i = 0;
-        lbl_38:
-            if( i>n-1 )
-            {
-                goto lbl_40;
-            }
-            for(i_=0; i_<=k-1;i_++)
-            {
-                state.c[i_] = state.c[i_];
-            }
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.x[i_] = state.taskx[i,i_];
-            }
-            state.pointindex = i;
-            lsfitclearrequestfields(state);
-            state.needf = true;
-            state.rstate.stage = 6;
-            goto lbl_rcomm;
-        lbl_6:
-            state.needf = false;
-            v = state.f;
-            state.reprmserror = state.reprmserror+math.sqr(v-state.tasky[i]);
-            state.repavgerror = state.repavgerror+Math.Abs(v-state.tasky[i]);
-            if( (double)(state.tasky[i])!=(double)(0) )
-            {
-                state.repavgrelerror = state.repavgrelerror+Math.Abs(v-state.tasky[i])/Math.Abs(state.tasky[i]);
-                relcnt = relcnt+1;
-            }
-            state.repmaxerror = Math.Max(state.repmaxerror, Math.Abs(v-state.tasky[i]));
-            i = i+1;
-            goto lbl_38;
-        lbl_40:
-            state.reprmserror = Math.Sqrt(state.reprmserror/n);
-            state.repavgerror = state.repavgerror/n;
-            if( (double)(relcnt)!=(double)(0) )
-            {
-                state.repavgrelerror = state.repavgrelerror/relcnt;
-            }
-        lbl_36:
-            result = false;
-            return result;
-            
-            //
-            // Saving state
-            //
-        lbl_rcomm:
-            result = true;
-            state.rstate.ia[0] = n;
-            state.rstate.ia[1] = m;
-            state.rstate.ia[2] = k;
-            state.rstate.ia[3] = i;
-            state.rstate.ia[4] = j;
-            state.rstate.ra[0] = v;
-            state.rstate.ra[1] = relcnt;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Nonlinear least squares fitting results.
-
-        Called after return from LSFitFit().
-
-        INPUT PARAMETERS:
-            State   -   algorithm state
-
-        OUTPUT PARAMETERS:
-            Info    -   completetion code:
-                            *  1    relative function improvement is no more than
-                                    EpsF.
-                            *  2    relative step is no more than EpsX.
-                            *  4    gradient norm is no more than EpsG
-                            *  5    MaxIts steps was taken
-                            *  7    stopping conditions are too stringent,
-                                    further improvement is impossible
-            C       -   array[0..K-1], solution
-            Rep     -   optimization report. Following fields are set:
-                        * Rep.TerminationType completetion code:
-                        * RMSError          rms error on the (X,Y).
-                        * AvgError          average error on the (X,Y).
-                        * AvgRelError       average relative error on the non-zero Y
-                        * MaxError          maximum error
-                                            NON-WEIGHTED ERRORS ARE CALCULATED
-
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitresults(lsfitstate state,
-            ref int info,
-            ref double[] c,
-            lsfitreport rep)
-        {
-            int i_ = 0;
-
-            info = 0;
-            c = new double[0];
-
-            info = state.repterminationtype;
-            if( info>0 )
-            {
-                c = new double[state.k];
-                for(i_=0; i_<=state.k-1;i_++)
-                {
-                    c[i_] = state.c[i_];
-                }
-                rep.rmserror = state.reprmserror;
-                rep.avgerror = state.repavgerror;
-                rep.avgrelerror = state.repavgrelerror;
-                rep.maxerror = state.repmaxerror;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine: automatic scaling for LLS tasks.
-        NEVER CALL IT DIRECTLY!
-
-        Maps abscissas to [-1,1], standartizes ordinates and correspondingly scales
-        constraints. It also scales weights so that max(W[i])=1
-
-        Transformations performed:
-        * X, XC         [XA,XB] => [-1,+1]
-                        transformation makes min(X)=-1, max(X)=+1
-
-        * Y             [SA,SB] => [0,1]
-                        transformation makes mean(Y)=0, stddev(Y)=1
-                        
-        * YC            transformed accordingly to SA, SB, DC[I]
-
-          -- ALGLIB PROJECT --
-             Copyright 08.09.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void lsfitscalexy(ref double[] x,
-            ref double[] y,
-            ref double[] w,
-            int n,
-            ref double[] xc,
-            ref double[] yc,
-            int[] dc,
-            int k,
-            ref double xa,
-            ref double xb,
-            ref double sa,
-            ref double sb,
-            ref double[] xoriginal,
-            ref double[] yoriginal)
-        {
-            double xmin = 0;
-            double xmax = 0;
-            int i = 0;
-            double mx = 0;
-            int i_ = 0;
-
-            xa = 0;
-            xb = 0;
-            sa = 0;
-            sb = 0;
-            xoriginal = new double[0];
-            yoriginal = new double[0];
-
-            ap.assert(n>=1, "LSFitScaleXY: incorrect N");
-            ap.assert(k>=0, "LSFitScaleXY: incorrect K");
-            
-            //
-            // Calculate xmin/xmax.
-            // Force xmin<>xmax.
-            //
-            xmin = x[0];
-            xmax = x[0];
-            for(i=1; i<=n-1; i++)
-            {
-                xmin = Math.Min(xmin, x[i]);
-                xmax = Math.Max(xmax, x[i]);
-            }
-            for(i=0; i<=k-1; i++)
-            {
-                xmin = Math.Min(xmin, xc[i]);
-                xmax = Math.Max(xmax, xc[i]);
-            }
-            if( (double)(xmin)==(double)(xmax) )
-            {
-                if( (double)(xmin)==(double)(0) )
-                {
-                    xmin = -1;
-                    xmax = 1;
-                }
-                else
-                {
-                    if( (double)(xmin)>(double)(0) )
-                    {
-                        xmin = 0.5*xmin;
-                    }
-                    else
-                    {
-                        xmax = 0.5*xmax;
-                    }
-                }
-            }
-            
-            //
-            // Transform abscissas: map [XA,XB] to [0,1]
-            //
-            // Store old X[] in XOriginal[] (it will be used
-            // to calculate relative error).
-            //
-            xoriginal = new double[n];
-            for(i_=0; i_<=n-1;i_++)
-            {
-                xoriginal[i_] = x[i_];
-            }
-            xa = xmin;
-            xb = xmax;
-            for(i=0; i<=n-1; i++)
-            {
-                x[i] = 2*(x[i]-0.5*(xa+xb))/(xb-xa);
-            }
-            for(i=0; i<=k-1; i++)
-            {
-                ap.assert(dc[i]>=0, "LSFitScaleXY: internal error!");
-                xc[i] = 2*(xc[i]-0.5*(xa+xb))/(xb-xa);
-                yc[i] = yc[i]*Math.Pow(0.5*(xb-xa), dc[i]);
-            }
-            
-            //
-            // Transform function values: map [SA,SB] to [0,1]
-            // SA = mean(Y),
-            // SB = SA+stddev(Y).
-            //
-            // Store old Y[] in YOriginal[] (it will be used
-            // to calculate relative error).
-            //
-            yoriginal = new double[n];
-            for(i_=0; i_<=n-1;i_++)
-            {
-                yoriginal[i_] = y[i_];
-            }
-            sa = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                sa = sa+y[i];
-            }
-            sa = sa/n;
-            sb = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                sb = sb+math.sqr(y[i]-sa);
-            }
-            sb = Math.Sqrt(sb/n)+sa;
-            if( (double)(sb)==(double)(sa) )
-            {
-                sb = 2*sa;
-            }
-            if( (double)(sb)==(double)(sa) )
-            {
-                sb = sa+1;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                y[i] = (y[i]-sa)/(sb-sa);
-            }
-            for(i=0; i<=k-1; i++)
-            {
-                if( dc[i]==0 )
-                {
-                    yc[i] = (yc[i]-sa)/(sb-sa);
-                }
-                else
-                {
-                    yc[i] = yc[i]/(sb-sa);
-                }
-            }
-            
-            //
-            // Scale weights
-            //
-            mx = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                mx = Math.Max(mx, Math.Abs(w[i]));
-            }
-            if( (double)(mx)!=(double)(0) )
-            {
-                for(i=0; i<=n-1; i++)
-                {
-                    w[i] = w[i]/mx;
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal spline fitting subroutine
-
-          -- ALGLIB PROJECT --
-             Copyright 08.09.2009 by Bochkanov Sergey
-        *************************************************************************/
-        private static void spline1dfitinternal(int st,
-            double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            double[] xc,
-            double[] yc,
-            int[] dc,
-            int k,
-            int m,
-            ref int info,
-            spline1d.spline1dinterpolant s,
-            spline1dfitreport rep)
-        {
-            double[,] fmatrix = new double[0,0];
-            double[,] cmatrix = new double[0,0];
-            double[] y2 = new double[0];
-            double[] w2 = new double[0];
-            double[] sx = new double[0];
-            double[] sy = new double[0];
-            double[] sd = new double[0];
-            double[] tmp = new double[0];
-            double[] xoriginal = new double[0];
-            double[] yoriginal = new double[0];
-            lsfitreport lrep = new lsfitreport();
-            double v0 = 0;
-            double v1 = 0;
-            double v2 = 0;
-            double mx = 0;
-            spline1d.spline1dinterpolant s2 = new spline1d.spline1dinterpolant();
-            int i = 0;
-            int j = 0;
-            int relcnt = 0;
-            double xa = 0;
-            double xb = 0;
-            double sa = 0;
-            double sb = 0;
-            double bl = 0;
-            double br = 0;
-            double decay = 0;
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            w = (double[])w.Clone();
-            xc = (double[])xc.Clone();
-            yc = (double[])yc.Clone();
-            info = 0;
-
-            ap.assert(st==0 | st==1, "Spline1DFit: internal error!");
-            if( st==0 & m<4 )
-            {
-                info = -1;
-                return;
-            }
-            if( st==1 & m<4 )
-            {
-                info = -1;
-                return;
-            }
-            if( (n<1 | k<0) | k>=m )
-            {
-                info = -1;
-                return;
-            }
-            for(i=0; i<=k-1; i++)
-            {
-                info = 0;
-                if( dc[i]<0 )
-                {
-                    info = -1;
-                }
-                if( dc[i]>1 )
-                {
-                    info = -1;
-                }
-                if( info<0 )
-                {
-                    return;
-                }
-            }
-            if( st==1 & m%2!=0 )
-            {
-                
-                //
-                // Hermite fitter must have even number of basis functions
-                //
-                info = -2;
-                return;
-            }
-            
-            //
-            // weight decay for correct handling of task which becomes
-            // degenerate after constraints are applied
-            //
-            decay = 10000*math.machineepsilon;
-            
-            //
-            // Scale X, Y, XC, YC
-            //
-            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
-            
-            //
-            // allocate space, initialize:
-            // * SX     -   grid for basis functions
-            // * SY     -   values of basis functions at grid points
-            // * FMatrix-   values of basis functions at X[]
-            // * CMatrix-   values (derivatives) of basis functions at XC[]
-            //
-            y2 = new double[n+m];
-            w2 = new double[n+m];
-            fmatrix = new double[n+m, m];
-            if( k>0 )
-            {
-                cmatrix = new double[k, m+1];
-            }
-            if( st==0 )
-            {
-                
-                //
-                // allocate space for cubic spline
-                //
-                sx = new double[m-2];
-                sy = new double[m-2];
-                for(j=0; j<=m-2-1; j++)
-                {
-                    sx[j] = (double)(2*j)/(double)(m-2-1)-1;
-                }
-            }
-            if( st==1 )
-            {
-                
-                //
-                // allocate space for Hermite spline
-                //
-                sx = new double[m/2];
-                sy = new double[m/2];
-                sd = new double[m/2];
-                for(j=0; j<=m/2-1; j++)
-                {
-                    sx[j] = (double)(2*j)/(double)(m/2-1)-1;
-                }
-            }
-            
-            //
-            // Prepare design and constraints matrices:
-            // * fill constraints matrix
-            // * fill first N rows of design matrix with values
-            // * fill next M rows of design matrix with regularizing term
-            // * append M zeros to Y
-            // * append M elements, mean(abs(W)) each, to W
-            //
-            for(j=0; j<=m-1; j++)
-            {
-                
-                //
-                // prepare Jth basis function
-                //
-                if( st==0 )
-                {
-                    
-                    //
-                    // cubic spline basis
-                    //
-                    for(i=0; i<=m-2-1; i++)
-                    {
-                        sy[i] = 0;
-                    }
-                    bl = 0;
-                    br = 0;
-                    if( j<m-2 )
-                    {
-                        sy[j] = 1;
-                    }
-                    if( j==m-2 )
-                    {
-                        bl = 1;
-                    }
-                    if( j==m-1 )
-                    {
-                        br = 1;
-                    }
-                    spline1d.spline1dbuildcubic(sx, sy, m-2, 1, bl, 1, br, s2);
-                }
-                if( st==1 )
-                {
-                    
-                    //
-                    // Hermite basis
-                    //
-                    for(i=0; i<=m/2-1; i++)
-                    {
-                        sy[i] = 0;
-                        sd[i] = 0;
-                    }
-                    if( j%2==0 )
-                    {
-                        sy[j/2] = 1;
-                    }
-                    else
-                    {
-                        sd[j/2] = 1;
-                    }
-                    spline1d.spline1dbuildhermite(sx, sy, sd, m/2, s2);
-                }
-                
-                //
-                // values at X[], XC[]
-                //
-                for(i=0; i<=n-1; i++)
-                {
-                    fmatrix[i,j] = spline1d.spline1dcalc(s2, x[i]);
-                }
-                for(i=0; i<=k-1; i++)
-                {
-                    ap.assert(dc[i]>=0 & dc[i]<=2, "Spline1DFit: internal error!");
-                    spline1d.spline1ddiff(s2, xc[i], ref v0, ref v1, ref v2);
-                    if( dc[i]==0 )
-                    {
-                        cmatrix[i,j] = v0;
-                    }
-                    if( dc[i]==1 )
-                    {
-                        cmatrix[i,j] = v1;
-                    }
-                    if( dc[i]==2 )
-                    {
-                        cmatrix[i,j] = v2;
-                    }
-                }
-            }
-            for(i=0; i<=k-1; i++)
-            {
-                cmatrix[i,m] = yc[i];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=m-1; j++)
-                {
-                    if( i==j )
-                    {
-                        fmatrix[n+i,j] = decay;
-                    }
-                    else
-                    {
-                        fmatrix[n+i,j] = 0;
-                    }
-                }
-            }
-            y2 = new double[n+m];
-            w2 = new double[n+m];
-            for(i_=0; i_<=n-1;i_++)
-            {
-                y2[i_] = y[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                w2[i_] = w[i_];
-            }
-            mx = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                mx = mx+Math.Abs(w[i]);
-            }
-            mx = mx/n;
-            for(i=0; i<=m-1; i++)
-            {
-                y2[n+i] = 0;
-                w2[n+i] = mx;
-            }
-            
-            //
-            // Solve constrained task
-            //
-            if( k>0 )
-            {
-                
-                //
-                // solve using regularization
-                //
-                lsfitlinearwc(y2, w2, fmatrix, cmatrix, n+m, m, k, ref info, ref tmp, lrep);
-            }
-            else
-            {
-                
-                //
-                // no constraints, no regularization needed
-                //
-                lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref tmp, lrep);
-            }
-            if( info<0 )
-            {
-                return;
-            }
-            
-            //
-            // Generate spline and scale it
-            //
-            if( st==0 )
-            {
-                
-                //
-                // cubic spline basis
-                //
-                for(i_=0; i_<=m-2-1;i_++)
-                {
-                    sy[i_] = tmp[i_];
-                }
-                spline1d.spline1dbuildcubic(sx, sy, m-2, 1, tmp[m-2], 1, tmp[m-1], s);
-            }
-            if( st==1 )
-            {
-                
-                //
-                // Hermite basis
-                //
-                for(i=0; i<=m/2-1; i++)
-                {
-                    sy[i] = tmp[2*i];
-                    sd[i] = tmp[2*i+1];
-                }
-                spline1d.spline1dbuildhermite(sx, sy, sd, m/2, s);
-            }
-            spline1d.spline1dlintransx(s, 2/(xb-xa), -((xa+xb)/(xb-xa)));
-            spline1d.spline1dlintransy(s, sb-sa, sa);
-            
-            //
-            // Scale absolute errors obtained from LSFitLinearW.
-            // Relative error should be calculated separately
-            // (because of shifting/scaling of the task)
-            //
-            rep.taskrcond = lrep.taskrcond;
-            rep.rmserror = lrep.rmserror*(sb-sa);
-            rep.avgerror = lrep.avgerror*(sb-sa);
-            rep.maxerror = lrep.maxerror*(sb-sa);
-            rep.avgrelerror = 0;
-            relcnt = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                if( (double)(yoriginal[i])!=(double)(0) )
-                {
-                    rep.avgrelerror = rep.avgrelerror+Math.Abs(spline1d.spline1dcalc(s, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
-                    relcnt = relcnt+1;
-                }
-            }
-            if( relcnt!=0 )
-            {
-                rep.avgrelerror = rep.avgrelerror/relcnt;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal fitting subroutine
-        *************************************************************************/
-        private static void lsfitlinearinternal(double[] y,
-            double[] w,
-            double[,] fmatrix,
-            int n,
-            int m,
-            ref int info,
-            ref double[] c,
-            lsfitreport rep)
-        {
-            double threshold = 0;
-            double[,] ft = new double[0,0];
-            double[,] q = new double[0,0];
-            double[,] l = new double[0,0];
-            double[,] r = new double[0,0];
-            double[] b = new double[0];
-            double[] wmod = new double[0];
-            double[] tau = new double[0];
-            int i = 0;
-            int j = 0;
-            double v = 0;
-            double[] sv = new double[0];
-            double[,] u = new double[0,0];
-            double[,] vt = new double[0,0];
-            double[] tmp = new double[0];
-            double[] utb = new double[0];
-            double[] sutb = new double[0];
-            int relcnt = 0;
-            int i_ = 0;
-
-            info = 0;
-            c = new double[0];
-
-            if( n<1 | m<1 )
-            {
-                info = -1;
-                return;
-            }
-            info = 1;
-            threshold = Math.Sqrt(math.machineepsilon);
-            
-            //
-            // Degenerate case, needs special handling
-            //
-            if( n<m )
-            {
-                
-                //
-                // Create design matrix.
-                //
-                ft = new double[n, m];
-                b = new double[n];
-                wmod = new double[n];
-                for(j=0; j<=n-1; j++)
-                {
-                    v = w[j];
-                    for(i_=0; i_<=m-1;i_++)
-                    {
-                        ft[j,i_] = v*fmatrix[j,i_];
-                    }
-                    b[j] = w[j]*y[j];
-                    wmod[j] = 1;
-                }
-                
-                //
-                // LQ decomposition and reduction to M=N
-                //
-                c = new double[m];
-                for(i=0; i<=m-1; i++)
-                {
-                    c[i] = 0;
-                }
-                rep.taskrcond = 0;
-                ortfac.rmatrixlq(ref ft, n, m, ref tau);
-                ortfac.rmatrixlqunpackq(ft, n, m, tau, n, ref q);
-                ortfac.rmatrixlqunpackl(ft, n, m, ref l);
-                lsfitlinearinternal(b, wmod, l, n, n, ref info, ref tmp, rep);
-                if( info<=0 )
-                {
-                    return;
-                }
-                for(i=0; i<=n-1; i++)
-                {
-                    v = tmp[i];
-                    for(i_=0; i_<=m-1;i_++)
-                    {
-                        c[i_] = c[i_] + v*q[i,i_];
-                    }
-                }
-                return;
-            }
-            
-            //
-            // N>=M. Generate design matrix and reduce to N=M using
-            // QR decomposition.
-            //
-            ft = new double[n, m];
-            b = new double[n];
-            for(j=0; j<=n-1; j++)
-            {
-                v = w[j];
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    ft[j,i_] = v*fmatrix[j,i_];
-                }
-                b[j] = w[j]*y[j];
-            }
-            ortfac.rmatrixqr(ref ft, n, m, ref tau);
-            ortfac.rmatrixqrunpackq(ft, n, m, tau, m, ref q);
-            ortfac.rmatrixqrunpackr(ft, n, m, ref r);
-            tmp = new double[m];
-            for(i=0; i<=m-1; i++)
-            {
-                tmp[i] = 0;
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                v = b[i];
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    tmp[i_] = tmp[i_] + v*q[i,i_];
-                }
-            }
-            b = new double[m];
-            for(i_=0; i_<=m-1;i_++)
-            {
-                b[i_] = tmp[i_];
-            }
-            
-            //
-            // R contains reduced MxM design upper triangular matrix,
-            // B contains reduced Mx1 right part.
-            //
-            // Determine system condition number and decide
-            // should we use triangular solver (faster) or
-            // SVD-based solver (more stable).
-            //
-            // We can use LU-based RCond estimator for this task.
-            //
-            rep.taskrcond = rcond.rmatrixlurcondinf(r, m);
-            if( (double)(rep.taskrcond)>(double)(threshold) )
-            {
-                
-                //
-                // use QR-based solver
-                //
-                c = new double[m];
-                c[m-1] = b[m-1]/r[m-1,m-1];
-                for(i=m-2; i>=0; i--)
-                {
-                    v = 0.0;
-                    for(i_=i+1; i_<=m-1;i_++)
-                    {
-                        v += r[i,i_]*c[i_];
-                    }
-                    c[i] = (b[i]-v)/r[i,i];
-                }
-            }
-            else
-            {
-                
-                //
-                // use SVD-based solver
-                //
-                if( !svd.rmatrixsvd(r, m, m, 1, 1, 2, ref sv, ref u, ref vt) )
-                {
-                    info = -4;
-                    return;
-                }
-                utb = new double[m];
-                sutb = new double[m];
-                for(i=0; i<=m-1; i++)
-                {
-                    utb[i] = 0;
-                }
-                for(i=0; i<=m-1; i++)
-                {
-                    v = b[i];
-                    for(i_=0; i_<=m-1;i_++)
-                    {
-                        utb[i_] = utb[i_] + v*u[i,i_];
-                    }
-                }
-                if( (double)(sv[0])>(double)(0) )
-                {
-                    rep.taskrcond = sv[m-1]/sv[0];
-                    for(i=0; i<=m-1; i++)
-                    {
-                        if( (double)(sv[i])>(double)(threshold*sv[0]) )
-                        {
-                            sutb[i] = utb[i]/sv[i];
-                        }
-                        else
-                        {
-                            sutb[i] = 0;
-                        }
-                    }
-                }
-                else
-                {
-                    rep.taskrcond = 0;
-                    for(i=0; i<=m-1; i++)
-                    {
-                        sutb[i] = 0;
-                    }
-                }
-                c = new double[m];
-                for(i=0; i<=m-1; i++)
-                {
-                    c[i] = 0;
-                }
-                for(i=0; i<=m-1; i++)
-                {
-                    v = sutb[i];
-                    for(i_=0; i_<=m-1;i_++)
-                    {
-                        c[i_] = c[i_] + v*vt[i,i_];
-                    }
-                }
-            }
-            
-            //
-            // calculate errors
-            //
-            rep.rmserror = 0;
-            rep.avgerror = 0;
-            rep.avgrelerror = 0;
-            rep.maxerror = 0;
-            relcnt = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                v = 0.0;
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    v += fmatrix[i,i_]*c[i_];
-                }
-                rep.rmserror = rep.rmserror+math.sqr(v-y[i]);
-                rep.avgerror = rep.avgerror+Math.Abs(v-y[i]);
-                if( (double)(y[i])!=(double)(0) )
-                {
-                    rep.avgrelerror = rep.avgrelerror+Math.Abs(v-y[i])/Math.Abs(y[i]);
-                    relcnt = relcnt+1;
-                }
-                rep.maxerror = Math.Max(rep.maxerror, Math.Abs(v-y[i]));
-            }
-            rep.rmserror = Math.Sqrt(rep.rmserror/n);
-            rep.avgerror = rep.avgerror/n;
-            if( relcnt!=0 )
-            {
-                rep.avgrelerror = rep.avgrelerror/relcnt;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine
-        *************************************************************************/
-        private static void lsfitclearrequestfields(lsfitstate state)
-        {
-            state.needf = false;
-            state.needfg = false;
-            state.needfgh = false;
-            state.xupdated = false;
-        }
-
-
-        /*************************************************************************
-        Internal subroutine, calculates barycentric basis functions.
-        Used for efficient simultaneous calculation of N basis functions.
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        private static void barycentriccalcbasis(ratint.barycentricinterpolant b,
-            double t,
-            ref double[] y)
-        {
-            double s2 = 0;
-            double s = 0;
-            double v = 0;
-            int i = 0;
-            int j = 0;
-            int i_ = 0;
-
-            
-            //
-            // special case: N=1
-            //
-            if( b.n==1 )
-            {
-                y[0] = 1;
-                return;
-            }
-            
-            //
-            // Here we assume that task is normalized, i.e.:
-            // 1. abs(Y[i])<=1
-            // 2. abs(W[i])<=1
-            // 3. X[] is ordered
-            //
-            // First, we decide: should we use "safe" formula (guarded
-            // against overflow) or fast one?
-            //
-            s = Math.Abs(t-b.x[0]);
-            for(i=0; i<=b.n-1; i++)
-            {
-                v = b.x[i];
-                if( (double)(v)==(double)(t) )
-                {
-                    for(j=0; j<=b.n-1; j++)
-                    {
-                        y[j] = 0;
-                    }
-                    y[i] = 1;
-                    return;
-                }
-                v = Math.Abs(t-v);
-                if( (double)(v)<(double)(s) )
-                {
-                    s = v;
-                }
-            }
-            s2 = 0;
-            for(i=0; i<=b.n-1; i++)
-            {
-                v = s/(t-b.x[i]);
-                v = v*b.w[i];
-                y[i] = v;
-                s2 = s2+v;
-            }
-            v = 1/s2;
-            for(i_=0; i_<=b.n-1;i_++)
-            {
-                y[i_] = v*y[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        This is internal function for Chebyshev fitting.
-
-        It assumes that input data are normalized:
-        * X/XC belong to [-1,+1],
-        * mean(Y)=0, stddev(Y)=1.
-
-        It does not checks inputs for errors.
-
-        This function is used to fit general (shifted) Chebyshev models, power
-        basis models or barycentric models.
-
-        INPUT PARAMETERS:
-            X   -   points, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            W   -   weights, array[0..N-1]
-            N   -   number of points, N>0.
-            XC  -   points where polynomial values/derivatives are constrained,
-                    array[0..K-1].
-            YC  -   values of constraints, array[0..K-1]
-            DC  -   array[0..K-1], types of constraints:
-                    * DC[i]=0   means that P(XC[i])=YC[i]
-                    * DC[i]=1   means that P'(XC[i])=YC[i]
-            K   -   number of constraints, 0<=K<M.
-                    K=0 means no constraints (XC/YC/DC are not used in such cases)
-            M   -   number of basis functions (= polynomial_degree + 1), M>=1
-
-        OUTPUT PARAMETERS:
-            Info-   same format as in LSFitLinearW() subroutine:
-                    * Info>0    task is solved
-                    * Info<=0   an error occured:
-                                -4 means inconvergence of internal SVD
-                                -3 means inconsistent constraints
-            C   -   interpolant in Chebyshev form; [-1,+1] is used as base interval
-            Rep -   report, same format as in LSFitLinearW() subroutine.
-                    Following fields are set:
-                    * RMSError      rms error on the (X,Y).
-                    * AvgError      average error on the (X,Y).
-                    * AvgRelError   average relative error on the non-zero Y
-                    * MaxError      maximum error
-                                    NON-WEIGHTED ERRORS ARE CALCULATED
-
-        IMPORTANT:
-            this subroitine doesn't calculate task's condition number for K<>0.
-
-          -- ALGLIB PROJECT --
-             Copyright 10.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        private static void internalchebyshevfit(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            double[] xc,
-            double[] yc,
-            int[] dc,
-            int k,
-            int m,
-            ref int info,
-            ref double[] c,
-            lsfitreport rep)
-        {
-            double[] y2 = new double[0];
-            double[] w2 = new double[0];
-            double[] tmp = new double[0];
-            double[] tmp2 = new double[0];
-            double[] tmpdiff = new double[0];
-            double[] bx = new double[0];
-            double[] by = new double[0];
-            double[] bw = new double[0];
-            double[,] fmatrix = new double[0,0];
-            double[,] cmatrix = new double[0,0];
-            int i = 0;
-            int j = 0;
-            double mx = 0;
-            double decay = 0;
-            int i_ = 0;
-
-            xc = (double[])xc.Clone();
-            yc = (double[])yc.Clone();
-            info = 0;
-            c = new double[0];
-
-            
-            //
-            // weight decay for correct handling of task which becomes
-            // degenerate after constraints are applied
-            //
-            decay = 10000*math.machineepsilon;
-            
-            //
-            // allocate space, initialize/fill:
-            // * FMatrix-   values of basis functions at X[]
-            // * CMatrix-   values (derivatives) of basis functions at XC[]
-            // * fill constraints matrix
-            // * fill first N rows of design matrix with values
-            // * fill next M rows of design matrix with regularizing term
-            // * append M zeros to Y
-            // * append M elements, mean(abs(W)) each, to W
-            //
-            y2 = new double[n+m];
-            w2 = new double[n+m];
-            tmp = new double[m];
-            tmpdiff = new double[m];
-            fmatrix = new double[n+m, m];
-            if( k>0 )
-            {
-                cmatrix = new double[k, m+1];
-            }
-            
-            //
-            // Fill design matrix, Y2, W2:
-            // * first N rows with basis functions for original points
-            // * next M rows with decay terms
-            //
-            for(i=0; i<=n-1; i++)
-            {
-                
-                //
-                // prepare Ith row
-                // use Tmp for calculations to avoid multidimensional arrays overhead
-                //
-                for(j=0; j<=m-1; j++)
-                {
-                    if( j==0 )
-                    {
-                        tmp[j] = 1;
-                    }
-                    else
-                    {
-                        if( j==1 )
-                        {
-                            tmp[j] = x[i];
-                        }
-                        else
-                        {
-                            tmp[j] = 2*x[i]*tmp[j-1]-tmp[j-2];
-                        }
-                    }
-                }
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    fmatrix[i,i_] = tmp[i_];
-                }
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=m-1; j++)
-                {
-                    if( i==j )
-                    {
-                        fmatrix[n+i,j] = decay;
-                    }
-                    else
-                    {
-                        fmatrix[n+i,j] = 0;
-                    }
-                }
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                y2[i_] = y[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                w2[i_] = w[i_];
-            }
-            mx = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                mx = mx+Math.Abs(w[i]);
-            }
-            mx = mx/n;
-            for(i=0; i<=m-1; i++)
-            {
-                y2[n+i] = 0;
-                w2[n+i] = mx;
-            }
-            
-            //
-            // fill constraints matrix
-            //
-            for(i=0; i<=k-1; i++)
-            {
-                
-                //
-                // prepare Ith row
-                // use Tmp for basis function values,
-                // TmpDiff for basos function derivatives
-                //
-                for(j=0; j<=m-1; j++)
-                {
-                    if( j==0 )
-                    {
-                        tmp[j] = 1;
-                        tmpdiff[j] = 0;
-                    }
-                    else
-                    {
-                        if( j==1 )
-                        {
-                            tmp[j] = xc[i];
-                            tmpdiff[j] = 1;
-                        }
-                        else
-                        {
-                            tmp[j] = 2*xc[i]*tmp[j-1]-tmp[j-2];
-                            tmpdiff[j] = 2*(tmp[j-1]+xc[i]*tmpdiff[j-1])-tmpdiff[j-2];
-                        }
-                    }
-                }
-                if( dc[i]==0 )
-                {
-                    for(i_=0; i_<=m-1;i_++)
-                    {
-                        cmatrix[i,i_] = tmp[i_];
-                    }
-                }
-                if( dc[i]==1 )
-                {
-                    for(i_=0; i_<=m-1;i_++)
-                    {
-                        cmatrix[i,i_] = tmpdiff[i_];
-                    }
-                }
-                cmatrix[i,m] = yc[i];
-            }
-            
-            //
-            // Solve constrained task
-            //
-            if( k>0 )
-            {
-                
-                //
-                // solve using regularization
-                //
-                lsfitlinearwc(y2, w2, fmatrix, cmatrix, n+m, m, k, ref info, ref c, rep);
-            }
-            else
-            {
-                
-                //
-                // no constraints, no regularization needed
-                //
-                lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, 0, ref info, ref c, rep);
-            }
-            if( info<0 )
-            {
-                return;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal Floater-Hormann fitting subroutine for fixed D
-        *************************************************************************/
-        private static void barycentricfitwcfixedd(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            double[] xc,
-            double[] yc,
-            int[] dc,
-            int k,
-            int m,
-            int d,
-            ref int info,
-            ratint.barycentricinterpolant b,
-            barycentricfitreport rep)
-        {
-            double[,] fmatrix = new double[0,0];
-            double[,] cmatrix = new double[0,0];
-            double[] y2 = new double[0];
-            double[] w2 = new double[0];
-            double[] sx = new double[0];
-            double[] sy = new double[0];
-            double[] sbf = new double[0];
-            double[] xoriginal = new double[0];
-            double[] yoriginal = new double[0];
-            double[] tmp = new double[0];
-            lsfitreport lrep = new lsfitreport();
-            double v0 = 0;
-            double v1 = 0;
-            double mx = 0;
-            ratint.barycentricinterpolant b2 = new ratint.barycentricinterpolant();
-            int i = 0;
-            int j = 0;
-            int relcnt = 0;
-            double xa = 0;
-            double xb = 0;
-            double sa = 0;
-            double sb = 0;
-            double decay = 0;
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            w = (double[])w.Clone();
-            xc = (double[])xc.Clone();
-            yc = (double[])yc.Clone();
-            info = 0;
-
-            if( ((n<1 | m<2) | k<0) | k>=m )
-            {
-                info = -1;
-                return;
-            }
-            for(i=0; i<=k-1; i++)
-            {
-                info = 0;
-                if( dc[i]<0 )
-                {
-                    info = -1;
-                }
-                if( dc[i]>1 )
-                {
-                    info = -1;
-                }
-                if( info<0 )
-                {
-                    return;
-                }
-            }
-            
-            //
-            // weight decay for correct handling of task which becomes
-            // degenerate after constraints are applied
-            //
-            decay = 10000*math.machineepsilon;
-            
-            //
-            // Scale X, Y, XC, YC
-            //
-            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
-            
-            //
-            // allocate space, initialize:
-            // * FMatrix-   values of basis functions at X[]
-            // * CMatrix-   values (derivatives) of basis functions at XC[]
-            //
-            y2 = new double[n+m];
-            w2 = new double[n+m];
-            fmatrix = new double[n+m, m];
-            if( k>0 )
-            {
-                cmatrix = new double[k, m+1];
-            }
-            y2 = new double[n+m];
-            w2 = new double[n+m];
-            
-            //
-            // Prepare design and constraints matrices:
-            // * fill constraints matrix
-            // * fill first N rows of design matrix with values
-            // * fill next M rows of design matrix with regularizing term
-            // * append M zeros to Y
-            // * append M elements, mean(abs(W)) each, to W
-            //
-            sx = new double[m];
-            sy = new double[m];
-            sbf = new double[m];
-            for(j=0; j<=m-1; j++)
-            {
-                sx[j] = (double)(2*j)/(double)(m-1)-1;
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                sy[i] = 1;
-            }
-            ratint.barycentricbuildfloaterhormann(sx, sy, m, d, b2);
-            mx = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                barycentriccalcbasis(b2, x[i], ref sbf);
-                for(i_=0; i_<=m-1;i_++)
-                {
-                    fmatrix[i,i_] = sbf[i_];
-                }
-                y2[i] = y[i];
-                w2[i] = w[i];
-                mx = mx+Math.Abs(w[i])/n;
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=m-1; j++)
-                {
-                    if( i==j )
-                    {
-                        fmatrix[n+i,j] = decay;
-                    }
-                    else
-                    {
-                        fmatrix[n+i,j] = 0;
-                    }
-                }
-                y2[n+i] = 0;
-                w2[n+i] = mx;
-            }
-            if( k>0 )
-            {
-                for(j=0; j<=m-1; j++)
-                {
-                    for(i=0; i<=m-1; i++)
-                    {
-                        sy[i] = 0;
-                    }
-                    sy[j] = 1;
-                    ratint.barycentricbuildfloaterhormann(sx, sy, m, d, b2);
-                    for(i=0; i<=k-1; i++)
-                    {
-                        ap.assert(dc[i]>=0 & dc[i]<=1, "BarycentricFit: internal error!");
-                        ratint.barycentricdiff1(b2, xc[i], ref v0, ref v1);
-                        if( dc[i]==0 )
-                        {
-                            cmatrix[i,j] = v0;
-                        }
-                        if( dc[i]==1 )
-                        {
-                            cmatrix[i,j] = v1;
-                        }
-                    }
-                }
-                for(i=0; i<=k-1; i++)
-                {
-                    cmatrix[i,m] = yc[i];
-                }
-            }
-            
-            //
-            // Solve constrained task
-            //
-            if( k>0 )
-            {
-                
-                //
-                // solve using regularization
-                //
-                lsfitlinearwc(y2, w2, fmatrix, cmatrix, n+m, m, k, ref info, ref tmp, lrep);
-            }
-            else
-            {
-                
-                //
-                // no constraints, no regularization needed
-                //
-                lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref tmp, lrep);
-            }
-            if( info<0 )
-            {
-                return;
-            }
-            
-            //
-            // Generate interpolant and scale it
-            //
-            for(i_=0; i_<=m-1;i_++)
-            {
-                sy[i_] = tmp[i_];
-            }
-            ratint.barycentricbuildfloaterhormann(sx, sy, m, d, b);
-            ratint.barycentriclintransx(b, 2/(xb-xa), -((xa+xb)/(xb-xa)));
-            ratint.barycentriclintransy(b, sb-sa, sa);
-            
-            //
-            // Scale absolute errors obtained from LSFitLinearW.
-            // Relative error should be calculated separately
-            // (because of shifting/scaling of the task)
-            //
-            rep.taskrcond = lrep.taskrcond;
-            rep.rmserror = lrep.rmserror*(sb-sa);
-            rep.avgerror = lrep.avgerror*(sb-sa);
-            rep.maxerror = lrep.maxerror*(sb-sa);
-            rep.avgrelerror = 0;
-            relcnt = 0;
-            for(i=0; i<=n-1; i++)
-            {
-                if( (double)(yoriginal[i])!=(double)(0) )
-                {
-                    rep.avgrelerror = rep.avgrelerror+Math.Abs(ratint.barycentriccalc(b, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
-                    relcnt = relcnt+1;
-                }
-            }
-            if( relcnt!=0 )
-            {
-                rep.avgrelerror = rep.avgrelerror/relcnt;
-            }
-        }
-
-
-    }
-    public class ratint
-    {
-        /*************************************************************************
-        Barycentric interpolant.
-        *************************************************************************/
-        public class barycentricinterpolant
-        {
-            public int n;
-            public double sy;
-            public double[] x;
-            public double[] y;
-            public double[] w;
-            public barycentricinterpolant()
-            {
-                x = new double[0];
-                y = new double[0];
-                w = new double[0];
-            }
-        };
-
-
-
-
-        /*************************************************************************
-        Rational interpolation using barycentric formula
-
-        F(t) = SUM(i=0,n-1,w[i]*f[i]/(t-x[i])) / SUM(i=0,n-1,w[i]/(t-x[i]))
-
-        Input parameters:
-            B   -   barycentric interpolant built with one of model building
-                    subroutines.
-            T   -   interpolation point
-
-        Result:
-            barycentric interpolant F(t)
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double barycentriccalc(barycentricinterpolant b,
-            double t)
-        {
-            double result = 0;
-            double s1 = 0;
-            double s2 = 0;
-            double s = 0;
-            double v = 0;
-            int i = 0;
-
-            ap.assert(!Double.IsInfinity(t), "BarycentricCalc: infinite T!");
-            
-            //
-            // special case: NaN
-            //
-            if( Double.IsNaN(t) )
-            {
-                result = Double.NaN;
-                return result;
-            }
-            
-            //
-            // special case: N=1
-            //
-            if( b.n==1 )
-            {
-                result = b.sy*b.y[0];
-                return result;
-            }
-            
-            //
-            // Here we assume that task is normalized, i.e.:
-            // 1. abs(Y[i])<=1
-            // 2. abs(W[i])<=1
-            // 3. X[] is ordered
-            //
-            s = Math.Abs(t-b.x[0]);
-            for(i=0; i<=b.n-1; i++)
-            {
-                v = b.x[i];
-                if( (double)(v)==(double)(t) )
-                {
-                    result = b.sy*b.y[i];
-                    return result;
-                }
-                v = Math.Abs(t-v);
-                if( (double)(v)<(double)(s) )
-                {
-                    s = v;
-                }
-            }
-            s1 = 0;
-            s2 = 0;
-            for(i=0; i<=b.n-1; i++)
-            {
-                v = s/(t-b.x[i]);
-                v = v*b.w[i];
-                s1 = s1+v*b.y[i];
-                s2 = s2+v;
-            }
-            result = b.sy*s1/s2;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Differentiation of barycentric interpolant: first derivative.
-
-        Algorithm used in this subroutine is very robust and should not fail until
-        provided with values too close to MaxRealNumber  (usually  MaxRealNumber/N
-        or greater will overflow).
-
-        INPUT PARAMETERS:
-            B   -   barycentric interpolant built with one of model building
-                    subroutines.
-            T   -   interpolation point
-
-        OUTPUT PARAMETERS:
-            F   -   barycentric interpolant at T
-            DF  -   first derivative
-            
-        NOTE
-
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentricdiff1(barycentricinterpolant b,
-            double t,
-            ref double f,
-            ref double df)
-        {
-            double v = 0;
-            double vv = 0;
-            int i = 0;
-            int k = 0;
-            double n0 = 0;
-            double n1 = 0;
-            double d0 = 0;
-            double d1 = 0;
-            double s0 = 0;
-            double s1 = 0;
-            double xk = 0;
-            double xi = 0;
-            double xmin = 0;
-            double xmax = 0;
-            double xscale1 = 0;
-            double xoffs1 = 0;
-            double xscale2 = 0;
-            double xoffs2 = 0;
-            double xprev = 0;
-
-            f = 0;
-            df = 0;
-
-            ap.assert(!Double.IsInfinity(t), "BarycentricDiff1: infinite T!");
-            
-            //
-            // special case: NaN
-            //
-            if( Double.IsNaN(t) )
-            {
-                f = Double.NaN;
-                df = Double.NaN;
-                return;
-            }
-            
-            //
-            // special case: N=1
-            //
-            if( b.n==1 )
-            {
-                f = b.sy*b.y[0];
-                df = 0;
-                return;
-            }
-            if( (double)(b.sy)==(double)(0) )
-            {
-                f = 0;
-                df = 0;
-                return;
-            }
-            ap.assert((double)(b.sy)>(double)(0), "BarycentricDiff1: internal error");
-            
-            //
-            // We assume than N>1 and B.SY>0. Find:
-            // 1. pivot point (X[i] closest to T)
-            // 2. width of interval containing X[i]
-            //
-            v = Math.Abs(b.x[0]-t);
-            k = 0;
-            xmin = b.x[0];
-            xmax = b.x[0];
-            for(i=1; i<=b.n-1; i++)
-            {
-                vv = b.x[i];
-                if( (double)(Math.Abs(vv-t))<(double)(v) )
-                {
-                    v = Math.Abs(vv-t);
-                    k = i;
-                }
-                xmin = Math.Min(xmin, vv);
-                xmax = Math.Max(xmax, vv);
-            }
-            
-            //
-            // pivot point found, calculate dNumerator and dDenominator
-            //
-            xscale1 = 1/(xmax-xmin);
-            xoffs1 = -(xmin/(xmax-xmin))+1;
-            xscale2 = 2;
-            xoffs2 = -3;
-            t = t*xscale1+xoffs1;
-            t = t*xscale2+xoffs2;
-            xk = b.x[k];
-            xk = xk*xscale1+xoffs1;
-            xk = xk*xscale2+xoffs2;
-            v = t-xk;
-            n0 = 0;
-            n1 = 0;
-            d0 = 0;
-            d1 = 0;
-            xprev = -2;
-            for(i=0; i<=b.n-1; i++)
-            {
-                xi = b.x[i];
-                xi = xi*xscale1+xoffs1;
-                xi = xi*xscale2+xoffs2;
-                ap.assert((double)(xi)>(double)(xprev), "BarycentricDiff1: points are too close!");
-                xprev = xi;
-                if( i!=k )
-                {
-                    vv = math.sqr(t-xi);
-                    s0 = (t-xk)/(t-xi);
-                    s1 = (xk-xi)/vv;
-                }
-                else
-                {
-                    s0 = 1;
-                    s1 = 0;
-                }
-                vv = b.w[i]*b.y[i];
-                n0 = n0+s0*vv;
-                n1 = n1+s1*vv;
-                vv = b.w[i];
-                d0 = d0+s0*vv;
-                d1 = d1+s1*vv;
-            }
-            f = b.sy*n0/d0;
-            df = (n1*d0-n0*d1)/math.sqr(d0);
-            if( (double)(df)!=(double)(0) )
-            {
-                df = Math.Sign(df)*Math.Exp(Math.Log(Math.Abs(df))+Math.Log(b.sy)+Math.Log(xscale1)+Math.Log(xscale2));
-            }
-        }
-
-
-        /*************************************************************************
-        Differentiation of barycentric interpolant: first/second derivatives.
-
-        INPUT PARAMETERS:
-            B   -   barycentric interpolant built with one of model building
-                    subroutines.
-            T   -   interpolation point
-
-        OUTPUT PARAMETERS:
-            F   -   barycentric interpolant at T
-            DF  -   first derivative
-            D2F -   second derivative
-
-        NOTE: this algorithm may fail due to overflow/underflor if  used  on  data
-        whose values are close to MaxRealNumber or MinRealNumber.  Use more robust
-        BarycentricDiff1() subroutine in such cases.
-
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentricdiff2(barycentricinterpolant b,
-            double t,
-            ref double f,
-            ref double df,
-            ref double d2f)
-        {
-            double v = 0;
-            double vv = 0;
-            int i = 0;
-            int k = 0;
-            double n0 = 0;
-            double n1 = 0;
-            double n2 = 0;
-            double d0 = 0;
-            double d1 = 0;
-            double d2 = 0;
-            double s0 = 0;
-            double s1 = 0;
-            double s2 = 0;
-            double xk = 0;
-            double xi = 0;
-
-            f = 0;
-            df = 0;
-            d2f = 0;
-
-            ap.assert(!Double.IsInfinity(t), "BarycentricDiff1: infinite T!");
-            
-            //
-            // special case: NaN
-            //
-            if( Double.IsNaN(t) )
-            {
-                f = Double.NaN;
-                df = Double.NaN;
-                d2f = Double.NaN;
-                return;
-            }
-            
-            //
-            // special case: N=1
-            //
-            if( b.n==1 )
-            {
-                f = b.sy*b.y[0];
-                df = 0;
-                d2f = 0;
-                return;
-            }
-            if( (double)(b.sy)==(double)(0) )
-            {
-                f = 0;
-                df = 0;
-                d2f = 0;
-                return;
-            }
-            
-            //
-            // We assume than N>1 and B.SY>0. Find:
-            // 1. pivot point (X[i] closest to T)
-            // 2. width of interval containing X[i]
-            //
-            ap.assert((double)(b.sy)>(double)(0), "BarycentricDiff: internal error");
-            f = 0;
-            df = 0;
-            d2f = 0;
-            v = Math.Abs(b.x[0]-t);
-            k = 0;
-            for(i=1; i<=b.n-1; i++)
-            {
-                vv = b.x[i];
-                if( (double)(Math.Abs(vv-t))<(double)(v) )
-                {
-                    v = Math.Abs(vv-t);
-                    k = i;
-                }
-            }
-            
-            //
-            // pivot point found, calculate dNumerator and dDenominator
-            //
-            xk = b.x[k];
-            v = t-xk;
-            n0 = 0;
-            n1 = 0;
-            n2 = 0;
-            d0 = 0;
-            d1 = 0;
-            d2 = 0;
-            for(i=0; i<=b.n-1; i++)
-            {
-                if( i!=k )
-                {
-                    xi = b.x[i];
-                    vv = math.sqr(t-xi);
-                    s0 = (t-xk)/(t-xi);
-                    s1 = (xk-xi)/vv;
-                    s2 = -(2*(xk-xi)/(vv*(t-xi)));
-                }
-                else
-                {
-                    s0 = 1;
-                    s1 = 0;
-                    s2 = 0;
-                }
-                vv = b.w[i]*b.y[i];
-                n0 = n0+s0*vv;
-                n1 = n1+s1*vv;
-                n2 = n2+s2*vv;
-                vv = b.w[i];
-                d0 = d0+s0*vv;
-                d1 = d1+s1*vv;
-                d2 = d2+s2*vv;
-            }
-            f = b.sy*n0/d0;
-            df = b.sy*(n1*d0-n0*d1)/math.sqr(d0);
-            d2f = b.sy*((n2*d0-n0*d2)*math.sqr(d0)-(n1*d0-n0*d1)*2*d0*d1)/math.sqr(math.sqr(d0));
-        }
-
-
-        /*************************************************************************
-        This subroutine performs linear transformation of the argument.
-
-        INPUT PARAMETERS:
-            B       -   rational interpolant in barycentric form
-            CA, CB  -   transformation coefficients: x = CA*t + CB
-
-        OUTPUT PARAMETERS:
-            B       -   transformed interpolant with X replaced by T
-
-          -- ALGLIB PROJECT --
-             Copyright 19.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentriclintransx(barycentricinterpolant b,
-            double ca,
-            double cb)
-        {
-            int i = 0;
-            int j = 0;
-            double v = 0;
-
-            
-            //
-            // special case, replace by constant F(CB)
-            //
-            if( (double)(ca)==(double)(0) )
-            {
-                b.sy = barycentriccalc(b, cb);
-                v = 1;
-                for(i=0; i<=b.n-1; i++)
-                {
-                    b.y[i] = 1;
-                    b.w[i] = v;
-                    v = -v;
-                }
-                return;
-            }
-            
-            //
-            // general case: CA<>0
-            //
-            for(i=0; i<=b.n-1; i++)
-            {
-                b.x[i] = (b.x[i]-cb)/ca;
-            }
-            if( (double)(ca)<(double)(0) )
-            {
-                for(i=0; i<=b.n-1; i++)
-                {
-                    if( i<b.n-1-i )
-                    {
-                        j = b.n-1-i;
-                        v = b.x[i];
-                        b.x[i] = b.x[j];
-                        b.x[j] = v;
-                        v = b.y[i];
-                        b.y[i] = b.y[j];
-                        b.y[j] = v;
-                        v = b.w[i];
-                        b.w[i] = b.w[j];
-                        b.w[j] = v;
-                    }
-                    else
-                    {
-                        break;
-                    }
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        This  subroutine   performs   linear  transformation  of  the  barycentric
-        interpolant.
-
-        INPUT PARAMETERS:
-            B       -   rational interpolant in barycentric form
-            CA, CB  -   transformation coefficients: B2(x) = CA*B(x) + CB
-
-        OUTPUT PARAMETERS:
-            B       -   transformed interpolant
-
-          -- ALGLIB PROJECT --
-             Copyright 19.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentriclintransy(barycentricinterpolant b,
-            double ca,
-            double cb)
-        {
-            int i = 0;
-            double v = 0;
-            int i_ = 0;
-
-            for(i=0; i<=b.n-1; i++)
-            {
-                b.y[i] = ca*b.sy*b.y[i]+cb;
-            }
-            b.sy = 0;
-            for(i=0; i<=b.n-1; i++)
-            {
-                b.sy = Math.Max(b.sy, Math.Abs(b.y[i]));
-            }
-            if( (double)(b.sy)>(double)(0) )
-            {
-                v = 1/b.sy;
-                for(i_=0; i_<=b.n-1;i_++)
-                {
-                    b.y[i_] = v*b.y[i_];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Extracts X/Y/W arrays from rational interpolant
-
-        INPUT PARAMETERS:
-            B   -   barycentric interpolant
-
-        OUTPUT PARAMETERS:
-            N   -   nodes count, N>0
-            X   -   interpolation nodes, array[0..N-1]
-            F   -   function values, array[0..N-1]
-            W   -   barycentric weights, array[0..N-1]
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentricunpack(barycentricinterpolant b,
-            ref int n,
-            ref double[] x,
-            ref double[] y,
-            ref double[] w)
-        {
-            double v = 0;
-            int i_ = 0;
-
-            n = 0;
-            x = new double[0];
-            y = new double[0];
-            w = new double[0];
-
-            n = b.n;
-            x = new double[n];
-            y = new double[n];
-            w = new double[n];
-            v = b.sy;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                x[i_] = b.x[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                y[i_] = v*b.y[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                w[i_] = b.w[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        Rational interpolant from X/Y/W arrays
-
-        F(t) = SUM(i=0,n-1,w[i]*f[i]/(t-x[i])) / SUM(i=0,n-1,w[i]/(t-x[i]))
-
-        INPUT PARAMETERS:
-            X   -   interpolation nodes, array[0..N-1]
-            F   -   function values, array[0..N-1]
-            W   -   barycentric weights, array[0..N-1]
-            N   -   nodes count, N>0
-
-        OUTPUT PARAMETERS:
-            B   -   barycentric interpolant built from (X, Y, W)
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentricbuildxyw(double[] x,
-            double[] y,
-            double[] w,
-            int n,
-            barycentricinterpolant b)
-        {
-            int i_ = 0;
-
-            ap.assert(n>0, "BarycentricBuildXYW: incorrect N!");
-            
-            //
-            // fill X/Y/W
-            //
-            b.x = new double[n];
-            b.y = new double[n];
-            b.w = new double[n];
-            for(i_=0; i_<=n-1;i_++)
-            {
-                b.x[i_] = x[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                b.y[i_] = y[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                b.w[i_] = w[i_];
-            }
-            b.n = n;
-            
-            //
-            // Normalize
-            //
-            barycentricnormalize(b);
-        }
-
-
-        /*************************************************************************
-        Rational interpolant without poles
-
-        The subroutine constructs the rational interpolating function without real
-        poles  (see  'Barycentric rational interpolation with no  poles  and  high
-        rates of approximation', Michael S. Floater. and  Kai  Hormann,  for  more
-        information on this subject).
-
-        Input parameters:
-            X   -   interpolation nodes, array[0..N-1].
-            Y   -   function values, array[0..N-1].
-            N   -   number of nodes, N>0.
-            D   -   order of the interpolation scheme, 0 <= D <= N-1.
-                    D<0 will cause an error.
-                    D>=N it will be replaced with D=N-1.
-                    if you don't know what D to choose, use small value about 3-5.
-
-        Output parameters:
-            B   -   barycentric interpolant.
-
-        Note:
-            this algorithm always succeeds and calculates the weights  with  close
-            to machine precision.
-
-          -- ALGLIB PROJECT --
-             Copyright 17.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentricbuildfloaterhormann(double[] x,
-            double[] y,
-            int n,
-            int d,
-            barycentricinterpolant b)
-        {
-            double s0 = 0;
-            double s = 0;
-            double v = 0;
-            int i = 0;
-            int j = 0;
-            int k = 0;
-            int[] perm = new int[0];
-            double[] wtemp = new double[0];
-            double[] sortrbuf = new double[0];
-            double[] sortrbuf2 = new double[0];
-            int i_ = 0;
-
-            ap.assert(n>0, "BarycentricFloaterHormann: N<=0!");
-            ap.assert(d>=0, "BarycentricFloaterHormann: incorrect D!");
-            
-            //
-            // Prepare
-            //
-            if( d>n-1 )
-            {
-                d = n-1;
-            }
-            b.n = n;
-            
-            //
-            // special case: N=1
-            //
-            if( n==1 )
-            {
-                b.x = new double[n];
-                b.y = new double[n];
-                b.w = new double[n];
-                b.x[0] = x[0];
-                b.y[0] = y[0];
-                b.w[0] = 1;
-                barycentricnormalize(b);
-                return;
-            }
-            
-            //
-            // Fill X/Y
-            //
-            b.x = new double[n];
-            b.y = new double[n];
-            for(i_=0; i_<=n-1;i_++)
-            {
-                b.x[i_] = x[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                b.y[i_] = y[i_];
-            }
-            tsort.tagsortfastr(ref b.x, ref b.y, ref sortrbuf, ref sortrbuf2, n);
-            
-            //
-            // Calculate Wk
-            //
-            b.w = new double[n];
-            s0 = 1;
-            for(k=1; k<=d; k++)
-            {
-                s0 = -s0;
-            }
-            for(k=0; k<=n-1; k++)
-            {
-                
-                //
-                // Wk
-                //
-                s = 0;
-                for(i=Math.Max(k-d, 0); i<=Math.Min(k, n-1-d); i++)
-                {
-                    v = 1;
-                    for(j=i; j<=i+d; j++)
-                    {
-                        if( j!=k )
-                        {
-                            v = v/Math.Abs(b.x[k]-b.x[j]);
-                        }
-                    }
-                    s = s+v;
-                }
-                b.w[k] = s0*s;
-                
-                //
-                // Next S0
-                //
-                s0 = -s0;
-            }
-            
-            //
-            // Normalize
-            //
-            barycentricnormalize(b);
-        }
-
-
-        /*************************************************************************
-        Copying of the barycentric interpolant (for internal use only)
-
-        INPUT PARAMETERS:
-            B   -   barycentric interpolant
-
-        OUTPUT PARAMETERS:
-            B2  -   copy(B1)
-
-          -- ALGLIB --
-             Copyright 17.08.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void barycentriccopy(barycentricinterpolant b,
-            barycentricinterpolant b2)
-        {
-            int i_ = 0;
-
-            b2.n = b.n;
-            b2.sy = b.sy;
-            b2.x = new double[b2.n];
-            b2.y = new double[b2.n];
-            b2.w = new double[b2.n];
-            for(i_=0; i_<=b2.n-1;i_++)
-            {
-                b2.x[i_] = b.x[i_];
-            }
-            for(i_=0; i_<=b2.n-1;i_++)
-            {
-                b2.y[i_] = b.y[i_];
-            }
-            for(i_=0; i_<=b2.n-1;i_++)
-            {
-                b2.w[i_] = b.w[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        Normalization of barycentric interpolant:
-        * B.N, B.X, B.Y and B.W are initialized
-        * B.SY is NOT initialized
-        * Y[] is normalized, scaling coefficient is stored in B.SY
-        * W[] is normalized, no scaling coefficient is stored
-        * X[] is sorted
-
-        Internal subroutine.
-        *************************************************************************/
-        private static void barycentricnormalize(barycentricinterpolant b)
-        {
-            int[] p1 = new int[0];
-            int[] p2 = new int[0];
-            int i = 0;
-            int j = 0;
-            int j2 = 0;
-            double v = 0;
-            int i_ = 0;
-
-            
-            //
-            // Normalize task: |Y|<=1, |W|<=1, sort X[]
-            //
-            b.sy = 0;
-            for(i=0; i<=b.n-1; i++)
-            {
-                b.sy = Math.Max(b.sy, Math.Abs(b.y[i]));
-            }
-            if( (double)(b.sy)>(double)(0) & (double)(Math.Abs(b.sy-1))>(double)(10*math.machineepsilon) )
-            {
-                v = 1/b.sy;
-                for(i_=0; i_<=b.n-1;i_++)
-                {
-                    b.y[i_] = v*b.y[i_];
-                }
-            }
-            v = 0;
-            for(i=0; i<=b.n-1; i++)
-            {
-                v = Math.Max(v, Math.Abs(b.w[i]));
-            }
-            if( (double)(v)>(double)(0) & (double)(Math.Abs(v-1))>(double)(10*math.machineepsilon) )
-            {
-                v = 1/v;
-                for(i_=0; i_<=b.n-1;i_++)
-                {
-                    b.w[i_] = v*b.w[i_];
-                }
-            }
-            for(i=0; i<=b.n-2; i++)
-            {
-                if( (double)(b.x[i+1])<(double)(b.x[i]) )
-                {
-                    tsort.tagsort(ref b.x, b.n, ref p1, ref p2);
-                    for(j=0; j<=b.n-1; j++)
-                    {
-                        j2 = p2[j];
-                        v = b.y[j];
-                        b.y[j] = b.y[j2];
-                        b.y[j2] = v;
-                        v = b.w[j];
-                        b.w[j] = b.w[j2];
-                        b.w[j2] = v;
-                    }
-                    break;
-                }
-            }
-        }
-
-
-    }
-    public class polint
-    {
-        /*************************************************************************
-        Conversion from barycentric representation to Chebyshev basis.
-        This function has O(N^2) complexity.
-
-        INPUT PARAMETERS:
-            P   -   polynomial in barycentric form
-            A,B -   base interval for Chebyshev polynomials (see below)
-                    A<>B
-
-        OUTPUT PARAMETERS
-            T   -   coefficients of Chebyshev representation;
-                    P(x) = sum { T[i]*Ti(2*(x-A)/(B-A)-1), i=0..N-1 },
-                    where Ti - I-th Chebyshev polynomial.
-
-        NOTES:
-            barycentric interpolant passed as P may be either polynomial  obtained
-            from  polynomial  interpolation/ fitting or rational function which is
-            NOT polynomial. We can't distinguish between these two cases, and this
-            algorithm just tries to work assuming that P IS a polynomial.  If not,
-            algorithm will return results, but they won't have any meaning.
-
-          -- ALGLIB --
-             Copyright 30.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialbar2cheb(ratint.barycentricinterpolant p,
-            double a,
-            double b,
-            ref double[] t)
-        {
-            int i = 0;
-            int k = 0;
-            double[] vp = new double[0];
-            double[] vx = new double[0];
-            double[] tk = new double[0];
-            double[] tk1 = new double[0];
-            double v = 0;
-            int i_ = 0;
-
-            t = new double[0];
-
-            ap.assert(math.isfinite(a), "PolynomialBar2Cheb: A is not finite!");
-            ap.assert(math.isfinite(b), "PolynomialBar2Cheb: B is not finite!");
-            ap.assert((double)(a)!=(double)(b), "PolynomialBar2Cheb: A=B!");
-            ap.assert(p.n>0, "PolynomialBar2Cheb: P is not correctly initialized barycentric interpolant!");
-            
-            //
-            // Calculate function values on a Chebyshev grid
-            //
-            vp = new double[p.n];
-            vx = new double[p.n];
-            for(i=0; i<=p.n-1; i++)
-            {
-                vx[i] = Math.Cos(Math.PI*(i+0.5)/p.n);
-                vp[i] = ratint.barycentriccalc(p, 0.5*(vx[i]+1)*(b-a)+a);
-            }
-            
-            //
-            // T[0]
-            //
-            t = new double[p.n];
-            v = 0;
-            for(i=0; i<=p.n-1; i++)
-            {
-                v = v+vp[i];
-            }
-            t[0] = v/p.n;
-            
-            //
-            // other T's.
-            //
-            // NOTES:
-            // 1. TK stores T{k} on VX, TK1 stores T{k-1} on VX
-            // 2. we can do same calculations with fast DCT, but it
-            //    * adds dependencies
-            //    * still leaves us with O(N^2) algorithm because
-            //      preparation of function values is O(N^2) process
-            //
-            if( p.n>1 )
-            {
-                tk = new double[p.n];
-                tk1 = new double[p.n];
-                for(i=0; i<=p.n-1; i++)
-                {
-                    tk[i] = vx[i];
-                    tk1[i] = 1;
-                }
-                for(k=1; k<=p.n-1; k++)
-                {
-                    
-                    //
-                    // calculate discrete product of function vector and TK
-                    //
-                    v = 0.0;
-                    for(i_=0; i_<=p.n-1;i_++)
-                    {
-                        v += tk[i_]*vp[i_];
-                    }
-                    t[k] = v/(0.5*p.n);
-                    
-                    //
-                    // Update TK and TK1
-                    //
-                    for(i=0; i<=p.n-1; i++)
-                    {
-                        v = 2*vx[i]*tk[i]-tk1[i];
-                        tk1[i] = tk[i];
-                        tk[i] = v;
-                    }
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Conversion from Chebyshev basis to barycentric representation.
-        This function has O(N^2) complexity.
-
-        INPUT PARAMETERS:
-            T   -   coefficients of Chebyshev representation;
-                    P(x) = sum { T[i]*Ti(2*(x-A)/(B-A)-1), i=0..N },
-                    where Ti - I-th Chebyshev polynomial.
-            N   -   number of coefficients:
-                    * if given, only leading N elements of T are used
-                    * if not given, automatically determined from size of T
-            A,B -   base interval for Chebyshev polynomials (see above)
-                    A<B
-
-        OUTPUT PARAMETERS
-            P   -   polynomial in barycentric form
-
-          -- ALGLIB --
-             Copyright 30.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialcheb2bar(double[] t,
-            int n,
-            double a,
-            double b,
-            ratint.barycentricinterpolant p)
-        {
-            int i = 0;
-            int k = 0;
-            double[] y = new double[0];
-            double tk = 0;
-            double tk1 = 0;
-            double vx = 0;
-            double vy = 0;
-            double v = 0;
-
-            ap.assert(math.isfinite(a), "PolynomialBar2Cheb: A is not finite!");
-            ap.assert(math.isfinite(b), "PolynomialBar2Cheb: B is not finite!");
-            ap.assert((double)(a)!=(double)(b), "PolynomialBar2Cheb: A=B!");
-            ap.assert(n>=1, "PolynomialBar2Cheb: N<1");
-            ap.assert(ap.len(t)>=n, "PolynomialBar2Cheb: Length(T)<N");
-            ap.assert(apserv.isfinitevector(t, n), "PolynomialBar2Cheb: T[] contains INF or NAN");
-            
-            //
-            // Calculate function values on a Chebyshev grid spanning [-1,+1]
-            //
-            y = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                
-                //
-                // Calculate value on a grid spanning [-1,+1]
-                //
-                vx = Math.Cos(Math.PI*(i+0.5)/n);
-                vy = t[0];
-                tk1 = 1;
-                tk = vx;
-                for(k=1; k<=n-1; k++)
-                {
-                    vy = vy+t[k]*tk;
-                    v = 2*vx*tk-tk1;
-                    tk1 = tk;
-                    tk = v;
-                }
-                y[i] = vy;
-            }
-            
-            //
-            // Build barycentric interpolant, map grid from [-1,+1] to [A,B]
-            //
-            polynomialbuildcheb1(a, b, y, n, p);
-        }
-
-
-        /*************************************************************************
-        Conversion from barycentric representation to power basis.
-        This function has O(N^2) complexity.
-
-        INPUT PARAMETERS:
-            P   -   polynomial in barycentric form
-            C   -   offset (see below); 0.0 is used as default value.
-            S   -   scale (see below);  1.0 is used as default value. S<>0.
-
-        OUTPUT PARAMETERS
-            A   -   coefficients, P(x) = sum { A[i]*((X-C)/S)^i, i=0..N-1 }
-            N   -   number of coefficients (polynomial degree plus 1)
-
-        NOTES:
-        1.  this function accepts offset and scale, which can be  set  to  improve
-            numerical properties of polynomial. For example, if P was obtained  as
-            result of interpolation on [-1,+1],  you  can  set  C=0  and  S=1  and
-            represent  P  as sum of 1, x, x^2, x^3 and so on. In most cases you it
-            is exactly what you need.
-
-            However, if your interpolation model was built on [999,1001], you will
-            see significant growth of numerical errors when using {1, x, x^2, x^3}
-            as basis. Representing P as sum of 1, (x-1000), (x-1000)^2, (x-1000)^3
-            will be better option. Such representation can be  obtained  by  using
-            1000.0 as offset C and 1.0 as scale S.
-
-        2.  power basis is ill-conditioned and tricks described above can't  solve
-            this problem completely. This function  will  return  coefficients  in
-            any  case,  but  for  N>8  they  will  become unreliable. However, N's
-            less than 5 are pretty safe.
-            
-        3.  barycentric interpolant passed as P may be either polynomial  obtained
-            from  polynomial  interpolation/ fitting or rational function which is
-            NOT polynomial. We can't distinguish between these two cases, and this
-            algorithm just tries to work assuming that P IS a polynomial.  If not,
-            algorithm will return results, but they won't have any meaning.
-
-          -- ALGLIB --
-             Copyright 30.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialbar2pow(ratint.barycentricinterpolant p,
-            double c,
-            double s,
-            ref double[] a)
-        {
-            int i = 0;
-            int k = 0;
-            double e = 0;
-            double d = 0;
-            double[] vp = new double[0];
-            double[] vx = new double[0];
-            double[] tk = new double[0];
-            double[] tk1 = new double[0];
-            double[] t = new double[0];
-            double v = 0;
-            int i_ = 0;
-
-            a = new double[0];
-
-            ap.assert(math.isfinite(c), "PolynomialBar2Pow: C is not finite!");
-            ap.assert(math.isfinite(s), "PolynomialBar2Pow: S is not finite!");
-            ap.assert((double)(s)!=(double)(0), "PolynomialBar2Pow: S=0!");
-            ap.assert(p.n>0, "PolynomialBar2Pow: P is not correctly initialized barycentric interpolant!");
-            
-            //
-            // Calculate function values on a Chebyshev grid
-            //
-            vp = new double[p.n];
-            vx = new double[p.n];
-            for(i=0; i<=p.n-1; i++)
-            {
-                vx[i] = Math.Cos(Math.PI*(i+0.5)/p.n);
-                vp[i] = ratint.barycentriccalc(p, s*vx[i]+c);
-            }
-            
-            //
-            // T[0]
-            //
-            t = new double[p.n];
-            v = 0;
-            for(i=0; i<=p.n-1; i++)
-            {
-                v = v+vp[i];
-            }
-            t[0] = v/p.n;
-            
-            //
-            // other T's.
-            //
-            // NOTES:
-            // 1. TK stores T{k} on VX, TK1 stores T{k-1} on VX
-            // 2. we can do same calculations with fast DCT, but it
-            //    * adds dependencies
-            //    * still leaves us with O(N^2) algorithm because
-            //      preparation of function values is O(N^2) process
-            //
-            if( p.n>1 )
-            {
-                tk = new double[p.n];
-                tk1 = new double[p.n];
-                for(i=0; i<=p.n-1; i++)
-                {
-                    tk[i] = vx[i];
-                    tk1[i] = 1;
-                }
-                for(k=1; k<=p.n-1; k++)
-                {
-                    
-                    //
-                    // calculate discrete product of function vector and TK
-                    //
-                    v = 0.0;
-                    for(i_=0; i_<=p.n-1;i_++)
-                    {
-                        v += tk[i_]*vp[i_];
-                    }
-                    t[k] = v/(0.5*p.n);
-                    
-                    //
-                    // Update TK and TK1
-                    //
-                    for(i=0; i<=p.n-1; i++)
-                    {
-                        v = 2*vx[i]*tk[i]-tk1[i];
-                        tk1[i] = tk[i];
-                        tk[i] = v;
-                    }
-                }
-            }
-            
-            //
-            // Convert from Chebyshev basis to power basis
-            //
-            a = new double[p.n];
-            for(i=0; i<=p.n-1; i++)
-            {
-                a[i] = 0;
-            }
-            d = 0;
-            for(i=0; i<=p.n-1; i++)
-            {
-                for(k=i; k<=p.n-1; k++)
-                {
-                    e = a[k];
-                    a[k] = 0;
-                    if( i<=1 & k==i )
-                    {
-                        a[k] = 1;
-                    }
-                    else
-                    {
-                        if( i!=0 )
-                        {
-                            a[k] = 2*d;
-                        }
-                        if( k>i+1 )
-                        {
-                            a[k] = a[k]-a[k-2];
-                        }
-                    }
-                    d = e;
-                }
-                d = a[i];
-                e = 0;
-                k = i;
-                while( k<=p.n-1 )
-                {
-                    e = e+a[k]*t[k];
-                    k = k+2;
-                }
-                a[i] = e;
-            }
-        }
-
-
-        /*************************************************************************
-        Conversion from power basis to barycentric representation.
-        This function has O(N^2) complexity.
-
-        INPUT PARAMETERS:
-            A   -   coefficients, P(x) = sum { A[i]*((X-C)/S)^i, i=0..N-1 }
-            N   -   number of coefficients (polynomial degree plus 1)
-                    * if given, only leading N elements of A are used
-                    * if not given, automatically determined from size of A
-            C   -   offset (see below); 0.0 is used as default value.
-            S   -   scale (see below);  1.0 is used as default value. S<>0.
-
-        OUTPUT PARAMETERS
-            P   -   polynomial in barycentric form
-
-
-        NOTES:
-        1.  this function accepts offset and scale, which can be  set  to  improve
-            numerical properties of polynomial. For example, if you interpolate on
-            [-1,+1],  you  can  set C=0 and S=1 and convert from sum of 1, x, x^2,
-            x^3 and so on. In most cases you it is exactly what you need.
-
-            However, if your interpolation model was built on [999,1001], you will
-            see significant growth of numerical errors when using {1, x, x^2, x^3}
-            as  input  basis.  Converting  from  sum  of  1, (x-1000), (x-1000)^2,
-            (x-1000)^3 will be better option (you have to specify 1000.0 as offset
-            C and 1.0 as scale S).
-
-        2.  power basis is ill-conditioned and tricks described above can't  solve
-            this problem completely. This function  will  return barycentric model
-            in any case, but for N>8 accuracy well degrade. However, N's less than
-            5 are pretty safe.
-
-          -- ALGLIB --
-             Copyright 30.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialpow2bar(double[] a,
-            int n,
-            double c,
-            double s,
-            ratint.barycentricinterpolant p)
-        {
-            int i = 0;
-            int k = 0;
-            double[] y = new double[0];
-            double vx = 0;
-            double vy = 0;
-            double px = 0;
-
-            ap.assert(math.isfinite(c), "PolynomialPow2Bar: C is not finite!");
-            ap.assert(math.isfinite(s), "PolynomialPow2Bar: S is not finite!");
-            ap.assert((double)(s)!=(double)(0), "PolynomialPow2Bar: S is zero!");
-            ap.assert(n>=1, "PolynomialPow2Bar: N<1");
-            ap.assert(ap.len(a)>=n, "PolynomialPow2Bar: Length(A)<N");
-            ap.assert(apserv.isfinitevector(a, n), "PolynomialPow2Bar: A[] contains INF or NAN");
-            
-            //
-            // Calculate function values on a Chebyshev grid spanning [-1,+1]
-            //
-            y = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                
-                //
-                // Calculate value on a grid spanning [-1,+1]
-                //
-                vx = Math.Cos(Math.PI*(i+0.5)/n);
-                vy = a[0];
-                px = vx;
-                for(k=1; k<=n-1; k++)
-                {
-                    vy = vy+px*a[k];
-                    px = px*vx;
-                }
-                y[i] = vy;
-            }
-            
-            //
-            // Build barycentric interpolant, map grid from [-1,+1] to [A,B]
-            //
-            polynomialbuildcheb1(c-s, c+s, y, n, p);
-        }
-
-
-        /*************************************************************************
-        Lagrange intepolant: generation of the model on the general grid.
-        This function has O(N^2) complexity.
-
-        INPUT PARAMETERS:
-            X   -   abscissas, array[0..N-1]
-            Y   -   function values, array[0..N-1]
-            N   -   number of points, N>=1
-
-        OUTPUT PARAMETERS
-            P   -   barycentric model which represents Lagrange interpolant
-                    (see ratint unit info and BarycentricCalc() description for
-                    more information).
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialbuild(double[] x,
-            double[] y,
-            int n,
-            ratint.barycentricinterpolant p)
-        {
-            int j = 0;
-            int k = 0;
-            double[] w = new double[0];
-            double b = 0;
-            double a = 0;
-            double v = 0;
-            double mx = 0;
-            double[] sortrbuf = new double[0];
-            double[] sortrbuf2 = new double[0];
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-
-            ap.assert(n>0, "PolynomialBuild: N<=0!");
-            ap.assert(ap.len(x)>=n, "PolynomialBuild: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "PolynomialBuild: Length(Y)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "PolynomialBuild: X contains infinite or NaN values!");
-            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuild: Y contains infinite or NaN values!");
-            tsort.tagsortfastr(ref x, ref y, ref sortrbuf, ref sortrbuf2, n);
-            ap.assert(apserv.aredistinct(x, n), "PolynomialBuild: at least two consequent points are too close!");
-            
-            //
-            // calculate W[j]
-            // multi-pass algorithm is used to avoid overflow
-            //
-            w = new double[n];
-            a = x[0];
-            b = x[0];
-            for(j=0; j<=n-1; j++)
-            {
-                w[j] = 1;
-                a = Math.Min(a, x[j]);
-                b = Math.Max(b, x[j]);
-            }
-            for(k=0; k<=n-1; k++)
-            {
-                
-                //
-                // W[K] is used instead of 0.0 because
-                // cycle on J does not touch K-th element
-                // and we MUST get maximum from ALL elements
-                //
-                mx = Math.Abs(w[k]);
-                for(j=0; j<=n-1; j++)
-                {
-                    if( j!=k )
-                    {
-                        v = (b-a)/(x[j]-x[k]);
-                        w[j] = w[j]*v;
-                        mx = Math.Max(mx, Math.Abs(w[j]));
-                    }
-                }
-                if( k%5==0 )
-                {
-                    
-                    //
-                    // every 5-th run we renormalize W[]
-                    //
-                    v = 1/mx;
-                    for(i_=0; i_<=n-1;i_++)
-                    {
-                        w[i_] = v*w[i_];
-                    }
-                }
-            }
-            ratint.barycentricbuildxyw(x, y, w, n, p);
-        }
-
-
-        /*************************************************************************
-        Lagrange intepolant: generation of the model on equidistant grid.
-        This function has O(N) complexity.
-
-        INPUT PARAMETERS:
-            A   -   left boundary of [A,B]
-            B   -   right boundary of [A,B]
-            Y   -   function values at the nodes, array[0..N-1]
-            N   -   number of points, N>=1
-                    for N=1 a constant model is constructed.
-
-        OUTPUT PARAMETERS
-            P   -   barycentric model which represents Lagrange interpolant
-                    (see ratint unit info and BarycentricCalc() description for
-                    more information).
-
-          -- ALGLIB --
-             Copyright 03.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialbuildeqdist(double a,
-            double b,
-            double[] y,
-            int n,
-            ratint.barycentricinterpolant p)
-        {
-            int i = 0;
-            double[] w = new double[0];
-            double[] x = new double[0];
-            double v = 0;
-
-            ap.assert(n>0, "PolynomialBuildEqDist: N<=0!");
-            ap.assert(ap.len(y)>=n, "PolynomialBuildEqDist: Length(Y)<N!");
-            ap.assert(math.isfinite(a), "PolynomialBuildEqDist: A is infinite or NaN!");
-            ap.assert(math.isfinite(b), "PolynomialBuildEqDist: B is infinite or NaN!");
-            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuildEqDist: Y contains infinite or NaN values!");
-            ap.assert((double)(b)!=(double)(a), "PolynomialBuildEqDist: B=A!");
-            ap.assert((double)(a+(b-a)/n)!=(double)(a), "PolynomialBuildEqDist: B is too close to A!");
-            
-            //
-            // Special case: N=1
-            //
-            if( n==1 )
-            {
-                x = new double[1];
-                w = new double[1];
-                x[0] = 0.5*(b+a);
-                w[0] = 1;
-                ratint.barycentricbuildxyw(x, y, w, 1, p);
-                return;
-            }
-            
-            //
-            // general case
-            //
-            x = new double[n];
-            w = new double[n];
-            v = 1;
-            for(i=0; i<=n-1; i++)
-            {
-                w[i] = v;
-                x[i] = a+(b-a)*i/(n-1);
-                v = -(v*(n-1-i));
-                v = v/(i+1);
-            }
-            ratint.barycentricbuildxyw(x, y, w, n, p);
-        }
-
-
-        /*************************************************************************
-        Lagrange intepolant on Chebyshev grid (first kind).
-        This function has O(N) complexity.
-
-        INPUT PARAMETERS:
-            A   -   left boundary of [A,B]
-            B   -   right boundary of [A,B]
-            Y   -   function values at the nodes, array[0..N-1],
-                    Y[I] = Y(0.5*(B+A) + 0.5*(B-A)*Cos(PI*(2*i+1)/(2*n)))
-            N   -   number of points, N>=1
-                    for N=1 a constant model is constructed.
-
-        OUTPUT PARAMETERS
-            P   -   barycentric model which represents Lagrange interpolant
-                    (see ratint unit info and BarycentricCalc() description for
-                    more information).
-
-          -- ALGLIB --
-             Copyright 03.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialbuildcheb1(double a,
-            double b,
-            double[] y,
-            int n,
-            ratint.barycentricinterpolant p)
-        {
-            int i = 0;
-            double[] w = new double[0];
-            double[] x = new double[0];
-            double v = 0;
-            double t = 0;
-
-            ap.assert(n>0, "PolynomialBuildCheb1: N<=0!");
-            ap.assert(ap.len(y)>=n, "PolynomialBuildCheb1: Length(Y)<N!");
-            ap.assert(math.isfinite(a), "PolynomialBuildCheb1: A is infinite or NaN!");
-            ap.assert(math.isfinite(b), "PolynomialBuildCheb1: B is infinite or NaN!");
-            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuildCheb1: Y contains infinite or NaN values!");
-            ap.assert((double)(b)!=(double)(a), "PolynomialBuildCheb1: B=A!");
-            
-            //
-            // Special case: N=1
-            //
-            if( n==1 )
-            {
-                x = new double[1];
-                w = new double[1];
-                x[0] = 0.5*(b+a);
-                w[0] = 1;
-                ratint.barycentricbuildxyw(x, y, w, 1, p);
-                return;
-            }
-            
-            //
-            // general case
-            //
-            x = new double[n];
-            w = new double[n];
-            v = 1;
-            for(i=0; i<=n-1; i++)
-            {
-                t = Math.Tan(0.5*Math.PI*(2*i+1)/(2*n));
-                w[i] = 2*v*t/(1+math.sqr(t));
-                x[i] = 0.5*(b+a)+0.5*(b-a)*(1-math.sqr(t))/(1+math.sqr(t));
-                v = -v;
-            }
-            ratint.barycentricbuildxyw(x, y, w, n, p);
-        }
-
-
-        /*************************************************************************
-        Lagrange intepolant on Chebyshev grid (second kind).
-        This function has O(N) complexity.
-
-        INPUT PARAMETERS:
-            A   -   left boundary of [A,B]
-            B   -   right boundary of [A,B]
-            Y   -   function values at the nodes, array[0..N-1],
-                    Y[I] = Y(0.5*(B+A) + 0.5*(B-A)*Cos(PI*i/(n-1)))
-            N   -   number of points, N>=1
-                    for N=1 a constant model is constructed.
-
-        OUTPUT PARAMETERS
-            P   -   barycentric model which represents Lagrange interpolant
-                    (see ratint unit info and BarycentricCalc() description for
-                    more information).
-
-          -- ALGLIB --
-             Copyright 03.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void polynomialbuildcheb2(double a,
-            double b,
-            double[] y,
-            int n,
-            ratint.barycentricinterpolant p)
-        {
-            int i = 0;
-            double[] w = new double[0];
-            double[] x = new double[0];
-            double v = 0;
-
-            ap.assert(n>0, "PolynomialBuildCheb2: N<=0!");
-            ap.assert(ap.len(y)>=n, "PolynomialBuildCheb2: Length(Y)<N!");
-            ap.assert(math.isfinite(a), "PolynomialBuildCheb2: A is infinite or NaN!");
-            ap.assert(math.isfinite(b), "PolynomialBuildCheb2: B is infinite or NaN!");
-            ap.assert((double)(b)!=(double)(a), "PolynomialBuildCheb2: B=A!");
-            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuildCheb2: Y contains infinite or NaN values!");
-            
-            //
-            // Special case: N=1
-            //
-            if( n==1 )
-            {
-                x = new double[1];
-                w = new double[1];
-                x[0] = 0.5*(b+a);
-                w[0] = 1;
-                ratint.barycentricbuildxyw(x, y, w, 1, p);
-                return;
-            }
-            
-            //
-            // general case
-            //
-            x = new double[n];
-            w = new double[n];
-            v = 1;
-            for(i=0; i<=n-1; i++)
-            {
-                if( i==0 | i==n-1 )
-                {
-                    w[i] = v*0.5;
-                }
-                else
-                {
-                    w[i] = v;
-                }
-                x[i] = 0.5*(b+a)+0.5*(b-a)*Math.Cos(Math.PI*i/(n-1));
-                v = -v;
-            }
-            ratint.barycentricbuildxyw(x, y, w, n, p);
-        }
-
-
-        /*************************************************************************
-        Fast equidistant polynomial interpolation function with O(N) complexity
-
-        INPUT PARAMETERS:
-            A   -   left boundary of [A,B]
-            B   -   right boundary of [A,B]
-            F   -   function values, array[0..N-1]
-            N   -   number of points on equidistant grid, N>=1
-                    for N=1 a constant model is constructed.
-            T   -   position where P(x) is calculated
-
-        RESULT
-            value of the Lagrange interpolant at T
-            
-        IMPORTANT
-            this function provides fast interface which is not overflow-safe
-            nor it is very precise.
-            the best option is to use  PolynomialBuildEqDist()/BarycentricCalc()
-            subroutines unless you are pretty sure that your data will not result
-            in overflow.
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double polynomialcalceqdist(double a,
-            double b,
-            double[] f,
-            int n,
-            double t)
-        {
-            double result = 0;
-            double s1 = 0;
-            double s2 = 0;
-            double v = 0;
-            double threshold = 0;
-            double s = 0;
-            double h = 0;
-            int i = 0;
-            int j = 0;
-            double w = 0;
-            double x = 0;
-
-            ap.assert(n>0, "PolynomialCalcEqDist: N<=0!");
-            ap.assert(ap.len(f)>=n, "PolynomialCalcEqDist: Length(F)<N!");
-            ap.assert(math.isfinite(a), "PolynomialCalcEqDist: A is infinite or NaN!");
-            ap.assert(math.isfinite(b), "PolynomialCalcEqDist: B is infinite or NaN!");
-            ap.assert(apserv.isfinitevector(f, n), "PolynomialCalcEqDist: F contains infinite or NaN values!");
-            ap.assert((double)(b)!=(double)(a), "PolynomialCalcEqDist: B=A!");
-            ap.assert(!Double.IsInfinity(t), "PolynomialCalcEqDist: T is infinite!");
-            
-            //
-            // Special case: T is NAN
-            //
-            if( Double.IsNaN(t) )
-            {
-                result = Double.NaN;
-                return result;
-            }
-            
-            //
-            // Special case: N=1
-            //
-            if( n==1 )
-            {
-                result = f[0];
-                return result;
-            }
-            
-            //
-            // First, decide: should we use "safe" formula (guarded
-            // against overflow) or fast one?
-            //
-            threshold = Math.Sqrt(math.minrealnumber);
-            j = 0;
-            s = t-a;
-            for(i=1; i<=n-1; i++)
-            {
-                x = a+(double)i/(double)(n-1)*(b-a);
-                if( (double)(Math.Abs(t-x))<(double)(Math.Abs(s)) )
-                {
-                    s = t-x;
-                    j = i;
-                }
-            }
-            if( (double)(s)==(double)(0) )
-            {
-                result = f[j];
-                return result;
-            }
-            if( (double)(Math.Abs(s))>(double)(threshold) )
-            {
-                
-                //
-                // use fast formula
-                //
-                j = -1;
-                s = 1.0;
-            }
-            
-            //
-            // Calculate using safe or fast barycentric formula
-            //
-            s1 = 0;
-            s2 = 0;
-            w = 1.0;
-            h = (b-a)/(n-1);
-            for(i=0; i<=n-1; i++)
-            {
-                if( i!=j )
-                {
-                    v = s*w/(t-(a+i*h));
-                    s1 = s1+v*f[i];
-                    s2 = s2+v;
-                }
-                else
-                {
-                    v = w;
-                    s1 = s1+v*f[i];
-                    s2 = s2+v;
-                }
-                w = -(w*(n-1-i));
-                w = w/(i+1);
-            }
-            result = s1/s2;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Fast polynomial interpolation function on Chebyshev points (first kind)
-        with O(N) complexity.
-
-        INPUT PARAMETERS:
-            A   -   left boundary of [A,B]
-            B   -   right boundary of [A,B]
-            F   -   function values, array[0..N-1]
-            N   -   number of points on Chebyshev grid (first kind),
-                    X[i] = 0.5*(B+A) + 0.5*(B-A)*Cos(PI*(2*i+1)/(2*n))
-                    for N=1 a constant model is constructed.
-            T   -   position where P(x) is calculated
-
-        RESULT
-            value of the Lagrange interpolant at T
-
-        IMPORTANT
-            this function provides fast interface which is not overflow-safe
-            nor it is very precise.
-            the best option is to use  PolIntBuildCheb1()/BarycentricCalc()
-            subroutines unless you are pretty sure that your data will not result
-            in overflow.
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double polynomialcalccheb1(double a,
-            double b,
-            double[] f,
-            int n,
-            double t)
-        {
-            double result = 0;
-            double s1 = 0;
-            double s2 = 0;
-            double v = 0;
-            double threshold = 0;
-            double s = 0;
-            int i = 0;
-            int j = 0;
-            double a0 = 0;
-            double delta = 0;
-            double alpha = 0;
-            double beta = 0;
-            double ca = 0;
-            double sa = 0;
-            double tempc = 0;
-            double temps = 0;
-            double x = 0;
-            double w = 0;
-            double p1 = 0;
-
-            ap.assert(n>0, "PolynomialCalcCheb1: N<=0!");
-            ap.assert(ap.len(f)>=n, "PolynomialCalcCheb1: Length(F)<N!");
-            ap.assert(math.isfinite(a), "PolynomialCalcCheb1: A is infinite or NaN!");
-            ap.assert(math.isfinite(b), "PolynomialCalcCheb1: B is infinite or NaN!");
-            ap.assert(apserv.isfinitevector(f, n), "PolynomialCalcCheb1: F contains infinite or NaN values!");
-            ap.assert((double)(b)!=(double)(a), "PolynomialCalcCheb1: B=A!");
-            ap.assert(!Double.IsInfinity(t), "PolynomialCalcCheb1: T is infinite!");
-            
-            //
-            // Special case: T is NAN
-            //
-            if( Double.IsNaN(t) )
-            {
-                result = Double.NaN;
-                return result;
-            }
-            
-            //
-            // Special case: N=1
-            //
-            if( n==1 )
-            {
-                result = f[0];
-                return result;
-            }
-            
-            //
-            // Prepare information for the recurrence formula
-            // used to calculate sin(pi*(2j+1)/(2n+2)) and
-            // cos(pi*(2j+1)/(2n+2)):
-            //
-            // A0    = pi/(2n+2)
-            // Delta = pi/(n+1)
-            // Alpha = 2 sin^2 (Delta/2)
-            // Beta  = sin(Delta)
-            //
-            // so that sin(..) = sin(A0+j*delta) and cos(..) = cos(A0+j*delta).
-            // Then we use
-            //
-            // sin(x+delta) = sin(x) - (alpha*sin(x) - beta*cos(x))
-            // cos(x+delta) = cos(x) - (alpha*cos(x) - beta*sin(x))
-            //
-            // to repeatedly calculate sin(..) and cos(..).
-            //
-            threshold = Math.Sqrt(math.minrealnumber);
-            t = (t-0.5*(a+b))/(0.5*(b-a));
-            a0 = Math.PI/(2*(n-1)+2);
-            delta = 2*Math.PI/(2*(n-1)+2);
-            alpha = 2*math.sqr(Math.Sin(delta/2));
-            beta = Math.Sin(delta);
-            
-            //
-            // First, decide: should we use "safe" formula (guarded
-            // against overflow) or fast one?
-            //
-            ca = Math.Cos(a0);
-            sa = Math.Sin(a0);
-            j = 0;
-            x = ca;
-            s = t-x;
-            for(i=1; i<=n-1; i++)
-            {
-                
-                //
-                // Next X[i]
-                //
-                temps = sa-(alpha*sa-beta*ca);
-                tempc = ca-(alpha*ca+beta*sa);
-                sa = temps;
-                ca = tempc;
-                x = ca;
-                
-                //
-                // Use X[i]
-                //
-                if( (double)(Math.Abs(t-x))<(double)(Math.Abs(s)) )
-                {
-                    s = t-x;
-                    j = i;
-                }
-            }
-            if( (double)(s)==(double)(0) )
-            {
-                result = f[j];
-                return result;
-            }
-            if( (double)(Math.Abs(s))>(double)(threshold) )
-            {
-                
-                //
-                // use fast formula
-                //
-                j = -1;
-                s = 1.0;
-            }
-            
-            //
-            // Calculate using safe or fast barycentric formula
-            //
-            s1 = 0;
-            s2 = 0;
-            ca = Math.Cos(a0);
-            sa = Math.Sin(a0);
-            p1 = 1.0;
-            for(i=0; i<=n-1; i++)
-            {
-                
-                //
-                // Calculate X[i], W[i]
-                //
-                x = ca;
-                w = p1*sa;
-                
-                //
-                // Proceed
-                //
-                if( i!=j )
-                {
-                    v = s*w/(t-x);
-                    s1 = s1+v*f[i];
-                    s2 = s2+v;
-                }
-                else
-                {
-                    v = w;
-                    s1 = s1+v*f[i];
-                    s2 = s2+v;
-                }
-                
-                //
-                // Next CA, SA, P1
-                //
-                temps = sa-(alpha*sa-beta*ca);
-                tempc = ca-(alpha*ca+beta*sa);
-                sa = temps;
-                ca = tempc;
-                p1 = -p1;
-            }
-            result = s1/s2;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Fast polynomial interpolation function on Chebyshev points (second kind)
-        with O(N) complexity.
-
-        INPUT PARAMETERS:
-            A   -   left boundary of [A,B]
-            B   -   right boundary of [A,B]
-            F   -   function values, array[0..N-1]
-            N   -   number of points on Chebyshev grid (second kind),
-                    X[i] = 0.5*(B+A) + 0.5*(B-A)*Cos(PI*i/(n-1))
-                    for N=1 a constant model is constructed.
-            T   -   position where P(x) is calculated
-
-        RESULT
-            value of the Lagrange interpolant at T
-
-        IMPORTANT
-            this function provides fast interface which is not overflow-safe
-            nor it is very precise.
-            the best option is to use PolIntBuildCheb2()/BarycentricCalc()
-            subroutines unless you are pretty sure that your data will not result
-            in overflow.
-
-          -- ALGLIB --
-             Copyright 02.12.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static double polynomialcalccheb2(double a,
-            double b,
-            double[] f,
-            int n,
-            double t)
-        {
-            double result = 0;
-            double s1 = 0;
-            double s2 = 0;
-            double v = 0;
-            double threshold = 0;
-            double s = 0;
-            int i = 0;
-            int j = 0;
-            double a0 = 0;
-            double delta = 0;
-            double alpha = 0;
-            double beta = 0;
-            double ca = 0;
-            double sa = 0;
-            double tempc = 0;
-            double temps = 0;
-            double x = 0;
-            double w = 0;
-            double p1 = 0;
-
-            ap.assert(n>0, "PolynomialCalcCheb2: N<=0!");
-            ap.assert(ap.len(f)>=n, "PolynomialCalcCheb2: Length(F)<N!");
-            ap.assert(math.isfinite(a), "PolynomialCalcCheb2: A is infinite or NaN!");
-            ap.assert(math.isfinite(b), "PolynomialCalcCheb2: B is infinite or NaN!");
-            ap.assert((double)(b)!=(double)(a), "PolynomialCalcCheb2: B=A!");
-            ap.assert(apserv.isfinitevector(f, n), "PolynomialCalcCheb2: F contains infinite or NaN values!");
-            ap.assert(!Double.IsInfinity(t), "PolynomialCalcEqDist: T is infinite!");
-            
-            //
-            // Special case: T is NAN
-            //
-            if( Double.IsNaN(t) )
-            {
-                result = Double.NaN;
-                return result;
-            }
-            
-            //
-            // Special case: N=1
-            //
-            if( n==1 )
-            {
-                result = f[0];
-                return result;
-            }
-            
-            //
-            // Prepare information for the recurrence formula
-            // used to calculate sin(pi*i/n) and
-            // cos(pi*i/n):
-            //
-            // A0    = 0
-            // Delta = pi/n
-            // Alpha = 2 sin^2 (Delta/2)
-            // Beta  = sin(Delta)
-            //
-            // so that sin(..) = sin(A0+j*delta) and cos(..) = cos(A0+j*delta).
-            // Then we use
-            //
-            // sin(x+delta) = sin(x) - (alpha*sin(x) - beta*cos(x))
-            // cos(x+delta) = cos(x) - (alpha*cos(x) - beta*sin(x))
-            //
-            // to repeatedly calculate sin(..) and cos(..).
-            //
-            threshold = Math.Sqrt(math.minrealnumber);
-            t = (t-0.5*(a+b))/(0.5*(b-a));
-            a0 = 0.0;
-            delta = Math.PI/(n-1);
-            alpha = 2*math.sqr(Math.Sin(delta/2));
-            beta = Math.Sin(delta);
-            
-            //
-            // First, decide: should we use "safe" formula (guarded
-            // against overflow) or fast one?
-            //
-            ca = Math.Cos(a0);
-            sa = Math.Sin(a0);
-            j = 0;
-            x = ca;
-            s = t-x;
-            for(i=1; i<=n-1; i++)
-            {
-                
-                //
-                // Next X[i]
-                //
-                temps = sa-(alpha*sa-beta*ca);
-                tempc = ca-(alpha*ca+beta*sa);
-                sa = temps;
-                ca = tempc;
-                x = ca;
-                
-                //
-                // Use X[i]
-                //
-                if( (double)(Math.Abs(t-x))<(double)(Math.Abs(s)) )
-                {
-                    s = t-x;
-                    j = i;
-                }
-            }
-            if( (double)(s)==(double)(0) )
-            {
-                result = f[j];
-                return result;
-            }
-            if( (double)(Math.Abs(s))>(double)(threshold) )
-            {
-                
-                //
-                // use fast formula
-                //
-                j = -1;
-                s = 1.0;
-            }
-            
-            //
-            // Calculate using safe or fast barycentric formula
-            //
-            s1 = 0;
-            s2 = 0;
-            ca = Math.Cos(a0);
-            sa = Math.Sin(a0);
-            p1 = 1.0;
-            for(i=0; i<=n-1; i++)
-            {
-                
-                //
-                // Calculate X[i], W[i]
-                //
-                x = ca;
-                if( i==0 | i==n-1 )
-                {
-                    w = 0.5*p1;
-                }
-                else
-                {
-                    w = 1.0*p1;
-                }
-                
-                //
-                // Proceed
-                //
-                if( i!=j )
-                {
-                    v = s*w/(t-x);
-                    s1 = s1+v*f[i];
-                    s2 = s2+v;
-                }
-                else
-                {
-                    v = w;
-                    s1 = s1+v*f[i];
-                    s2 = s2+v;
-                }
-                
-                //
-                // Next CA, SA, P1
-                //
-                temps = sa-(alpha*sa-beta*ca);
-                tempc = ca-(alpha*ca+beta*sa);
-                sa = temps;
-                ca = tempc;
-                p1 = -p1;
-            }
-            result = s1/s2;
-            return result;
-        }
-
-
-    }
-    public class spline1d
-    {
-        /*************************************************************************
-        1-dimensional spline inteprolant
-        *************************************************************************/
-        public class spline1dinterpolant
-        {
-            public bool periodic;
-            public int n;
-            public int k;
-            public double[] x;
-            public double[] c;
-            public spline1dinterpolant()
-            {
-                x = new double[0];
-                c = new double[0];
-            }
-        };
-
-
-
-
-        /*************************************************************************
-        This subroutine builds linear spline interpolant
-
-        INPUT PARAMETERS:
-            X   -   spline nodes, array[0..N-1]
-            Y   -   function values, array[0..N-1]
-            N   -   points count (optional):
-                    * N>=2
-                    * if given, only first N points are used to build spline
-                    * if not given, automatically detected from X/Y sizes
-                      (len(X) must be equal to len(Y))
-            
-        OUTPUT PARAMETERS:
-            C   -   spline interpolant
-
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-
-          -- ALGLIB PROJECT --
-             Copyright 24.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dbuildlinear(double[] x,
-            double[] y,
-            int n,
-            spline1dinterpolant c)
-        {
-            int i = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-
-            ap.assert(n>1, "Spline1DBuildLinear: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DBuildLinear: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DBuildLinear: Length(Y)<N!");
-            
-            //
-            // check and sort points
-            //
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildLinear: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildLinear: Y contains infinite or NAN values!");
-            heapsortpoints(ref x, ref y, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildLinear: at least two consequent points are too close!");
-            
-            //
-            // Build
-            //
-            c.periodic = false;
-            c.n = n;
-            c.k = 3;
-            c.x = new double[n];
-            c.c = new double[4*(n-1)];
-            for(i=0; i<=n-1; i++)
-            {
-                c.x[i] = x[i];
-            }
-            for(i=0; i<=n-2; i++)
-            {
-                c.c[4*i+0] = y[i];
-                c.c[4*i+1] = (y[i+1]-y[i])/(x[i+1]-x[i]);
-                c.c[4*i+2] = 0;
-                c.c[4*i+3] = 0;
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine builds cubic spline interpolant.
-
-        INPUT PARAMETERS:
-            X           -   spline nodes, array[0..N-1].
-            Y           -   function values, array[0..N-1].
-            
-        OPTIONAL PARAMETERS:
-            N           -   points count:
-                            * N>=2
-                            * if given, only first N points are used to build spline
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-            BoundLType  -   boundary condition type for the left boundary
-            BoundL      -   left boundary condition (first or second derivative,
-                            depending on the BoundLType)
-            BoundRType  -   boundary condition type for the right boundary
-            BoundR      -   right boundary condition (first or second derivative,
-                            depending on the BoundRType)
-
-        OUTPUT PARAMETERS:
-            C           -   spline interpolant
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-
-        SETTING BOUNDARY VALUES:
-
-        The BoundLType/BoundRType parameters can have the following values:
-            * -1, which corresonds to the periodic (cyclic) boundary conditions.
-                  In this case:
-                  * both BoundLType and BoundRType must be equal to -1.
-                  * BoundL/BoundR are ignored
-                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
-            *  0, which  corresponds  to  the  parabolically   terminated  spline
-                  (BoundL and/or BoundR are ignored).
-            *  1, which corresponds to the first derivative boundary condition
-            *  2, which corresponds to the second derivative boundary condition
-            *  by default, BoundType=0 is used
-
-        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
-
-        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
-        However, this subroutine doesn't require you to specify equal  values  for
-        the first and last points - it automatically forces them  to  be  equal by
-        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
-        Y[last_point]. However it is recommended to pass consistent values of Y[],
-        i.e. to make Y[first_point]=Y[last_point].
-
-          -- ALGLIB PROJECT --
-             Copyright 23.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dbuildcubic(double[] x,
-            double[] y,
-            int n,
-            int boundltype,
-            double boundl,
-            int boundrtype,
-            double boundr,
-            spline1dinterpolant c)
-        {
-            double[] a1 = new double[0];
-            double[] a2 = new double[0];
-            double[] a3 = new double[0];
-            double[] b = new double[0];
-            double[] dt = new double[0];
-            double[] d = new double[0];
-            int[] p = new int[0];
-            int ylen = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-
-            
-            //
-            // check correctness of boundary conditions
-            //
-            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DBuildCubic: incorrect BoundLType!");
-            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DBuildCubic: incorrect BoundRType!");
-            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DBuildCubic: incorrect BoundLType/BoundRType!");
-            if( boundltype==1 | boundltype==2 )
-            {
-                ap.assert(math.isfinite(boundl), "Spline1DBuildCubic: BoundL is infinite or NAN!");
-            }
-            if( boundrtype==1 | boundrtype==2 )
-            {
-                ap.assert(math.isfinite(boundr), "Spline1DBuildCubic: BoundR is infinite or NAN!");
-            }
-            
-            //
-            // check lengths of arguments
-            //
-            ap.assert(n>=2, "Spline1DBuildCubic: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DBuildCubic: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DBuildCubic: Length(Y)<N!");
-            
-            //
-            // check and sort points
-            //
-            ylen = n;
-            if( boundltype==-1 )
-            {
-                ylen = n-1;
-            }
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildCubic: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DBuildCubic: Y contains infinite or NAN values!");
-            heapsortppoints(ref x, ref y, ref p, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildCubic: at least two consequent points are too close!");
-            
-            //
-            // Now we've checked and preordered everything,
-            // so we can call internal function to calculate derivatives,
-            // and then build Hermite spline using these derivatives
-            //
-            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
-            spline1dbuildhermite(x, y, d, n, c);
-            c.periodic = boundltype==-1 | boundrtype==-1;
-        }
-
-
-        /*************************************************************************
-        This function solves following problem: given table y[] of function values
-        at nodes x[], it calculates and returns table of function derivatives  d[]
-        (calculated at the same nodes x[]).
-
-        This function yields same result as Spline1DBuildCubic() call followed  by
-        sequence of Spline1DDiff() calls, but it can be several times faster  when
-        called for ordered X[] and X2[].
-
-        INPUT PARAMETERS:
-            X           -   spline nodes
-            Y           -   function values
-
-        OPTIONAL PARAMETERS:
-            N           -   points count:
-                            * N>=2
-                            * if given, only first N points are used
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-            BoundLType  -   boundary condition type for the left boundary
-            BoundL      -   left boundary condition (first or second derivative,
-                            depending on the BoundLType)
-            BoundRType  -   boundary condition type for the right boundary
-            BoundR      -   right boundary condition (first or second derivative,
-                            depending on the BoundRType)
-
-        OUTPUT PARAMETERS:
-            D           -   derivative values at X[]
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-        Derivative values are correctly reordered on return, so  D[I]  is  always
-        equal to S'(X[I]) independently of points order.
-
-        SETTING BOUNDARY VALUES:
-
-        The BoundLType/BoundRType parameters can have the following values:
-            * -1, which corresonds to the periodic (cyclic) boundary conditions.
-                  In this case:
-                  * both BoundLType and BoundRType must be equal to -1.
-                  * BoundL/BoundR are ignored
-                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
-            *  0, which  corresponds  to  the  parabolically   terminated  spline
-                  (BoundL and/or BoundR are ignored).
-            *  1, which corresponds to the first derivative boundary condition
-            *  2, which corresponds to the second derivative boundary condition
-            *  by default, BoundType=0 is used
-
-        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
-
-        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
-        However, this subroutine doesn't require you to specify equal  values  for
-        the first and last points - it automatically forces them  to  be  equal by
-        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
-        Y[last_point]. However it is recommended to pass consistent values of Y[],
-        i.e. to make Y[first_point]=Y[last_point].
-
-          -- ALGLIB PROJECT --
-             Copyright 03.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dgriddiffcubic(double[] x,
-            double[] y,
-            int n,
-            int boundltype,
-            double boundl,
-            int boundrtype,
-            double boundr,
-            ref double[] d)
-        {
-            double[] a1 = new double[0];
-            double[] a2 = new double[0];
-            double[] a3 = new double[0];
-            double[] b = new double[0];
-            double[] dt = new double[0];
-            int[] p = new int[0];
-            int i = 0;
-            int ylen = 0;
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            d = new double[0];
-
-            
-            //
-            // check correctness of boundary conditions
-            //
-            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DGridDiffCubic: incorrect BoundLType!");
-            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DGridDiffCubic: incorrect BoundRType!");
-            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DGridDiffCubic: incorrect BoundLType/BoundRType!");
-            if( boundltype==1 | boundltype==2 )
-            {
-                ap.assert(math.isfinite(boundl), "Spline1DGridDiffCubic: BoundL is infinite or NAN!");
-            }
-            if( boundrtype==1 | boundrtype==2 )
-            {
-                ap.assert(math.isfinite(boundr), "Spline1DGridDiffCubic: BoundR is infinite or NAN!");
-            }
-            
-            //
-            // check lengths of arguments
-            //
-            ap.assert(n>=2, "Spline1DGridDiffCubic: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DGridDiffCubic: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DGridDiffCubic: Length(Y)<N!");
-            
-            //
-            // check and sort points
-            //
-            ylen = n;
-            if( boundltype==-1 )
-            {
-                ylen = n-1;
-            }
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DGridDiffCubic: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DGridDiffCubic: Y contains infinite or NAN values!");
-            heapsortppoints(ref x, ref y, ref p, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DGridDiffCubic: at least two consequent points are too close!");
-            
-            //
-            // Now we've checked and preordered everything,
-            // so we can call internal function.
-            //
-            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
-            
-            //
-            // Remember that HeapSortPPoints() call?
-            // Now we have to reorder them back.
-            //
-            if( ap.len(dt)<n )
-            {
-                dt = new double[n];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                dt[p[i]] = d[i];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                d[i_] = dt[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        This function solves following problem: given table y[] of function values
-        at  nodes  x[],  it  calculates  and  returns  tables  of first and second
-        function derivatives d1[] and d2[] (calculated at the same nodes x[]).
-
-        This function yields same result as Spline1DBuildCubic() call followed  by
-        sequence of Spline1DDiff() calls, but it can be several times faster  when
-        called for ordered X[] and X2[].
-
-        INPUT PARAMETERS:
-            X           -   spline nodes
-            Y           -   function values
-
-        OPTIONAL PARAMETERS:
-            N           -   points count:
-                            * N>=2
-                            * if given, only first N points are used
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-            BoundLType  -   boundary condition type for the left boundary
-            BoundL      -   left boundary condition (first or second derivative,
-                            depending on the BoundLType)
-            BoundRType  -   boundary condition type for the right boundary
-            BoundR      -   right boundary condition (first or second derivative,
-                            depending on the BoundRType)
-
-        OUTPUT PARAMETERS:
-            D1          -   S' values at X[]
-            D2          -   S'' values at X[]
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-        Derivative values are correctly reordered on return, so  D[I]  is  always
-        equal to S'(X[I]) independently of points order.
-
-        SETTING BOUNDARY VALUES:
-
-        The BoundLType/BoundRType parameters can have the following values:
-            * -1, which corresonds to the periodic (cyclic) boundary conditions.
-                  In this case:
-                  * both BoundLType and BoundRType must be equal to -1.
-                  * BoundL/BoundR are ignored
-                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
-            *  0, which  corresponds  to  the  parabolically   terminated  spline
-                  (BoundL and/or BoundR are ignored).
-            *  1, which corresponds to the first derivative boundary condition
-            *  2, which corresponds to the second derivative boundary condition
-            *  by default, BoundType=0 is used
-
-        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
-
-        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
-        However, this subroutine doesn't require you to specify equal  values  for
-        the first and last points - it automatically forces them  to  be  equal by
-        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
-        Y[last_point]. However it is recommended to pass consistent values of Y[],
-        i.e. to make Y[first_point]=Y[last_point].
-
-          -- ALGLIB PROJECT --
-             Copyright 03.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dgriddiff2cubic(double[] x,
-            double[] y,
-            int n,
-            int boundltype,
-            double boundl,
-            int boundrtype,
-            double boundr,
-            ref double[] d1,
-            ref double[] d2)
-        {
-            double[] a1 = new double[0];
-            double[] a2 = new double[0];
-            double[] a3 = new double[0];
-            double[] b = new double[0];
-            double[] dt = new double[0];
-            int[] p = new int[0];
-            int i = 0;
-            int ylen = 0;
-            double delta = 0;
-            double delta2 = 0;
-            double delta3 = 0;
-            double s0 = 0;
-            double s1 = 0;
-            double s2 = 0;
-            double s3 = 0;
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            d1 = new double[0];
-            d2 = new double[0];
-
-            
-            //
-            // check correctness of boundary conditions
-            //
-            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DGridDiff2Cubic: incorrect BoundLType!");
-            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DGridDiff2Cubic: incorrect BoundRType!");
-            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DGridDiff2Cubic: incorrect BoundLType/BoundRType!");
-            if( boundltype==1 | boundltype==2 )
-            {
-                ap.assert(math.isfinite(boundl), "Spline1DGridDiff2Cubic: BoundL is infinite or NAN!");
-            }
-            if( boundrtype==1 | boundrtype==2 )
-            {
-                ap.assert(math.isfinite(boundr), "Spline1DGridDiff2Cubic: BoundR is infinite or NAN!");
-            }
-            
-            //
-            // check lengths of arguments
-            //
-            ap.assert(n>=2, "Spline1DGridDiff2Cubic: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DGridDiff2Cubic: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DGridDiff2Cubic: Length(Y)<N!");
-            
-            //
-            // check and sort points
-            //
-            ylen = n;
-            if( boundltype==-1 )
-            {
-                ylen = n-1;
-            }
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DGridDiff2Cubic: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DGridDiff2Cubic: Y contains infinite or NAN values!");
-            heapsortppoints(ref x, ref y, ref p, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DGridDiff2Cubic: at least two consequent points are too close!");
-            
-            //
-            // Now we've checked and preordered everything,
-            // so we can call internal function.
-            //
-            // After this call we will calculate second derivatives
-            // (manually, by converting to the power basis)
-            //
-            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d1, ref a1, ref a2, ref a3, ref b, ref dt);
-            d2 = new double[n];
-            delta = 0;
-            s2 = 0;
-            s3 = 0;
-            for(i=0; i<=n-2; i++)
-            {
-                
-                //
-                // We convert from Hermite basis to the power basis.
-                // Si is coefficient before x^i.
-                //
-                // Inside this cycle we need just S2,
-                // because we calculate S'' exactly at spline node,
-                // (only x^2 matters at x=0), but after iterations
-                // will be over, we will need other coefficients
-                // to calculate spline value at the last node.
-                //
-                delta = x[i+1]-x[i];
-                delta2 = math.sqr(delta);
-                delta3 = delta*delta2;
-                s0 = y[i];
-                s1 = d1[i];
-                s2 = (3*(y[i+1]-y[i])-2*d1[i]*delta-d1[i+1]*delta)/delta2;
-                s3 = (2*(y[i]-y[i+1])+d1[i]*delta+d1[i+1]*delta)/delta3;
-                d2[i] = 2*s2;
-            }
-            d2[n-1] = 2*s2+6*s3*delta;
-            
-            //
-            // Remember that HeapSortPPoints() call?
-            // Now we have to reorder them back.
-            //
-            if( ap.len(dt)<n )
-            {
-                dt = new double[n];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                dt[p[i]] = d1[i];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                d1[i_] = dt[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                dt[p[i]] = d2[i];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                d2[i_] = dt[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        This function solves following problem: given table y[] of function values
-        at old nodes x[]  and new nodes  x2[],  it calculates and returns table of
-        function values y2[] (calculated at x2[]).
-
-        This function yields same result as Spline1DBuildCubic() call followed  by
-        sequence of Spline1DDiff() calls, but it can be several times faster  when
-        called for ordered X[] and X2[].
-
-        INPUT PARAMETERS:
-            X           -   old spline nodes
-            Y           -   function values
-            X2           -  new spline nodes
-
-        OPTIONAL PARAMETERS:
-            N           -   points count:
-                            * N>=2
-                            * if given, only first N points from X/Y are used
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-            BoundLType  -   boundary condition type for the left boundary
-            BoundL      -   left boundary condition (first or second derivative,
-                            depending on the BoundLType)
-            BoundRType  -   boundary condition type for the right boundary
-            BoundR      -   right boundary condition (first or second derivative,
-                            depending on the BoundRType)
-            N2          -   new points count:
-                            * N2>=2
-                            * if given, only first N2 points from X2 are used
-                            * if not given, automatically detected from X2 size
-
-        OUTPUT PARAMETERS:
-            F2          -   function values at X2[]
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller  may pass unsorted array.
-        Function  values  are correctly reordered on  return, so F2[I]  is  always
-        equal to S(X2[I]) independently of points order.
-
-        SETTING BOUNDARY VALUES:
-
-        The BoundLType/BoundRType parameters can have the following values:
-            * -1, which corresonds to the periodic (cyclic) boundary conditions.
-                  In this case:
-                  * both BoundLType and BoundRType must be equal to -1.
-                  * BoundL/BoundR are ignored
-                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
-            *  0, which  corresponds  to  the  parabolically   terminated  spline
-                  (BoundL and/or BoundR are ignored).
-            *  1, which corresponds to the first derivative boundary condition
-            *  2, which corresponds to the second derivative boundary condition
-            *  by default, BoundType=0 is used
-
-        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
-
-        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
-        However, this subroutine doesn't require you to specify equal  values  for
-        the first and last points - it automatically forces them  to  be  equal by
-        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
-        Y[last_point]. However it is recommended to pass consistent values of Y[],
-        i.e. to make Y[first_point]=Y[last_point].
-
-          -- ALGLIB PROJECT --
-             Copyright 03.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dconvcubic(double[] x,
-            double[] y,
-            int n,
-            int boundltype,
-            double boundl,
-            int boundrtype,
-            double boundr,
-            double[] x2,
-            int n2,
-            ref double[] y2)
-        {
-            double[] a1 = new double[0];
-            double[] a2 = new double[0];
-            double[] a3 = new double[0];
-            double[] b = new double[0];
-            double[] d = new double[0];
-            double[] dt = new double[0];
-            double[] d1 = new double[0];
-            double[] d2 = new double[0];
-            int[] p = new int[0];
-            int[] p2 = new int[0];
-            int i = 0;
-            int ylen = 0;
-            double t = 0;
-            double t2 = 0;
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            x2 = (double[])x2.Clone();
-            y2 = new double[0];
-
-            
-            //
-            // check correctness of boundary conditions
-            //
-            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DConvCubic: incorrect BoundLType!");
-            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DConvCubic: incorrect BoundRType!");
-            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DConvCubic: incorrect BoundLType/BoundRType!");
-            if( boundltype==1 | boundltype==2 )
-            {
-                ap.assert(math.isfinite(boundl), "Spline1DConvCubic: BoundL is infinite or NAN!");
-            }
-            if( boundrtype==1 | boundrtype==2 )
-            {
-                ap.assert(math.isfinite(boundr), "Spline1DConvCubic: BoundR is infinite or NAN!");
-            }
-            
-            //
-            // check lengths of arguments
-            //
-            ap.assert(n>=2, "Spline1DConvCubic: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DConvCubic: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DConvCubic: Length(Y)<N!");
-            ap.assert(n2>=2, "Spline1DConvCubic: N2<2!");
-            ap.assert(ap.len(x2)>=n2, "Spline1DConvCubic: Length(X2)<N2!");
-            
-            //
-            // check and sort X/Y
-            //
-            ylen = n;
-            if( boundltype==-1 )
-            {
-                ylen = n-1;
-            }
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DConvCubic: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DConvCubic: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(x2, n2), "Spline1DConvCubic: X2 contains infinite or NAN values!");
-            heapsortppoints(ref x, ref y, ref p, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DConvCubic: at least two consequent points are too close!");
-            
-            //
-            // set up DT (we will need it below)
-            //
-            dt = new double[Math.Max(n, n2)];
-            
-            //
-            // sort X2:
-            // * use fake array DT because HeapSortPPoints() needs both integer AND real arrays
-            // * if we have periodic problem, wrap points
-            // * sort them, store permutation at P2
-            //
-            if( boundrtype==-1 & boundltype==-1 )
-            {
-                for(i=0; i<=n2-1; i++)
-                {
-                    t = x2[i];
-                    apserv.apperiodicmap(ref t, x[0], x[n-1], ref t2);
-                    x2[i] = t;
-                }
-            }
-            heapsortppoints(ref x2, ref dt, ref p2, n2);
-            
-            //
-            // Now we've checked and preordered everything, so we:
-            // * call internal GridDiff() function to get Hermite form of spline
-            // * convert using internal Conv() function
-            // * convert Y2 back to original order
-            //
-            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
-            spline1dconvdiffinternal(x, y, d, n, x2, n2, ref y2, true, ref d1, false, ref d2, false);
-            ap.assert(ap.len(dt)>=n2, "Spline1DConvCubic: internal error!");
-            for(i=0; i<=n2-1; i++)
-            {
-                dt[p2[i]] = y2[i];
-            }
-            for(i_=0; i_<=n2-1;i_++)
-            {
-                y2[i_] = dt[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        This function solves following problem: given table y[] of function values
-        at old nodes x[]  and new nodes  x2[],  it calculates and returns table of
-        function values y2[] and derivatives d2[] (calculated at x2[]).
-
-        This function yields same result as Spline1DBuildCubic() call followed  by
-        sequence of Spline1DDiff() calls, but it can be several times faster  when
-        called for ordered X[] and X2[].
-
-        INPUT PARAMETERS:
-            X           -   old spline nodes
-            Y           -   function values
-            X2           -  new spline nodes
-
-        OPTIONAL PARAMETERS:
-            N           -   points count:
-                            * N>=2
-                            * if given, only first N points from X/Y are used
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-            BoundLType  -   boundary condition type for the left boundary
-            BoundL      -   left boundary condition (first or second derivative,
-                            depending on the BoundLType)
-            BoundRType  -   boundary condition type for the right boundary
-            BoundR      -   right boundary condition (first or second derivative,
-                            depending on the BoundRType)
-            N2          -   new points count:
-                            * N2>=2
-                            * if given, only first N2 points from X2 are used
-                            * if not given, automatically detected from X2 size
-
-        OUTPUT PARAMETERS:
-            F2          -   function values at X2[]
-            D2          -   first derivatives at X2[]
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller  may pass unsorted array.
-        Function  values  are correctly reordered on  return, so F2[I]  is  always
-        equal to S(X2[I]) independently of points order.
-
-        SETTING BOUNDARY VALUES:
-
-        The BoundLType/BoundRType parameters can have the following values:
-            * -1, which corresonds to the periodic (cyclic) boundary conditions.
-                  In this case:
-                  * both BoundLType and BoundRType must be equal to -1.
-                  * BoundL/BoundR are ignored
-                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
-            *  0, which  corresponds  to  the  parabolically   terminated  spline
-                  (BoundL and/or BoundR are ignored).
-            *  1, which corresponds to the first derivative boundary condition
-            *  2, which corresponds to the second derivative boundary condition
-            *  by default, BoundType=0 is used
-
-        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
-
-        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
-        However, this subroutine doesn't require you to specify equal  values  for
-        the first and last points - it automatically forces them  to  be  equal by
-        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
-        Y[last_point]. However it is recommended to pass consistent values of Y[],
-        i.e. to make Y[first_point]=Y[last_point].
-
-          -- ALGLIB PROJECT --
-             Copyright 03.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dconvdiffcubic(double[] x,
-            double[] y,
-            int n,
-            int boundltype,
-            double boundl,
-            int boundrtype,
-            double boundr,
-            double[] x2,
-            int n2,
-            ref double[] y2,
-            ref double[] d2)
-        {
-            double[] a1 = new double[0];
-            double[] a2 = new double[0];
-            double[] a3 = new double[0];
-            double[] b = new double[0];
-            double[] d = new double[0];
-            double[] dt = new double[0];
-            double[] rt1 = new double[0];
-            int[] p = new int[0];
-            int[] p2 = new int[0];
-            int i = 0;
-            int ylen = 0;
-            double t = 0;
-            double t2 = 0;
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            x2 = (double[])x2.Clone();
-            y2 = new double[0];
-            d2 = new double[0];
-
-            
-            //
-            // check correctness of boundary conditions
-            //
-            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DConvDiffCubic: incorrect BoundLType!");
-            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DConvDiffCubic: incorrect BoundRType!");
-            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DConvDiffCubic: incorrect BoundLType/BoundRType!");
-            if( boundltype==1 | boundltype==2 )
-            {
-                ap.assert(math.isfinite(boundl), "Spline1DConvDiffCubic: BoundL is infinite or NAN!");
-            }
-            if( boundrtype==1 | boundrtype==2 )
-            {
-                ap.assert(math.isfinite(boundr), "Spline1DConvDiffCubic: BoundR is infinite or NAN!");
-            }
-            
-            //
-            // check lengths of arguments
-            //
-            ap.assert(n>=2, "Spline1DConvDiffCubic: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DConvDiffCubic: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DConvDiffCubic: Length(Y)<N!");
-            ap.assert(n2>=2, "Spline1DConvDiffCubic: N2<2!");
-            ap.assert(ap.len(x2)>=n2, "Spline1DConvDiffCubic: Length(X2)<N2!");
-            
-            //
-            // check and sort X/Y
-            //
-            ylen = n;
-            if( boundltype==-1 )
-            {
-                ylen = n-1;
-            }
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DConvDiffCubic: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DConvDiffCubic: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(x2, n2), "Spline1DConvDiffCubic: X2 contains infinite or NAN values!");
-            heapsortppoints(ref x, ref y, ref p, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DConvDiffCubic: at least two consequent points are too close!");
-            
-            //
-            // set up DT (we will need it below)
-            //
-            dt = new double[Math.Max(n, n2)];
-            
-            //
-            // sort X2:
-            // * use fake array DT because HeapSortPPoints() needs both integer AND real arrays
-            // * if we have periodic problem, wrap points
-            // * sort them, store permutation at P2
-            //
-            if( boundrtype==-1 & boundltype==-1 )
-            {
-                for(i=0; i<=n2-1; i++)
-                {
-                    t = x2[i];
-                    apserv.apperiodicmap(ref t, x[0], x[n-1], ref t2);
-                    x2[i] = t;
-                }
-            }
-            heapsortppoints(ref x2, ref dt, ref p2, n2);
-            
-            //
-            // Now we've checked and preordered everything, so we:
-            // * call internal GridDiff() function to get Hermite form of spline
-            // * convert using internal Conv() function
-            // * convert Y2 back to original order
-            //
-            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
-            spline1dconvdiffinternal(x, y, d, n, x2, n2, ref y2, true, ref d2, true, ref rt1, false);
-            ap.assert(ap.len(dt)>=n2, "Spline1DConvDiffCubic: internal error!");
-            for(i=0; i<=n2-1; i++)
-            {
-                dt[p2[i]] = y2[i];
-            }
-            for(i_=0; i_<=n2-1;i_++)
-            {
-                y2[i_] = dt[i_];
-            }
-            for(i=0; i<=n2-1; i++)
-            {
-                dt[p2[i]] = d2[i];
-            }
-            for(i_=0; i_<=n2-1;i_++)
-            {
-                d2[i_] = dt[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        This function solves following problem: given table y[] of function values
-        at old nodes x[]  and new nodes  x2[],  it calculates and returns table of
-        function  values  y2[],  first  and  second  derivatives  d2[]  and  dd2[]
-        (calculated at x2[]).
-
-        This function yields same result as Spline1DBuildCubic() call followed  by
-        sequence of Spline1DDiff() calls, but it can be several times faster  when
-        called for ordered X[] and X2[].
-
-        INPUT PARAMETERS:
-            X           -   old spline nodes
-            Y           -   function values
-            X2           -  new spline nodes
-
-        OPTIONAL PARAMETERS:
-            N           -   points count:
-                            * N>=2
-                            * if given, only first N points from X/Y are used
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-            BoundLType  -   boundary condition type for the left boundary
-            BoundL      -   left boundary condition (first or second derivative,
-                            depending on the BoundLType)
-            BoundRType  -   boundary condition type for the right boundary
-            BoundR      -   right boundary condition (first or second derivative,
-                            depending on the BoundRType)
-            N2          -   new points count:
-                            * N2>=2
-                            * if given, only first N2 points from X2 are used
-                            * if not given, automatically detected from X2 size
-
-        OUTPUT PARAMETERS:
-            F2          -   function values at X2[]
-            D2          -   first derivatives at X2[]
-            DD2         -   second derivatives at X2[]
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller  may pass unsorted array.
-        Function  values  are correctly reordered on  return, so F2[I]  is  always
-        equal to S(X2[I]) independently of points order.
-
-        SETTING BOUNDARY VALUES:
-
-        The BoundLType/BoundRType parameters can have the following values:
-            * -1, which corresonds to the periodic (cyclic) boundary conditions.
-                  In this case:
-                  * both BoundLType and BoundRType must be equal to -1.
-                  * BoundL/BoundR are ignored
-                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
-            *  0, which  corresponds  to  the  parabolically   terminated  spline
-                  (BoundL and/or BoundR are ignored).
-            *  1, which corresponds to the first derivative boundary condition
-            *  2, which corresponds to the second derivative boundary condition
-            *  by default, BoundType=0 is used
-
-        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
-
-        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
-        However, this subroutine doesn't require you to specify equal  values  for
-        the first and last points - it automatically forces them  to  be  equal by
-        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
-        Y[last_point]. However it is recommended to pass consistent values of Y[],
-        i.e. to make Y[first_point]=Y[last_point].
-
-          -- ALGLIB PROJECT --
-             Copyright 03.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dconvdiff2cubic(double[] x,
-            double[] y,
-            int n,
-            int boundltype,
-            double boundl,
-            int boundrtype,
-            double boundr,
-            double[] x2,
-            int n2,
-            ref double[] y2,
-            ref double[] d2,
-            ref double[] dd2)
-        {
-            double[] a1 = new double[0];
-            double[] a2 = new double[0];
-            double[] a3 = new double[0];
-            double[] b = new double[0];
-            double[] d = new double[0];
-            double[] dt = new double[0];
-            int[] p = new int[0];
-            int[] p2 = new int[0];
-            int i = 0;
-            int ylen = 0;
-            double t = 0;
-            double t2 = 0;
-            int i_ = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            x2 = (double[])x2.Clone();
-            y2 = new double[0];
-            d2 = new double[0];
-            dd2 = new double[0];
-
-            
-            //
-            // check correctness of boundary conditions
-            //
-            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DConvDiff2Cubic: incorrect BoundLType!");
-            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DConvDiff2Cubic: incorrect BoundRType!");
-            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DConvDiff2Cubic: incorrect BoundLType/BoundRType!");
-            if( boundltype==1 | boundltype==2 )
-            {
-                ap.assert(math.isfinite(boundl), "Spline1DConvDiff2Cubic: BoundL is infinite or NAN!");
-            }
-            if( boundrtype==1 | boundrtype==2 )
-            {
-                ap.assert(math.isfinite(boundr), "Spline1DConvDiff2Cubic: BoundR is infinite or NAN!");
-            }
-            
-            //
-            // check lengths of arguments
-            //
-            ap.assert(n>=2, "Spline1DConvDiff2Cubic: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DConvDiff2Cubic: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DConvDiff2Cubic: Length(Y)<N!");
-            ap.assert(n2>=2, "Spline1DConvDiff2Cubic: N2<2!");
-            ap.assert(ap.len(x2)>=n2, "Spline1DConvDiff2Cubic: Length(X2)<N2!");
-            
-            //
-            // check and sort X/Y
-            //
-            ylen = n;
-            if( boundltype==-1 )
-            {
-                ylen = n-1;
-            }
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DConvDiff2Cubic: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DConvDiff2Cubic: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(x2, n2), "Spline1DConvDiff2Cubic: X2 contains infinite or NAN values!");
-            heapsortppoints(ref x, ref y, ref p, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DConvDiff2Cubic: at least two consequent points are too close!");
-            
-            //
-            // set up DT (we will need it below)
-            //
-            dt = new double[Math.Max(n, n2)];
-            
-            //
-            // sort X2:
-            // * use fake array DT because HeapSortPPoints() needs both integer AND real arrays
-            // * if we have periodic problem, wrap points
-            // * sort them, store permutation at P2
-            //
-            if( boundrtype==-1 & boundltype==-1 )
-            {
-                for(i=0; i<=n2-1; i++)
-                {
-                    t = x2[i];
-                    apserv.apperiodicmap(ref t, x[0], x[n-1], ref t2);
-                    x2[i] = t;
-                }
-            }
-            heapsortppoints(ref x2, ref dt, ref p2, n2);
-            
-            //
-            // Now we've checked and preordered everything, so we:
-            // * call internal GridDiff() function to get Hermite form of spline
-            // * convert using internal Conv() function
-            // * convert Y2 back to original order
-            //
-            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
-            spline1dconvdiffinternal(x, y, d, n, x2, n2, ref y2, true, ref d2, true, ref dd2, true);
-            ap.assert(ap.len(dt)>=n2, "Spline1DConvDiff2Cubic: internal error!");
-            for(i=0; i<=n2-1; i++)
-            {
-                dt[p2[i]] = y2[i];
-            }
-            for(i_=0; i_<=n2-1;i_++)
-            {
-                y2[i_] = dt[i_];
-            }
-            for(i=0; i<=n2-1; i++)
-            {
-                dt[p2[i]] = d2[i];
-            }
-            for(i_=0; i_<=n2-1;i_++)
-            {
-                d2[i_] = dt[i_];
-            }
-            for(i=0; i<=n2-1; i++)
-            {
-                dt[p2[i]] = dd2[i];
-            }
-            for(i_=0; i_<=n2-1;i_++)
-            {
-                dd2[i_] = dt[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine builds Catmull-Rom spline interpolant.
-
-        INPUT PARAMETERS:
-            X           -   spline nodes, array[0..N-1].
-            Y           -   function values, array[0..N-1].
-            
-        OPTIONAL PARAMETERS:
-            N           -   points count:
-                            * N>=2
-                            * if given, only first N points are used to build spline
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-            BoundType   -   boundary condition type:
-                            * -1 for periodic boundary condition
-                            *  0 for parabolically terminated spline (default)
-            Tension     -   tension parameter:
-                            * tension=0   corresponds to classic Catmull-Rom spline (default)
-                            * 0<tension<1 corresponds to more general form - cardinal spline
-
-        OUTPUT PARAMETERS:
-            C           -   spline interpolant
-
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-
-        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
-
-        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
-        However, this subroutine doesn't require you to specify equal  values  for
-        the first and last points - it automatically forces them  to  be  equal by
-        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
-        Y[last_point]. However it is recommended to pass consistent values of Y[],
-        i.e. to make Y[first_point]=Y[last_point].
-
-          -- ALGLIB PROJECT --
-             Copyright 23.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dbuildcatmullrom(double[] x,
-            double[] y,
-            int n,
-            int boundtype,
-            double tension,
-            spline1dinterpolant c)
-        {
-            double[] d = new double[0];
-            int i = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-
-            ap.assert(n>=2, "Spline1DBuildCatmullRom: N<2!");
-            ap.assert(boundtype==-1 | boundtype==0, "Spline1DBuildCatmullRom: incorrect BoundType!");
-            ap.assert((double)(tension)>=(double)(0), "Spline1DBuildCatmullRom: Tension<0!");
-            ap.assert((double)(tension)<=(double)(1), "Spline1DBuildCatmullRom: Tension>1!");
-            ap.assert(ap.len(x)>=n, "Spline1DBuildCatmullRom: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DBuildCatmullRom: Length(Y)<N!");
-            
-            //
-            // check and sort points
-            //
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildCatmullRom: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildCatmullRom: Y contains infinite or NAN values!");
-            heapsortpoints(ref x, ref y, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildCatmullRom: at least two consequent points are too close!");
-            
-            //
-            // Special cases:
-            // * N=2, parabolic terminated boundary condition on both ends
-            // * N=2, periodic boundary condition
-            //
-            if( n==2 & boundtype==0 )
-            {
-                
-                //
-                // Just linear spline
-                //
-                spline1dbuildlinear(x, y, n, c);
-                return;
-            }
-            if( n==2 & boundtype==-1 )
-            {
-                
-                //
-                // Same as cubic spline with periodic conditions
-                //
-                spline1dbuildcubic(x, y, n, -1, 0.0, -1, 0.0, c);
-                return;
-            }
-            
-            //
-            // Periodic or non-periodic boundary conditions
-            //
-            if( boundtype==-1 )
-            {
-                
-                //
-                // Periodic boundary conditions
-                //
-                y[n-1] = y[0];
-                d = new double[n];
-                d[0] = (y[1]-y[n-2])/(2*(x[1]-x[0]+x[n-1]-x[n-2]));
-                for(i=1; i<=n-2; i++)
-                {
-                    d[i] = (1-tension)*(y[i+1]-y[i-1])/(x[i+1]-x[i-1]);
-                }
-                d[n-1] = d[0];
-                
-                //
-                // Now problem is reduced to the cubic Hermite spline
-                //
-                spline1dbuildhermite(x, y, d, n, c);
-                c.periodic = true;
-            }
-            else
-            {
-                
-                //
-                // Non-periodic boundary conditions
-                //
-                d = new double[n];
-                for(i=1; i<=n-2; i++)
-                {
-                    d[i] = (1-tension)*(y[i+1]-y[i-1])/(x[i+1]-x[i-1]);
-                }
-                d[0] = 2*(y[1]-y[0])/(x[1]-x[0])-d[1];
-                d[n-1] = 2*(y[n-1]-y[n-2])/(x[n-1]-x[n-2])-d[n-2];
-                
-                //
-                // Now problem is reduced to the cubic Hermite spline
-                //
-                spline1dbuildhermite(x, y, d, n, c);
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine builds Hermite spline interpolant.
-
-        INPUT PARAMETERS:
-            X           -   spline nodes, array[0..N-1]
-            Y           -   function values, array[0..N-1]
-            D           -   derivatives, array[0..N-1]
-            N           -   points count (optional):
-                            * N>=2
-                            * if given, only first N points are used to build spline
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-
-        OUTPUT PARAMETERS:
-            C           -   spline interpolant.
-
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-
-          -- ALGLIB PROJECT --
-             Copyright 23.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dbuildhermite(double[] x,
-            double[] y,
-            double[] d,
-            int n,
-            spline1dinterpolant c)
-        {
-            int i = 0;
-            double delta = 0;
-            double delta2 = 0;
-            double delta3 = 0;
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            d = (double[])d.Clone();
-
-            ap.assert(n>=2, "Spline1DBuildHermite: N<2!");
-            ap.assert(ap.len(x)>=n, "Spline1DBuildHermite: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DBuildHermite: Length(Y)<N!");
-            ap.assert(ap.len(d)>=n, "Spline1DBuildHermite: Length(D)<N!");
-            
-            //
-            // check and sort points
-            //
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildHermite: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildHermite: Y contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(d, n), "Spline1DBuildHermite: D contains infinite or NAN values!");
-            heapsortdpoints(ref x, ref y, ref d, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildHermite: at least two consequent points are too close!");
-            
-            //
-            // Build
-            //
-            c.x = new double[n];
-            c.c = new double[4*(n-1)];
-            c.periodic = false;
-            c.k = 3;
-            c.n = n;
-            for(i=0; i<=n-1; i++)
-            {
-                c.x[i] = x[i];
-            }
-            for(i=0; i<=n-2; i++)
-            {
-                delta = x[i+1]-x[i];
-                delta2 = math.sqr(delta);
-                delta3 = delta*delta2;
-                c.c[4*i+0] = y[i];
-                c.c[4*i+1] = d[i];
-                c.c[4*i+2] = (3*(y[i+1]-y[i])-2*d[i]*delta-d[i+1]*delta)/delta2;
-                c.c[4*i+3] = (2*(y[i]-y[i+1])+d[i]*delta+d[i+1]*delta)/delta3;
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine builds Akima spline interpolant
-
-        INPUT PARAMETERS:
-            X           -   spline nodes, array[0..N-1]
-            Y           -   function values, array[0..N-1]
-            N           -   points count (optional):
-                            * N>=5
-                            * if given, only first N points are used to build spline
-                            * if not given, automatically detected from X/Y sizes
-                              (len(X) must be equal to len(Y))
-
-        OUTPUT PARAMETERS:
-            C           -   spline interpolant
-
-
-        ORDER OF POINTS
-
-        Subroutine automatically sorts points, so caller may pass unsorted array.
-
-          -- ALGLIB PROJECT --
-             Copyright 24.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dbuildakima(double[] x,
-            double[] y,
-            int n,
-            spline1dinterpolant c)
-        {
-            int i = 0;
-            double[] d = new double[0];
-            double[] w = new double[0];
-            double[] diff = new double[0];
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-
-            ap.assert(n>=5, "Spline1DBuildAkima: N<5!");
-            ap.assert(ap.len(x)>=n, "Spline1DBuildAkima: Length(X)<N!");
-            ap.assert(ap.len(y)>=n, "Spline1DBuildAkima: Length(Y)<N!");
-            
-            //
-            // check and sort points
-            //
-            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildAkima: X contains infinite or NAN values!");
-            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildAkima: Y contains infinite or NAN values!");
-            heapsortpoints(ref x, ref y, n);
-            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildAkima: at least two consequent points are too close!");
-            
-            //
-            // Prepare W (weights), Diff (divided differences)
-            //
-            w = new double[n-1];
-            diff = new double[n-1];
-            for(i=0; i<=n-2; i++)
-            {
-                diff[i] = (y[i+1]-y[i])/(x[i+1]-x[i]);
-            }
-            for(i=1; i<=n-2; i++)
-            {
-                w[i] = Math.Abs(diff[i]-diff[i-1]);
-            }
-            
-            //
-            // Prepare Hermite interpolation scheme
-            //
-            d = new double[n];
-            for(i=2; i<=n-3; i++)
-            {
-                if( (double)(Math.Abs(w[i-1])+Math.Abs(w[i+1]))!=(double)(0) )
-                {
-                    d[i] = (w[i+1]*diff[i-1]+w[i-1]*diff[i])/(w[i+1]+w[i-1]);
-                }
-                else
-                {
-                    d[i] = ((x[i+1]-x[i])*diff[i-1]+(x[i]-x[i-1])*diff[i])/(x[i+1]-x[i-1]);
-                }
-            }
-            d[0] = diffthreepoint(x[0], x[0], y[0], x[1], y[1], x[2], y[2]);
-            d[1] = diffthreepoint(x[1], x[0], y[0], x[1], y[1], x[2], y[2]);
-            d[n-2] = diffthreepoint(x[n-2], x[n-3], y[n-3], x[n-2], y[n-2], x[n-1], y[n-1]);
-            d[n-1] = diffthreepoint(x[n-1], x[n-3], y[n-3], x[n-2], y[n-2], x[n-1], y[n-1]);
-            
-            //
-            // Build Akima spline using Hermite interpolation scheme
-            //
-            spline1dbuildhermite(x, y, d, n, c);
-        }
-
-
-        /*************************************************************************
-        This subroutine calculates the value of the spline at the given point X.
-
-        INPUT PARAMETERS:
-            C   -   spline interpolant
-            X   -   point
-
-        Result:
-            S(x)
-
-          -- ALGLIB PROJECT --
-             Copyright 23.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static double spline1dcalc(spline1dinterpolant c,
-            double x)
-        {
-            double result = 0;
-            int l = 0;
-            int r = 0;
-            int m = 0;
-            double t = 0;
-
-            ap.assert(c.k==3, "Spline1DCalc: internal error");
-            ap.assert(!Double.IsInfinity(x), "Spline1DCalc: infinite X!");
-            
-            //
-            // special case: NaN
-            //
-            if( Double.IsNaN(x) )
-            {
-                result = Double.NaN;
-                return result;
-            }
-            
-            //
-            // correct if periodic
-            //
-            if( c.periodic )
-            {
-                apserv.apperiodicmap(ref x, c.x[0], c.x[c.n-1], ref t);
-            }
-            
-            //
-            // Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
-            //
-            l = 0;
-            r = c.n-2+1;
-            while( l!=r-1 )
-            {
-                m = (l+r)/2;
-                if( c.x[m]>=x )
-                {
-                    r = m;
-                }
-                else
-                {
-                    l = m;
-                }
-            }
-            
-            //
-            // Interpolation
-            //
-            x = x-c.x[l];
-            m = 4*l;
-            result = c.c[m]+x*(c.c[m+1]+x*(c.c[m+2]+x*c.c[m+3]));
-            return result;
-        }
-
-
-        /*************************************************************************
-        This subroutine differentiates the spline.
-
-        INPUT PARAMETERS:
-            C   -   spline interpolant.
-            X   -   point
-
-        Result:
-            S   -   S(x)
-            DS  -   S'(x)
-            D2S -   S''(x)
-
-          -- ALGLIB PROJECT --
-             Copyright 24.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1ddiff(spline1dinterpolant c,
-            double x,
-            ref double s,
-            ref double ds,
-            ref double d2s)
-        {
-            int l = 0;
-            int r = 0;
-            int m = 0;
-            double t = 0;
-
-            s = 0;
-            ds = 0;
-            d2s = 0;
-
-            ap.assert(c.k==3, "Spline1DDiff: internal error");
-            ap.assert(!Double.IsInfinity(x), "Spline1DDiff: infinite X!");
-            
-            //
-            // special case: NaN
-            //
-            if( Double.IsNaN(x) )
-            {
-                s = Double.NaN;
-                ds = Double.NaN;
-                d2s = Double.NaN;
-                return;
-            }
-            
-            //
-            // correct if periodic
-            //
-            if( c.periodic )
-            {
-                apserv.apperiodicmap(ref x, c.x[0], c.x[c.n-1], ref t);
-            }
-            
-            //
-            // Binary search
-            //
-            l = 0;
-            r = c.n-2+1;
-            while( l!=r-1 )
-            {
-                m = (l+r)/2;
-                if( c.x[m]>=x )
-                {
-                    r = m;
-                }
-                else
-                {
-                    l = m;
-                }
-            }
-            
-            //
-            // Differentiation
-            //
-            x = x-c.x[l];
-            m = 4*l;
-            s = c.c[m]+x*(c.c[m+1]+x*(c.c[m+2]+x*c.c[m+3]));
-            ds = c.c[m+1]+2*x*c.c[m+2]+3*math.sqr(x)*c.c[m+3];
-            d2s = 2*c.c[m+2]+6*x*c.c[m+3];
-        }
-
-
-        /*************************************************************************
-        This subroutine makes the copy of the spline.
-
-        INPUT PARAMETERS:
-            C   -   spline interpolant.
-
-        Result:
-            CC  -   spline copy
-
-          -- ALGLIB PROJECT --
-             Copyright 29.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dcopy(spline1dinterpolant c,
-            spline1dinterpolant cc)
-        {
-            int i_ = 0;
-
-            cc.periodic = c.periodic;
-            cc.n = c.n;
-            cc.k = c.k;
-            cc.x = new double[cc.n];
-            for(i_=0; i_<=cc.n-1;i_++)
-            {
-                cc.x[i_] = c.x[i_];
-            }
-            cc.c = new double[(cc.k+1)*(cc.n-1)];
-            for(i_=0; i_<=(cc.k+1)*(cc.n-1)-1;i_++)
-            {
-                cc.c[i_] = c.c[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine unpacks the spline into the coefficients table.
-
-        INPUT PARAMETERS:
-            C   -   spline interpolant.
-            X   -   point
-
-        Result:
-            Tbl -   coefficients table, unpacked format, array[0..N-2, 0..5].
-                    For I = 0...N-2:
-                        Tbl[I,0] = X[i]
-                        Tbl[I,1] = X[i+1]
-                        Tbl[I,2] = C0
-                        Tbl[I,3] = C1
-                        Tbl[I,4] = C2
-                        Tbl[I,5] = C3
-                    On [x[i], x[i+1]] spline is equals to:
-                        S(x) = C0 + C1*t + C2*t^2 + C3*t^3
-                        t = x-x[i]
-
-          -- ALGLIB PROJECT --
-             Copyright 29.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dunpack(spline1dinterpolant c,
-            ref int n,
-            ref double[,] tbl)
-        {
-            int i = 0;
-            int j = 0;
-
-            n = 0;
-            tbl = new double[0,0];
-
-            tbl = new double[c.n-2+1, 2+c.k+1];
-            n = c.n;
-            
-            //
-            // Fill
-            //
-            for(i=0; i<=n-2; i++)
-            {
-                tbl[i,0] = c.x[i];
-                tbl[i,1] = c.x[i+1];
-                for(j=0; j<=c.k; j++)
-                {
-                    tbl[i,2+j] = c.c[(c.k+1)*i+j];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine performs linear transformation of the spline argument.
-
-        INPUT PARAMETERS:
-            C   -   spline interpolant.
-            A, B-   transformation coefficients: x = A*t + B
-        Result:
-            C   -   transformed spline
-
-          -- ALGLIB PROJECT --
-             Copyright 30.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dlintransx(spline1dinterpolant c,
-            double a,
-            double b)
-        {
-            int i = 0;
-            int j = 0;
-            int n = 0;
-            double v = 0;
-            double dv = 0;
-            double d2v = 0;
-            double[] x = new double[0];
-            double[] y = new double[0];
-            double[] d = new double[0];
-
-            n = c.n;
-            
-            //
-            // Special case: A=0
-            //
-            if( (double)(a)==(double)(0) )
-            {
-                v = spline1dcalc(c, b);
-                for(i=0; i<=n-2; i++)
-                {
-                    c.c[(c.k+1)*i] = v;
-                    for(j=1; j<=c.k; j++)
-                    {
-                        c.c[(c.k+1)*i+j] = 0;
-                    }
-                }
-                return;
-            }
-            
-            //
-            // General case: A<>0.
-            // Unpack, X, Y, dY/dX.
-            // Scale and pack again.
-            //
-            ap.assert(c.k==3, "Spline1DLinTransX: internal error");
-            x = new double[n-1+1];
-            y = new double[n-1+1];
-            d = new double[n-1+1];
-            for(i=0; i<=n-1; i++)
-            {
-                x[i] = c.x[i];
-                spline1ddiff(c, x[i], ref v, ref dv, ref d2v);
-                x[i] = (x[i]-b)/a;
-                y[i] = v;
-                d[i] = a*dv;
-            }
-            spline1dbuildhermite(x, y, d, n, c);
-        }
-
-
-        /*************************************************************************
-        This subroutine performs linear transformation of the spline.
-
-        INPUT PARAMETERS:
-            C   -   spline interpolant.
-            A, B-   transformation coefficients: S2(x) = A*S(x) + B
-        Result:
-            C   -   transformed spline
-
-          -- ALGLIB PROJECT --
-             Copyright 30.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dlintransy(spline1dinterpolant c,
-            double a,
-            double b)
-        {
-            int i = 0;
-            int j = 0;
-            int n = 0;
-
-            n = c.n;
-            for(i=0; i<=n-2; i++)
-            {
-                c.c[(c.k+1)*i] = a*c.c[(c.k+1)*i]+b;
-                for(j=1; j<=c.k; j++)
-                {
-                    c.c[(c.k+1)*i+j] = a*c.c[(c.k+1)*i+j];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine integrates the spline.
-
-        INPUT PARAMETERS:
-            C   -   spline interpolant.
-            X   -   right bound of the integration interval [a, x],
-                    here 'a' denotes min(x[])
-        Result:
-            integral(S(t)dt,a,x)
-
-          -- ALGLIB PROJECT --
-             Copyright 23.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static double spline1dintegrate(spline1dinterpolant c,
-            double x)
-        {
-            double result = 0;
-            int n = 0;
-            int i = 0;
-            int j = 0;
-            int l = 0;
-            int r = 0;
-            int m = 0;
-            double w = 0;
-            double v = 0;
-            double t = 0;
-            double intab = 0;
-            double additionalterm = 0;
-
-            n = c.n;
-            
-            //
-            // Periodic splines require special treatment. We make
-            // following transformation:
-            //
-            //     integral(S(t)dt,A,X) = integral(S(t)dt,A,Z)+AdditionalTerm
-            //
-            // here X may lie outside of [A,B], Z lies strictly in [A,B],
-            // AdditionalTerm is equals to integral(S(t)dt,A,B) times some
-            // integer number (may be zero).
-            //
-            if( c.periodic & ((double)(x)<(double)(c.x[0]) | (double)(x)>(double)(c.x[c.n-1])) )
-            {
-                
-                //
-                // compute integral(S(x)dx,A,B)
-                //
-                intab = 0;
-                for(i=0; i<=c.n-2; i++)
-                {
-                    w = c.x[i+1]-c.x[i];
-                    m = (c.k+1)*i;
-                    intab = intab+c.c[m]*w;
-                    v = w;
-                    for(j=1; j<=c.k; j++)
-                    {
-                        v = v*w;
-                        intab = intab+c.c[m+j]*v/(j+1);
-                    }
-                }
-                
-                //
-                // map X into [A,B]
-                //
-                apserv.apperiodicmap(ref x, c.x[0], c.x[c.n-1], ref t);
-                additionalterm = t*intab;
-            }
-            else
-            {
-                additionalterm = 0;
-            }
-            
-            //
-            // Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
-            //
-            l = 0;
-            r = n-2+1;
-            while( l!=r-1 )
-            {
-                m = (l+r)/2;
-                if( (double)(c.x[m])>=(double)(x) )
-                {
-                    r = m;
-                }
-                else
-                {
-                    l = m;
-                }
-            }
-            
-            //
-            // Integration
-            //
-            result = 0;
-            for(i=0; i<=l-1; i++)
-            {
-                w = c.x[i+1]-c.x[i];
-                m = (c.k+1)*i;
-                result = result+c.c[m]*w;
-                v = w;
-                for(j=1; j<=c.k; j++)
-                {
-                    v = v*w;
-                    result = result+c.c[m+j]*v/(j+1);
-                }
-            }
-            w = x-c.x[l];
-            m = (c.k+1)*l;
-            v = w;
-            result = result+c.c[m]*w;
-            for(j=1; j<=c.k; j++)
-            {
-                v = v*w;
-                result = result+c.c[m+j]*v/(j+1);
-            }
-            result = result+additionalterm;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Internal version of Spline1DConvDiff
-
-        Converts from Hermite spline given by grid XOld to new grid X2
-
-        INPUT PARAMETERS:
-            XOld    -   old grid
-            YOld    -   values at old grid
-            DOld    -   first derivative at old grid
-            N       -   grid size
-            X2      -   new grid
-            N2      -   new grid size
-            Y       -   possibly preallocated output array
-                        (reallocate if too small)
-            NeedY   -   do we need Y?
-            D1      -   possibly preallocated output array
-                        (reallocate if too small)
-            NeedD1  -   do we need D1?
-            D2      -   possibly preallocated output array
-                        (reallocate if too small)
-            NeedD2  -   do we need D1?
-
-        OUTPUT ARRAYS:
-            Y       -   values, if needed
-            D1      -   first derivative, if needed
-            D2      -   second derivative, if needed
-
-          -- ALGLIB PROJECT --
-             Copyright 03.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline1dconvdiffinternal(double[] xold,
-            double[] yold,
-            double[] dold,
-            int n,
-            double[] x2,
-            int n2,
-            ref double[] y,
-            bool needy,
-            ref double[] d1,
-            bool needd1,
-            ref double[] d2,
-            bool needd2)
-        {
-            int intervalindex = 0;
-            int pointindex = 0;
-            bool havetoadvance = new bool();
-            double c0 = 0;
-            double c1 = 0;
-            double c2 = 0;
-            double c3 = 0;
-            double a = 0;
-            double b = 0;
-            double w = 0;
-            double w2 = 0;
-            double w3 = 0;
-            double fa = 0;
-            double fb = 0;
-            double da = 0;
-            double db = 0;
-            double t = 0;
-
-            
-            //
-            // Prepare space
-            //
-            if( needy & ap.len(y)<n2 )
-            {
-                y = new double[n2];
-            }
-            if( needd1 & ap.len(d1)<n2 )
-            {
-                d1 = new double[n2];
-            }
-            if( needd2 & ap.len(d2)<n2 )
-            {
-                d2 = new double[n2];
-            }
-            
-            //
-            // These assignments aren't actually needed
-            // (variables are initialized in the loop below),
-            // but without them compiler will complain about uninitialized locals
-            //
-            c0 = 0;
-            c1 = 0;
-            c2 = 0;
-            c3 = 0;
-            a = 0;
-            b = 0;
-            
-            //
-            // Cycle
-            //
-            intervalindex = -1;
-            pointindex = 0;
-            while( true )
-            {
-                
-                //
-                // are we ready to exit?
-                //
-                if( pointindex>=n2 )
-                {
-                    break;
-                }
-                t = x2[pointindex];
-                
-                //
-                // do we need to advance interval?
-                //
-                havetoadvance = false;
-                if( intervalindex==-1 )
-                {
-                    havetoadvance = true;
-                }
-                else
-                {
-                    if( intervalindex<n-2 )
-                    {
-                        havetoadvance = (double)(t)>=(double)(b);
-                    }
-                }
-                if( havetoadvance )
-                {
-                    intervalindex = intervalindex+1;
-                    a = xold[intervalindex];
-                    b = xold[intervalindex+1];
-                    w = b-a;
-                    w2 = w*w;
-                    w3 = w*w2;
-                    fa = yold[intervalindex];
-                    fb = yold[intervalindex+1];
-                    da = dold[intervalindex];
-                    db = dold[intervalindex+1];
-                    c0 = fa;
-                    c1 = da;
-                    c2 = (3*(fb-fa)-2*da*w-db*w)/w2;
-                    c3 = (2*(fa-fb)+da*w+db*w)/w3;
-                    continue;
-                }
-                
-                //
-                // Calculate spline and its derivatives using power basis
-                //
-                t = t-a;
-                if( needy )
-                {
-                    y[pointindex] = c0+t*(c1+t*(c2+t*c3));
-                }
-                if( needd1 )
-                {
-                    d1[pointindex] = c1+2*t*c2+3*t*t*c3;
-                }
-                if( needd2 )
-                {
-                    d2[pointindex] = 2*c2+6*t*c3;
-                }
-                pointindex = pointindex+1;
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Heap sort.
-        *************************************************************************/
-        public static void heapsortdpoints(ref double[] x,
-            ref double[] y,
-            ref double[] d,
-            int n)
-        {
-            double[] rbuf = new double[0];
-            int[] ibuf = new int[0];
-            double[] rbuf2 = new double[0];
-            int[] ibuf2 = new int[0];
-            int i = 0;
-            int i_ = 0;
-
-            ibuf = new int[n];
-            rbuf = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                ibuf[i] = i;
-            }
-            tsort.tagsortfasti(ref x, ref ibuf, ref rbuf2, ref ibuf2, n);
-            for(i=0; i<=n-1; i++)
-            {
-                rbuf[i] = y[ibuf[i]];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                y[i_] = rbuf[i_];
-            }
-            for(i=0; i<=n-1; i++)
-            {
-                rbuf[i] = d[ibuf[i]];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                d[i_] = rbuf[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        Internal version of Spline1DGridDiffCubic.
-
-        Accepts pre-ordered X/Y, temporary arrays (which may be  preallocated,  if
-        you want to save time, or not) and output array (which may be preallocated
-        too).
-
-        Y is passed as var-parameter because we may need to force last element  to
-        be equal to the first one (if periodic boundary conditions are specified).
-
-          -- ALGLIB PROJECT --
-             Copyright 03.09.2010 by Bochkanov Sergey
-        *************************************************************************/
-        private static void spline1dgriddiffcubicinternal(double[] x,
-            ref double[] y,
-            int n,
-            int boundltype,
-            double boundl,
-            int boundrtype,
-            double boundr,
-            ref double[] d,
-            ref double[] a1,
-            ref double[] a2,
-            ref double[] a3,
-            ref double[] b,
-            ref double[] dt)
-        {
-            int i = 0;
-            int i_ = 0;
-
-            
-            //
-            // allocate arrays
-            //
-            if( ap.len(d)<n )
-            {
-                d = new double[n];
-            }
-            if( ap.len(a1)<n )
-            {
-                a1 = new double[n];
-            }
-            if( ap.len(a2)<n )
-            {
-                a2 = new double[n];
-            }
-            if( ap.len(a3)<n )
-            {
-                a3 = new double[n];
-            }
-            if( ap.len(b)<n )
-            {
-                b = new double[n];
-            }
-            if( ap.len(dt)<n )
-            {
-                dt = new double[n];
-            }
-            
-            //
-            // Special cases:
-            // * N=2, parabolic terminated boundary condition on both ends
-            // * N=2, periodic boundary condition
-            //
-            if( (n==2 & boundltype==0) & boundrtype==0 )
-            {
-                d[0] = (y[1]-y[0])/(x[1]-x[0]);
-                d[1] = d[0];
-                return;
-            }
-            if( (n==2 & boundltype==-1) & boundrtype==-1 )
-            {
-                d[0] = 0;
-                d[1] = 0;
-                return;
-            }
-            
-            //
-            // Periodic and non-periodic boundary conditions are
-            // two separate classes
-            //
-            if( boundrtype==-1 & boundltype==-1 )
-            {
-                
-                //
-                // Periodic boundary conditions
-                //
-                y[n-1] = y[0];
-                
-                //
-                // Boundary conditions at N-1 points
-                // (one point less because last point is the same as first point).
-                //
-                a1[0] = x[1]-x[0];
-                a2[0] = 2*(x[1]-x[0]+x[n-1]-x[n-2]);
-                a3[0] = x[n-1]-x[n-2];
-                b[0] = 3*(y[n-1]-y[n-2])/(x[n-1]-x[n-2])*(x[1]-x[0])+3*(y[1]-y[0])/(x[1]-x[0])*(x[n-1]-x[n-2]);
-                for(i=1; i<=n-2; i++)
-                {
-                    
-                    //
-                    // Altough last point is [N-2], we use X[N-1] and Y[N-1]
-                    // (because of periodicity)
-                    //
-                    a1[i] = x[i+1]-x[i];
-                    a2[i] = 2*(x[i+1]-x[i-1]);
-                    a3[i] = x[i]-x[i-1];
-                    b[i] = 3*(y[i]-y[i-1])/(x[i]-x[i-1])*(x[i+1]-x[i])+3*(y[i+1]-y[i])/(x[i+1]-x[i])*(x[i]-x[i-1]);
-                }
-                
-                //
-                // Solve, add last point (with index N-1)
-                //
-                solvecyclictridiagonal(a1, a2, a3, b, n-1, ref dt);
-                for(i_=0; i_<=n-2;i_++)
-                {
-                    d[i_] = dt[i_];
-                }
-                d[n-1] = d[0];
-            }
-            else
-            {
-                
-                //
-                // Non-periodic boundary condition.
-                // Left boundary conditions.
-                //
-                if( boundltype==0 )
-                {
-                    a1[0] = 0;
-                    a2[0] = 1;
-                    a3[0] = 1;
-                    b[0] = 2*(y[1]-y[0])/(x[1]-x[0]);
-                }
-                if( boundltype==1 )
-                {
-                    a1[0] = 0;
-                    a2[0] = 1;
-                    a3[0] = 0;
-                    b[0] = boundl;
-                }
-                if( boundltype==2 )
-                {
-                    a1[0] = 0;
-                    a2[0] = 2;
-                    a3[0] = 1;
-                    b[0] = 3*(y[1]-y[0])/(x[1]-x[0])-0.5*boundl*(x[1]-x[0]);
-                }
-                
-                //
-                // Central conditions
-                //
-                for(i=1; i<=n-2; i++)
-                {
-                    a1[i] = x[i+1]-x[i];
-                    a2[i] = 2*(x[i+1]-x[i-1]);
-                    a3[i] = x[i]-x[i-1];
-                    b[i] = 3*(y[i]-y[i-1])/(x[i]-x[i-1])*(x[i+1]-x[i])+3*(y[i+1]-y[i])/(x[i+1]-x[i])*(x[i]-x[i-1]);
-                }
-                
-                //
-                // Right boundary conditions
-                //
-                if( boundrtype==0 )
-                {
-                    a1[n-1] = 1;
-                    a2[n-1] = 1;
-                    a3[n-1] = 0;
-                    b[n-1] = 2*(y[n-1]-y[n-2])/(x[n-1]-x[n-2]);
-                }
-                if( boundrtype==1 )
-                {
-                    a1[n-1] = 0;
-                    a2[n-1] = 1;
-                    a3[n-1] = 0;
-                    b[n-1] = boundr;
-                }
-                if( boundrtype==2 )
-                {
-                    a1[n-1] = 1;
-                    a2[n-1] = 2;
-                    a3[n-1] = 0;
-                    b[n-1] = 3*(y[n-1]-y[n-2])/(x[n-1]-x[n-2])+0.5*boundr*(x[n-1]-x[n-2]);
-                }
-                
-                //
-                // Solve
-                //
-                solvetridiagonal(a1, a2, a3, b, n, ref d);
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Heap sort.
-        *************************************************************************/
-        private static void heapsortpoints(ref double[] x,
-            ref double[] y,
-            int n)
-        {
-            double[] bufx = new double[0];
-            double[] bufy = new double[0];
-
-            tsort.tagsortfastr(ref x, ref y, ref bufx, ref bufy, n);
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Heap sort.
-
-        Accepts:
-            X, Y    -   points
-            P       -   empty or preallocated array
-            
-        Returns:
-            X, Y    -   sorted by X
-            P       -   array of permutations; I-th position of output
-                        arrays X/Y contains (X[P[I]],Y[P[I]])
-        *************************************************************************/
-        private static void heapsortppoints(ref double[] x,
-            ref double[] y,
-            ref int[] p,
-            int n)
-        {
-            double[] rbuf = new double[0];
-            int[] ibuf = new int[0];
-            int i = 0;
-            int i_ = 0;
-
-            if( ap.len(p)<n )
-            {
-                p = new int[n];
-            }
-            rbuf = new double[n];
-            for(i=0; i<=n-1; i++)
-            {
-                p[i] = i;
-            }
-            tsort.tagsortfasti(ref x, ref p, ref rbuf, ref ibuf, n);
-            for(i=0; i<=n-1; i++)
-            {
-                rbuf[i] = y[p[i]];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                y[i_] = rbuf[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Tridiagonal solver. Solves
-
-        ( B[0] C[0]                      )
-        ( A[1] B[1] C[1]                 )
-        (      A[2] B[2] C[2]            )
-        (            ..........          ) * X = D
-        (            ..........          )
-        (           A[N-2] B[N-2] C[N-2] )
-        (                  A[N-1] B[N-1] )
-
-        *************************************************************************/
-        private static void solvetridiagonal(double[] a,
-            double[] b,
-            double[] c,
-            double[] d,
-            int n,
-            ref double[] x)
-        {
-            int k = 0;
-            double t = 0;
-
-            b = (double[])b.Clone();
-            d = (double[])d.Clone();
-
-            if( ap.len(x)<n )
-            {
-                x = new double[n];
-            }
-            for(k=1; k<=n-1; k++)
-            {
-                t = a[k]/b[k-1];
-                b[k] = b[k]-t*c[k-1];
-                d[k] = d[k]-t*d[k-1];
-            }
-            x[n-1] = d[n-1]/b[n-1];
-            for(k=n-2; k>=0; k--)
-            {
-                x[k] = (d[k]-c[k]*x[k+1])/b[k];
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Cyclic tridiagonal solver. Solves
-
-        ( B[0] C[0]                 A[0] )
-        ( A[1] B[1] C[1]                 )
-        (      A[2] B[2] C[2]            )
-        (            ..........          ) * X = D
-        (            ..........          )
-        (           A[N-2] B[N-2] C[N-2] )
-        ( C[N-1]           A[N-1] B[N-1] )
-        *************************************************************************/
-        private static void solvecyclictridiagonal(double[] a,
-            double[] b,
-            double[] c,
-            double[] d,
-            int n,
-            ref double[] x)
-        {
-            int k = 0;
-            double alpha = 0;
-            double beta = 0;
-            double gamma = 0;
-            double[] y = new double[0];
-            double[] z = new double[0];
-            double[] u = new double[0];
-
-            b = (double[])b.Clone();
-
-            if( ap.len(x)<n )
-            {
-                x = new double[n];
-            }
-            beta = a[0];
-            alpha = c[n-1];
-            gamma = -b[0];
-            b[0] = 2*b[0];
-            b[n-1] = b[n-1]-alpha*beta/gamma;
-            u = new double[n];
-            for(k=0; k<=n-1; k++)
-            {
-                u[k] = 0;
-            }
-            u[0] = gamma;
-            u[n-1] = alpha;
-            solvetridiagonal(a, b, c, d, n, ref y);
-            solvetridiagonal(a, b, c, u, n, ref z);
-            for(k=0; k<=n-1; k++)
-            {
-                x[k] = y[k]-(y[0]+beta/gamma*y[n-1])/(1+z[0]+beta/gamma*z[n-1])*z[k];
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine. Three-point differentiation
-        *************************************************************************/
-        private static double diffthreepoint(double t,
-            double x0,
-            double f0,
-            double x1,
-            double f1,
-            double x2,
-            double f2)
-        {
-            double result = 0;
-            double a = 0;
-            double b = 0;
-
-            t = t-x0;
-            x1 = x1-x0;
-            x2 = x2-x0;
-            a = (f2-f0-x2/x1*(f1-f0))/(math.sqr(x2)-x1*x2);
-            b = (f1-f0-a*math.sqr(x1))/x1;
-            result = 2*a*t+b;
-            return result;
-        }
-
-
-    }
-    public class spline2d
-    {
-        /*************************************************************************
-        2-dimensional spline inteprolant
-        *************************************************************************/
-        public class spline2dinterpolant
-        {
-            public int k;
-            public double[] c;
-            public spline2dinterpolant()
-            {
-                c = new double[0];
-            }
-        };
-
-
-
-
-        /*************************************************************************
-        This subroutine builds bilinear spline coefficients table.
-
-        Input parameters:
-            X   -   spline abscissas, array[0..N-1]
-            Y   -   spline ordinates, array[0..M-1]
-            F   -   function values, array[0..M-1,0..N-1]
-            M,N -   grid size, M>=2, N>=2
-
-        Output parameters:
-            C   -   spline interpolant
-
-          -- ALGLIB PROJECT --
-             Copyright 05.07.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dbuildbilinear(double[] x,
-            double[] y,
-            double[,] f,
-            int m,
-            int n,
-            spline2dinterpolant c)
-        {
-            int i = 0;
-            int j = 0;
-            int k = 0;
-            int tblsize = 0;
-            int shift = 0;
-            double t = 0;
-            double[,] dx = new double[0,0];
-            double[,] dy = new double[0,0];
-            double[,] dxy = new double[0,0];
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            f = (double[,])f.Clone();
-
-            ap.assert(n>=2 & m>=2, "Spline2DBuildBilinear: N<2 or M<2!");
-            
-            //
-            // Sort points
-            //
-            for(j=0; j<=n-1; j++)
-            {
-                k = j;
-                for(i=j+1; i<=n-1; i++)
-                {
-                    if( (double)(x[i])<(double)(x[k]) )
-                    {
-                        k = i;
-                    }
-                }
-                if( k!=j )
-                {
-                    for(i=0; i<=m-1; i++)
-                    {
-                        t = f[i,j];
-                        f[i,j] = f[i,k];
-                        f[i,k] = t;
-                    }
-                    t = x[j];
-                    x[j] = x[k];
-                    x[k] = t;
-                }
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                k = i;
-                for(j=i+1; j<=m-1; j++)
-                {
-                    if( (double)(y[j])<(double)(y[k]) )
-                    {
-                        k = j;
-                    }
-                }
-                if( k!=i )
-                {
-                    for(j=0; j<=n-1; j++)
-                    {
-                        t = f[i,j];
-                        f[i,j] = f[k,j];
-                        f[k,j] = t;
-                    }
-                    t = y[i];
-                    y[i] = y[k];
-                    y[k] = t;
-                }
-            }
-            
-            //
-            // Fill C:
-            //  C[0]            -   length(C)
-            //  C[1]            -   type(C):
-            //                      -1 = bilinear interpolant
-            //                      -3 = general cubic spline
-            //                           (see BuildBicubicSpline)
-            //  C[2]:
-            //      N (x count)
-            //  C[3]:
-            //      M (y count)
-            //  C[4]...C[4+N-1]:
-            //      x[i], i = 0...N-1
-            //  C[4+N]...C[4+N+M-1]:
-            //      y[i], i = 0...M-1
-            //  C[4+N+M]...C[4+N+M+(N*M-1)]:
-            //      f(i,j) table. f(0,0), f(0, 1), f(0,2) and so on...
-            //
-            c.k = 1;
-            tblsize = 4+n+m+n*m;
-            c.c = new double[tblsize-1+1];
-            c.c[0] = tblsize;
-            c.c[1] = -1;
-            c.c[2] = n;
-            c.c[3] = m;
-            for(i=0; i<=n-1; i++)
-            {
-                c.c[4+i] = x[i];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                c.c[4+n+i] = y[i];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    shift = i*n+j;
-                    c.c[4+n+m+shift] = f[i,j];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine builds bicubic spline coefficients table.
-
-        Input parameters:
-            X   -   spline abscissas, array[0..N-1]
-            Y   -   spline ordinates, array[0..M-1]
-            F   -   function values, array[0..M-1,0..N-1]
-            M,N -   grid size, M>=2, N>=2
-
-        Output parameters:
-            C   -   spline interpolant
-
-          -- ALGLIB PROJECT --
-             Copyright 05.07.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dbuildbicubic(double[] x,
-            double[] y,
-            double[,] f,
-            int m,
-            int n,
-            spline2dinterpolant c)
-        {
-            int i = 0;
-            int j = 0;
-            int k = 0;
-            int tblsize = 0;
-            int shift = 0;
-            double t = 0;
-            double[,] dx = new double[0,0];
-            double[,] dy = new double[0,0];
-            double[,] dxy = new double[0,0];
-
-            x = (double[])x.Clone();
-            y = (double[])y.Clone();
-            f = (double[,])f.Clone();
-
-            ap.assert(n>=2 & m>=2, "BuildBicubicSpline: N<2 or M<2!");
-            
-            //
-            // Sort points
-            //
-            for(j=0; j<=n-1; j++)
-            {
-                k = j;
-                for(i=j+1; i<=n-1; i++)
-                {
-                    if( (double)(x[i])<(double)(x[k]) )
-                    {
-                        k = i;
-                    }
-                }
-                if( k!=j )
-                {
-                    for(i=0; i<=m-1; i++)
-                    {
-                        t = f[i,j];
-                        f[i,j] = f[i,k];
-                        f[i,k] = t;
-                    }
-                    t = x[j];
-                    x[j] = x[k];
-                    x[k] = t;
-                }
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                k = i;
-                for(j=i+1; j<=m-1; j++)
-                {
-                    if( (double)(y[j])<(double)(y[k]) )
-                    {
-                        k = j;
-                    }
-                }
-                if( k!=i )
-                {
-                    for(j=0; j<=n-1; j++)
-                    {
-                        t = f[i,j];
-                        f[i,j] = f[k,j];
-                        f[k,j] = t;
-                    }
-                    t = y[i];
-                    y[i] = y[k];
-                    y[k] = t;
-                }
-            }
-            
-            //
-            // Fill C:
-            //  C[0]            -   length(C)
-            //  C[1]            -   type(C):
-            //                      -1 = bilinear interpolant
-            //                           (see BuildBilinearInterpolant)
-            //                      -3 = general cubic spline
-            //  C[2]:
-            //      N (x count)
-            //  C[3]:
-            //      M (y count)
-            //  C[4]...C[4+N-1]:
-            //      x[i], i = 0...N-1
-            //  C[4+N]...C[4+N+M-1]:
-            //      y[i], i = 0...M-1
-            //  C[4+N+M]...C[4+N+M+(N*M-1)]:
-            //      f(i,j) table. f(0,0), f(0, 1), f(0,2) and so on...
-            //  C[4+N+M+N*M]...C[4+N+M+(2*N*M-1)]:
-            //      df(i,j)/dx table.
-            //  C[4+N+M+2*N*M]...C[4+N+M+(3*N*M-1)]:
-            //      df(i,j)/dy table.
-            //  C[4+N+M+3*N*M]...C[4+N+M+(4*N*M-1)]:
-            //      d2f(i,j)/dxdy table.
-            //
-            c.k = 3;
-            tblsize = 4+n+m+4*n*m;
-            c.c = new double[tblsize-1+1];
-            c.c[0] = tblsize;
-            c.c[1] = -3;
-            c.c[2] = n;
-            c.c[3] = m;
-            for(i=0; i<=n-1; i++)
-            {
-                c.c[4+i] = x[i];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                c.c[4+n+i] = y[i];
-            }
-            bicubiccalcderivatives(f, x, y, m, n, ref dx, ref dy, ref dxy);
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    shift = i*n+j;
-                    c.c[4+n+m+shift] = f[i,j];
-                    c.c[4+n+m+n*m+shift] = dx[i,j];
-                    c.c[4+n+m+2*n*m+shift] = dy[i,j];
-                    c.c[4+n+m+3*n*m+shift] = dxy[i,j];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine calculates the value of the bilinear or bicubic spline  at
-        the given point X.
-
-        Input parameters:
-            C   -   coefficients table.
-                    Built by BuildBilinearSpline or BuildBicubicSpline.
-            X, Y-   point
-
-        Result:
-            S(x,y)
-
-          -- ALGLIB PROJECT --
-             Copyright 05.07.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static double spline2dcalc(spline2dinterpolant c,
-            double x,
-            double y)
-        {
-            double result = 0;
-            double v = 0;
-            double vx = 0;
-            double vy = 0;
-            double vxy = 0;
-
-            spline2ddiff(c, x, y, ref v, ref vx, ref vy, ref vxy);
-            result = v;
-            return result;
-        }
-
-
-        /*************************************************************************
-        This subroutine calculates the value of the bilinear or bicubic spline  at
-        the given point X and its derivatives.
-
-        Input parameters:
-            C   -   spline interpolant.
-            X, Y-   point
-
-        Output parameters:
-            F   -   S(x,y)
-            FX  -   dS(x,y)/dX
-            FY  -   dS(x,y)/dY
-            FXY -   d2S(x,y)/dXdY
-
-          -- ALGLIB PROJECT --
-             Copyright 05.07.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2ddiff(spline2dinterpolant c,
-            double x,
-            double y,
-            ref double f,
-            ref double fx,
-            ref double fy,
-            ref double fxy)
-        {
-            int n = 0;
-            int m = 0;
-            double t = 0;
-            double dt = 0;
-            double u = 0;
-            double du = 0;
-            int ix = 0;
-            int iy = 0;
-            int l = 0;
-            int r = 0;
-            int h = 0;
-            int shift1 = 0;
-            int s1 = 0;
-            int s2 = 0;
-            int s3 = 0;
-            int s4 = 0;
-            int sf = 0;
-            int sfx = 0;
-            int sfy = 0;
-            int sfxy = 0;
-            double y1 = 0;
-            double y2 = 0;
-            double y3 = 0;
-            double y4 = 0;
-            double v = 0;
-            double t0 = 0;
-            double t1 = 0;
-            double t2 = 0;
-            double t3 = 0;
-            double u0 = 0;
-            double u1 = 0;
-            double u2 = 0;
-            double u3 = 0;
-
-            f = 0;
-            fx = 0;
-            fy = 0;
-            fxy = 0;
-
-            ap.assert((int)Math.Round(c.c[1])==-1 | (int)Math.Round(c.c[1])==-3, "Spline2DDiff: incorrect C!");
-            n = (int)Math.Round(c.c[2]);
-            m = (int)Math.Round(c.c[3]);
-            
-            //
-            // Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
-            //
-            l = 4;
-            r = 4+n-2+1;
-            while( l!=r-1 )
-            {
-                h = (l+r)/2;
-                if( (double)(c.c[h])>=(double)(x) )
-                {
-                    r = h;
-                }
-                else
-                {
-                    l = h;
-                }
-            }
-            t = (x-c.c[l])/(c.c[l+1]-c.c[l]);
-            dt = 1.0/(c.c[l+1]-c.c[l]);
-            ix = l-4;
-            
-            //
-            // Binary search in the [ y[0], ..., y[m-2] ] (y[m-1] is not included)
-            //
-            l = 4+n;
-            r = 4+n+(m-2)+1;
-            while( l!=r-1 )
-            {
-                h = (l+r)/2;
-                if( (double)(c.c[h])>=(double)(y) )
-                {
-                    r = h;
-                }
-                else
-                {
-                    l = h;
-                }
-            }
-            u = (y-c.c[l])/(c.c[l+1]-c.c[l]);
-            du = 1.0/(c.c[l+1]-c.c[l]);
-            iy = l-(4+n);
-            
-            //
-            // Prepare F, dF/dX, dF/dY, d2F/dXdY
-            //
-            f = 0;
-            fx = 0;
-            fy = 0;
-            fxy = 0;
-            
-            //
-            // Bilinear interpolation
-            //
-            if( (int)Math.Round(c.c[1])==-1 )
-            {
-                shift1 = 4+n+m;
-                y1 = c.c[shift1+n*iy+ix];
-                y2 = c.c[shift1+n*iy+(ix+1)];
-                y3 = c.c[shift1+n*(iy+1)+(ix+1)];
-                y4 = c.c[shift1+n*(iy+1)+ix];
-                f = (1-t)*(1-u)*y1+t*(1-u)*y2+t*u*y3+(1-t)*u*y4;
-                fx = (-((1-u)*y1)+(1-u)*y2+u*y3-u*y4)*dt;
-                fy = (-((1-t)*y1)-t*y2+t*y3+(1-t)*y4)*du;
-                fxy = (y1-y2+y3-y4)*du*dt;
-                return;
-            }
-            
-            //
-            // Bicubic interpolation
-            //
-            if( (int)Math.Round(c.c[1])==-3 )
-            {
-                
-                //
-                // Prepare info
-                //
-                t0 = 1;
-                t1 = t;
-                t2 = math.sqr(t);
-                t3 = t*t2;
-                u0 = 1;
-                u1 = u;
-                u2 = math.sqr(u);
-                u3 = u*u2;
-                sf = 4+n+m;
-                sfx = 4+n+m+n*m;
-                sfy = 4+n+m+2*n*m;
-                sfxy = 4+n+m+3*n*m;
-                s1 = n*iy+ix;
-                s2 = n*iy+(ix+1);
-                s3 = n*(iy+1)+(ix+1);
-                s4 = n*(iy+1)+ix;
-                
-                //
-                // Calculate
-                //
-                v = 1*c.c[sf+s1];
-                f = f+v*t0*u0;
-                v = 1*c.c[sfy+s1]/du;
-                f = f+v*t0*u1;
-                fy = fy+1*v*t0*u0*du;
-                v = -(3*c.c[sf+s1])+3*c.c[sf+s4]-2*c.c[sfy+s1]/du-1*c.c[sfy+s4]/du;
-                f = f+v*t0*u2;
-                fy = fy+2*v*t0*u1*du;
-                v = 2*c.c[sf+s1]-2*c.c[sf+s4]+1*c.c[sfy+s1]/du+1*c.c[sfy+s4]/du;
-                f = f+v*t0*u3;
-                fy = fy+3*v*t0*u2*du;
-                v = 1*c.c[sfx+s1]/dt;
-                f = f+v*t1*u0;
-                fx = fx+1*v*t0*u0*dt;
-                v = 1*c.c[sfxy+s1]/(dt*du);
-                f = f+v*t1*u1;
-                fx = fx+1*v*t0*u1*dt;
-                fy = fy+1*v*t1*u0*du;
-                fxy = fxy+1*v*t0*u0*dt*du;
-                v = -(3*c.c[sfx+s1]/dt)+3*c.c[sfx+s4]/dt-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
-                f = f+v*t1*u2;
-                fx = fx+1*v*t0*u2*dt;
-                fy = fy+2*v*t1*u1*du;
-                fxy = fxy+2*v*t0*u1*dt*du;
-                v = 2*c.c[sfx+s1]/dt-2*c.c[sfx+s4]/dt+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
-                f = f+v*t1*u3;
-                fx = fx+1*v*t0*u3*dt;
-                fy = fy+3*v*t1*u2*du;
-                fxy = fxy+3*v*t0*u2*dt*du;
-                v = -(3*c.c[sf+s1])+3*c.c[sf+s2]-2*c.c[sfx+s1]/dt-1*c.c[sfx+s2]/dt;
-                f = f+v*t2*u0;
-                fx = fx+2*v*t1*u0*dt;
-                v = -(3*c.c[sfy+s1]/du)+3*c.c[sfy+s2]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du);
-                f = f+v*t2*u1;
-                fx = fx+2*v*t1*u1*dt;
-                fy = fy+1*v*t2*u0*du;
-                fxy = fxy+2*v*t1*u0*dt*du;
-                v = 9*c.c[sf+s1]-9*c.c[sf+s2]+9*c.c[sf+s3]-9*c.c[sf+s4]+6*c.c[sfx+s1]/dt+3*c.c[sfx+s2]/dt-3*c.c[sfx+s3]/dt-6*c.c[sfx+s4]/dt+6*c.c[sfy+s1]/du-6*c.c[sfy+s2]/du-3*c.c[sfy+s3]/du+3*c.c[sfy+s4]/du+4*c.c[sfxy+s1]/(dt*du)+2*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+2*c.c[sfxy+s4]/(dt*du);
-                f = f+v*t2*u2;
-                fx = fx+2*v*t1*u2*dt;
-                fy = fy+2*v*t2*u1*du;
-                fxy = fxy+4*v*t1*u1*dt*du;
-                v = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-4*c.c[sfx+s1]/dt-2*c.c[sfx+s2]/dt+2*c.c[sfx+s3]/dt+4*c.c[sfx+s4]/dt-3*c.c[sfy+s1]/du+3*c.c[sfy+s2]/du+3*c.c[sfy+s3]/du-3*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-2*c.c[sfxy+s4]/(dt*du);
-                f = f+v*t2*u3;
-                fx = fx+2*v*t1*u3*dt;
-                fy = fy+3*v*t2*u2*du;
-                fxy = fxy+6*v*t1*u2*dt*du;
-                v = 2*c.c[sf+s1]-2*c.c[sf+s2]+1*c.c[sfx+s1]/dt+1*c.c[sfx+s2]/dt;
-                f = f+v*t3*u0;
-                fx = fx+3*v*t2*u0*dt;
-                v = 2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du);
-                f = f+v*t3*u1;
-                fx = fx+3*v*t2*u1*dt;
-                fy = fy+1*v*t3*u0*du;
-                fxy = fxy+3*v*t2*u0*dt*du;
-                v = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-3*c.c[sfx+s1]/dt-3*c.c[sfx+s2]/dt+3*c.c[sfx+s3]/dt+3*c.c[sfx+s4]/dt-4*c.c[sfy+s1]/du+4*c.c[sfy+s2]/du+2*c.c[sfy+s3]/du-2*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-2*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
-                f = f+v*t3*u2;
-                fx = fx+3*v*t2*u2*dt;
-                fy = fy+2*v*t3*u1*du;
-                fxy = fxy+6*v*t2*u1*dt*du;
-                v = 4*c.c[sf+s1]-4*c.c[sf+s2]+4*c.c[sf+s3]-4*c.c[sf+s4]+2*c.c[sfx+s1]/dt+2*c.c[sfx+s2]/dt-2*c.c[sfx+s3]/dt-2*c.c[sfx+s4]/dt+2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du-2*c.c[sfy+s3]/du+2*c.c[sfy+s4]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
-                f = f+v*t3*u3;
-                fx = fx+3*v*t2*u3*dt;
-                fy = fy+3*v*t3*u2*du;
-                fxy = fxy+9*v*t2*u2*dt*du;
-                return;
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine unpacks two-dimensional spline into the coefficients table
-
-        Input parameters:
-            C   -   spline interpolant.
-
-        Result:
-            M, N-   grid size (x-axis and y-axis)
-            Tbl -   coefficients table, unpacked format,
-                    [0..(N-1)*(M-1)-1, 0..19].
-                    For I = 0...M-2, J=0..N-2:
-                        K =  I*(N-1)+J
-                        Tbl[K,0] = X[j]
-                        Tbl[K,1] = X[j+1]
-                        Tbl[K,2] = Y[i]
-                        Tbl[K,3] = Y[i+1]
-                        Tbl[K,4] = C00
-                        Tbl[K,5] = C01
-                        Tbl[K,6] = C02
-                        Tbl[K,7] = C03
-                        Tbl[K,8] = C10
-                        Tbl[K,9] = C11
-                        ...
-                        Tbl[K,19] = C33
-                    On each grid square spline is equals to:
-                        S(x) = SUM(c[i,j]*(x^i)*(y^j), i=0..3, j=0..3)
-                        t = x-x[j]
-                        u = y-y[i]
-
-          -- ALGLIB PROJECT --
-             Copyright 29.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dunpack(spline2dinterpolant c,
-            ref int m,
-            ref int n,
-            ref double[,] tbl)
-        {
-            int i = 0;
-            int j = 0;
-            int ci = 0;
-            int cj = 0;
-            int k = 0;
-            int p = 0;
-            int shift = 0;
-            int s1 = 0;
-            int s2 = 0;
-            int s3 = 0;
-            int s4 = 0;
-            int sf = 0;
-            int sfx = 0;
-            int sfy = 0;
-            int sfxy = 0;
-            double y1 = 0;
-            double y2 = 0;
-            double y3 = 0;
-            double y4 = 0;
-            double dt = 0;
-            double du = 0;
-
-            m = 0;
-            n = 0;
-            tbl = new double[0,0];
-
-            ap.assert((int)Math.Round(c.c[1])==-3 | (int)Math.Round(c.c[1])==-1, "SplineUnpack2D: incorrect C!");
-            n = (int)Math.Round(c.c[2]);
-            m = (int)Math.Round(c.c[3]);
-            tbl = new double[(n-1)*(m-1)-1+1, 19+1];
-            
-            //
-            // Fill
-            //
-            for(i=0; i<=m-2; i++)
-            {
-                for(j=0; j<=n-2; j++)
-                {
-                    p = i*(n-1)+j;
-                    tbl[p,0] = c.c[4+j];
-                    tbl[p,1] = c.c[4+j+1];
-                    tbl[p,2] = c.c[4+n+i];
-                    tbl[p,3] = c.c[4+n+i+1];
-                    dt = 1/(tbl[p,1]-tbl[p,0]);
-                    du = 1/(tbl[p,3]-tbl[p,2]);
-                    
-                    //
-                    // Bilinear interpolation
-                    //
-                    if( (int)Math.Round(c.c[1])==-1 )
-                    {
-                        for(k=4; k<=19; k++)
-                        {
-                            tbl[p,k] = 0;
-                        }
-                        shift = 4+n+m;
-                        y1 = c.c[shift+n*i+j];
-                        y2 = c.c[shift+n*i+(j+1)];
-                        y3 = c.c[shift+n*(i+1)+(j+1)];
-                        y4 = c.c[shift+n*(i+1)+j];
-                        tbl[p,4] = y1;
-                        tbl[p,4+1*4+0] = y2-y1;
-                        tbl[p,4+0*4+1] = y4-y1;
-                        tbl[p,4+1*4+1] = y3-y2-y4+y1;
-                    }
-                    
-                    //
-                    // Bicubic interpolation
-                    //
-                    if( (int)Math.Round(c.c[1])==-3 )
-                    {
-                        sf = 4+n+m;
-                        sfx = 4+n+m+n*m;
-                        sfy = 4+n+m+2*n*m;
-                        sfxy = 4+n+m+3*n*m;
-                        s1 = n*i+j;
-                        s2 = n*i+(j+1);
-                        s3 = n*(i+1)+(j+1);
-                        s4 = n*(i+1)+j;
-                        tbl[p,4+0*4+0] = 1*c.c[sf+s1];
-                        tbl[p,4+0*4+1] = 1*c.c[sfy+s1]/du;
-                        tbl[p,4+0*4+2] = -(3*c.c[sf+s1])+3*c.c[sf+s4]-2*c.c[sfy+s1]/du-1*c.c[sfy+s4]/du;
-                        tbl[p,4+0*4+3] = 2*c.c[sf+s1]-2*c.c[sf+s4]+1*c.c[sfy+s1]/du+1*c.c[sfy+s4]/du;
-                        tbl[p,4+1*4+0] = 1*c.c[sfx+s1]/dt;
-                        tbl[p,4+1*4+1] = 1*c.c[sfxy+s1]/(dt*du);
-                        tbl[p,4+1*4+2] = -(3*c.c[sfx+s1]/dt)+3*c.c[sfx+s4]/dt-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
-                        tbl[p,4+1*4+3] = 2*c.c[sfx+s1]/dt-2*c.c[sfx+s4]/dt+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
-                        tbl[p,4+2*4+0] = -(3*c.c[sf+s1])+3*c.c[sf+s2]-2*c.c[sfx+s1]/dt-1*c.c[sfx+s2]/dt;
-                        tbl[p,4+2*4+1] = -(3*c.c[sfy+s1]/du)+3*c.c[sfy+s2]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du);
-                        tbl[p,4+2*4+2] = 9*c.c[sf+s1]-9*c.c[sf+s2]+9*c.c[sf+s3]-9*c.c[sf+s4]+6*c.c[sfx+s1]/dt+3*c.c[sfx+s2]/dt-3*c.c[sfx+s3]/dt-6*c.c[sfx+s4]/dt+6*c.c[sfy+s1]/du-6*c.c[sfy+s2]/du-3*c.c[sfy+s3]/du+3*c.c[sfy+s4]/du+4*c.c[sfxy+s1]/(dt*du)+2*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+2*c.c[sfxy+s4]/(dt*du);
-                        tbl[p,4+2*4+3] = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-4*c.c[sfx+s1]/dt-2*c.c[sfx+s2]/dt+2*c.c[sfx+s3]/dt+4*c.c[sfx+s4]/dt-3*c.c[sfy+s1]/du+3*c.c[sfy+s2]/du+3*c.c[sfy+s3]/du-3*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-2*c.c[sfxy+s4]/(dt*du);
-                        tbl[p,4+3*4+0] = 2*c.c[sf+s1]-2*c.c[sf+s2]+1*c.c[sfx+s1]/dt+1*c.c[sfx+s2]/dt;
-                        tbl[p,4+3*4+1] = 2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du);
-                        tbl[p,4+3*4+2] = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-3*c.c[sfx+s1]/dt-3*c.c[sfx+s2]/dt+3*c.c[sfx+s3]/dt+3*c.c[sfx+s4]/dt-4*c.c[sfy+s1]/du+4*c.c[sfy+s2]/du+2*c.c[sfy+s3]/du-2*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-2*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
-                        tbl[p,4+3*4+3] = 4*c.c[sf+s1]-4*c.c[sf+s2]+4*c.c[sf+s3]-4*c.c[sf+s4]+2*c.c[sfx+s1]/dt+2*c.c[sfx+s2]/dt-2*c.c[sfx+s3]/dt-2*c.c[sfx+s4]/dt+2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du-2*c.c[sfy+s3]/du+2*c.c[sfy+s4]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
-                    }
-                    
-                    //
-                    // Rescale Cij
-                    //
-                    for(ci=0; ci<=3; ci++)
-                    {
-                        for(cj=0; cj<=3; cj++)
-                        {
-                            tbl[p,4+ci*4+cj] = tbl[p,4+ci*4+cj]*Math.Pow(dt, ci)*Math.Pow(du, cj);
-                        }
-                    }
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine performs linear transformation of the spline argument.
-
-        Input parameters:
-            C       -   spline interpolant
-            AX, BX  -   transformation coefficients: x = A*t + B
-            AY, BY  -   transformation coefficients: y = A*u + B
-        Result:
-            C   -   transformed spline
-
-          -- ALGLIB PROJECT --
-             Copyright 30.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dlintransxy(spline2dinterpolant c,
-            double ax,
-            double bx,
-            double ay,
-            double by)
-        {
-            int i = 0;
-            int j = 0;
-            int n = 0;
-            int m = 0;
-            double v = 0;
-            double[] x = new double[0];
-            double[] y = new double[0];
-            double[,] f = new double[0,0];
-            int typec = 0;
-
-            typec = (int)Math.Round(c.c[1]);
-            ap.assert(typec==-3 | typec==-1, "Spline2DLinTransXY: incorrect C!");
-            n = (int)Math.Round(c.c[2]);
-            m = (int)Math.Round(c.c[3]);
-            x = new double[n-1+1];
-            y = new double[m-1+1];
-            f = new double[m-1+1, n-1+1];
-            for(j=0; j<=n-1; j++)
-            {
-                x[j] = c.c[4+j];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                y[i] = c.c[4+n+i];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    f[i,j] = c.c[4+n+m+i*n+j];
-                }
-            }
-            
-            //
-            // Special case: AX=0 or AY=0
-            //
-            if( (double)(ax)==(double)(0) )
-            {
-                for(i=0; i<=m-1; i++)
-                {
-                    v = spline2dcalc(c, bx, y[i]);
-                    for(j=0; j<=n-1; j++)
-                    {
-                        f[i,j] = v;
-                    }
-                }
-                if( typec==-3 )
-                {
-                    spline2dbuildbicubic(x, y, f, m, n, c);
-                }
-                if( typec==-1 )
-                {
-                    spline2dbuildbilinear(x, y, f, m, n, c);
-                }
-                ax = 1;
-                bx = 0;
-            }
-            if( (double)(ay)==(double)(0) )
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    v = spline2dcalc(c, x[j], by);
-                    for(i=0; i<=m-1; i++)
-                    {
-                        f[i,j] = v;
-                    }
-                }
-                if( typec==-3 )
-                {
-                    spline2dbuildbicubic(x, y, f, m, n, c);
-                }
-                if( typec==-1 )
-                {
-                    spline2dbuildbilinear(x, y, f, m, n, c);
-                }
-                ay = 1;
-                by = 0;
-            }
-            
-            //
-            // General case: AX<>0, AY<>0
-            // Unpack, scale and pack again.
-            //
-            for(j=0; j<=n-1; j++)
-            {
-                x[j] = (x[j]-bx)/ax;
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                y[i] = (y[i]-by)/ay;
-            }
-            if( typec==-3 )
-            {
-                spline2dbuildbicubic(x, y, f, m, n, c);
-            }
-            if( typec==-1 )
-            {
-                spline2dbuildbilinear(x, y, f, m, n, c);
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine performs linear transformation of the spline.
-
-        Input parameters:
-            C   -   spline interpolant.
-            A, B-   transformation coefficients: S2(x,y) = A*S(x,y) + B
-            
-        Output parameters:
-            C   -   transformed spline
-
-          -- ALGLIB PROJECT --
-             Copyright 30.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dlintransf(spline2dinterpolant c,
-            double a,
-            double b)
-        {
-            int i = 0;
-            int j = 0;
-            int n = 0;
-            int m = 0;
-            double[] x = new double[0];
-            double[] y = new double[0];
-            double[,] f = new double[0,0];
-            int typec = 0;
-
-            typec = (int)Math.Round(c.c[1]);
-            ap.assert(typec==-3 | typec==-1, "Spline2DLinTransXY: incorrect C!");
-            n = (int)Math.Round(c.c[2]);
-            m = (int)Math.Round(c.c[3]);
-            x = new double[n-1+1];
-            y = new double[m-1+1];
-            f = new double[m-1+1, n-1+1];
-            for(j=0; j<=n-1; j++)
-            {
-                x[j] = c.c[4+j];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                y[i] = c.c[4+n+i];
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    f[i,j] = a*c.c[4+n+m+i*n+j]+b;
-                }
-            }
-            if( typec==-3 )
-            {
-                spline2dbuildbicubic(x, y, f, m, n, c);
-            }
-            if( typec==-1 )
-            {
-                spline2dbuildbilinear(x, y, f, m, n, c);
-            }
-        }
-
-
-        /*************************************************************************
-        This subroutine makes the copy of the spline model.
-
-        Input parameters:
-            C   -   spline interpolant
-
-        Output parameters:
-            CC  -   spline copy
-
-          -- ALGLIB PROJECT --
-             Copyright 29.06.2007 by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dcopy(spline2dinterpolant c,
-            spline2dinterpolant cc)
-        {
-            int n = 0;
-            int i_ = 0;
-
-            ap.assert(c.k==1 | c.k==3, "Spline2DCopy: incorrect C!");
-            cc.k = c.k;
-            n = (int)Math.Round(c.c[0]);
-            cc.c = new double[n];
-            for(i_=0; i_<=n-1;i_++)
-            {
-                cc.c[i_] = c.c[i_];
-            }
-        }
-
-
-        /*************************************************************************
-        Bicubic spline resampling
-
-        Input parameters:
-            A           -   function values at the old grid,
-                            array[0..OldHeight-1, 0..OldWidth-1]
-            OldHeight   -   old grid height, OldHeight>1
-            OldWidth    -   old grid width, OldWidth>1
-            NewHeight   -   new grid height, NewHeight>1
-            NewWidth    -   new grid width, NewWidth>1
-            
-        Output parameters:
-            B           -   function values at the new grid,
-                            array[0..NewHeight-1, 0..NewWidth-1]
-
-          -- ALGLIB routine --
-             15 May, 2007
-             Copyright by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dresamplebicubic(double[,] a,
-            int oldheight,
-            int oldwidth,
-            ref double[,] b,
-            int newheight,
-            int newwidth)
-        {
-            double[,] buf = new double[0,0];
-            double[] x = new double[0];
-            double[] y = new double[0];
-            spline1d.spline1dinterpolant c = new spline1d.spline1dinterpolant();
-            int i = 0;
-            int j = 0;
-            int mw = 0;
-            int mh = 0;
-
-            b = new double[0,0];
-
-            ap.assert(oldwidth>1 & oldheight>1, "Spline2DResampleBicubic: width/height less than 1");
-            ap.assert(newwidth>1 & newheight>1, "Spline2DResampleBicubic: width/height less than 1");
-            
-            //
-            // Prepare
-            //
-            mw = Math.Max(oldwidth, newwidth);
-            mh = Math.Max(oldheight, newheight);
-            b = new double[newheight-1+1, newwidth-1+1];
-            buf = new double[oldheight-1+1, newwidth-1+1];
-            x = new double[Math.Max(mw, mh)-1+1];
-            y = new double[Math.Max(mw, mh)-1+1];
-            
-            //
-            // Horizontal interpolation
-            //
-            for(i=0; i<=oldheight-1; i++)
-            {
-                
-                //
-                // Fill X, Y
-                //
-                for(j=0; j<=oldwidth-1; j++)
-                {
-                    x[j] = (double)j/(double)(oldwidth-1);
-                    y[j] = a[i,j];
-                }
-                
-                //
-                // Interpolate and place result into temporary matrix
-                //
-                spline1d.spline1dbuildcubic(x, y, oldwidth, 0, 0.0, 0, 0.0, c);
-                for(j=0; j<=newwidth-1; j++)
-                {
-                    buf[i,j] = spline1d.spline1dcalc(c, (double)j/(double)(newwidth-1));
-                }
-            }
-            
-            //
-            // Vertical interpolation
-            //
-            for(j=0; j<=newwidth-1; j++)
-            {
-                
-                //
-                // Fill X, Y
-                //
-                for(i=0; i<=oldheight-1; i++)
-                {
-                    x[i] = (double)i/(double)(oldheight-1);
-                    y[i] = buf[i,j];
-                }
-                
-                //
-                // Interpolate and place result into B
-                //
-                spline1d.spline1dbuildcubic(x, y, oldheight, 0, 0.0, 0, 0.0, c);
-                for(i=0; i<=newheight-1; i++)
-                {
-                    b[i,j] = spline1d.spline1dcalc(c, (double)i/(double)(newheight-1));
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Bilinear spline resampling
-
-        Input parameters:
-            A           -   function values at the old grid,
-                            array[0..OldHeight-1, 0..OldWidth-1]
-            OldHeight   -   old grid height, OldHeight>1
-            OldWidth    -   old grid width, OldWidth>1
-            NewHeight   -   new grid height, NewHeight>1
-            NewWidth    -   new grid width, NewWidth>1
-
-        Output parameters:
-            B           -   function values at the new grid,
-                            array[0..NewHeight-1, 0..NewWidth-1]
-
-          -- ALGLIB routine --
-             09.07.2007
-             Copyright by Bochkanov Sergey
-        *************************************************************************/
-        public static void spline2dresamplebilinear(double[,] a,
-            int oldheight,
-            int oldwidth,
-            ref double[,] b,
-            int newheight,
-            int newwidth)
-        {
-            int i = 0;
-            int j = 0;
-            int l = 0;
-            int c = 0;
-            double t = 0;
-            double u = 0;
-
-            b = new double[0,0];
-
-            b = new double[newheight-1+1, newwidth-1+1];
-            for(i=0; i<=newheight-1; i++)
-            {
-                for(j=0; j<=newwidth-1; j++)
-                {
-                    l = i*(oldheight-1)/(newheight-1);
-                    if( l==oldheight-1 )
-                    {
-                        l = oldheight-2;
-                    }
-                    u = (double)i/(double)(newheight-1)*(oldheight-1)-l;
-                    c = j*(oldwidth-1)/(newwidth-1);
-                    if( c==oldwidth-1 )
-                    {
-                        c = oldwidth-2;
-                    }
-                    t = (double)(j*(oldwidth-1))/(double)(newwidth-1)-c;
-                    b[i,j] = (1-t)*(1-u)*a[l,c]+t*(1-u)*a[l,c+1]+t*u*a[l+1,c+1]+(1-t)*u*a[l+1,c];
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Internal subroutine.
-        Calculation of the first derivatives and the cross-derivative.
-        *************************************************************************/
-        private static void bicubiccalcderivatives(double[,] a,
-            double[] x,
-            double[] y,
-            int m,
-            int n,
-            ref double[,] dx,
-            ref double[,] dy,
-            ref double[,] dxy)
-        {
-            int i = 0;
-            int j = 0;
-            double[] xt = new double[0];
-            double[] ft = new double[0];
-            double s = 0;
-            double ds = 0;
-            double d2s = 0;
-            spline1d.spline1dinterpolant c = new spline1d.spline1dinterpolant();
-
-            dx = new double[0,0];
-            dy = new double[0,0];
-            dxy = new double[0,0];
-
-            dx = new double[m, n];
-            dy = new double[m, n];
-            dxy = new double[m, n];
-            
-            //
-            // dF/dX
-            //
-            xt = new double[n];
-            ft = new double[n];
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    xt[j] = x[j];
-                    ft[j] = a[i,j];
-                }
-                spline1d.spline1dbuildcubic(xt, ft, n, 0, 0.0, 0, 0.0, c);
-                for(j=0; j<=n-1; j++)
-                {
-                    spline1d.spline1ddiff(c, x[j], ref s, ref ds, ref d2s);
-                    dx[i,j] = ds;
-                }
-            }
-            
-            //
-            // dF/dY
-            //
-            xt = new double[m];
-            ft = new double[m];
-            for(j=0; j<=n-1; j++)
-            {
-                for(i=0; i<=m-1; i++)
-                {
-                    xt[i] = y[i];
-                    ft[i] = a[i,j];
-                }
-                spline1d.spline1dbuildcubic(xt, ft, m, 0, 0.0, 0, 0.0, c);
-                for(i=0; i<=m-1; i++)
-                {
-                    spline1d.spline1ddiff(c, y[i], ref s, ref ds, ref d2s);
-                    dy[i,j] = ds;
-                }
-            }
-            
-            //
-            // d2F/dXdY
-            //
-            xt = new double[n];
-            ft = new double[n];
-            for(i=0; i<=m-1; i++)
-            {
-                for(j=0; j<=n-1; j++)
-                {
-                    xt[j] = x[j];
-                    ft[j] = dy[i,j];
-                }
-                spline1d.spline1dbuildcubic(xt, ft, n, 0, 0.0, 0, 0.0, c);
-                for(j=0; j<=n-1; j++)
-                {
-                    spline1d.spline1ddiff(c, x[j], ref s, ref ds, ref d2s);
-                    dxy[i,j] = ds;
-                }
-            }
-        }
-
-
-    }
     public class idwint
     {
@@ -15830,4 +5737,9274 @@
 
     }
+    public class ratint
+    {
+        /*************************************************************************
+        Barycentric interpolant.
+        *************************************************************************/
+        public class barycentricinterpolant
+        {
+            public int n;
+            public double sy;
+            public double[] x;
+            public double[] y;
+            public double[] w;
+            public barycentricinterpolant()
+            {
+                x = new double[0];
+                y = new double[0];
+                w = new double[0];
+            }
+        };
+
+
+
+
+        /*************************************************************************
+        Rational interpolation using barycentric formula
+
+        F(t) = SUM(i=0,n-1,w[i]*f[i]/(t-x[i])) / SUM(i=0,n-1,w[i]/(t-x[i]))
+
+        Input parameters:
+            B   -   barycentric interpolant built with one of model building
+                    subroutines.
+            T   -   interpolation point
+
+        Result:
+            barycentric interpolant F(t)
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double barycentriccalc(barycentricinterpolant b,
+            double t)
+        {
+            double result = 0;
+            double s1 = 0;
+            double s2 = 0;
+            double s = 0;
+            double v = 0;
+            int i = 0;
+
+            ap.assert(!Double.IsInfinity(t), "BarycentricCalc: infinite T!");
+            
+            //
+            // special case: NaN
+            //
+            if( Double.IsNaN(t) )
+            {
+                result = Double.NaN;
+                return result;
+            }
+            
+            //
+            // special case: N=1
+            //
+            if( b.n==1 )
+            {
+                result = b.sy*b.y[0];
+                return result;
+            }
+            
+            //
+            // Here we assume that task is normalized, i.e.:
+            // 1. abs(Y[i])<=1
+            // 2. abs(W[i])<=1
+            // 3. X[] is ordered
+            //
+            s = Math.Abs(t-b.x[0]);
+            for(i=0; i<=b.n-1; i++)
+            {
+                v = b.x[i];
+                if( (double)(v)==(double)(t) )
+                {
+                    result = b.sy*b.y[i];
+                    return result;
+                }
+                v = Math.Abs(t-v);
+                if( (double)(v)<(double)(s) )
+                {
+                    s = v;
+                }
+            }
+            s1 = 0;
+            s2 = 0;
+            for(i=0; i<=b.n-1; i++)
+            {
+                v = s/(t-b.x[i]);
+                v = v*b.w[i];
+                s1 = s1+v*b.y[i];
+                s2 = s2+v;
+            }
+            result = b.sy*s1/s2;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Differentiation of barycentric interpolant: first derivative.
+
+        Algorithm used in this subroutine is very robust and should not fail until
+        provided with values too close to MaxRealNumber  (usually  MaxRealNumber/N
+        or greater will overflow).
+
+        INPUT PARAMETERS:
+            B   -   barycentric interpolant built with one of model building
+                    subroutines.
+            T   -   interpolation point
+
+        OUTPUT PARAMETERS:
+            F   -   barycentric interpolant at T
+            DF  -   first derivative
+            
+        NOTE
+
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentricdiff1(barycentricinterpolant b,
+            double t,
+            ref double f,
+            ref double df)
+        {
+            double v = 0;
+            double vv = 0;
+            int i = 0;
+            int k = 0;
+            double n0 = 0;
+            double n1 = 0;
+            double d0 = 0;
+            double d1 = 0;
+            double s0 = 0;
+            double s1 = 0;
+            double xk = 0;
+            double xi = 0;
+            double xmin = 0;
+            double xmax = 0;
+            double xscale1 = 0;
+            double xoffs1 = 0;
+            double xscale2 = 0;
+            double xoffs2 = 0;
+            double xprev = 0;
+
+            f = 0;
+            df = 0;
+
+            ap.assert(!Double.IsInfinity(t), "BarycentricDiff1: infinite T!");
+            
+            //
+            // special case: NaN
+            //
+            if( Double.IsNaN(t) )
+            {
+                f = Double.NaN;
+                df = Double.NaN;
+                return;
+            }
+            
+            //
+            // special case: N=1
+            //
+            if( b.n==1 )
+            {
+                f = b.sy*b.y[0];
+                df = 0;
+                return;
+            }
+            if( (double)(b.sy)==(double)(0) )
+            {
+                f = 0;
+                df = 0;
+                return;
+            }
+            ap.assert((double)(b.sy)>(double)(0), "BarycentricDiff1: internal error");
+            
+            //
+            // We assume than N>1 and B.SY>0. Find:
+            // 1. pivot point (X[i] closest to T)
+            // 2. width of interval containing X[i]
+            //
+            v = Math.Abs(b.x[0]-t);
+            k = 0;
+            xmin = b.x[0];
+            xmax = b.x[0];
+            for(i=1; i<=b.n-1; i++)
+            {
+                vv = b.x[i];
+                if( (double)(Math.Abs(vv-t))<(double)(v) )
+                {
+                    v = Math.Abs(vv-t);
+                    k = i;
+                }
+                xmin = Math.Min(xmin, vv);
+                xmax = Math.Max(xmax, vv);
+            }
+            
+            //
+            // pivot point found, calculate dNumerator and dDenominator
+            //
+            xscale1 = 1/(xmax-xmin);
+            xoffs1 = -(xmin/(xmax-xmin))+1;
+            xscale2 = 2;
+            xoffs2 = -3;
+            t = t*xscale1+xoffs1;
+            t = t*xscale2+xoffs2;
+            xk = b.x[k];
+            xk = xk*xscale1+xoffs1;
+            xk = xk*xscale2+xoffs2;
+            v = t-xk;
+            n0 = 0;
+            n1 = 0;
+            d0 = 0;
+            d1 = 0;
+            xprev = -2;
+            for(i=0; i<=b.n-1; i++)
+            {
+                xi = b.x[i];
+                xi = xi*xscale1+xoffs1;
+                xi = xi*xscale2+xoffs2;
+                ap.assert((double)(xi)>(double)(xprev), "BarycentricDiff1: points are too close!");
+                xprev = xi;
+                if( i!=k )
+                {
+                    vv = math.sqr(t-xi);
+                    s0 = (t-xk)/(t-xi);
+                    s1 = (xk-xi)/vv;
+                }
+                else
+                {
+                    s0 = 1;
+                    s1 = 0;
+                }
+                vv = b.w[i]*b.y[i];
+                n0 = n0+s0*vv;
+                n1 = n1+s1*vv;
+                vv = b.w[i];
+                d0 = d0+s0*vv;
+                d1 = d1+s1*vv;
+            }
+            f = b.sy*n0/d0;
+            df = (n1*d0-n0*d1)/math.sqr(d0);
+            if( (double)(df)!=(double)(0) )
+            {
+                df = Math.Sign(df)*Math.Exp(Math.Log(Math.Abs(df))+Math.Log(b.sy)+Math.Log(xscale1)+Math.Log(xscale2));
+            }
+        }
+
+
+        /*************************************************************************
+        Differentiation of barycentric interpolant: first/second derivatives.
+
+        INPUT PARAMETERS:
+            B   -   barycentric interpolant built with one of model building
+                    subroutines.
+            T   -   interpolation point
+
+        OUTPUT PARAMETERS:
+            F   -   barycentric interpolant at T
+            DF  -   first derivative
+            D2F -   second derivative
+
+        NOTE: this algorithm may fail due to overflow/underflor if  used  on  data
+        whose values are close to MaxRealNumber or MinRealNumber.  Use more robust
+        BarycentricDiff1() subroutine in such cases.
+
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentricdiff2(barycentricinterpolant b,
+            double t,
+            ref double f,
+            ref double df,
+            ref double d2f)
+        {
+            double v = 0;
+            double vv = 0;
+            int i = 0;
+            int k = 0;
+            double n0 = 0;
+            double n1 = 0;
+            double n2 = 0;
+            double d0 = 0;
+            double d1 = 0;
+            double d2 = 0;
+            double s0 = 0;
+            double s1 = 0;
+            double s2 = 0;
+            double xk = 0;
+            double xi = 0;
+
+            f = 0;
+            df = 0;
+            d2f = 0;
+
+            ap.assert(!Double.IsInfinity(t), "BarycentricDiff1: infinite T!");
+            
+            //
+            // special case: NaN
+            //
+            if( Double.IsNaN(t) )
+            {
+                f = Double.NaN;
+                df = Double.NaN;
+                d2f = Double.NaN;
+                return;
+            }
+            
+            //
+            // special case: N=1
+            //
+            if( b.n==1 )
+            {
+                f = b.sy*b.y[0];
+                df = 0;
+                d2f = 0;
+                return;
+            }
+            if( (double)(b.sy)==(double)(0) )
+            {
+                f = 0;
+                df = 0;
+                d2f = 0;
+                return;
+            }
+            
+            //
+            // We assume than N>1 and B.SY>0. Find:
+            // 1. pivot point (X[i] closest to T)
+            // 2. width of interval containing X[i]
+            //
+            ap.assert((double)(b.sy)>(double)(0), "BarycentricDiff: internal error");
+            f = 0;
+            df = 0;
+            d2f = 0;
+            v = Math.Abs(b.x[0]-t);
+            k = 0;
+            for(i=1; i<=b.n-1; i++)
+            {
+                vv = b.x[i];
+                if( (double)(Math.Abs(vv-t))<(double)(v) )
+                {
+                    v = Math.Abs(vv-t);
+                    k = i;
+                }
+            }
+            
+            //
+            // pivot point found, calculate dNumerator and dDenominator
+            //
+            xk = b.x[k];
+            v = t-xk;
+            n0 = 0;
+            n1 = 0;
+            n2 = 0;
+            d0 = 0;
+            d1 = 0;
+            d2 = 0;
+            for(i=0; i<=b.n-1; i++)
+            {
+                if( i!=k )
+                {
+                    xi = b.x[i];
+                    vv = math.sqr(t-xi);
+                    s0 = (t-xk)/(t-xi);
+                    s1 = (xk-xi)/vv;
+                    s2 = -(2*(xk-xi)/(vv*(t-xi)));
+                }
+                else
+                {
+                    s0 = 1;
+                    s1 = 0;
+                    s2 = 0;
+                }
+                vv = b.w[i]*b.y[i];
+                n0 = n0+s0*vv;
+                n1 = n1+s1*vv;
+                n2 = n2+s2*vv;
+                vv = b.w[i];
+                d0 = d0+s0*vv;
+                d1 = d1+s1*vv;
+                d2 = d2+s2*vv;
+            }
+            f = b.sy*n0/d0;
+            df = b.sy*(n1*d0-n0*d1)/math.sqr(d0);
+            d2f = b.sy*((n2*d0-n0*d2)*math.sqr(d0)-(n1*d0-n0*d1)*2*d0*d1)/math.sqr(math.sqr(d0));
+        }
+
+
+        /*************************************************************************
+        This subroutine performs linear transformation of the argument.
+
+        INPUT PARAMETERS:
+            B       -   rational interpolant in barycentric form
+            CA, CB  -   transformation coefficients: x = CA*t + CB
+
+        OUTPUT PARAMETERS:
+            B       -   transformed interpolant with X replaced by T
+
+          -- ALGLIB PROJECT --
+             Copyright 19.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentriclintransx(barycentricinterpolant b,
+            double ca,
+            double cb)
+        {
+            int i = 0;
+            int j = 0;
+            double v = 0;
+
+            
+            //
+            // special case, replace by constant F(CB)
+            //
+            if( (double)(ca)==(double)(0) )
+            {
+                b.sy = barycentriccalc(b, cb);
+                v = 1;
+                for(i=0; i<=b.n-1; i++)
+                {
+                    b.y[i] = 1;
+                    b.w[i] = v;
+                    v = -v;
+                }
+                return;
+            }
+            
+            //
+            // general case: CA<>0
+            //
+            for(i=0; i<=b.n-1; i++)
+            {
+                b.x[i] = (b.x[i]-cb)/ca;
+            }
+            if( (double)(ca)<(double)(0) )
+            {
+                for(i=0; i<=b.n-1; i++)
+                {
+                    if( i<b.n-1-i )
+                    {
+                        j = b.n-1-i;
+                        v = b.x[i];
+                        b.x[i] = b.x[j];
+                        b.x[j] = v;
+                        v = b.y[i];
+                        b.y[i] = b.y[j];
+                        b.y[j] = v;
+                        v = b.w[i];
+                        b.w[i] = b.w[j];
+                        b.w[j] = v;
+                    }
+                    else
+                    {
+                        break;
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This  subroutine   performs   linear  transformation  of  the  barycentric
+        interpolant.
+
+        INPUT PARAMETERS:
+            B       -   rational interpolant in barycentric form
+            CA, CB  -   transformation coefficients: B2(x) = CA*B(x) + CB
+
+        OUTPUT PARAMETERS:
+            B       -   transformed interpolant
+
+          -- ALGLIB PROJECT --
+             Copyright 19.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentriclintransy(barycentricinterpolant b,
+            double ca,
+            double cb)
+        {
+            int i = 0;
+            double v = 0;
+            int i_ = 0;
+
+            for(i=0; i<=b.n-1; i++)
+            {
+                b.y[i] = ca*b.sy*b.y[i]+cb;
+            }
+            b.sy = 0;
+            for(i=0; i<=b.n-1; i++)
+            {
+                b.sy = Math.Max(b.sy, Math.Abs(b.y[i]));
+            }
+            if( (double)(b.sy)>(double)(0) )
+            {
+                v = 1/b.sy;
+                for(i_=0; i_<=b.n-1;i_++)
+                {
+                    b.y[i_] = v*b.y[i_];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Extracts X/Y/W arrays from rational interpolant
+
+        INPUT PARAMETERS:
+            B   -   barycentric interpolant
+
+        OUTPUT PARAMETERS:
+            N   -   nodes count, N>0
+            X   -   interpolation nodes, array[0..N-1]
+            F   -   function values, array[0..N-1]
+            W   -   barycentric weights, array[0..N-1]
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentricunpack(barycentricinterpolant b,
+            ref int n,
+            ref double[] x,
+            ref double[] y,
+            ref double[] w)
+        {
+            double v = 0;
+            int i_ = 0;
+
+            n = 0;
+            x = new double[0];
+            y = new double[0];
+            w = new double[0];
+
+            n = b.n;
+            x = new double[n];
+            y = new double[n];
+            w = new double[n];
+            v = b.sy;
+            for(i_=0; i_<=n-1;i_++)
+            {
+                x[i_] = b.x[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                y[i_] = v*b.y[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                w[i_] = b.w[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        Rational interpolant from X/Y/W arrays
+
+        F(t) = SUM(i=0,n-1,w[i]*f[i]/(t-x[i])) / SUM(i=0,n-1,w[i]/(t-x[i]))
+
+        INPUT PARAMETERS:
+            X   -   interpolation nodes, array[0..N-1]
+            F   -   function values, array[0..N-1]
+            W   -   barycentric weights, array[0..N-1]
+            N   -   nodes count, N>0
+
+        OUTPUT PARAMETERS:
+            B   -   barycentric interpolant built from (X, Y, W)
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentricbuildxyw(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            barycentricinterpolant b)
+        {
+            int i_ = 0;
+
+            ap.assert(n>0, "BarycentricBuildXYW: incorrect N!");
+            
+            //
+            // fill X/Y/W
+            //
+            b.x = new double[n];
+            b.y = new double[n];
+            b.w = new double[n];
+            for(i_=0; i_<=n-1;i_++)
+            {
+                b.x[i_] = x[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                b.y[i_] = y[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                b.w[i_] = w[i_];
+            }
+            b.n = n;
+            
+            //
+            // Normalize
+            //
+            barycentricnormalize(b);
+        }
+
+
+        /*************************************************************************
+        Rational interpolant without poles
+
+        The subroutine constructs the rational interpolating function without real
+        poles  (see  'Barycentric rational interpolation with no  poles  and  high
+        rates of approximation', Michael S. Floater. and  Kai  Hormann,  for  more
+        information on this subject).
+
+        Input parameters:
+            X   -   interpolation nodes, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            N   -   number of nodes, N>0.
+            D   -   order of the interpolation scheme, 0 <= D <= N-1.
+                    D<0 will cause an error.
+                    D>=N it will be replaced with D=N-1.
+                    if you don't know what D to choose, use small value about 3-5.
+
+        Output parameters:
+            B   -   barycentric interpolant.
+
+        Note:
+            this algorithm always succeeds and calculates the weights  with  close
+            to machine precision.
+
+          -- ALGLIB PROJECT --
+             Copyright 17.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentricbuildfloaterhormann(double[] x,
+            double[] y,
+            int n,
+            int d,
+            barycentricinterpolant b)
+        {
+            double s0 = 0;
+            double s = 0;
+            double v = 0;
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int[] perm = new int[0];
+            double[] wtemp = new double[0];
+            double[] sortrbuf = new double[0];
+            double[] sortrbuf2 = new double[0];
+            int i_ = 0;
+
+            ap.assert(n>0, "BarycentricFloaterHormann: N<=0!");
+            ap.assert(d>=0, "BarycentricFloaterHormann: incorrect D!");
+            
+            //
+            // Prepare
+            //
+            if( d>n-1 )
+            {
+                d = n-1;
+            }
+            b.n = n;
+            
+            //
+            // special case: N=1
+            //
+            if( n==1 )
+            {
+                b.x = new double[n];
+                b.y = new double[n];
+                b.w = new double[n];
+                b.x[0] = x[0];
+                b.y[0] = y[0];
+                b.w[0] = 1;
+                barycentricnormalize(b);
+                return;
+            }
+            
+            //
+            // Fill X/Y
+            //
+            b.x = new double[n];
+            b.y = new double[n];
+            for(i_=0; i_<=n-1;i_++)
+            {
+                b.x[i_] = x[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                b.y[i_] = y[i_];
+            }
+            tsort.tagsortfastr(ref b.x, ref b.y, ref sortrbuf, ref sortrbuf2, n);
+            
+            //
+            // Calculate Wk
+            //
+            b.w = new double[n];
+            s0 = 1;
+            for(k=1; k<=d; k++)
+            {
+                s0 = -s0;
+            }
+            for(k=0; k<=n-1; k++)
+            {
+                
+                //
+                // Wk
+                //
+                s = 0;
+                for(i=Math.Max(k-d, 0); i<=Math.Min(k, n-1-d); i++)
+                {
+                    v = 1;
+                    for(j=i; j<=i+d; j++)
+                    {
+                        if( j!=k )
+                        {
+                            v = v/Math.Abs(b.x[k]-b.x[j]);
+                        }
+                    }
+                    s = s+v;
+                }
+                b.w[k] = s0*s;
+                
+                //
+                // Next S0
+                //
+                s0 = -s0;
+            }
+            
+            //
+            // Normalize
+            //
+            barycentricnormalize(b);
+        }
+
+
+        /*************************************************************************
+        Copying of the barycentric interpolant (for internal use only)
+
+        INPUT PARAMETERS:
+            B   -   barycentric interpolant
+
+        OUTPUT PARAMETERS:
+            B2  -   copy(B1)
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentriccopy(barycentricinterpolant b,
+            barycentricinterpolant b2)
+        {
+            int i_ = 0;
+
+            b2.n = b.n;
+            b2.sy = b.sy;
+            b2.x = new double[b2.n];
+            b2.y = new double[b2.n];
+            b2.w = new double[b2.n];
+            for(i_=0; i_<=b2.n-1;i_++)
+            {
+                b2.x[i_] = b.x[i_];
+            }
+            for(i_=0; i_<=b2.n-1;i_++)
+            {
+                b2.y[i_] = b.y[i_];
+            }
+            for(i_=0; i_<=b2.n-1;i_++)
+            {
+                b2.w[i_] = b.w[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        Normalization of barycentric interpolant:
+        * B.N, B.X, B.Y and B.W are initialized
+        * B.SY is NOT initialized
+        * Y[] is normalized, scaling coefficient is stored in B.SY
+        * W[] is normalized, no scaling coefficient is stored
+        * X[] is sorted
+
+        Internal subroutine.
+        *************************************************************************/
+        private static void barycentricnormalize(barycentricinterpolant b)
+        {
+            int[] p1 = new int[0];
+            int[] p2 = new int[0];
+            int i = 0;
+            int j = 0;
+            int j2 = 0;
+            double v = 0;
+            int i_ = 0;
+
+            
+            //
+            // Normalize task: |Y|<=1, |W|<=1, sort X[]
+            //
+            b.sy = 0;
+            for(i=0; i<=b.n-1; i++)
+            {
+                b.sy = Math.Max(b.sy, Math.Abs(b.y[i]));
+            }
+            if( (double)(b.sy)>(double)(0) & (double)(Math.Abs(b.sy-1))>(double)(10*math.machineepsilon) )
+            {
+                v = 1/b.sy;
+                for(i_=0; i_<=b.n-1;i_++)
+                {
+                    b.y[i_] = v*b.y[i_];
+                }
+            }
+            v = 0;
+            for(i=0; i<=b.n-1; i++)
+            {
+                v = Math.Max(v, Math.Abs(b.w[i]));
+            }
+            if( (double)(v)>(double)(0) & (double)(Math.Abs(v-1))>(double)(10*math.machineepsilon) )
+            {
+                v = 1/v;
+                for(i_=0; i_<=b.n-1;i_++)
+                {
+                    b.w[i_] = v*b.w[i_];
+                }
+            }
+            for(i=0; i<=b.n-2; i++)
+            {
+                if( (double)(b.x[i+1])<(double)(b.x[i]) )
+                {
+                    tsort.tagsort(ref b.x, b.n, ref p1, ref p2);
+                    for(j=0; j<=b.n-1; j++)
+                    {
+                        j2 = p2[j];
+                        v = b.y[j];
+                        b.y[j] = b.y[j2];
+                        b.y[j2] = v;
+                        v = b.w[j];
+                        b.w[j] = b.w[j2];
+                        b.w[j2] = v;
+                    }
+                    break;
+                }
+            }
+        }
+
+
+    }
+    public class polint
+    {
+        /*************************************************************************
+        Conversion from barycentric representation to Chebyshev basis.
+        This function has O(N^2) complexity.
+
+        INPUT PARAMETERS:
+            P   -   polynomial in barycentric form
+            A,B -   base interval for Chebyshev polynomials (see below)
+                    A<>B
+
+        OUTPUT PARAMETERS
+            T   -   coefficients of Chebyshev representation;
+                    P(x) = sum { T[i]*Ti(2*(x-A)/(B-A)-1), i=0..N-1 },
+                    where Ti - I-th Chebyshev polynomial.
+
+        NOTES:
+            barycentric interpolant passed as P may be either polynomial  obtained
+            from  polynomial  interpolation/ fitting or rational function which is
+            NOT polynomial. We can't distinguish between these two cases, and this
+            algorithm just tries to work assuming that P IS a polynomial.  If not,
+            algorithm will return results, but they won't have any meaning.
+
+          -- ALGLIB --
+             Copyright 30.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialbar2cheb(ratint.barycentricinterpolant p,
+            double a,
+            double b,
+            ref double[] t)
+        {
+            int i = 0;
+            int k = 0;
+            double[] vp = new double[0];
+            double[] vx = new double[0];
+            double[] tk = new double[0];
+            double[] tk1 = new double[0];
+            double v = 0;
+            int i_ = 0;
+
+            t = new double[0];
+
+            ap.assert(math.isfinite(a), "PolynomialBar2Cheb: A is not finite!");
+            ap.assert(math.isfinite(b), "PolynomialBar2Cheb: B is not finite!");
+            ap.assert((double)(a)!=(double)(b), "PolynomialBar2Cheb: A=B!");
+            ap.assert(p.n>0, "PolynomialBar2Cheb: P is not correctly initialized barycentric interpolant!");
+            
+            //
+            // Calculate function values on a Chebyshev grid
+            //
+            vp = new double[p.n];
+            vx = new double[p.n];
+            for(i=0; i<=p.n-1; i++)
+            {
+                vx[i] = Math.Cos(Math.PI*(i+0.5)/p.n);
+                vp[i] = ratint.barycentriccalc(p, 0.5*(vx[i]+1)*(b-a)+a);
+            }
+            
+            //
+            // T[0]
+            //
+            t = new double[p.n];
+            v = 0;
+            for(i=0; i<=p.n-1; i++)
+            {
+                v = v+vp[i];
+            }
+            t[0] = v/p.n;
+            
+            //
+            // other T's.
+            //
+            // NOTES:
+            // 1. TK stores T{k} on VX, TK1 stores T{k-1} on VX
+            // 2. we can do same calculations with fast DCT, but it
+            //    * adds dependencies
+            //    * still leaves us with O(N^2) algorithm because
+            //      preparation of function values is O(N^2) process
+            //
+            if( p.n>1 )
+            {
+                tk = new double[p.n];
+                tk1 = new double[p.n];
+                for(i=0; i<=p.n-1; i++)
+                {
+                    tk[i] = vx[i];
+                    tk1[i] = 1;
+                }
+                for(k=1; k<=p.n-1; k++)
+                {
+                    
+                    //
+                    // calculate discrete product of function vector and TK
+                    //
+                    v = 0.0;
+                    for(i_=0; i_<=p.n-1;i_++)
+                    {
+                        v += tk[i_]*vp[i_];
+                    }
+                    t[k] = v/(0.5*p.n);
+                    
+                    //
+                    // Update TK and TK1
+                    //
+                    for(i=0; i<=p.n-1; i++)
+                    {
+                        v = 2*vx[i]*tk[i]-tk1[i];
+                        tk1[i] = tk[i];
+                        tk[i] = v;
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Conversion from Chebyshev basis to barycentric representation.
+        This function has O(N^2) complexity.
+
+        INPUT PARAMETERS:
+            T   -   coefficients of Chebyshev representation;
+                    P(x) = sum { T[i]*Ti(2*(x-A)/(B-A)-1), i=0..N },
+                    where Ti - I-th Chebyshev polynomial.
+            N   -   number of coefficients:
+                    * if given, only leading N elements of T are used
+                    * if not given, automatically determined from size of T
+            A,B -   base interval for Chebyshev polynomials (see above)
+                    A<B
+
+        OUTPUT PARAMETERS
+            P   -   polynomial in barycentric form
+
+          -- ALGLIB --
+             Copyright 30.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialcheb2bar(double[] t,
+            int n,
+            double a,
+            double b,
+            ratint.barycentricinterpolant p)
+        {
+            int i = 0;
+            int k = 0;
+            double[] y = new double[0];
+            double tk = 0;
+            double tk1 = 0;
+            double vx = 0;
+            double vy = 0;
+            double v = 0;
+
+            ap.assert(math.isfinite(a), "PolynomialBar2Cheb: A is not finite!");
+            ap.assert(math.isfinite(b), "PolynomialBar2Cheb: B is not finite!");
+            ap.assert((double)(a)!=(double)(b), "PolynomialBar2Cheb: A=B!");
+            ap.assert(n>=1, "PolynomialBar2Cheb: N<1");
+            ap.assert(ap.len(t)>=n, "PolynomialBar2Cheb: Length(T)<N");
+            ap.assert(apserv.isfinitevector(t, n), "PolynomialBar2Cheb: T[] contains INF or NAN");
+            
+            //
+            // Calculate function values on a Chebyshev grid spanning [-1,+1]
+            //
+            y = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                
+                //
+                // Calculate value on a grid spanning [-1,+1]
+                //
+                vx = Math.Cos(Math.PI*(i+0.5)/n);
+                vy = t[0];
+                tk1 = 1;
+                tk = vx;
+                for(k=1; k<=n-1; k++)
+                {
+                    vy = vy+t[k]*tk;
+                    v = 2*vx*tk-tk1;
+                    tk1 = tk;
+                    tk = v;
+                }
+                y[i] = vy;
+            }
+            
+            //
+            // Build barycentric interpolant, map grid from [-1,+1] to [A,B]
+            //
+            polynomialbuildcheb1(a, b, y, n, p);
+        }
+
+
+        /*************************************************************************
+        Conversion from barycentric representation to power basis.
+        This function has O(N^2) complexity.
+
+        INPUT PARAMETERS:
+            P   -   polynomial in barycentric form
+            C   -   offset (see below); 0.0 is used as default value.
+            S   -   scale (see below);  1.0 is used as default value. S<>0.
+
+        OUTPUT PARAMETERS
+            A   -   coefficients, P(x) = sum { A[i]*((X-C)/S)^i, i=0..N-1 }
+            N   -   number of coefficients (polynomial degree plus 1)
+
+        NOTES:
+        1.  this function accepts offset and scale, which can be  set  to  improve
+            numerical properties of polynomial. For example, if P was obtained  as
+            result of interpolation on [-1,+1],  you  can  set  C=0  and  S=1  and
+            represent  P  as sum of 1, x, x^2, x^3 and so on. In most cases you it
+            is exactly what you need.
+
+            However, if your interpolation model was built on [999,1001], you will
+            see significant growth of numerical errors when using {1, x, x^2, x^3}
+            as basis. Representing P as sum of 1, (x-1000), (x-1000)^2, (x-1000)^3
+            will be better option. Such representation can be  obtained  by  using
+            1000.0 as offset C and 1.0 as scale S.
+
+        2.  power basis is ill-conditioned and tricks described above can't  solve
+            this problem completely. This function  will  return  coefficients  in
+            any  case,  but  for  N>8  they  will  become unreliable. However, N's
+            less than 5 are pretty safe.
+            
+        3.  barycentric interpolant passed as P may be either polynomial  obtained
+            from  polynomial  interpolation/ fitting or rational function which is
+            NOT polynomial. We can't distinguish between these two cases, and this
+            algorithm just tries to work assuming that P IS a polynomial.  If not,
+            algorithm will return results, but they won't have any meaning.
+
+          -- ALGLIB --
+             Copyright 30.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialbar2pow(ratint.barycentricinterpolant p,
+            double c,
+            double s,
+            ref double[] a)
+        {
+            int i = 0;
+            int k = 0;
+            double e = 0;
+            double d = 0;
+            double[] vp = new double[0];
+            double[] vx = new double[0];
+            double[] tk = new double[0];
+            double[] tk1 = new double[0];
+            double[] t = new double[0];
+            double v = 0;
+            int i_ = 0;
+
+            a = new double[0];
+
+            ap.assert(math.isfinite(c), "PolynomialBar2Pow: C is not finite!");
+            ap.assert(math.isfinite(s), "PolynomialBar2Pow: S is not finite!");
+            ap.assert((double)(s)!=(double)(0), "PolynomialBar2Pow: S=0!");
+            ap.assert(p.n>0, "PolynomialBar2Pow: P is not correctly initialized barycentric interpolant!");
+            
+            //
+            // Calculate function values on a Chebyshev grid
+            //
+            vp = new double[p.n];
+            vx = new double[p.n];
+            for(i=0; i<=p.n-1; i++)
+            {
+                vx[i] = Math.Cos(Math.PI*(i+0.5)/p.n);
+                vp[i] = ratint.barycentriccalc(p, s*vx[i]+c);
+            }
+            
+            //
+            // T[0]
+            //
+            t = new double[p.n];
+            v = 0;
+            for(i=0; i<=p.n-1; i++)
+            {
+                v = v+vp[i];
+            }
+            t[0] = v/p.n;
+            
+            //
+            // other T's.
+            //
+            // NOTES:
+            // 1. TK stores T{k} on VX, TK1 stores T{k-1} on VX
+            // 2. we can do same calculations with fast DCT, but it
+            //    * adds dependencies
+            //    * still leaves us with O(N^2) algorithm because
+            //      preparation of function values is O(N^2) process
+            //
+            if( p.n>1 )
+            {
+                tk = new double[p.n];
+                tk1 = new double[p.n];
+                for(i=0; i<=p.n-1; i++)
+                {
+                    tk[i] = vx[i];
+                    tk1[i] = 1;
+                }
+                for(k=1; k<=p.n-1; k++)
+                {
+                    
+                    //
+                    // calculate discrete product of function vector and TK
+                    //
+                    v = 0.0;
+                    for(i_=0; i_<=p.n-1;i_++)
+                    {
+                        v += tk[i_]*vp[i_];
+                    }
+                    t[k] = v/(0.5*p.n);
+                    
+                    //
+                    // Update TK and TK1
+                    //
+                    for(i=0; i<=p.n-1; i++)
+                    {
+                        v = 2*vx[i]*tk[i]-tk1[i];
+                        tk1[i] = tk[i];
+                        tk[i] = v;
+                    }
+                }
+            }
+            
+            //
+            // Convert from Chebyshev basis to power basis
+            //
+            a = new double[p.n];
+            for(i=0; i<=p.n-1; i++)
+            {
+                a[i] = 0;
+            }
+            d = 0;
+            for(i=0; i<=p.n-1; i++)
+            {
+                for(k=i; k<=p.n-1; k++)
+                {
+                    e = a[k];
+                    a[k] = 0;
+                    if( i<=1 & k==i )
+                    {
+                        a[k] = 1;
+                    }
+                    else
+                    {
+                        if( i!=0 )
+                        {
+                            a[k] = 2*d;
+                        }
+                        if( k>i+1 )
+                        {
+                            a[k] = a[k]-a[k-2];
+                        }
+                    }
+                    d = e;
+                }
+                d = a[i];
+                e = 0;
+                k = i;
+                while( k<=p.n-1 )
+                {
+                    e = e+a[k]*t[k];
+                    k = k+2;
+                }
+                a[i] = e;
+            }
+        }
+
+
+        /*************************************************************************
+        Conversion from power basis to barycentric representation.
+        This function has O(N^2) complexity.
+
+        INPUT PARAMETERS:
+            A   -   coefficients, P(x) = sum { A[i]*((X-C)/S)^i, i=0..N-1 }
+            N   -   number of coefficients (polynomial degree plus 1)
+                    * if given, only leading N elements of A are used
+                    * if not given, automatically determined from size of A
+            C   -   offset (see below); 0.0 is used as default value.
+            S   -   scale (see below);  1.0 is used as default value. S<>0.
+
+        OUTPUT PARAMETERS
+            P   -   polynomial in barycentric form
+
+
+        NOTES:
+        1.  this function accepts offset and scale, which can be  set  to  improve
+            numerical properties of polynomial. For example, if you interpolate on
+            [-1,+1],  you  can  set C=0 and S=1 and convert from sum of 1, x, x^2,
+            x^3 and so on. In most cases you it is exactly what you need.
+
+            However, if your interpolation model was built on [999,1001], you will
+            see significant growth of numerical errors when using {1, x, x^2, x^3}
+            as  input  basis.  Converting  from  sum  of  1, (x-1000), (x-1000)^2,
+            (x-1000)^3 will be better option (you have to specify 1000.0 as offset
+            C and 1.0 as scale S).
+
+        2.  power basis is ill-conditioned and tricks described above can't  solve
+            this problem completely. This function  will  return barycentric model
+            in any case, but for N>8 accuracy well degrade. However, N's less than
+            5 are pretty safe.
+
+          -- ALGLIB --
+             Copyright 30.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialpow2bar(double[] a,
+            int n,
+            double c,
+            double s,
+            ratint.barycentricinterpolant p)
+        {
+            int i = 0;
+            int k = 0;
+            double[] y = new double[0];
+            double vx = 0;
+            double vy = 0;
+            double px = 0;
+
+            ap.assert(math.isfinite(c), "PolynomialPow2Bar: C is not finite!");
+            ap.assert(math.isfinite(s), "PolynomialPow2Bar: S is not finite!");
+            ap.assert((double)(s)!=(double)(0), "PolynomialPow2Bar: S is zero!");
+            ap.assert(n>=1, "PolynomialPow2Bar: N<1");
+            ap.assert(ap.len(a)>=n, "PolynomialPow2Bar: Length(A)<N");
+            ap.assert(apserv.isfinitevector(a, n), "PolynomialPow2Bar: A[] contains INF or NAN");
+            
+            //
+            // Calculate function values on a Chebyshev grid spanning [-1,+1]
+            //
+            y = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                
+                //
+                // Calculate value on a grid spanning [-1,+1]
+                //
+                vx = Math.Cos(Math.PI*(i+0.5)/n);
+                vy = a[0];
+                px = vx;
+                for(k=1; k<=n-1; k++)
+                {
+                    vy = vy+px*a[k];
+                    px = px*vx;
+                }
+                y[i] = vy;
+            }
+            
+            //
+            // Build barycentric interpolant, map grid from [-1,+1] to [A,B]
+            //
+            polynomialbuildcheb1(c-s, c+s, y, n, p);
+        }
+
+
+        /*************************************************************************
+        Lagrange intepolant: generation of the model on the general grid.
+        This function has O(N^2) complexity.
+
+        INPUT PARAMETERS:
+            X   -   abscissas, array[0..N-1]
+            Y   -   function values, array[0..N-1]
+            N   -   number of points, N>=1
+
+        OUTPUT PARAMETERS
+            P   -   barycentric model which represents Lagrange interpolant
+                    (see ratint unit info and BarycentricCalc() description for
+                    more information).
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialbuild(double[] x,
+            double[] y,
+            int n,
+            ratint.barycentricinterpolant p)
+        {
+            int j = 0;
+            int k = 0;
+            double[] w = new double[0];
+            double b = 0;
+            double a = 0;
+            double v = 0;
+            double mx = 0;
+            double[] sortrbuf = new double[0];
+            double[] sortrbuf2 = new double[0];
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+
+            ap.assert(n>0, "PolynomialBuild: N<=0!");
+            ap.assert(ap.len(x)>=n, "PolynomialBuild: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "PolynomialBuild: Length(Y)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "PolynomialBuild: X contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuild: Y contains infinite or NaN values!");
+            tsort.tagsortfastr(ref x, ref y, ref sortrbuf, ref sortrbuf2, n);
+            ap.assert(apserv.aredistinct(x, n), "PolynomialBuild: at least two consequent points are too close!");
+            
+            //
+            // calculate W[j]
+            // multi-pass algorithm is used to avoid overflow
+            //
+            w = new double[n];
+            a = x[0];
+            b = x[0];
+            for(j=0; j<=n-1; j++)
+            {
+                w[j] = 1;
+                a = Math.Min(a, x[j]);
+                b = Math.Max(b, x[j]);
+            }
+            for(k=0; k<=n-1; k++)
+            {
+                
+                //
+                // W[K] is used instead of 0.0 because
+                // cycle on J does not touch K-th element
+                // and we MUST get maximum from ALL elements
+                //
+                mx = Math.Abs(w[k]);
+                for(j=0; j<=n-1; j++)
+                {
+                    if( j!=k )
+                    {
+                        v = (b-a)/(x[j]-x[k]);
+                        w[j] = w[j]*v;
+                        mx = Math.Max(mx, Math.Abs(w[j]));
+                    }
+                }
+                if( k%5==0 )
+                {
+                    
+                    //
+                    // every 5-th run we renormalize W[]
+                    //
+                    v = 1/mx;
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        w[i_] = v*w[i_];
+                    }
+                }
+            }
+            ratint.barycentricbuildxyw(x, y, w, n, p);
+        }
+
+
+        /*************************************************************************
+        Lagrange intepolant: generation of the model on equidistant grid.
+        This function has O(N) complexity.
+
+        INPUT PARAMETERS:
+            A   -   left boundary of [A,B]
+            B   -   right boundary of [A,B]
+            Y   -   function values at the nodes, array[0..N-1]
+            N   -   number of points, N>=1
+                    for N=1 a constant model is constructed.
+
+        OUTPUT PARAMETERS
+            P   -   barycentric model which represents Lagrange interpolant
+                    (see ratint unit info and BarycentricCalc() description for
+                    more information).
+
+          -- ALGLIB --
+             Copyright 03.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialbuildeqdist(double a,
+            double b,
+            double[] y,
+            int n,
+            ratint.barycentricinterpolant p)
+        {
+            int i = 0;
+            double[] w = new double[0];
+            double[] x = new double[0];
+            double v = 0;
+
+            ap.assert(n>0, "PolynomialBuildEqDist: N<=0!");
+            ap.assert(ap.len(y)>=n, "PolynomialBuildEqDist: Length(Y)<N!");
+            ap.assert(math.isfinite(a), "PolynomialBuildEqDist: A is infinite or NaN!");
+            ap.assert(math.isfinite(b), "PolynomialBuildEqDist: B is infinite or NaN!");
+            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuildEqDist: Y contains infinite or NaN values!");
+            ap.assert((double)(b)!=(double)(a), "PolynomialBuildEqDist: B=A!");
+            ap.assert((double)(a+(b-a)/n)!=(double)(a), "PolynomialBuildEqDist: B is too close to A!");
+            
+            //
+            // Special case: N=1
+            //
+            if( n==1 )
+            {
+                x = new double[1];
+                w = new double[1];
+                x[0] = 0.5*(b+a);
+                w[0] = 1;
+                ratint.barycentricbuildxyw(x, y, w, 1, p);
+                return;
+            }
+            
+            //
+            // general case
+            //
+            x = new double[n];
+            w = new double[n];
+            v = 1;
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = v;
+                x[i] = a+(b-a)*i/(n-1);
+                v = -(v*(n-1-i));
+                v = v/(i+1);
+            }
+            ratint.barycentricbuildxyw(x, y, w, n, p);
+        }
+
+
+        /*************************************************************************
+        Lagrange intepolant on Chebyshev grid (first kind).
+        This function has O(N) complexity.
+
+        INPUT PARAMETERS:
+            A   -   left boundary of [A,B]
+            B   -   right boundary of [A,B]
+            Y   -   function values at the nodes, array[0..N-1],
+                    Y[I] = Y(0.5*(B+A) + 0.5*(B-A)*Cos(PI*(2*i+1)/(2*n)))
+            N   -   number of points, N>=1
+                    for N=1 a constant model is constructed.
+
+        OUTPUT PARAMETERS
+            P   -   barycentric model which represents Lagrange interpolant
+                    (see ratint unit info and BarycentricCalc() description for
+                    more information).
+
+          -- ALGLIB --
+             Copyright 03.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialbuildcheb1(double a,
+            double b,
+            double[] y,
+            int n,
+            ratint.barycentricinterpolant p)
+        {
+            int i = 0;
+            double[] w = new double[0];
+            double[] x = new double[0];
+            double v = 0;
+            double t = 0;
+
+            ap.assert(n>0, "PolynomialBuildCheb1: N<=0!");
+            ap.assert(ap.len(y)>=n, "PolynomialBuildCheb1: Length(Y)<N!");
+            ap.assert(math.isfinite(a), "PolynomialBuildCheb1: A is infinite or NaN!");
+            ap.assert(math.isfinite(b), "PolynomialBuildCheb1: B is infinite or NaN!");
+            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuildCheb1: Y contains infinite or NaN values!");
+            ap.assert((double)(b)!=(double)(a), "PolynomialBuildCheb1: B=A!");
+            
+            //
+            // Special case: N=1
+            //
+            if( n==1 )
+            {
+                x = new double[1];
+                w = new double[1];
+                x[0] = 0.5*(b+a);
+                w[0] = 1;
+                ratint.barycentricbuildxyw(x, y, w, 1, p);
+                return;
+            }
+            
+            //
+            // general case
+            //
+            x = new double[n];
+            w = new double[n];
+            v = 1;
+            for(i=0; i<=n-1; i++)
+            {
+                t = Math.Tan(0.5*Math.PI*(2*i+1)/(2*n));
+                w[i] = 2*v*t/(1+math.sqr(t));
+                x[i] = 0.5*(b+a)+0.5*(b-a)*(1-math.sqr(t))/(1+math.sqr(t));
+                v = -v;
+            }
+            ratint.barycentricbuildxyw(x, y, w, n, p);
+        }
+
+
+        /*************************************************************************
+        Lagrange intepolant on Chebyshev grid (second kind).
+        This function has O(N) complexity.
+
+        INPUT PARAMETERS:
+            A   -   left boundary of [A,B]
+            B   -   right boundary of [A,B]
+            Y   -   function values at the nodes, array[0..N-1],
+                    Y[I] = Y(0.5*(B+A) + 0.5*(B-A)*Cos(PI*i/(n-1)))
+            N   -   number of points, N>=1
+                    for N=1 a constant model is constructed.
+
+        OUTPUT PARAMETERS
+            P   -   barycentric model which represents Lagrange interpolant
+                    (see ratint unit info and BarycentricCalc() description for
+                    more information).
+
+          -- ALGLIB --
+             Copyright 03.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialbuildcheb2(double a,
+            double b,
+            double[] y,
+            int n,
+            ratint.barycentricinterpolant p)
+        {
+            int i = 0;
+            double[] w = new double[0];
+            double[] x = new double[0];
+            double v = 0;
+
+            ap.assert(n>0, "PolynomialBuildCheb2: N<=0!");
+            ap.assert(ap.len(y)>=n, "PolynomialBuildCheb2: Length(Y)<N!");
+            ap.assert(math.isfinite(a), "PolynomialBuildCheb2: A is infinite or NaN!");
+            ap.assert(math.isfinite(b), "PolynomialBuildCheb2: B is infinite or NaN!");
+            ap.assert((double)(b)!=(double)(a), "PolynomialBuildCheb2: B=A!");
+            ap.assert(apserv.isfinitevector(y, n), "PolynomialBuildCheb2: Y contains infinite or NaN values!");
+            
+            //
+            // Special case: N=1
+            //
+            if( n==1 )
+            {
+                x = new double[1];
+                w = new double[1];
+                x[0] = 0.5*(b+a);
+                w[0] = 1;
+                ratint.barycentricbuildxyw(x, y, w, 1, p);
+                return;
+            }
+            
+            //
+            // general case
+            //
+            x = new double[n];
+            w = new double[n];
+            v = 1;
+            for(i=0; i<=n-1; i++)
+            {
+                if( i==0 | i==n-1 )
+                {
+                    w[i] = v*0.5;
+                }
+                else
+                {
+                    w[i] = v;
+                }
+                x[i] = 0.5*(b+a)+0.5*(b-a)*Math.Cos(Math.PI*i/(n-1));
+                v = -v;
+            }
+            ratint.barycentricbuildxyw(x, y, w, n, p);
+        }
+
+
+        /*************************************************************************
+        Fast equidistant polynomial interpolation function with O(N) complexity
+
+        INPUT PARAMETERS:
+            A   -   left boundary of [A,B]
+            B   -   right boundary of [A,B]
+            F   -   function values, array[0..N-1]
+            N   -   number of points on equidistant grid, N>=1
+                    for N=1 a constant model is constructed.
+            T   -   position where P(x) is calculated
+
+        RESULT
+            value of the Lagrange interpolant at T
+            
+        IMPORTANT
+            this function provides fast interface which is not overflow-safe
+            nor it is very precise.
+            the best option is to use  PolynomialBuildEqDist()/BarycentricCalc()
+            subroutines unless you are pretty sure that your data will not result
+            in overflow.
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double polynomialcalceqdist(double a,
+            double b,
+            double[] f,
+            int n,
+            double t)
+        {
+            double result = 0;
+            double s1 = 0;
+            double s2 = 0;
+            double v = 0;
+            double threshold = 0;
+            double s = 0;
+            double h = 0;
+            int i = 0;
+            int j = 0;
+            double w = 0;
+            double x = 0;
+
+            ap.assert(n>0, "PolynomialCalcEqDist: N<=0!");
+            ap.assert(ap.len(f)>=n, "PolynomialCalcEqDist: Length(F)<N!");
+            ap.assert(math.isfinite(a), "PolynomialCalcEqDist: A is infinite or NaN!");
+            ap.assert(math.isfinite(b), "PolynomialCalcEqDist: B is infinite or NaN!");
+            ap.assert(apserv.isfinitevector(f, n), "PolynomialCalcEqDist: F contains infinite or NaN values!");
+            ap.assert((double)(b)!=(double)(a), "PolynomialCalcEqDist: B=A!");
+            ap.assert(!Double.IsInfinity(t), "PolynomialCalcEqDist: T is infinite!");
+            
+            //
+            // Special case: T is NAN
+            //
+            if( Double.IsNaN(t) )
+            {
+                result = Double.NaN;
+                return result;
+            }
+            
+            //
+            // Special case: N=1
+            //
+            if( n==1 )
+            {
+                result = f[0];
+                return result;
+            }
+            
+            //
+            // First, decide: should we use "safe" formula (guarded
+            // against overflow) or fast one?
+            //
+            threshold = Math.Sqrt(math.minrealnumber);
+            j = 0;
+            s = t-a;
+            for(i=1; i<=n-1; i++)
+            {
+                x = a+(double)i/(double)(n-1)*(b-a);
+                if( (double)(Math.Abs(t-x))<(double)(Math.Abs(s)) )
+                {
+                    s = t-x;
+                    j = i;
+                }
+            }
+            if( (double)(s)==(double)(0) )
+            {
+                result = f[j];
+                return result;
+            }
+            if( (double)(Math.Abs(s))>(double)(threshold) )
+            {
+                
+                //
+                // use fast formula
+                //
+                j = -1;
+                s = 1.0;
+            }
+            
+            //
+            // Calculate using safe or fast barycentric formula
+            //
+            s1 = 0;
+            s2 = 0;
+            w = 1.0;
+            h = (b-a)/(n-1);
+            for(i=0; i<=n-1; i++)
+            {
+                if( i!=j )
+                {
+                    v = s*w/(t-(a+i*h));
+                    s1 = s1+v*f[i];
+                    s2 = s2+v;
+                }
+                else
+                {
+                    v = w;
+                    s1 = s1+v*f[i];
+                    s2 = s2+v;
+                }
+                w = -(w*(n-1-i));
+                w = w/(i+1);
+            }
+            result = s1/s2;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Fast polynomial interpolation function on Chebyshev points (first kind)
+        with O(N) complexity.
+
+        INPUT PARAMETERS:
+            A   -   left boundary of [A,B]
+            B   -   right boundary of [A,B]
+            F   -   function values, array[0..N-1]
+            N   -   number of points on Chebyshev grid (first kind),
+                    X[i] = 0.5*(B+A) + 0.5*(B-A)*Cos(PI*(2*i+1)/(2*n))
+                    for N=1 a constant model is constructed.
+            T   -   position where P(x) is calculated
+
+        RESULT
+            value of the Lagrange interpolant at T
+
+        IMPORTANT
+            this function provides fast interface which is not overflow-safe
+            nor it is very precise.
+            the best option is to use  PolIntBuildCheb1()/BarycentricCalc()
+            subroutines unless you are pretty sure that your data will not result
+            in overflow.
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double polynomialcalccheb1(double a,
+            double b,
+            double[] f,
+            int n,
+            double t)
+        {
+            double result = 0;
+            double s1 = 0;
+            double s2 = 0;
+            double v = 0;
+            double threshold = 0;
+            double s = 0;
+            int i = 0;
+            int j = 0;
+            double a0 = 0;
+            double delta = 0;
+            double alpha = 0;
+            double beta = 0;
+            double ca = 0;
+            double sa = 0;
+            double tempc = 0;
+            double temps = 0;
+            double x = 0;
+            double w = 0;
+            double p1 = 0;
+
+            ap.assert(n>0, "PolynomialCalcCheb1: N<=0!");
+            ap.assert(ap.len(f)>=n, "PolynomialCalcCheb1: Length(F)<N!");
+            ap.assert(math.isfinite(a), "PolynomialCalcCheb1: A is infinite or NaN!");
+            ap.assert(math.isfinite(b), "PolynomialCalcCheb1: B is infinite or NaN!");
+            ap.assert(apserv.isfinitevector(f, n), "PolynomialCalcCheb1: F contains infinite or NaN values!");
+            ap.assert((double)(b)!=(double)(a), "PolynomialCalcCheb1: B=A!");
+            ap.assert(!Double.IsInfinity(t), "PolynomialCalcCheb1: T is infinite!");
+            
+            //
+            // Special case: T is NAN
+            //
+            if( Double.IsNaN(t) )
+            {
+                result = Double.NaN;
+                return result;
+            }
+            
+            //
+            // Special case: N=1
+            //
+            if( n==1 )
+            {
+                result = f[0];
+                return result;
+            }
+            
+            //
+            // Prepare information for the recurrence formula
+            // used to calculate sin(pi*(2j+1)/(2n+2)) and
+            // cos(pi*(2j+1)/(2n+2)):
+            //
+            // A0    = pi/(2n+2)
+            // Delta = pi/(n+1)
+            // Alpha = 2 sin^2 (Delta/2)
+            // Beta  = sin(Delta)
+            //
+            // so that sin(..) = sin(A0+j*delta) and cos(..) = cos(A0+j*delta).
+            // Then we use
+            //
+            // sin(x+delta) = sin(x) - (alpha*sin(x) - beta*cos(x))
+            // cos(x+delta) = cos(x) - (alpha*cos(x) - beta*sin(x))
+            //
+            // to repeatedly calculate sin(..) and cos(..).
+            //
+            threshold = Math.Sqrt(math.minrealnumber);
+            t = (t-0.5*(a+b))/(0.5*(b-a));
+            a0 = Math.PI/(2*(n-1)+2);
+            delta = 2*Math.PI/(2*(n-1)+2);
+            alpha = 2*math.sqr(Math.Sin(delta/2));
+            beta = Math.Sin(delta);
+            
+            //
+            // First, decide: should we use "safe" formula (guarded
+            // against overflow) or fast one?
+            //
+            ca = Math.Cos(a0);
+            sa = Math.Sin(a0);
+            j = 0;
+            x = ca;
+            s = t-x;
+            for(i=1; i<=n-1; i++)
+            {
+                
+                //
+                // Next X[i]
+                //
+                temps = sa-(alpha*sa-beta*ca);
+                tempc = ca-(alpha*ca+beta*sa);
+                sa = temps;
+                ca = tempc;
+                x = ca;
+                
+                //
+                // Use X[i]
+                //
+                if( (double)(Math.Abs(t-x))<(double)(Math.Abs(s)) )
+                {
+                    s = t-x;
+                    j = i;
+                }
+            }
+            if( (double)(s)==(double)(0) )
+            {
+                result = f[j];
+                return result;
+            }
+            if( (double)(Math.Abs(s))>(double)(threshold) )
+            {
+                
+                //
+                // use fast formula
+                //
+                j = -1;
+                s = 1.0;
+            }
+            
+            //
+            // Calculate using safe or fast barycentric formula
+            //
+            s1 = 0;
+            s2 = 0;
+            ca = Math.Cos(a0);
+            sa = Math.Sin(a0);
+            p1 = 1.0;
+            for(i=0; i<=n-1; i++)
+            {
+                
+                //
+                // Calculate X[i], W[i]
+                //
+                x = ca;
+                w = p1*sa;
+                
+                //
+                // Proceed
+                //
+                if( i!=j )
+                {
+                    v = s*w/(t-x);
+                    s1 = s1+v*f[i];
+                    s2 = s2+v;
+                }
+                else
+                {
+                    v = w;
+                    s1 = s1+v*f[i];
+                    s2 = s2+v;
+                }
+                
+                //
+                // Next CA, SA, P1
+                //
+                temps = sa-(alpha*sa-beta*ca);
+                tempc = ca-(alpha*ca+beta*sa);
+                sa = temps;
+                ca = tempc;
+                p1 = -p1;
+            }
+            result = s1/s2;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Fast polynomial interpolation function on Chebyshev points (second kind)
+        with O(N) complexity.
+
+        INPUT PARAMETERS:
+            A   -   left boundary of [A,B]
+            B   -   right boundary of [A,B]
+            F   -   function values, array[0..N-1]
+            N   -   number of points on Chebyshev grid (second kind),
+                    X[i] = 0.5*(B+A) + 0.5*(B-A)*Cos(PI*i/(n-1))
+                    for N=1 a constant model is constructed.
+            T   -   position where P(x) is calculated
+
+        RESULT
+            value of the Lagrange interpolant at T
+
+        IMPORTANT
+            this function provides fast interface which is not overflow-safe
+            nor it is very precise.
+            the best option is to use PolIntBuildCheb2()/BarycentricCalc()
+            subroutines unless you are pretty sure that your data will not result
+            in overflow.
+
+          -- ALGLIB --
+             Copyright 02.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static double polynomialcalccheb2(double a,
+            double b,
+            double[] f,
+            int n,
+            double t)
+        {
+            double result = 0;
+            double s1 = 0;
+            double s2 = 0;
+            double v = 0;
+            double threshold = 0;
+            double s = 0;
+            int i = 0;
+            int j = 0;
+            double a0 = 0;
+            double delta = 0;
+            double alpha = 0;
+            double beta = 0;
+            double ca = 0;
+            double sa = 0;
+            double tempc = 0;
+            double temps = 0;
+            double x = 0;
+            double w = 0;
+            double p1 = 0;
+
+            ap.assert(n>0, "PolynomialCalcCheb2: N<=0!");
+            ap.assert(ap.len(f)>=n, "PolynomialCalcCheb2: Length(F)<N!");
+            ap.assert(math.isfinite(a), "PolynomialCalcCheb2: A is infinite or NaN!");
+            ap.assert(math.isfinite(b), "PolynomialCalcCheb2: B is infinite or NaN!");
+            ap.assert((double)(b)!=(double)(a), "PolynomialCalcCheb2: B=A!");
+            ap.assert(apserv.isfinitevector(f, n), "PolynomialCalcCheb2: F contains infinite or NaN values!");
+            ap.assert(!Double.IsInfinity(t), "PolynomialCalcEqDist: T is infinite!");
+            
+            //
+            // Special case: T is NAN
+            //
+            if( Double.IsNaN(t) )
+            {
+                result = Double.NaN;
+                return result;
+            }
+            
+            //
+            // Special case: N=1
+            //
+            if( n==1 )
+            {
+                result = f[0];
+                return result;
+            }
+            
+            //
+            // Prepare information for the recurrence formula
+            // used to calculate sin(pi*i/n) and
+            // cos(pi*i/n):
+            //
+            // A0    = 0
+            // Delta = pi/n
+            // Alpha = 2 sin^2 (Delta/2)
+            // Beta  = sin(Delta)
+            //
+            // so that sin(..) = sin(A0+j*delta) and cos(..) = cos(A0+j*delta).
+            // Then we use
+            //
+            // sin(x+delta) = sin(x) - (alpha*sin(x) - beta*cos(x))
+            // cos(x+delta) = cos(x) - (alpha*cos(x) - beta*sin(x))
+            //
+            // to repeatedly calculate sin(..) and cos(..).
+            //
+            threshold = Math.Sqrt(math.minrealnumber);
+            t = (t-0.5*(a+b))/(0.5*(b-a));
+            a0 = 0.0;
+            delta = Math.PI/(n-1);
+            alpha = 2*math.sqr(Math.Sin(delta/2));
+            beta = Math.Sin(delta);
+            
+            //
+            // First, decide: should we use "safe" formula (guarded
+            // against overflow) or fast one?
+            //
+            ca = Math.Cos(a0);
+            sa = Math.Sin(a0);
+            j = 0;
+            x = ca;
+            s = t-x;
+            for(i=1; i<=n-1; i++)
+            {
+                
+                //
+                // Next X[i]
+                //
+                temps = sa-(alpha*sa-beta*ca);
+                tempc = ca-(alpha*ca+beta*sa);
+                sa = temps;
+                ca = tempc;
+                x = ca;
+                
+                //
+                // Use X[i]
+                //
+                if( (double)(Math.Abs(t-x))<(double)(Math.Abs(s)) )
+                {
+                    s = t-x;
+                    j = i;
+                }
+            }
+            if( (double)(s)==(double)(0) )
+            {
+                result = f[j];
+                return result;
+            }
+            if( (double)(Math.Abs(s))>(double)(threshold) )
+            {
+                
+                //
+                // use fast formula
+                //
+                j = -1;
+                s = 1.0;
+            }
+            
+            //
+            // Calculate using safe or fast barycentric formula
+            //
+            s1 = 0;
+            s2 = 0;
+            ca = Math.Cos(a0);
+            sa = Math.Sin(a0);
+            p1 = 1.0;
+            for(i=0; i<=n-1; i++)
+            {
+                
+                //
+                // Calculate X[i], W[i]
+                //
+                x = ca;
+                if( i==0 | i==n-1 )
+                {
+                    w = 0.5*p1;
+                }
+                else
+                {
+                    w = 1.0*p1;
+                }
+                
+                //
+                // Proceed
+                //
+                if( i!=j )
+                {
+                    v = s*w/(t-x);
+                    s1 = s1+v*f[i];
+                    s2 = s2+v;
+                }
+                else
+                {
+                    v = w;
+                    s1 = s1+v*f[i];
+                    s2 = s2+v;
+                }
+                
+                //
+                // Next CA, SA, P1
+                //
+                temps = sa-(alpha*sa-beta*ca);
+                tempc = ca-(alpha*ca+beta*sa);
+                sa = temps;
+                ca = tempc;
+                p1 = -p1;
+            }
+            result = s1/s2;
+            return result;
+        }
+
+
+    }
+    public class spline1d
+    {
+        /*************************************************************************
+        1-dimensional spline inteprolant
+        *************************************************************************/
+        public class spline1dinterpolant
+        {
+            public bool periodic;
+            public int n;
+            public int k;
+            public double[] x;
+            public double[] c;
+            public spline1dinterpolant()
+            {
+                x = new double[0];
+                c = new double[0];
+            }
+        };
+
+
+
+
+        /*************************************************************************
+        This subroutine builds linear spline interpolant
+
+        INPUT PARAMETERS:
+            X   -   spline nodes, array[0..N-1]
+            Y   -   function values, array[0..N-1]
+            N   -   points count (optional):
+                    * N>=2
+                    * if given, only first N points are used to build spline
+                    * if not given, automatically detected from X/Y sizes
+                      (len(X) must be equal to len(Y))
+            
+        OUTPUT PARAMETERS:
+            C   -   spline interpolant
+
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+
+          -- ALGLIB PROJECT --
+             Copyright 24.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dbuildlinear(double[] x,
+            double[] y,
+            int n,
+            spline1dinterpolant c)
+        {
+            int i = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+
+            ap.assert(n>1, "Spline1DBuildLinear: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DBuildLinear: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DBuildLinear: Length(Y)<N!");
+            
+            //
+            // check and sort points
+            //
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildLinear: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildLinear: Y contains infinite or NAN values!");
+            heapsortpoints(ref x, ref y, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildLinear: at least two consequent points are too close!");
+            
+            //
+            // Build
+            //
+            c.periodic = false;
+            c.n = n;
+            c.k = 3;
+            c.x = new double[n];
+            c.c = new double[4*(n-1)];
+            for(i=0; i<=n-1; i++)
+            {
+                c.x[i] = x[i];
+            }
+            for(i=0; i<=n-2; i++)
+            {
+                c.c[4*i+0] = y[i];
+                c.c[4*i+1] = (y[i+1]-y[i])/(x[i+1]-x[i]);
+                c.c[4*i+2] = 0;
+                c.c[4*i+3] = 0;
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine builds cubic spline interpolant.
+
+        INPUT PARAMETERS:
+            X           -   spline nodes, array[0..N-1].
+            Y           -   function values, array[0..N-1].
+            
+        OPTIONAL PARAMETERS:
+            N           -   points count:
+                            * N>=2
+                            * if given, only first N points are used to build spline
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+            BoundLType  -   boundary condition type for the left boundary
+            BoundL      -   left boundary condition (first or second derivative,
+                            depending on the BoundLType)
+            BoundRType  -   boundary condition type for the right boundary
+            BoundR      -   right boundary condition (first or second derivative,
+                            depending on the BoundRType)
+
+        OUTPUT PARAMETERS:
+            C           -   spline interpolant
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+
+        SETTING BOUNDARY VALUES:
+
+        The BoundLType/BoundRType parameters can have the following values:
+            * -1, which corresonds to the periodic (cyclic) boundary conditions.
+                  In this case:
+                  * both BoundLType and BoundRType must be equal to -1.
+                  * BoundL/BoundR are ignored
+                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
+            *  0, which  corresponds  to  the  parabolically   terminated  spline
+                  (BoundL and/or BoundR are ignored).
+            *  1, which corresponds to the first derivative boundary condition
+            *  2, which corresponds to the second derivative boundary condition
+            *  by default, BoundType=0 is used
+
+        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
+
+        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
+        However, this subroutine doesn't require you to specify equal  values  for
+        the first and last points - it automatically forces them  to  be  equal by
+        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
+        Y[last_point]. However it is recommended to pass consistent values of Y[],
+        i.e. to make Y[first_point]=Y[last_point].
+
+          -- ALGLIB PROJECT --
+             Copyright 23.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dbuildcubic(double[] x,
+            double[] y,
+            int n,
+            int boundltype,
+            double boundl,
+            int boundrtype,
+            double boundr,
+            spline1dinterpolant c)
+        {
+            double[] a1 = new double[0];
+            double[] a2 = new double[0];
+            double[] a3 = new double[0];
+            double[] b = new double[0];
+            double[] dt = new double[0];
+            double[] d = new double[0];
+            int[] p = new int[0];
+            int ylen = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+
+            
+            //
+            // check correctness of boundary conditions
+            //
+            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DBuildCubic: incorrect BoundLType!");
+            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DBuildCubic: incorrect BoundRType!");
+            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DBuildCubic: incorrect BoundLType/BoundRType!");
+            if( boundltype==1 | boundltype==2 )
+            {
+                ap.assert(math.isfinite(boundl), "Spline1DBuildCubic: BoundL is infinite or NAN!");
+            }
+            if( boundrtype==1 | boundrtype==2 )
+            {
+                ap.assert(math.isfinite(boundr), "Spline1DBuildCubic: BoundR is infinite or NAN!");
+            }
+            
+            //
+            // check lengths of arguments
+            //
+            ap.assert(n>=2, "Spline1DBuildCubic: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DBuildCubic: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DBuildCubic: Length(Y)<N!");
+            
+            //
+            // check and sort points
+            //
+            ylen = n;
+            if( boundltype==-1 )
+            {
+                ylen = n-1;
+            }
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildCubic: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DBuildCubic: Y contains infinite or NAN values!");
+            heapsortppoints(ref x, ref y, ref p, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildCubic: at least two consequent points are too close!");
+            
+            //
+            // Now we've checked and preordered everything,
+            // so we can call internal function to calculate derivatives,
+            // and then build Hermite spline using these derivatives
+            //
+            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
+            spline1dbuildhermite(x, y, d, n, c);
+            c.periodic = boundltype==-1 | boundrtype==-1;
+        }
+
+
+        /*************************************************************************
+        This function solves following problem: given table y[] of function values
+        at nodes x[], it calculates and returns table of function derivatives  d[]
+        (calculated at the same nodes x[]).
+
+        This function yields same result as Spline1DBuildCubic() call followed  by
+        sequence of Spline1DDiff() calls, but it can be several times faster  when
+        called for ordered X[] and X2[].
+
+        INPUT PARAMETERS:
+            X           -   spline nodes
+            Y           -   function values
+
+        OPTIONAL PARAMETERS:
+            N           -   points count:
+                            * N>=2
+                            * if given, only first N points are used
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+            BoundLType  -   boundary condition type for the left boundary
+            BoundL      -   left boundary condition (first or second derivative,
+                            depending on the BoundLType)
+            BoundRType  -   boundary condition type for the right boundary
+            BoundR      -   right boundary condition (first or second derivative,
+                            depending on the BoundRType)
+
+        OUTPUT PARAMETERS:
+            D           -   derivative values at X[]
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+        Derivative values are correctly reordered on return, so  D[I]  is  always
+        equal to S'(X[I]) independently of points order.
+
+        SETTING BOUNDARY VALUES:
+
+        The BoundLType/BoundRType parameters can have the following values:
+            * -1, which corresonds to the periodic (cyclic) boundary conditions.
+                  In this case:
+                  * both BoundLType and BoundRType must be equal to -1.
+                  * BoundL/BoundR are ignored
+                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
+            *  0, which  corresponds  to  the  parabolically   terminated  spline
+                  (BoundL and/or BoundR are ignored).
+            *  1, which corresponds to the first derivative boundary condition
+            *  2, which corresponds to the second derivative boundary condition
+            *  by default, BoundType=0 is used
+
+        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
+
+        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
+        However, this subroutine doesn't require you to specify equal  values  for
+        the first and last points - it automatically forces them  to  be  equal by
+        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
+        Y[last_point]. However it is recommended to pass consistent values of Y[],
+        i.e. to make Y[first_point]=Y[last_point].
+
+          -- ALGLIB PROJECT --
+             Copyright 03.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dgriddiffcubic(double[] x,
+            double[] y,
+            int n,
+            int boundltype,
+            double boundl,
+            int boundrtype,
+            double boundr,
+            ref double[] d)
+        {
+            double[] a1 = new double[0];
+            double[] a2 = new double[0];
+            double[] a3 = new double[0];
+            double[] b = new double[0];
+            double[] dt = new double[0];
+            int[] p = new int[0];
+            int i = 0;
+            int ylen = 0;
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            d = new double[0];
+
+            
+            //
+            // check correctness of boundary conditions
+            //
+            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DGridDiffCubic: incorrect BoundLType!");
+            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DGridDiffCubic: incorrect BoundRType!");
+            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DGridDiffCubic: incorrect BoundLType/BoundRType!");
+            if( boundltype==1 | boundltype==2 )
+            {
+                ap.assert(math.isfinite(boundl), "Spline1DGridDiffCubic: BoundL is infinite or NAN!");
+            }
+            if( boundrtype==1 | boundrtype==2 )
+            {
+                ap.assert(math.isfinite(boundr), "Spline1DGridDiffCubic: BoundR is infinite or NAN!");
+            }
+            
+            //
+            // check lengths of arguments
+            //
+            ap.assert(n>=2, "Spline1DGridDiffCubic: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DGridDiffCubic: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DGridDiffCubic: Length(Y)<N!");
+            
+            //
+            // check and sort points
+            //
+            ylen = n;
+            if( boundltype==-1 )
+            {
+                ylen = n-1;
+            }
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DGridDiffCubic: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DGridDiffCubic: Y contains infinite or NAN values!");
+            heapsortppoints(ref x, ref y, ref p, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DGridDiffCubic: at least two consequent points are too close!");
+            
+            //
+            // Now we've checked and preordered everything,
+            // so we can call internal function.
+            //
+            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
+            
+            //
+            // Remember that HeapSortPPoints() call?
+            // Now we have to reorder them back.
+            //
+            if( ap.len(dt)<n )
+            {
+                dt = new double[n];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                dt[p[i]] = d[i];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                d[i_] = dt[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This function solves following problem: given table y[] of function values
+        at  nodes  x[],  it  calculates  and  returns  tables  of first and second
+        function derivatives d1[] and d2[] (calculated at the same nodes x[]).
+
+        This function yields same result as Spline1DBuildCubic() call followed  by
+        sequence of Spline1DDiff() calls, but it can be several times faster  when
+        called for ordered X[] and X2[].
+
+        INPUT PARAMETERS:
+            X           -   spline nodes
+            Y           -   function values
+
+        OPTIONAL PARAMETERS:
+            N           -   points count:
+                            * N>=2
+                            * if given, only first N points are used
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+            BoundLType  -   boundary condition type for the left boundary
+            BoundL      -   left boundary condition (first or second derivative,
+                            depending on the BoundLType)
+            BoundRType  -   boundary condition type for the right boundary
+            BoundR      -   right boundary condition (first or second derivative,
+                            depending on the BoundRType)
+
+        OUTPUT PARAMETERS:
+            D1          -   S' values at X[]
+            D2          -   S'' values at X[]
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+        Derivative values are correctly reordered on return, so  D[I]  is  always
+        equal to S'(X[I]) independently of points order.
+
+        SETTING BOUNDARY VALUES:
+
+        The BoundLType/BoundRType parameters can have the following values:
+            * -1, which corresonds to the periodic (cyclic) boundary conditions.
+                  In this case:
+                  * both BoundLType and BoundRType must be equal to -1.
+                  * BoundL/BoundR are ignored
+                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
+            *  0, which  corresponds  to  the  parabolically   terminated  spline
+                  (BoundL and/or BoundR are ignored).
+            *  1, which corresponds to the first derivative boundary condition
+            *  2, which corresponds to the second derivative boundary condition
+            *  by default, BoundType=0 is used
+
+        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
+
+        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
+        However, this subroutine doesn't require you to specify equal  values  for
+        the first and last points - it automatically forces them  to  be  equal by
+        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
+        Y[last_point]. However it is recommended to pass consistent values of Y[],
+        i.e. to make Y[first_point]=Y[last_point].
+
+          -- ALGLIB PROJECT --
+             Copyright 03.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dgriddiff2cubic(double[] x,
+            double[] y,
+            int n,
+            int boundltype,
+            double boundl,
+            int boundrtype,
+            double boundr,
+            ref double[] d1,
+            ref double[] d2)
+        {
+            double[] a1 = new double[0];
+            double[] a2 = new double[0];
+            double[] a3 = new double[0];
+            double[] b = new double[0];
+            double[] dt = new double[0];
+            int[] p = new int[0];
+            int i = 0;
+            int ylen = 0;
+            double delta = 0;
+            double delta2 = 0;
+            double delta3 = 0;
+            double s0 = 0;
+            double s1 = 0;
+            double s2 = 0;
+            double s3 = 0;
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            d1 = new double[0];
+            d2 = new double[0];
+
+            
+            //
+            // check correctness of boundary conditions
+            //
+            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DGridDiff2Cubic: incorrect BoundLType!");
+            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DGridDiff2Cubic: incorrect BoundRType!");
+            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DGridDiff2Cubic: incorrect BoundLType/BoundRType!");
+            if( boundltype==1 | boundltype==2 )
+            {
+                ap.assert(math.isfinite(boundl), "Spline1DGridDiff2Cubic: BoundL is infinite or NAN!");
+            }
+            if( boundrtype==1 | boundrtype==2 )
+            {
+                ap.assert(math.isfinite(boundr), "Spline1DGridDiff2Cubic: BoundR is infinite or NAN!");
+            }
+            
+            //
+            // check lengths of arguments
+            //
+            ap.assert(n>=2, "Spline1DGridDiff2Cubic: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DGridDiff2Cubic: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DGridDiff2Cubic: Length(Y)<N!");
+            
+            //
+            // check and sort points
+            //
+            ylen = n;
+            if( boundltype==-1 )
+            {
+                ylen = n-1;
+            }
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DGridDiff2Cubic: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DGridDiff2Cubic: Y contains infinite or NAN values!");
+            heapsortppoints(ref x, ref y, ref p, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DGridDiff2Cubic: at least two consequent points are too close!");
+            
+            //
+            // Now we've checked and preordered everything,
+            // so we can call internal function.
+            //
+            // After this call we will calculate second derivatives
+            // (manually, by converting to the power basis)
+            //
+            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d1, ref a1, ref a2, ref a3, ref b, ref dt);
+            d2 = new double[n];
+            delta = 0;
+            s2 = 0;
+            s3 = 0;
+            for(i=0; i<=n-2; i++)
+            {
+                
+                //
+                // We convert from Hermite basis to the power basis.
+                // Si is coefficient before x^i.
+                //
+                // Inside this cycle we need just S2,
+                // because we calculate S'' exactly at spline node,
+                // (only x^2 matters at x=0), but after iterations
+                // will be over, we will need other coefficients
+                // to calculate spline value at the last node.
+                //
+                delta = x[i+1]-x[i];
+                delta2 = math.sqr(delta);
+                delta3 = delta*delta2;
+                s0 = y[i];
+                s1 = d1[i];
+                s2 = (3*(y[i+1]-y[i])-2*d1[i]*delta-d1[i+1]*delta)/delta2;
+                s3 = (2*(y[i]-y[i+1])+d1[i]*delta+d1[i+1]*delta)/delta3;
+                d2[i] = 2*s2;
+            }
+            d2[n-1] = 2*s2+6*s3*delta;
+            
+            //
+            // Remember that HeapSortPPoints() call?
+            // Now we have to reorder them back.
+            //
+            if( ap.len(dt)<n )
+            {
+                dt = new double[n];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                dt[p[i]] = d1[i];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                d1[i_] = dt[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                dt[p[i]] = d2[i];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                d2[i_] = dt[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This function solves following problem: given table y[] of function values
+        at old nodes x[]  and new nodes  x2[],  it calculates and returns table of
+        function values y2[] (calculated at x2[]).
+
+        This function yields same result as Spline1DBuildCubic() call followed  by
+        sequence of Spline1DDiff() calls, but it can be several times faster  when
+        called for ordered X[] and X2[].
+
+        INPUT PARAMETERS:
+            X           -   old spline nodes
+            Y           -   function values
+            X2           -  new spline nodes
+
+        OPTIONAL PARAMETERS:
+            N           -   points count:
+                            * N>=2
+                            * if given, only first N points from X/Y are used
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+            BoundLType  -   boundary condition type for the left boundary
+            BoundL      -   left boundary condition (first or second derivative,
+                            depending on the BoundLType)
+            BoundRType  -   boundary condition type for the right boundary
+            BoundR      -   right boundary condition (first or second derivative,
+                            depending on the BoundRType)
+            N2          -   new points count:
+                            * N2>=2
+                            * if given, only first N2 points from X2 are used
+                            * if not given, automatically detected from X2 size
+
+        OUTPUT PARAMETERS:
+            F2          -   function values at X2[]
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller  may pass unsorted array.
+        Function  values  are correctly reordered on  return, so F2[I]  is  always
+        equal to S(X2[I]) independently of points order.
+
+        SETTING BOUNDARY VALUES:
+
+        The BoundLType/BoundRType parameters can have the following values:
+            * -1, which corresonds to the periodic (cyclic) boundary conditions.
+                  In this case:
+                  * both BoundLType and BoundRType must be equal to -1.
+                  * BoundL/BoundR are ignored
+                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
+            *  0, which  corresponds  to  the  parabolically   terminated  spline
+                  (BoundL and/or BoundR are ignored).
+            *  1, which corresponds to the first derivative boundary condition
+            *  2, which corresponds to the second derivative boundary condition
+            *  by default, BoundType=0 is used
+
+        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
+
+        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
+        However, this subroutine doesn't require you to specify equal  values  for
+        the first and last points - it automatically forces them  to  be  equal by
+        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
+        Y[last_point]. However it is recommended to pass consistent values of Y[],
+        i.e. to make Y[first_point]=Y[last_point].
+
+          -- ALGLIB PROJECT --
+             Copyright 03.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dconvcubic(double[] x,
+            double[] y,
+            int n,
+            int boundltype,
+            double boundl,
+            int boundrtype,
+            double boundr,
+            double[] x2,
+            int n2,
+            ref double[] y2)
+        {
+            double[] a1 = new double[0];
+            double[] a2 = new double[0];
+            double[] a3 = new double[0];
+            double[] b = new double[0];
+            double[] d = new double[0];
+            double[] dt = new double[0];
+            double[] d1 = new double[0];
+            double[] d2 = new double[0];
+            int[] p = new int[0];
+            int[] p2 = new int[0];
+            int i = 0;
+            int ylen = 0;
+            double t = 0;
+            double t2 = 0;
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            x2 = (double[])x2.Clone();
+            y2 = new double[0];
+
+            
+            //
+            // check correctness of boundary conditions
+            //
+            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DConvCubic: incorrect BoundLType!");
+            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DConvCubic: incorrect BoundRType!");
+            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DConvCubic: incorrect BoundLType/BoundRType!");
+            if( boundltype==1 | boundltype==2 )
+            {
+                ap.assert(math.isfinite(boundl), "Spline1DConvCubic: BoundL is infinite or NAN!");
+            }
+            if( boundrtype==1 | boundrtype==2 )
+            {
+                ap.assert(math.isfinite(boundr), "Spline1DConvCubic: BoundR is infinite or NAN!");
+            }
+            
+            //
+            // check lengths of arguments
+            //
+            ap.assert(n>=2, "Spline1DConvCubic: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DConvCubic: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DConvCubic: Length(Y)<N!");
+            ap.assert(n2>=2, "Spline1DConvCubic: N2<2!");
+            ap.assert(ap.len(x2)>=n2, "Spline1DConvCubic: Length(X2)<N2!");
+            
+            //
+            // check and sort X/Y
+            //
+            ylen = n;
+            if( boundltype==-1 )
+            {
+                ylen = n-1;
+            }
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DConvCubic: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DConvCubic: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(x2, n2), "Spline1DConvCubic: X2 contains infinite or NAN values!");
+            heapsortppoints(ref x, ref y, ref p, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DConvCubic: at least two consequent points are too close!");
+            
+            //
+            // set up DT (we will need it below)
+            //
+            dt = new double[Math.Max(n, n2)];
+            
+            //
+            // sort X2:
+            // * use fake array DT because HeapSortPPoints() needs both integer AND real arrays
+            // * if we have periodic problem, wrap points
+            // * sort them, store permutation at P2
+            //
+            if( boundrtype==-1 & boundltype==-1 )
+            {
+                for(i=0; i<=n2-1; i++)
+                {
+                    t = x2[i];
+                    apserv.apperiodicmap(ref t, x[0], x[n-1], ref t2);
+                    x2[i] = t;
+                }
+            }
+            heapsortppoints(ref x2, ref dt, ref p2, n2);
+            
+            //
+            // Now we've checked and preordered everything, so we:
+            // * call internal GridDiff() function to get Hermite form of spline
+            // * convert using internal Conv() function
+            // * convert Y2 back to original order
+            //
+            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
+            spline1dconvdiffinternal(x, y, d, n, x2, n2, ref y2, true, ref d1, false, ref d2, false);
+            ap.assert(ap.len(dt)>=n2, "Spline1DConvCubic: internal error!");
+            for(i=0; i<=n2-1; i++)
+            {
+                dt[p2[i]] = y2[i];
+            }
+            for(i_=0; i_<=n2-1;i_++)
+            {
+                y2[i_] = dt[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This function solves following problem: given table y[] of function values
+        at old nodes x[]  and new nodes  x2[],  it calculates and returns table of
+        function values y2[] and derivatives d2[] (calculated at x2[]).
+
+        This function yields same result as Spline1DBuildCubic() call followed  by
+        sequence of Spline1DDiff() calls, but it can be several times faster  when
+        called for ordered X[] and X2[].
+
+        INPUT PARAMETERS:
+            X           -   old spline nodes
+            Y           -   function values
+            X2           -  new spline nodes
+
+        OPTIONAL PARAMETERS:
+            N           -   points count:
+                            * N>=2
+                            * if given, only first N points from X/Y are used
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+            BoundLType  -   boundary condition type for the left boundary
+            BoundL      -   left boundary condition (first or second derivative,
+                            depending on the BoundLType)
+            BoundRType  -   boundary condition type for the right boundary
+            BoundR      -   right boundary condition (first or second derivative,
+                            depending on the BoundRType)
+            N2          -   new points count:
+                            * N2>=2
+                            * if given, only first N2 points from X2 are used
+                            * if not given, automatically detected from X2 size
+
+        OUTPUT PARAMETERS:
+            F2          -   function values at X2[]
+            D2          -   first derivatives at X2[]
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller  may pass unsorted array.
+        Function  values  are correctly reordered on  return, so F2[I]  is  always
+        equal to S(X2[I]) independently of points order.
+
+        SETTING BOUNDARY VALUES:
+
+        The BoundLType/BoundRType parameters can have the following values:
+            * -1, which corresonds to the periodic (cyclic) boundary conditions.
+                  In this case:
+                  * both BoundLType and BoundRType must be equal to -1.
+                  * BoundL/BoundR are ignored
+                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
+            *  0, which  corresponds  to  the  parabolically   terminated  spline
+                  (BoundL and/or BoundR are ignored).
+            *  1, which corresponds to the first derivative boundary condition
+            *  2, which corresponds to the second derivative boundary condition
+            *  by default, BoundType=0 is used
+
+        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
+
+        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
+        However, this subroutine doesn't require you to specify equal  values  for
+        the first and last points - it automatically forces them  to  be  equal by
+        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
+        Y[last_point]. However it is recommended to pass consistent values of Y[],
+        i.e. to make Y[first_point]=Y[last_point].
+
+          -- ALGLIB PROJECT --
+             Copyright 03.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dconvdiffcubic(double[] x,
+            double[] y,
+            int n,
+            int boundltype,
+            double boundl,
+            int boundrtype,
+            double boundr,
+            double[] x2,
+            int n2,
+            ref double[] y2,
+            ref double[] d2)
+        {
+            double[] a1 = new double[0];
+            double[] a2 = new double[0];
+            double[] a3 = new double[0];
+            double[] b = new double[0];
+            double[] d = new double[0];
+            double[] dt = new double[0];
+            double[] rt1 = new double[0];
+            int[] p = new int[0];
+            int[] p2 = new int[0];
+            int i = 0;
+            int ylen = 0;
+            double t = 0;
+            double t2 = 0;
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            x2 = (double[])x2.Clone();
+            y2 = new double[0];
+            d2 = new double[0];
+
+            
+            //
+            // check correctness of boundary conditions
+            //
+            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DConvDiffCubic: incorrect BoundLType!");
+            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DConvDiffCubic: incorrect BoundRType!");
+            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DConvDiffCubic: incorrect BoundLType/BoundRType!");
+            if( boundltype==1 | boundltype==2 )
+            {
+                ap.assert(math.isfinite(boundl), "Spline1DConvDiffCubic: BoundL is infinite or NAN!");
+            }
+            if( boundrtype==1 | boundrtype==2 )
+            {
+                ap.assert(math.isfinite(boundr), "Spline1DConvDiffCubic: BoundR is infinite or NAN!");
+            }
+            
+            //
+            // check lengths of arguments
+            //
+            ap.assert(n>=2, "Spline1DConvDiffCubic: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DConvDiffCubic: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DConvDiffCubic: Length(Y)<N!");
+            ap.assert(n2>=2, "Spline1DConvDiffCubic: N2<2!");
+            ap.assert(ap.len(x2)>=n2, "Spline1DConvDiffCubic: Length(X2)<N2!");
+            
+            //
+            // check and sort X/Y
+            //
+            ylen = n;
+            if( boundltype==-1 )
+            {
+                ylen = n-1;
+            }
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DConvDiffCubic: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DConvDiffCubic: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(x2, n2), "Spline1DConvDiffCubic: X2 contains infinite or NAN values!");
+            heapsortppoints(ref x, ref y, ref p, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DConvDiffCubic: at least two consequent points are too close!");
+            
+            //
+            // set up DT (we will need it below)
+            //
+            dt = new double[Math.Max(n, n2)];
+            
+            //
+            // sort X2:
+            // * use fake array DT because HeapSortPPoints() needs both integer AND real arrays
+            // * if we have periodic problem, wrap points
+            // * sort them, store permutation at P2
+            //
+            if( boundrtype==-1 & boundltype==-1 )
+            {
+                for(i=0; i<=n2-1; i++)
+                {
+                    t = x2[i];
+                    apserv.apperiodicmap(ref t, x[0], x[n-1], ref t2);
+                    x2[i] = t;
+                }
+            }
+            heapsortppoints(ref x2, ref dt, ref p2, n2);
+            
+            //
+            // Now we've checked and preordered everything, so we:
+            // * call internal GridDiff() function to get Hermite form of spline
+            // * convert using internal Conv() function
+            // * convert Y2 back to original order
+            //
+            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
+            spline1dconvdiffinternal(x, y, d, n, x2, n2, ref y2, true, ref d2, true, ref rt1, false);
+            ap.assert(ap.len(dt)>=n2, "Spline1DConvDiffCubic: internal error!");
+            for(i=0; i<=n2-1; i++)
+            {
+                dt[p2[i]] = y2[i];
+            }
+            for(i_=0; i_<=n2-1;i_++)
+            {
+                y2[i_] = dt[i_];
+            }
+            for(i=0; i<=n2-1; i++)
+            {
+                dt[p2[i]] = d2[i];
+            }
+            for(i_=0; i_<=n2-1;i_++)
+            {
+                d2[i_] = dt[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This function solves following problem: given table y[] of function values
+        at old nodes x[]  and new nodes  x2[],  it calculates and returns table of
+        function  values  y2[],  first  and  second  derivatives  d2[]  and  dd2[]
+        (calculated at x2[]).
+
+        This function yields same result as Spline1DBuildCubic() call followed  by
+        sequence of Spline1DDiff() calls, but it can be several times faster  when
+        called for ordered X[] and X2[].
+
+        INPUT PARAMETERS:
+            X           -   old spline nodes
+            Y           -   function values
+            X2           -  new spline nodes
+
+        OPTIONAL PARAMETERS:
+            N           -   points count:
+                            * N>=2
+                            * if given, only first N points from X/Y are used
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+            BoundLType  -   boundary condition type for the left boundary
+            BoundL      -   left boundary condition (first or second derivative,
+                            depending on the BoundLType)
+            BoundRType  -   boundary condition type for the right boundary
+            BoundR      -   right boundary condition (first or second derivative,
+                            depending on the BoundRType)
+            N2          -   new points count:
+                            * N2>=2
+                            * if given, only first N2 points from X2 are used
+                            * if not given, automatically detected from X2 size
+
+        OUTPUT PARAMETERS:
+            F2          -   function values at X2[]
+            D2          -   first derivatives at X2[]
+            DD2         -   second derivatives at X2[]
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller  may pass unsorted array.
+        Function  values  are correctly reordered on  return, so F2[I]  is  always
+        equal to S(X2[I]) independently of points order.
+
+        SETTING BOUNDARY VALUES:
+
+        The BoundLType/BoundRType parameters can have the following values:
+            * -1, which corresonds to the periodic (cyclic) boundary conditions.
+                  In this case:
+                  * both BoundLType and BoundRType must be equal to -1.
+                  * BoundL/BoundR are ignored
+                  * Y[last] is ignored (it is assumed to be equal to Y[first]).
+            *  0, which  corresponds  to  the  parabolically   terminated  spline
+                  (BoundL and/or BoundR are ignored).
+            *  1, which corresponds to the first derivative boundary condition
+            *  2, which corresponds to the second derivative boundary condition
+            *  by default, BoundType=0 is used
+
+        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
+
+        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
+        However, this subroutine doesn't require you to specify equal  values  for
+        the first and last points - it automatically forces them  to  be  equal by
+        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
+        Y[last_point]. However it is recommended to pass consistent values of Y[],
+        i.e. to make Y[first_point]=Y[last_point].
+
+          -- ALGLIB PROJECT --
+             Copyright 03.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dconvdiff2cubic(double[] x,
+            double[] y,
+            int n,
+            int boundltype,
+            double boundl,
+            int boundrtype,
+            double boundr,
+            double[] x2,
+            int n2,
+            ref double[] y2,
+            ref double[] d2,
+            ref double[] dd2)
+        {
+            double[] a1 = new double[0];
+            double[] a2 = new double[0];
+            double[] a3 = new double[0];
+            double[] b = new double[0];
+            double[] d = new double[0];
+            double[] dt = new double[0];
+            int[] p = new int[0];
+            int[] p2 = new int[0];
+            int i = 0;
+            int ylen = 0;
+            double t = 0;
+            double t2 = 0;
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            x2 = (double[])x2.Clone();
+            y2 = new double[0];
+            d2 = new double[0];
+            dd2 = new double[0];
+
+            
+            //
+            // check correctness of boundary conditions
+            //
+            ap.assert(((boundltype==-1 | boundltype==0) | boundltype==1) | boundltype==2, "Spline1DConvDiff2Cubic: incorrect BoundLType!");
+            ap.assert(((boundrtype==-1 | boundrtype==0) | boundrtype==1) | boundrtype==2, "Spline1DConvDiff2Cubic: incorrect BoundRType!");
+            ap.assert((boundrtype==-1 & boundltype==-1) | (boundrtype!=-1 & boundltype!=-1), "Spline1DConvDiff2Cubic: incorrect BoundLType/BoundRType!");
+            if( boundltype==1 | boundltype==2 )
+            {
+                ap.assert(math.isfinite(boundl), "Spline1DConvDiff2Cubic: BoundL is infinite or NAN!");
+            }
+            if( boundrtype==1 | boundrtype==2 )
+            {
+                ap.assert(math.isfinite(boundr), "Spline1DConvDiff2Cubic: BoundR is infinite or NAN!");
+            }
+            
+            //
+            // check lengths of arguments
+            //
+            ap.assert(n>=2, "Spline1DConvDiff2Cubic: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DConvDiff2Cubic: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DConvDiff2Cubic: Length(Y)<N!");
+            ap.assert(n2>=2, "Spline1DConvDiff2Cubic: N2<2!");
+            ap.assert(ap.len(x2)>=n2, "Spline1DConvDiff2Cubic: Length(X2)<N2!");
+            
+            //
+            // check and sort X/Y
+            //
+            ylen = n;
+            if( boundltype==-1 )
+            {
+                ylen = n-1;
+            }
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DConvDiff2Cubic: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, ylen), "Spline1DConvDiff2Cubic: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(x2, n2), "Spline1DConvDiff2Cubic: X2 contains infinite or NAN values!");
+            heapsortppoints(ref x, ref y, ref p, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DConvDiff2Cubic: at least two consequent points are too close!");
+            
+            //
+            // set up DT (we will need it below)
+            //
+            dt = new double[Math.Max(n, n2)];
+            
+            //
+            // sort X2:
+            // * use fake array DT because HeapSortPPoints() needs both integer AND real arrays
+            // * if we have periodic problem, wrap points
+            // * sort them, store permutation at P2
+            //
+            if( boundrtype==-1 & boundltype==-1 )
+            {
+                for(i=0; i<=n2-1; i++)
+                {
+                    t = x2[i];
+                    apserv.apperiodicmap(ref t, x[0], x[n-1], ref t2);
+                    x2[i] = t;
+                }
+            }
+            heapsortppoints(ref x2, ref dt, ref p2, n2);
+            
+            //
+            // Now we've checked and preordered everything, so we:
+            // * call internal GridDiff() function to get Hermite form of spline
+            // * convert using internal Conv() function
+            // * convert Y2 back to original order
+            //
+            spline1dgriddiffcubicinternal(x, ref y, n, boundltype, boundl, boundrtype, boundr, ref d, ref a1, ref a2, ref a3, ref b, ref dt);
+            spline1dconvdiffinternal(x, y, d, n, x2, n2, ref y2, true, ref d2, true, ref dd2, true);
+            ap.assert(ap.len(dt)>=n2, "Spline1DConvDiff2Cubic: internal error!");
+            for(i=0; i<=n2-1; i++)
+            {
+                dt[p2[i]] = y2[i];
+            }
+            for(i_=0; i_<=n2-1;i_++)
+            {
+                y2[i_] = dt[i_];
+            }
+            for(i=0; i<=n2-1; i++)
+            {
+                dt[p2[i]] = d2[i];
+            }
+            for(i_=0; i_<=n2-1;i_++)
+            {
+                d2[i_] = dt[i_];
+            }
+            for(i=0; i<=n2-1; i++)
+            {
+                dt[p2[i]] = dd2[i];
+            }
+            for(i_=0; i_<=n2-1;i_++)
+            {
+                dd2[i_] = dt[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine builds Catmull-Rom spline interpolant.
+
+        INPUT PARAMETERS:
+            X           -   spline nodes, array[0..N-1].
+            Y           -   function values, array[0..N-1].
+            
+        OPTIONAL PARAMETERS:
+            N           -   points count:
+                            * N>=2
+                            * if given, only first N points are used to build spline
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+            BoundType   -   boundary condition type:
+                            * -1 for periodic boundary condition
+                            *  0 for parabolically terminated spline (default)
+            Tension     -   tension parameter:
+                            * tension=0   corresponds to classic Catmull-Rom spline (default)
+                            * 0<tension<1 corresponds to more general form - cardinal spline
+
+        OUTPUT PARAMETERS:
+            C           -   spline interpolant
+
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+
+        PROBLEMS WITH PERIODIC BOUNDARY CONDITIONS:
+
+        Problems with periodic boundary conditions have Y[first_point]=Y[last_point].
+        However, this subroutine doesn't require you to specify equal  values  for
+        the first and last points - it automatically forces them  to  be  equal by
+        copying  Y[first_point]  (corresponds  to the leftmost,  minimal  X[])  to
+        Y[last_point]. However it is recommended to pass consistent values of Y[],
+        i.e. to make Y[first_point]=Y[last_point].
+
+          -- ALGLIB PROJECT --
+             Copyright 23.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dbuildcatmullrom(double[] x,
+            double[] y,
+            int n,
+            int boundtype,
+            double tension,
+            spline1dinterpolant c)
+        {
+            double[] d = new double[0];
+            int i = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+
+            ap.assert(n>=2, "Spline1DBuildCatmullRom: N<2!");
+            ap.assert(boundtype==-1 | boundtype==0, "Spline1DBuildCatmullRom: incorrect BoundType!");
+            ap.assert((double)(tension)>=(double)(0), "Spline1DBuildCatmullRom: Tension<0!");
+            ap.assert((double)(tension)<=(double)(1), "Spline1DBuildCatmullRom: Tension>1!");
+            ap.assert(ap.len(x)>=n, "Spline1DBuildCatmullRom: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DBuildCatmullRom: Length(Y)<N!");
+            
+            //
+            // check and sort points
+            //
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildCatmullRom: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildCatmullRom: Y contains infinite or NAN values!");
+            heapsortpoints(ref x, ref y, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildCatmullRom: at least two consequent points are too close!");
+            
+            //
+            // Special cases:
+            // * N=2, parabolic terminated boundary condition on both ends
+            // * N=2, periodic boundary condition
+            //
+            if( n==2 & boundtype==0 )
+            {
+                
+                //
+                // Just linear spline
+                //
+                spline1dbuildlinear(x, y, n, c);
+                return;
+            }
+            if( n==2 & boundtype==-1 )
+            {
+                
+                //
+                // Same as cubic spline with periodic conditions
+                //
+                spline1dbuildcubic(x, y, n, -1, 0.0, -1, 0.0, c);
+                return;
+            }
+            
+            //
+            // Periodic or non-periodic boundary conditions
+            //
+            if( boundtype==-1 )
+            {
+                
+                //
+                // Periodic boundary conditions
+                //
+                y[n-1] = y[0];
+                d = new double[n];
+                d[0] = (y[1]-y[n-2])/(2*(x[1]-x[0]+x[n-1]-x[n-2]));
+                for(i=1; i<=n-2; i++)
+                {
+                    d[i] = (1-tension)*(y[i+1]-y[i-1])/(x[i+1]-x[i-1]);
+                }
+                d[n-1] = d[0];
+                
+                //
+                // Now problem is reduced to the cubic Hermite spline
+                //
+                spline1dbuildhermite(x, y, d, n, c);
+                c.periodic = true;
+            }
+            else
+            {
+                
+                //
+                // Non-periodic boundary conditions
+                //
+                d = new double[n];
+                for(i=1; i<=n-2; i++)
+                {
+                    d[i] = (1-tension)*(y[i+1]-y[i-1])/(x[i+1]-x[i-1]);
+                }
+                d[0] = 2*(y[1]-y[0])/(x[1]-x[0])-d[1];
+                d[n-1] = 2*(y[n-1]-y[n-2])/(x[n-1]-x[n-2])-d[n-2];
+                
+                //
+                // Now problem is reduced to the cubic Hermite spline
+                //
+                spline1dbuildhermite(x, y, d, n, c);
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine builds Hermite spline interpolant.
+
+        INPUT PARAMETERS:
+            X           -   spline nodes, array[0..N-1]
+            Y           -   function values, array[0..N-1]
+            D           -   derivatives, array[0..N-1]
+            N           -   points count (optional):
+                            * N>=2
+                            * if given, only first N points are used to build spline
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+
+        OUTPUT PARAMETERS:
+            C           -   spline interpolant.
+
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+
+          -- ALGLIB PROJECT --
+             Copyright 23.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dbuildhermite(double[] x,
+            double[] y,
+            double[] d,
+            int n,
+            spline1dinterpolant c)
+        {
+            int i = 0;
+            double delta = 0;
+            double delta2 = 0;
+            double delta3 = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            d = (double[])d.Clone();
+
+            ap.assert(n>=2, "Spline1DBuildHermite: N<2!");
+            ap.assert(ap.len(x)>=n, "Spline1DBuildHermite: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DBuildHermite: Length(Y)<N!");
+            ap.assert(ap.len(d)>=n, "Spline1DBuildHermite: Length(D)<N!");
+            
+            //
+            // check and sort points
+            //
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildHermite: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildHermite: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(d, n), "Spline1DBuildHermite: D contains infinite or NAN values!");
+            heapsortdpoints(ref x, ref y, ref d, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildHermite: at least two consequent points are too close!");
+            
+            //
+            // Build
+            //
+            c.x = new double[n];
+            c.c = new double[4*(n-1)];
+            c.periodic = false;
+            c.k = 3;
+            c.n = n;
+            for(i=0; i<=n-1; i++)
+            {
+                c.x[i] = x[i];
+            }
+            for(i=0; i<=n-2; i++)
+            {
+                delta = x[i+1]-x[i];
+                delta2 = math.sqr(delta);
+                delta3 = delta*delta2;
+                c.c[4*i+0] = y[i];
+                c.c[4*i+1] = d[i];
+                c.c[4*i+2] = (3*(y[i+1]-y[i])-2*d[i]*delta-d[i+1]*delta)/delta2;
+                c.c[4*i+3] = (2*(y[i]-y[i+1])+d[i]*delta+d[i+1]*delta)/delta3;
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine builds Akima spline interpolant
+
+        INPUT PARAMETERS:
+            X           -   spline nodes, array[0..N-1]
+            Y           -   function values, array[0..N-1]
+            N           -   points count (optional):
+                            * N>=5
+                            * if given, only first N points are used to build spline
+                            * if not given, automatically detected from X/Y sizes
+                              (len(X) must be equal to len(Y))
+
+        OUTPUT PARAMETERS:
+            C           -   spline interpolant
+
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+
+          -- ALGLIB PROJECT --
+             Copyright 24.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dbuildakima(double[] x,
+            double[] y,
+            int n,
+            spline1dinterpolant c)
+        {
+            int i = 0;
+            double[] d = new double[0];
+            double[] w = new double[0];
+            double[] diff = new double[0];
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+
+            ap.assert(n>=5, "Spline1DBuildAkima: N<5!");
+            ap.assert(ap.len(x)>=n, "Spline1DBuildAkima: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DBuildAkima: Length(Y)<N!");
+            
+            //
+            // check and sort points
+            //
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DBuildAkima: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DBuildAkima: Y contains infinite or NAN values!");
+            heapsortpoints(ref x, ref y, n);
+            ap.assert(apserv.aredistinct(x, n), "Spline1DBuildAkima: at least two consequent points are too close!");
+            
+            //
+            // Prepare W (weights), Diff (divided differences)
+            //
+            w = new double[n-1];
+            diff = new double[n-1];
+            for(i=0; i<=n-2; i++)
+            {
+                diff[i] = (y[i+1]-y[i])/(x[i+1]-x[i]);
+            }
+            for(i=1; i<=n-2; i++)
+            {
+                w[i] = Math.Abs(diff[i]-diff[i-1]);
+            }
+            
+            //
+            // Prepare Hermite interpolation scheme
+            //
+            d = new double[n];
+            for(i=2; i<=n-3; i++)
+            {
+                if( (double)(Math.Abs(w[i-1])+Math.Abs(w[i+1]))!=(double)(0) )
+                {
+                    d[i] = (w[i+1]*diff[i-1]+w[i-1]*diff[i])/(w[i+1]+w[i-1]);
+                }
+                else
+                {
+                    d[i] = ((x[i+1]-x[i])*diff[i-1]+(x[i]-x[i-1])*diff[i])/(x[i+1]-x[i-1]);
+                }
+            }
+            d[0] = diffthreepoint(x[0], x[0], y[0], x[1], y[1], x[2], y[2]);
+            d[1] = diffthreepoint(x[1], x[0], y[0], x[1], y[1], x[2], y[2]);
+            d[n-2] = diffthreepoint(x[n-2], x[n-3], y[n-3], x[n-2], y[n-2], x[n-1], y[n-1]);
+            d[n-1] = diffthreepoint(x[n-1], x[n-3], y[n-3], x[n-2], y[n-2], x[n-1], y[n-1]);
+            
+            //
+            // Build Akima spline using Hermite interpolation scheme
+            //
+            spline1dbuildhermite(x, y, d, n, c);
+        }
+
+
+        /*************************************************************************
+        This subroutine calculates the value of the spline at the given point X.
+
+        INPUT PARAMETERS:
+            C   -   spline interpolant
+            X   -   point
+
+        Result:
+            S(x)
+
+          -- ALGLIB PROJECT --
+             Copyright 23.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static double spline1dcalc(spline1dinterpolant c,
+            double x)
+        {
+            double result = 0;
+            int l = 0;
+            int r = 0;
+            int m = 0;
+            double t = 0;
+
+            ap.assert(c.k==3, "Spline1DCalc: internal error");
+            ap.assert(!Double.IsInfinity(x), "Spline1DCalc: infinite X!");
+            
+            //
+            // special case: NaN
+            //
+            if( Double.IsNaN(x) )
+            {
+                result = Double.NaN;
+                return result;
+            }
+            
+            //
+            // correct if periodic
+            //
+            if( c.periodic )
+            {
+                apserv.apperiodicmap(ref x, c.x[0], c.x[c.n-1], ref t);
+            }
+            
+            //
+            // Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
+            //
+            l = 0;
+            r = c.n-2+1;
+            while( l!=r-1 )
+            {
+                m = (l+r)/2;
+                if( c.x[m]>=x )
+                {
+                    r = m;
+                }
+                else
+                {
+                    l = m;
+                }
+            }
+            
+            //
+            // Interpolation
+            //
+            x = x-c.x[l];
+            m = 4*l;
+            result = c.c[m]+x*(c.c[m+1]+x*(c.c[m+2]+x*c.c[m+3]));
+            return result;
+        }
+
+
+        /*************************************************************************
+        This subroutine differentiates the spline.
+
+        INPUT PARAMETERS:
+            C   -   spline interpolant.
+            X   -   point
+
+        Result:
+            S   -   S(x)
+            DS  -   S'(x)
+            D2S -   S''(x)
+
+          -- ALGLIB PROJECT --
+             Copyright 24.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1ddiff(spline1dinterpolant c,
+            double x,
+            ref double s,
+            ref double ds,
+            ref double d2s)
+        {
+            int l = 0;
+            int r = 0;
+            int m = 0;
+            double t = 0;
+
+            s = 0;
+            ds = 0;
+            d2s = 0;
+
+            ap.assert(c.k==3, "Spline1DDiff: internal error");
+            ap.assert(!Double.IsInfinity(x), "Spline1DDiff: infinite X!");
+            
+            //
+            // special case: NaN
+            //
+            if( Double.IsNaN(x) )
+            {
+                s = Double.NaN;
+                ds = Double.NaN;
+                d2s = Double.NaN;
+                return;
+            }
+            
+            //
+            // correct if periodic
+            //
+            if( c.periodic )
+            {
+                apserv.apperiodicmap(ref x, c.x[0], c.x[c.n-1], ref t);
+            }
+            
+            //
+            // Binary search
+            //
+            l = 0;
+            r = c.n-2+1;
+            while( l!=r-1 )
+            {
+                m = (l+r)/2;
+                if( c.x[m]>=x )
+                {
+                    r = m;
+                }
+                else
+                {
+                    l = m;
+                }
+            }
+            
+            //
+            // Differentiation
+            //
+            x = x-c.x[l];
+            m = 4*l;
+            s = c.c[m]+x*(c.c[m+1]+x*(c.c[m+2]+x*c.c[m+3]));
+            ds = c.c[m+1]+2*x*c.c[m+2]+3*math.sqr(x)*c.c[m+3];
+            d2s = 2*c.c[m+2]+6*x*c.c[m+3];
+        }
+
+
+        /*************************************************************************
+        This subroutine makes the copy of the spline.
+
+        INPUT PARAMETERS:
+            C   -   spline interpolant.
+
+        Result:
+            CC  -   spline copy
+
+          -- ALGLIB PROJECT --
+             Copyright 29.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dcopy(spline1dinterpolant c,
+            spline1dinterpolant cc)
+        {
+            int i_ = 0;
+
+            cc.periodic = c.periodic;
+            cc.n = c.n;
+            cc.k = c.k;
+            cc.x = new double[cc.n];
+            for(i_=0; i_<=cc.n-1;i_++)
+            {
+                cc.x[i_] = c.x[i_];
+            }
+            cc.c = new double[(cc.k+1)*(cc.n-1)];
+            for(i_=0; i_<=(cc.k+1)*(cc.n-1)-1;i_++)
+            {
+                cc.c[i_] = c.c[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine unpacks the spline into the coefficients table.
+
+        INPUT PARAMETERS:
+            C   -   spline interpolant.
+            X   -   point
+
+        Result:
+            Tbl -   coefficients table, unpacked format, array[0..N-2, 0..5].
+                    For I = 0...N-2:
+                        Tbl[I,0] = X[i]
+                        Tbl[I,1] = X[i+1]
+                        Tbl[I,2] = C0
+                        Tbl[I,3] = C1
+                        Tbl[I,4] = C2
+                        Tbl[I,5] = C3
+                    On [x[i], x[i+1]] spline is equals to:
+                        S(x) = C0 + C1*t + C2*t^2 + C3*t^3
+                        t = x-x[i]
+
+          -- ALGLIB PROJECT --
+             Copyright 29.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dunpack(spline1dinterpolant c,
+            ref int n,
+            ref double[,] tbl)
+        {
+            int i = 0;
+            int j = 0;
+
+            n = 0;
+            tbl = new double[0,0];
+
+            tbl = new double[c.n-2+1, 2+c.k+1];
+            n = c.n;
+            
+            //
+            // Fill
+            //
+            for(i=0; i<=n-2; i++)
+            {
+                tbl[i,0] = c.x[i];
+                tbl[i,1] = c.x[i+1];
+                for(j=0; j<=c.k; j++)
+                {
+                    tbl[i,2+j] = c.c[(c.k+1)*i+j];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine performs linear transformation of the spline argument.
+
+        INPUT PARAMETERS:
+            C   -   spline interpolant.
+            A, B-   transformation coefficients: x = A*t + B
+        Result:
+            C   -   transformed spline
+
+          -- ALGLIB PROJECT --
+             Copyright 30.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dlintransx(spline1dinterpolant c,
+            double a,
+            double b)
+        {
+            int i = 0;
+            int j = 0;
+            int n = 0;
+            double v = 0;
+            double dv = 0;
+            double d2v = 0;
+            double[] x = new double[0];
+            double[] y = new double[0];
+            double[] d = new double[0];
+
+            n = c.n;
+            
+            //
+            // Special case: A=0
+            //
+            if( (double)(a)==(double)(0) )
+            {
+                v = spline1dcalc(c, b);
+                for(i=0; i<=n-2; i++)
+                {
+                    c.c[(c.k+1)*i] = v;
+                    for(j=1; j<=c.k; j++)
+                    {
+                        c.c[(c.k+1)*i+j] = 0;
+                    }
+                }
+                return;
+            }
+            
+            //
+            // General case: A<>0.
+            // Unpack, X, Y, dY/dX.
+            // Scale and pack again.
+            //
+            ap.assert(c.k==3, "Spline1DLinTransX: internal error");
+            x = new double[n-1+1];
+            y = new double[n-1+1];
+            d = new double[n-1+1];
+            for(i=0; i<=n-1; i++)
+            {
+                x[i] = c.x[i];
+                spline1ddiff(c, x[i], ref v, ref dv, ref d2v);
+                x[i] = (x[i]-b)/a;
+                y[i] = v;
+                d[i] = a*dv;
+            }
+            spline1dbuildhermite(x, y, d, n, c);
+        }
+
+
+        /*************************************************************************
+        This subroutine performs linear transformation of the spline.
+
+        INPUT PARAMETERS:
+            C   -   spline interpolant.
+            A, B-   transformation coefficients: S2(x) = A*S(x) + B
+        Result:
+            C   -   transformed spline
+
+          -- ALGLIB PROJECT --
+             Copyright 30.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dlintransy(spline1dinterpolant c,
+            double a,
+            double b)
+        {
+            int i = 0;
+            int j = 0;
+            int n = 0;
+
+            n = c.n;
+            for(i=0; i<=n-2; i++)
+            {
+                c.c[(c.k+1)*i] = a*c.c[(c.k+1)*i]+b;
+                for(j=1; j<=c.k; j++)
+                {
+                    c.c[(c.k+1)*i+j] = a*c.c[(c.k+1)*i+j];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine integrates the spline.
+
+        INPUT PARAMETERS:
+            C   -   spline interpolant.
+            X   -   right bound of the integration interval [a, x],
+                    here 'a' denotes min(x[])
+        Result:
+            integral(S(t)dt,a,x)
+
+          -- ALGLIB PROJECT --
+             Copyright 23.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static double spline1dintegrate(spline1dinterpolant c,
+            double x)
+        {
+            double result = 0;
+            int n = 0;
+            int i = 0;
+            int j = 0;
+            int l = 0;
+            int r = 0;
+            int m = 0;
+            double w = 0;
+            double v = 0;
+            double t = 0;
+            double intab = 0;
+            double additionalterm = 0;
+
+            n = c.n;
+            
+            //
+            // Periodic splines require special treatment. We make
+            // following transformation:
+            //
+            //     integral(S(t)dt,A,X) = integral(S(t)dt,A,Z)+AdditionalTerm
+            //
+            // here X may lie outside of [A,B], Z lies strictly in [A,B],
+            // AdditionalTerm is equals to integral(S(t)dt,A,B) times some
+            // integer number (may be zero).
+            //
+            if( c.periodic & ((double)(x)<(double)(c.x[0]) | (double)(x)>(double)(c.x[c.n-1])) )
+            {
+                
+                //
+                // compute integral(S(x)dx,A,B)
+                //
+                intab = 0;
+                for(i=0; i<=c.n-2; i++)
+                {
+                    w = c.x[i+1]-c.x[i];
+                    m = (c.k+1)*i;
+                    intab = intab+c.c[m]*w;
+                    v = w;
+                    for(j=1; j<=c.k; j++)
+                    {
+                        v = v*w;
+                        intab = intab+c.c[m+j]*v/(j+1);
+                    }
+                }
+                
+                //
+                // map X into [A,B]
+                //
+                apserv.apperiodicmap(ref x, c.x[0], c.x[c.n-1], ref t);
+                additionalterm = t*intab;
+            }
+            else
+            {
+                additionalterm = 0;
+            }
+            
+            //
+            // Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
+            //
+            l = 0;
+            r = n-2+1;
+            while( l!=r-1 )
+            {
+                m = (l+r)/2;
+                if( (double)(c.x[m])>=(double)(x) )
+                {
+                    r = m;
+                }
+                else
+                {
+                    l = m;
+                }
+            }
+            
+            //
+            // Integration
+            //
+            result = 0;
+            for(i=0; i<=l-1; i++)
+            {
+                w = c.x[i+1]-c.x[i];
+                m = (c.k+1)*i;
+                result = result+c.c[m]*w;
+                v = w;
+                for(j=1; j<=c.k; j++)
+                {
+                    v = v*w;
+                    result = result+c.c[m+j]*v/(j+1);
+                }
+            }
+            w = x-c.x[l];
+            m = (c.k+1)*l;
+            v = w;
+            result = result+c.c[m]*w;
+            for(j=1; j<=c.k; j++)
+            {
+                v = v*w;
+                result = result+c.c[m+j]*v/(j+1);
+            }
+            result = result+additionalterm;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Internal version of Spline1DConvDiff
+
+        Converts from Hermite spline given by grid XOld to new grid X2
+
+        INPUT PARAMETERS:
+            XOld    -   old grid
+            YOld    -   values at old grid
+            DOld    -   first derivative at old grid
+            N       -   grid size
+            X2      -   new grid
+            N2      -   new grid size
+            Y       -   possibly preallocated output array
+                        (reallocate if too small)
+            NeedY   -   do we need Y?
+            D1      -   possibly preallocated output array
+                        (reallocate if too small)
+            NeedD1  -   do we need D1?
+            D2      -   possibly preallocated output array
+                        (reallocate if too small)
+            NeedD2  -   do we need D1?
+
+        OUTPUT ARRAYS:
+            Y       -   values, if needed
+            D1      -   first derivative, if needed
+            D2      -   second derivative, if needed
+
+          -- ALGLIB PROJECT --
+             Copyright 03.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dconvdiffinternal(double[] xold,
+            double[] yold,
+            double[] dold,
+            int n,
+            double[] x2,
+            int n2,
+            ref double[] y,
+            bool needy,
+            ref double[] d1,
+            bool needd1,
+            ref double[] d2,
+            bool needd2)
+        {
+            int intervalindex = 0;
+            int pointindex = 0;
+            bool havetoadvance = new bool();
+            double c0 = 0;
+            double c1 = 0;
+            double c2 = 0;
+            double c3 = 0;
+            double a = 0;
+            double b = 0;
+            double w = 0;
+            double w2 = 0;
+            double w3 = 0;
+            double fa = 0;
+            double fb = 0;
+            double da = 0;
+            double db = 0;
+            double t = 0;
+
+            
+            //
+            // Prepare space
+            //
+            if( needy & ap.len(y)<n2 )
+            {
+                y = new double[n2];
+            }
+            if( needd1 & ap.len(d1)<n2 )
+            {
+                d1 = new double[n2];
+            }
+            if( needd2 & ap.len(d2)<n2 )
+            {
+                d2 = new double[n2];
+            }
+            
+            //
+            // These assignments aren't actually needed
+            // (variables are initialized in the loop below),
+            // but without them compiler will complain about uninitialized locals
+            //
+            c0 = 0;
+            c1 = 0;
+            c2 = 0;
+            c3 = 0;
+            a = 0;
+            b = 0;
+            
+            //
+            // Cycle
+            //
+            intervalindex = -1;
+            pointindex = 0;
+            while( true )
+            {
+                
+                //
+                // are we ready to exit?
+                //
+                if( pointindex>=n2 )
+                {
+                    break;
+                }
+                t = x2[pointindex];
+                
+                //
+                // do we need to advance interval?
+                //
+                havetoadvance = false;
+                if( intervalindex==-1 )
+                {
+                    havetoadvance = true;
+                }
+                else
+                {
+                    if( intervalindex<n-2 )
+                    {
+                        havetoadvance = (double)(t)>=(double)(b);
+                    }
+                }
+                if( havetoadvance )
+                {
+                    intervalindex = intervalindex+1;
+                    a = xold[intervalindex];
+                    b = xold[intervalindex+1];
+                    w = b-a;
+                    w2 = w*w;
+                    w3 = w*w2;
+                    fa = yold[intervalindex];
+                    fb = yold[intervalindex+1];
+                    da = dold[intervalindex];
+                    db = dold[intervalindex+1];
+                    c0 = fa;
+                    c1 = da;
+                    c2 = (3*(fb-fa)-2*da*w-db*w)/w2;
+                    c3 = (2*(fa-fb)+da*w+db*w)/w3;
+                    continue;
+                }
+                
+                //
+                // Calculate spline and its derivatives using power basis
+                //
+                t = t-a;
+                if( needy )
+                {
+                    y[pointindex] = c0+t*(c1+t*(c2+t*c3));
+                }
+                if( needd1 )
+                {
+                    d1[pointindex] = c1+2*t*c2+3*t*t*c3;
+                }
+                if( needd2 )
+                {
+                    d2[pointindex] = 2*c2+6*t*c3;
+                }
+                pointindex = pointindex+1;
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Heap sort.
+        *************************************************************************/
+        public static void heapsortdpoints(ref double[] x,
+            ref double[] y,
+            ref double[] d,
+            int n)
+        {
+            double[] rbuf = new double[0];
+            int[] ibuf = new int[0];
+            double[] rbuf2 = new double[0];
+            int[] ibuf2 = new int[0];
+            int i = 0;
+            int i_ = 0;
+
+            ibuf = new int[n];
+            rbuf = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                ibuf[i] = i;
+            }
+            tsort.tagsortfasti(ref x, ref ibuf, ref rbuf2, ref ibuf2, n);
+            for(i=0; i<=n-1; i++)
+            {
+                rbuf[i] = y[ibuf[i]];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                y[i_] = rbuf[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                rbuf[i] = d[ibuf[i]];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                d[i_] = rbuf[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        Internal version of Spline1DGridDiffCubic.
+
+        Accepts pre-ordered X/Y, temporary arrays (which may be  preallocated,  if
+        you want to save time, or not) and output array (which may be preallocated
+        too).
+
+        Y is passed as var-parameter because we may need to force last element  to
+        be equal to the first one (if periodic boundary conditions are specified).
+
+          -- ALGLIB PROJECT --
+             Copyright 03.09.2010 by Bochkanov Sergey
+        *************************************************************************/
+        private static void spline1dgriddiffcubicinternal(double[] x,
+            ref double[] y,
+            int n,
+            int boundltype,
+            double boundl,
+            int boundrtype,
+            double boundr,
+            ref double[] d,
+            ref double[] a1,
+            ref double[] a2,
+            ref double[] a3,
+            ref double[] b,
+            ref double[] dt)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            
+            //
+            // allocate arrays
+            //
+            if( ap.len(d)<n )
+            {
+                d = new double[n];
+            }
+            if( ap.len(a1)<n )
+            {
+                a1 = new double[n];
+            }
+            if( ap.len(a2)<n )
+            {
+                a2 = new double[n];
+            }
+            if( ap.len(a3)<n )
+            {
+                a3 = new double[n];
+            }
+            if( ap.len(b)<n )
+            {
+                b = new double[n];
+            }
+            if( ap.len(dt)<n )
+            {
+                dt = new double[n];
+            }
+            
+            //
+            // Special cases:
+            // * N=2, parabolic terminated boundary condition on both ends
+            // * N=2, periodic boundary condition
+            //
+            if( (n==2 & boundltype==0) & boundrtype==0 )
+            {
+                d[0] = (y[1]-y[0])/(x[1]-x[0]);
+                d[1] = d[0];
+                return;
+            }
+            if( (n==2 & boundltype==-1) & boundrtype==-1 )
+            {
+                d[0] = 0;
+                d[1] = 0;
+                return;
+            }
+            
+            //
+            // Periodic and non-periodic boundary conditions are
+            // two separate classes
+            //
+            if( boundrtype==-1 & boundltype==-1 )
+            {
+                
+                //
+                // Periodic boundary conditions
+                //
+                y[n-1] = y[0];
+                
+                //
+                // Boundary conditions at N-1 points
+                // (one point less because last point is the same as first point).
+                //
+                a1[0] = x[1]-x[0];
+                a2[0] = 2*(x[1]-x[0]+x[n-1]-x[n-2]);
+                a3[0] = x[n-1]-x[n-2];
+                b[0] = 3*(y[n-1]-y[n-2])/(x[n-1]-x[n-2])*(x[1]-x[0])+3*(y[1]-y[0])/(x[1]-x[0])*(x[n-1]-x[n-2]);
+                for(i=1; i<=n-2; i++)
+                {
+                    
+                    //
+                    // Altough last point is [N-2], we use X[N-1] and Y[N-1]
+                    // (because of periodicity)
+                    //
+                    a1[i] = x[i+1]-x[i];
+                    a2[i] = 2*(x[i+1]-x[i-1]);
+                    a3[i] = x[i]-x[i-1];
+                    b[i] = 3*(y[i]-y[i-1])/(x[i]-x[i-1])*(x[i+1]-x[i])+3*(y[i+1]-y[i])/(x[i+1]-x[i])*(x[i]-x[i-1]);
+                }
+                
+                //
+                // Solve, add last point (with index N-1)
+                //
+                solvecyclictridiagonal(a1, a2, a3, b, n-1, ref dt);
+                for(i_=0; i_<=n-2;i_++)
+                {
+                    d[i_] = dt[i_];
+                }
+                d[n-1] = d[0];
+            }
+            else
+            {
+                
+                //
+                // Non-periodic boundary condition.
+                // Left boundary conditions.
+                //
+                if( boundltype==0 )
+                {
+                    a1[0] = 0;
+                    a2[0] = 1;
+                    a3[0] = 1;
+                    b[0] = 2*(y[1]-y[0])/(x[1]-x[0]);
+                }
+                if( boundltype==1 )
+                {
+                    a1[0] = 0;
+                    a2[0] = 1;
+                    a3[0] = 0;
+                    b[0] = boundl;
+                }
+                if( boundltype==2 )
+                {
+                    a1[0] = 0;
+                    a2[0] = 2;
+                    a3[0] = 1;
+                    b[0] = 3*(y[1]-y[0])/(x[1]-x[0])-0.5*boundl*(x[1]-x[0]);
+                }
+                
+                //
+                // Central conditions
+                //
+                for(i=1; i<=n-2; i++)
+                {
+                    a1[i] = x[i+1]-x[i];
+                    a2[i] = 2*(x[i+1]-x[i-1]);
+                    a3[i] = x[i]-x[i-1];
+                    b[i] = 3*(y[i]-y[i-1])/(x[i]-x[i-1])*(x[i+1]-x[i])+3*(y[i+1]-y[i])/(x[i+1]-x[i])*(x[i]-x[i-1]);
+                }
+                
+                //
+                // Right boundary conditions
+                //
+                if( boundrtype==0 )
+                {
+                    a1[n-1] = 1;
+                    a2[n-1] = 1;
+                    a3[n-1] = 0;
+                    b[n-1] = 2*(y[n-1]-y[n-2])/(x[n-1]-x[n-2]);
+                }
+                if( boundrtype==1 )
+                {
+                    a1[n-1] = 0;
+                    a2[n-1] = 1;
+                    a3[n-1] = 0;
+                    b[n-1] = boundr;
+                }
+                if( boundrtype==2 )
+                {
+                    a1[n-1] = 1;
+                    a2[n-1] = 2;
+                    a3[n-1] = 0;
+                    b[n-1] = 3*(y[n-1]-y[n-2])/(x[n-1]-x[n-2])+0.5*boundr*(x[n-1]-x[n-2]);
+                }
+                
+                //
+                // Solve
+                //
+                solvetridiagonal(a1, a2, a3, b, n, ref d);
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Heap sort.
+        *************************************************************************/
+        private static void heapsortpoints(ref double[] x,
+            ref double[] y,
+            int n)
+        {
+            double[] bufx = new double[0];
+            double[] bufy = new double[0];
+
+            tsort.tagsortfastr(ref x, ref y, ref bufx, ref bufy, n);
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Heap sort.
+
+        Accepts:
+            X, Y    -   points
+            P       -   empty or preallocated array
+            
+        Returns:
+            X, Y    -   sorted by X
+            P       -   array of permutations; I-th position of output
+                        arrays X/Y contains (X[P[I]],Y[P[I]])
+        *************************************************************************/
+        private static void heapsortppoints(ref double[] x,
+            ref double[] y,
+            ref int[] p,
+            int n)
+        {
+            double[] rbuf = new double[0];
+            int[] ibuf = new int[0];
+            int i = 0;
+            int i_ = 0;
+
+            if( ap.len(p)<n )
+            {
+                p = new int[n];
+            }
+            rbuf = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                p[i] = i;
+            }
+            tsort.tagsortfasti(ref x, ref p, ref rbuf, ref ibuf, n);
+            for(i=0; i<=n-1; i++)
+            {
+                rbuf[i] = y[p[i]];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                y[i_] = rbuf[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Tridiagonal solver. Solves
+
+        ( B[0] C[0]                      )
+        ( A[1] B[1] C[1]                 )
+        (      A[2] B[2] C[2]            )
+        (            ..........          ) * X = D
+        (            ..........          )
+        (           A[N-2] B[N-2] C[N-2] )
+        (                  A[N-1] B[N-1] )
+
+        *************************************************************************/
+        private static void solvetridiagonal(double[] a,
+            double[] b,
+            double[] c,
+            double[] d,
+            int n,
+            ref double[] x)
+        {
+            int k = 0;
+            double t = 0;
+
+            b = (double[])b.Clone();
+            d = (double[])d.Clone();
+
+            if( ap.len(x)<n )
+            {
+                x = new double[n];
+            }
+            for(k=1; k<=n-1; k++)
+            {
+                t = a[k]/b[k-1];
+                b[k] = b[k]-t*c[k-1];
+                d[k] = d[k]-t*d[k-1];
+            }
+            x[n-1] = d[n-1]/b[n-1];
+            for(k=n-2; k>=0; k--)
+            {
+                x[k] = (d[k]-c[k]*x[k+1])/b[k];
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Cyclic tridiagonal solver. Solves
+
+        ( B[0] C[0]                 A[0] )
+        ( A[1] B[1] C[1]                 )
+        (      A[2] B[2] C[2]            )
+        (            ..........          ) * X = D
+        (            ..........          )
+        (           A[N-2] B[N-2] C[N-2] )
+        ( C[N-1]           A[N-1] B[N-1] )
+        *************************************************************************/
+        private static void solvecyclictridiagonal(double[] a,
+            double[] b,
+            double[] c,
+            double[] d,
+            int n,
+            ref double[] x)
+        {
+            int k = 0;
+            double alpha = 0;
+            double beta = 0;
+            double gamma = 0;
+            double[] y = new double[0];
+            double[] z = new double[0];
+            double[] u = new double[0];
+
+            b = (double[])b.Clone();
+
+            if( ap.len(x)<n )
+            {
+                x = new double[n];
+            }
+            beta = a[0];
+            alpha = c[n-1];
+            gamma = -b[0];
+            b[0] = 2*b[0];
+            b[n-1] = b[n-1]-alpha*beta/gamma;
+            u = new double[n];
+            for(k=0; k<=n-1; k++)
+            {
+                u[k] = 0;
+            }
+            u[0] = gamma;
+            u[n-1] = alpha;
+            solvetridiagonal(a, b, c, d, n, ref y);
+            solvetridiagonal(a, b, c, u, n, ref z);
+            for(k=0; k<=n-1; k++)
+            {
+                x[k] = y[k]-(y[0]+beta/gamma*y[n-1])/(1+z[0]+beta/gamma*z[n-1])*z[k];
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine. Three-point differentiation
+        *************************************************************************/
+        private static double diffthreepoint(double t,
+            double x0,
+            double f0,
+            double x1,
+            double f1,
+            double x2,
+            double f2)
+        {
+            double result = 0;
+            double a = 0;
+            double b = 0;
+
+            t = t-x0;
+            x1 = x1-x0;
+            x2 = x2-x0;
+            a = (f2-f0-x2/x1*(f1-f0))/(math.sqr(x2)-x1*x2);
+            b = (f1-f0-a*math.sqr(x1))/x1;
+            result = 2*a*t+b;
+            return result;
+        }
+
+
+    }
+    public class lsfit
+    {
+        /*************************************************************************
+        Polynomial fitting report:
+            TaskRCond       reciprocal of task's condition number
+            RMSError        RMS error
+            AvgError        average error
+            AvgRelError     average relative error (for non-zero Y[I])
+            MaxError        maximum error
+        *************************************************************************/
+        public class polynomialfitreport
+        {
+            public double taskrcond;
+            public double rmserror;
+            public double avgerror;
+            public double avgrelerror;
+            public double maxerror;
+        };
+
+
+        /*************************************************************************
+        Barycentric fitting report:
+            RMSError        RMS error
+            AvgError        average error
+            AvgRelError     average relative error (for non-zero Y[I])
+            MaxError        maximum error
+            TaskRCond       reciprocal of task's condition number
+        *************************************************************************/
+        public class barycentricfitreport
+        {
+            public double taskrcond;
+            public int dbest;
+            public double rmserror;
+            public double avgerror;
+            public double avgrelerror;
+            public double maxerror;
+        };
+
+
+        /*************************************************************************
+        Spline fitting report:
+            RMSError        RMS error
+            AvgError        average error
+            AvgRelError     average relative error (for non-zero Y[I])
+            MaxError        maximum error
+            
+        Fields  below are  filled  by   obsolete    functions   (Spline1DFitCubic,
+        Spline1DFitHermite). Modern fitting functions do NOT fill these fields:
+            TaskRCond       reciprocal of task's condition number
+        *************************************************************************/
+        public class spline1dfitreport
+        {
+            public double taskrcond;
+            public double rmserror;
+            public double avgerror;
+            public double avgrelerror;
+            public double maxerror;
+        };
+
+
+        /*************************************************************************
+        Least squares fitting report:
+            TaskRCond       reciprocal of task's condition number
+            IterationsCount number of internal iterations
+
+            RMSError        RMS error
+            AvgError        average error
+            AvgRelError     average relative error (for non-zero Y[I])
+            MaxError        maximum error
+
+            WRMSError       weighted RMS error
+        *************************************************************************/
+        public class lsfitreport
+        {
+            public double taskrcond;
+            public int iterationscount;
+            public double rmserror;
+            public double avgerror;
+            public double avgrelerror;
+            public double maxerror;
+            public double wrmserror;
+        };
+
+
+        /*************************************************************************
+        Nonlinear fitter.
+
+        You should use ALGLIB functions to work with fitter.
+        Never try to access its fields directly!
+        *************************************************************************/
+        public class lsfitstate
+        {
+            public int optalgo;
+            public int m;
+            public int k;
+            public double epsf;
+            public double epsx;
+            public int maxits;
+            public double stpmax;
+            public bool xrep;
+            public double[] s;
+            public double[] bndl;
+            public double[] bndu;
+            public double[,] taskx;
+            public double[] tasky;
+            public int npoints;
+            public double[] w;
+            public int nweights;
+            public int wkind;
+            public int wits;
+            public bool xupdated;
+            public bool needf;
+            public bool needfg;
+            public bool needfgh;
+            public int pointindex;
+            public double[] x;
+            public double[] c;
+            public double f;
+            public double[] g;
+            public double[,] h;
+            public int repiterationscount;
+            public int repterminationtype;
+            public double reprmserror;
+            public double repavgerror;
+            public double repavgrelerror;
+            public double repmaxerror;
+            public double repwrmserror;
+            public minlm.minlmstate optstate;
+            public minlm.minlmreport optrep;
+            public int prevnpt;
+            public int prevalgo;
+            public rcommstate rstate;
+            public lsfitstate()
+            {
+                s = new double[0];
+                bndl = new double[0];
+                bndu = new double[0];
+                taskx = new double[0,0];
+                tasky = new double[0];
+                w = new double[0];
+                x = new double[0];
+                c = new double[0];
+                g = new double[0];
+                h = new double[0,0];
+                optstate = new minlm.minlmstate();
+                optrep = new minlm.minlmreport();
+                rstate = new rcommstate();
+            }
+        };
+
+
+
+
+        public const int rfsmax = 10;
+
+
+        /*************************************************************************
+        Fitting by polynomials in barycentric form. This function provides  simple
+        unterface for unconstrained unweighted fitting. See  PolynomialFitWC()  if
+        you need constrained fitting.
+
+        Task is linear, so linear least squares solver is used. Complexity of this
+        computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+        SEE ALSO:
+            PolynomialFitWC()
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            N   -   number of points, N>0
+                    * if given, only leading N elements of X/Y are used
+                    * if not given, automatically determined from sizes of X/Y
+            M   -   number of basis functions (= polynomial_degree + 1), M>=1
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearW() subroutine:
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+            P   -   interpolant in barycentric form.
+            Rep -   report, same format as in LSFitLinearW() subroutine.
+                    Following fields are set:
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+        NOTES:
+            you can convert P from barycentric form  to  the  power  or  Chebyshev
+            basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
+            POLINT subpackage.
+
+          -- ALGLIB PROJECT --
+             Copyright 10.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialfit(double[] x,
+            double[] y,
+            int n,
+            int m,
+            ref int info,
+            ratint.barycentricinterpolant p,
+            polynomialfitreport rep)
+        {
+            int i = 0;
+            double[] w = new double[0];
+            double[] xc = new double[0];
+            double[] yc = new double[0];
+            int[] dc = new int[0];
+
+            info = 0;
+
+            ap.assert(n>0, "PolynomialFit: N<=0!");
+            ap.assert(m>0, "PolynomialFit: M<=0!");
+            ap.assert(ap.len(x)>=n, "PolynomialFit: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "PolynomialFit: Length(Y)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "PolynomialFit: X contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(y, n), "PolynomialFit: Y contains infinite or NaN values!");
+            w = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = 1;
+            }
+            polynomialfitwc(x, y, w, n, xc, yc, dc, 0, m, ref info, p, rep);
+        }
+
+
+        /*************************************************************************
+        Weighted  fitting by polynomials in barycentric form, with constraints  on
+        function values or first derivatives.
+
+        Small regularizing term is used when solving constrained tasks (to improve
+        stability).
+
+        Task is linear, so linear least squares solver is used. Complexity of this
+        computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+        SEE ALSO:
+            PolynomialFit()
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            W   -   weights, array[0..N-1]
+                    Each summand in square  sum  of  approximation deviations from
+                    given  values  is  multiplied  by  the square of corresponding
+                    weight. Fill it by 1's if you don't  want  to  solve  weighted
+                    task.
+            N   -   number of points, N>0.
+                    * if given, only leading N elements of X/Y/W are used
+                    * if not given, automatically determined from sizes of X/Y/W
+            XC  -   points where polynomial values/derivatives are constrained,
+                    array[0..K-1].
+            YC  -   values of constraints, array[0..K-1]
+            DC  -   array[0..K-1], types of constraints:
+                    * DC[i]=0   means that P(XC[i])=YC[i]
+                    * DC[i]=1   means that P'(XC[i])=YC[i]
+                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+            K   -   number of constraints, 0<=K<M.
+                    K=0 means no constraints (XC/YC/DC are not used in such cases)
+            M   -   number of basis functions (= polynomial_degree + 1), M>=1
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearW() subroutine:
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+                                -3 means inconsistent constraints
+            P   -   interpolant in barycentric form.
+            Rep -   report, same format as in LSFitLinearW() subroutine.
+                    Following fields are set:
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroitine doesn't calculate task's condition number for K<>0.
+
+        NOTES:
+            you can convert P from barycentric form  to  the  power  or  Chebyshev
+            basis with PolynomialBar2Pow() or PolynomialBar2Cheb() functions  from
+            POLINT subpackage.
+
+        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+        Setting constraints can lead  to undesired  results,  like ill-conditioned
+        behavior, or inconsistency being detected. From the other side,  it allows
+        us to improve quality of the fit. Here we summarize  our  experience  with
+        constrained regression splines:
+        * even simple constraints can be inconsistent, see  Wikipedia  article  on
+          this subject: http://en.wikipedia.org/wiki/Birkhoff_interpolation
+        * the  greater  is  M (given  fixed  constraints),  the  more chances that
+          constraints will be consistent
+        * in the general case, consistency of constraints is NOT GUARANTEED.
+        * in the one special cases, however, we can  guarantee  consistency.  This
+          case  is:  M>1  and constraints on the function values (NOT DERIVATIVES)
+
+        Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
+        can't solve your task without them. Anything beyond  special  cases  given
+        above is not guaranteed and may result in inconsistency.
+
+          -- ALGLIB PROJECT --
+             Copyright 10.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void polynomialfitwc(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            double[] xc,
+            double[] yc,
+            int[] dc,
+            int k,
+            int m,
+            ref int info,
+            ratint.barycentricinterpolant p,
+            polynomialfitreport rep)
+        {
+            double xa = 0;
+            double xb = 0;
+            double sa = 0;
+            double sb = 0;
+            double[] xoriginal = new double[0];
+            double[] yoriginal = new double[0];
+            double[] y2 = new double[0];
+            double[] w2 = new double[0];
+            double[] tmp = new double[0];
+            double[] tmp2 = new double[0];
+            double[] bx = new double[0];
+            double[] by = new double[0];
+            double[] bw = new double[0];
+            int i = 0;
+            int j = 0;
+            double u = 0;
+            double v = 0;
+            double s = 0;
+            int relcnt = 0;
+            lsfitreport lrep = new lsfitreport();
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            w = (double[])w.Clone();
+            xc = (double[])xc.Clone();
+            yc = (double[])yc.Clone();
+            info = 0;
+
+            ap.assert(n>0, "PolynomialFitWC: N<=0!");
+            ap.assert(m>0, "PolynomialFitWC: M<=0!");
+            ap.assert(k>=0, "PolynomialFitWC: K<0!");
+            ap.assert(k<m, "PolynomialFitWC: K>=M!");
+            ap.assert(ap.len(x)>=n, "PolynomialFitWC: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "PolynomialFitWC: Length(Y)<N!");
+            ap.assert(ap.len(w)>=n, "PolynomialFitWC: Length(W)<N!");
+            ap.assert(ap.len(xc)>=k, "PolynomialFitWC: Length(XC)<K!");
+            ap.assert(ap.len(yc)>=k, "PolynomialFitWC: Length(YC)<K!");
+            ap.assert(ap.len(dc)>=k, "PolynomialFitWC: Length(DC)<K!");
+            ap.assert(apserv.isfinitevector(x, n), "PolynomialFitWC: X contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(y, n), "PolynomialFitWC: Y contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(w, n), "PolynomialFitWC: X contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(xc, k), "PolynomialFitWC: XC contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(yc, k), "PolynomialFitWC: YC contains infinite or NaN values!");
+            for(i=0; i<=k-1; i++)
+            {
+                ap.assert(dc[i]==0 | dc[i]==1, "PolynomialFitWC: one of DC[] is not 0 or 1!");
+            }
+            
+            //
+            // Scale X, Y, XC, YC.
+            // Solve scaled problem using internal Chebyshev fitting function.
+            //
+            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
+            internalchebyshevfit(x, y, w, n, xc, yc, dc, k, m, ref info, ref tmp, lrep);
+            if( info<0 )
+            {
+                return;
+            }
+            
+            //
+            // Generate barycentric model and scale it
+            // * BX, BY store barycentric model nodes
+            // * FMatrix is reused (remember - it is at least MxM, what we need)
+            //
+            // Model intialization is done in O(M^2). In principle, it can be
+            // done in O(M*log(M)), but before it we solved task with O(N*M^2)
+            // complexity, so it is only a small amount of total time spent.
+            //
+            bx = new double[m];
+            by = new double[m];
+            bw = new double[m];
+            tmp2 = new double[m];
+            s = 1;
+            for(i=0; i<=m-1; i++)
+            {
+                if( m!=1 )
+                {
+                    u = Math.Cos(Math.PI*i/(m-1));
+                }
+                else
+                {
+                    u = 0;
+                }
+                v = 0;
+                for(j=0; j<=m-1; j++)
+                {
+                    if( j==0 )
+                    {
+                        tmp2[j] = 1;
+                    }
+                    else
+                    {
+                        if( j==1 )
+                        {
+                            tmp2[j] = u;
+                        }
+                        else
+                        {
+                            tmp2[j] = 2*u*tmp2[j-1]-tmp2[j-2];
+                        }
+                    }
+                    v = v+tmp[j]*tmp2[j];
+                }
+                bx[i] = u;
+                by[i] = v;
+                bw[i] = s;
+                if( i==0 | i==m-1 )
+                {
+                    bw[i] = 0.5*bw[i];
+                }
+                s = -s;
+            }
+            ratint.barycentricbuildxyw(bx, by, bw, m, p);
+            ratint.barycentriclintransx(p, 2/(xb-xa), -((xa+xb)/(xb-xa)));
+            ratint.barycentriclintransy(p, sb-sa, sa);
+            
+            //
+            // Scale absolute errors obtained from LSFitLinearW.
+            // Relative error should be calculated separately
+            // (because of shifting/scaling of the task)
+            //
+            rep.taskrcond = lrep.taskrcond;
+            rep.rmserror = lrep.rmserror*(sb-sa);
+            rep.avgerror = lrep.avgerror*(sb-sa);
+            rep.maxerror = lrep.maxerror*(sb-sa);
+            rep.avgrelerror = 0;
+            relcnt = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                if( (double)(yoriginal[i])!=(double)(0) )
+                {
+                    rep.avgrelerror = rep.avgrelerror+Math.Abs(ratint.barycentriccalc(p, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
+                    relcnt = relcnt+1;
+                }
+            }
+            if( relcnt!=0 )
+            {
+                rep.avgrelerror = rep.avgrelerror/relcnt;
+            }
+        }
+
+
+        /*************************************************************************
+        Weghted rational least  squares  fitting  using  Floater-Hormann  rational
+        functions  with  optimal  D  chosen  from  [0,9],  with  constraints   and
+        individual weights.
+
+        Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
+        functions. Different values of D are tried, optimal D (least WEIGHTED root
+        mean square error) is chosen.  Task  is  linear,  so  linear least squares
+        solver  is  used.  Complexity  of  this  computational  scheme is O(N*M^2)
+        (mostly dominated by the least squares solver).
+
+        SEE ALSO
+        * BarycentricFitFloaterHormann(), "lightweight" fitting without invididual
+          weights and constraints.
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            W   -   weights, array[0..N-1]
+                    Each summand in square  sum  of  approximation deviations from
+                    given  values  is  multiplied  by  the square of corresponding
+                    weight. Fill it by 1's if you don't  want  to  solve  weighted
+                    task.
+            N   -   number of points, N>0.
+            XC  -   points where function values/derivatives are constrained,
+                    array[0..K-1].
+            YC  -   values of constraints, array[0..K-1]
+            DC  -   array[0..K-1], types of constraints:
+                    * DC[i]=0   means that S(XC[i])=YC[i]
+                    * DC[i]=1   means that S'(XC[i])=YC[i]
+                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+            K   -   number of constraints, 0<=K<M.
+                    K=0 means no constraints (XC/YC/DC are not used in such cases)
+            M   -   number of basis functions ( = number_of_nodes), M>=2.
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearWC() subroutine.
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+                                -3 means inconsistent constraints
+                                -1 means another errors in parameters passed
+                                   (N<=0, for example)
+            B   -   barycentric interpolant.
+            Rep -   report, same format as in LSFitLinearWC() subroutine.
+                    Following fields are set:
+                    * DBest         best value of the D parameter
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroutine doesn't calculate task's condition number for K<>0.
+
+        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+        Setting constraints can lead  to undesired  results,  like ill-conditioned
+        behavior, or inconsistency being detected. From the other side,  it allows
+        us to improve quality of the fit. Here we summarize  our  experience  with
+        constrained barycentric interpolants:
+        * excessive  constraints  can  be  inconsistent.   Floater-Hormann   basis
+          functions aren't as flexible as splines (although they are very smooth).
+        * the more evenly constraints are spread across [min(x),max(x)],  the more
+          chances that they will be consistent
+        * the  greater  is  M (given  fixed  constraints),  the  more chances that
+          constraints will be consistent
+        * in the general case, consistency of constraints IS NOT GUARANTEED.
+        * in the several special cases, however, we CAN guarantee consistency.
+        * one of this cases is constraints on the function  VALUES at the interval
+          boundaries. Note that consustency of the  constraints  on  the  function
+          DERIVATIVES is NOT guaranteed (you can use in such cases  cubic  splines
+          which are more flexible).
+        * another  special  case  is ONE constraint on the function value (OR, but
+          not AND, derivative) anywhere in the interval
+
+        Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
+        can't solve your task without them. Anything beyond  special  cases  given
+        above is not guaranteed and may result in inconsistency.
+
+          -- ALGLIB PROJECT --
+             Copyright 18.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentricfitfloaterhormannwc(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            double[] xc,
+            double[] yc,
+            int[] dc,
+            int k,
+            int m,
+            ref int info,
+            ratint.barycentricinterpolant b,
+            barycentricfitreport rep)
+        {
+            int d = 0;
+            int i = 0;
+            double wrmscur = 0;
+            double wrmsbest = 0;
+            ratint.barycentricinterpolant locb = new ratint.barycentricinterpolant();
+            barycentricfitreport locrep = new barycentricfitreport();
+            int locinfo = 0;
+
+            info = 0;
+
+            ap.assert(n>0, "BarycentricFitFloaterHormannWC: N<=0!");
+            ap.assert(m>0, "BarycentricFitFloaterHormannWC: M<=0!");
+            ap.assert(k>=0, "BarycentricFitFloaterHormannWC: K<0!");
+            ap.assert(k<m, "BarycentricFitFloaterHormannWC: K>=M!");
+            ap.assert(ap.len(x)>=n, "BarycentricFitFloaterHormannWC: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "BarycentricFitFloaterHormannWC: Length(Y)<N!");
+            ap.assert(ap.len(w)>=n, "BarycentricFitFloaterHormannWC: Length(W)<N!");
+            ap.assert(ap.len(xc)>=k, "BarycentricFitFloaterHormannWC: Length(XC)<K!");
+            ap.assert(ap.len(yc)>=k, "BarycentricFitFloaterHormannWC: Length(YC)<K!");
+            ap.assert(ap.len(dc)>=k, "BarycentricFitFloaterHormannWC: Length(DC)<K!");
+            ap.assert(apserv.isfinitevector(x, n), "BarycentricFitFloaterHormannWC: X contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(y, n), "BarycentricFitFloaterHormannWC: Y contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(w, n), "BarycentricFitFloaterHormannWC: X contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(xc, k), "BarycentricFitFloaterHormannWC: XC contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(yc, k), "BarycentricFitFloaterHormannWC: YC contains infinite or NaN values!");
+            for(i=0; i<=k-1; i++)
+            {
+                ap.assert(dc[i]==0 | dc[i]==1, "BarycentricFitFloaterHormannWC: one of DC[] is not 0 or 1!");
+            }
+            
+            //
+            // Find optimal D
+            //
+            // Info is -3 by default (degenerate constraints).
+            // If LocInfo will always be equal to -3, Info will remain equal to -3.
+            // If at least once LocInfo will be -4, Info will be -4.
+            //
+            wrmsbest = math.maxrealnumber;
+            rep.dbest = -1;
+            info = -3;
+            for(d=0; d<=Math.Min(9, n-1); d++)
+            {
+                barycentricfitwcfixedd(x, y, w, n, xc, yc, dc, k, m, d, ref locinfo, locb, locrep);
+                ap.assert((locinfo==-4 | locinfo==-3) | locinfo>0, "BarycentricFitFloaterHormannWC: unexpected result from BarycentricFitWCFixedD!");
+                if( locinfo>0 )
+                {
+                    
+                    //
+                    // Calculate weghted RMS
+                    //
+                    wrmscur = 0;
+                    for(i=0; i<=n-1; i++)
+                    {
+                        wrmscur = wrmscur+math.sqr(w[i]*(y[i]-ratint.barycentriccalc(locb, x[i])));
+                    }
+                    wrmscur = Math.Sqrt(wrmscur/n);
+                    if( (double)(wrmscur)<(double)(wrmsbest) | rep.dbest<0 )
+                    {
+                        ratint.barycentriccopy(locb, b);
+                        rep.dbest = d;
+                        info = 1;
+                        rep.rmserror = locrep.rmserror;
+                        rep.avgerror = locrep.avgerror;
+                        rep.avgrelerror = locrep.avgrelerror;
+                        rep.maxerror = locrep.maxerror;
+                        rep.taskrcond = locrep.taskrcond;
+                        wrmsbest = wrmscur;
+                    }
+                }
+                else
+                {
+                    if( locinfo!=-3 & info<0 )
+                    {
+                        info = locinfo;
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Rational least squares fitting using  Floater-Hormann  rational  functions
+        with optimal D chosen from [0,9].
+
+        Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
+        functions. Different values of D are tried, optimal  D  (least  root  mean
+        square error) is chosen.  Task  is  linear, so linear least squares solver
+        is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
+        dominated by the least squares solver).
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            N   -   number of points, N>0.
+            M   -   number of basis functions ( = number_of_nodes), M>=2.
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearWC() subroutine.
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+                                -3 means inconsistent constraints
+            B   -   barycentric interpolant.
+            Rep -   report, same format as in LSFitLinearWC() subroutine.
+                    Following fields are set:
+                    * DBest         best value of the D parameter
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+          -- ALGLIB PROJECT --
+             Copyright 18.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void barycentricfitfloaterhormann(double[] x,
+            double[] y,
+            int n,
+            int m,
+            ref int info,
+            ratint.barycentricinterpolant b,
+            barycentricfitreport rep)
+        {
+            double[] w = new double[0];
+            double[] xc = new double[0];
+            double[] yc = new double[0];
+            int[] dc = new int[0];
+            int i = 0;
+
+            info = 0;
+
+            ap.assert(n>0, "BarycentricFitFloaterHormann: N<=0!");
+            ap.assert(m>0, "BarycentricFitFloaterHormann: M<=0!");
+            ap.assert(ap.len(x)>=n, "BarycentricFitFloaterHormann: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "BarycentricFitFloaterHormann: Length(Y)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "BarycentricFitFloaterHormann: X contains infinite or NaN values!");
+            ap.assert(apserv.isfinitevector(y, n), "BarycentricFitFloaterHormann: Y contains infinite or NaN values!");
+            w = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = 1;
+            }
+            barycentricfitfloaterhormannwc(x, y, w, n, xc, yc, dc, 0, m, ref info, b, rep);
+        }
+
+
+        /*************************************************************************
+        Rational least squares fitting using  Floater-Hormann  rational  functions
+        with optimal D chosen from [0,9].
+
+        Equidistant  grid  with M node on [min(x),max(x)]  is  used to build basis
+        functions. Different values of D are tried, optimal  D  (least  root  mean
+        square error) is chosen.  Task  is  linear, so linear least squares solver
+        is used. Complexity  of  this  computational  scheme is  O(N*M^2)  (mostly
+        dominated by the least squares solver).
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            N   -   number of points, N>0.
+            M   -   number of basis functions ( = number_of_nodes), M>=2.
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearWC() subroutine.
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+                                -3 means inconsistent constraints
+            B   -   barycentric interpolant.
+            Rep -   report, same format as in LSFitLinearWC() subroutine.
+                    Following fields are set:
+                    * DBest         best value of the D parameter
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+          -- ALGLIB PROJECT --
+             Copyright 18.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dfitpenalized(double[] x,
+            double[] y,
+            int n,
+            int m,
+            double rho,
+            ref int info,
+            spline1d.spline1dinterpolant s,
+            spline1dfitreport rep)
+        {
+            double[] w = new double[0];
+            int i = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            info = 0;
+
+            ap.assert(n>=1, "Spline1DFitPenalized: N<1!");
+            ap.assert(m>=4, "Spline1DFitPenalized: M<4!");
+            ap.assert(ap.len(x)>=n, "Spline1DFitPenalized: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DFitPenalized: Length(Y)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitPenalized: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitPenalized: Y contains infinite or NAN values!");
+            ap.assert(math.isfinite(rho), "Spline1DFitPenalized: Rho is infinite!");
+            w = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = 1;
+            }
+            spline1dfitpenalizedw(x, y, w, n, m, rho, ref info, s, rep);
+        }
+
+
+        /*************************************************************************
+        Weighted fitting by penalized cubic spline.
+
+        Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
+        basis functions. Basis functions are cubic splines with  natural  boundary
+        conditions. Problem is regularized by  adding non-linearity penalty to the
+        usual least squares penalty function:
+
+            S(x) = arg min { LS + P }, where
+            LS   = SUM { w[i]^2*(y[i] - S(x[i]))^2 } - least squares penalty
+            P    = C*10^rho*integral{ S''(x)^2*dx } - non-linearity penalty
+            rho  - tunable constant given by user
+            C    - automatically determined scale parameter,
+                   makes penalty invariant with respect to scaling of X, Y, W.
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            W   -   weights, array[0..N-1]
+                    Each summand in square  sum  of  approximation deviations from
+                    given  values  is  multiplied  by  the square of corresponding
+                    weight. Fill it by 1's if you don't  want  to  solve  weighted
+                    problem.
+            N   -   number of points (optional):
+                    * N>0
+                    * if given, only first N elements of X/Y/W are processed
+                    * if not given, automatically determined from X/Y/W sizes
+            M   -   number of basis functions ( = number_of_nodes), M>=4.
+            Rho -   regularization  constant  passed   by   user.   It   penalizes
+                    nonlinearity in the regression spline. It  is  logarithmically
+                    scaled,  i.e.  actual  value  of  regularization  constant  is
+                    calculated as 10^Rho. It is automatically scaled so that:
+                    * Rho=2.0 corresponds to moderate amount of nonlinearity
+                    * generally, it should be somewhere in the [-8.0,+8.0]
+                    If you do not want to penalize nonlineary,
+                    pass small Rho. Values as low as -15 should work.
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearWC() subroutine.
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD or
+                                   Cholesky decomposition; problem may be
+                                   too ill-conditioned (very rare)
+            S   -   spline interpolant.
+            Rep -   Following fields are set:
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroitine doesn't calculate task's condition number for K<>0.
+
+        NOTE 1: additional nodes are added to the spline outside  of  the  fitting
+        interval to force linearity when x<min(x,xc) or x>max(x,xc).  It  is  done
+        for consistency - we penalize non-linearity  at [min(x,xc),max(x,xc)],  so
+        it is natural to force linearity outside of this interval.
+
+        NOTE 2: function automatically sorts points,  so  caller may pass unsorted
+        array.
+
+          -- ALGLIB PROJECT --
+             Copyright 19.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dfitpenalizedw(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            int m,
+            double rho,
+            ref int info,
+            spline1d.spline1dinterpolant s,
+            spline1dfitreport rep)
+        {
+            int i = 0;
+            int j = 0;
+            int b = 0;
+            double v = 0;
+            double relcnt = 0;
+            double xa = 0;
+            double xb = 0;
+            double sa = 0;
+            double sb = 0;
+            double[] xoriginal = new double[0];
+            double[] yoriginal = new double[0];
+            double pdecay = 0;
+            double tdecay = 0;
+            double[,] fmatrix = new double[0,0];
+            double[] fcolumn = new double[0];
+            double[] y2 = new double[0];
+            double[] w2 = new double[0];
+            double[] xc = new double[0];
+            double[] yc = new double[0];
+            int[] dc = new int[0];
+            double fdmax = 0;
+            double admax = 0;
+            double[,] amatrix = new double[0,0];
+            double[,] d2matrix = new double[0,0];
+            double fa = 0;
+            double ga = 0;
+            double fb = 0;
+            double gb = 0;
+            double lambdav = 0;
+            double[] bx = new double[0];
+            double[] by = new double[0];
+            double[] bd1 = new double[0];
+            double[] bd2 = new double[0];
+            double[] tx = new double[0];
+            double[] ty = new double[0];
+            double[] td = new double[0];
+            spline1d.spline1dinterpolant bs = new spline1d.spline1dinterpolant();
+            double[,] nmatrix = new double[0,0];
+            double[] rightpart = new double[0];
+            fbls.fblslincgstate cgstate = new fbls.fblslincgstate();
+            double[] c = new double[0];
+            double[] tmp0 = new double[0];
+            int i_ = 0;
+            int i1_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            w = (double[])w.Clone();
+            info = 0;
+
+            ap.assert(n>=1, "Spline1DFitPenalizedW: N<1!");
+            ap.assert(m>=4, "Spline1DFitPenalizedW: M<4!");
+            ap.assert(ap.len(x)>=n, "Spline1DFitPenalizedW: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DFitPenalizedW: Length(Y)<N!");
+            ap.assert(ap.len(w)>=n, "Spline1DFitPenalizedW: Length(W)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitPenalizedW: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitPenalizedW: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(w, n), "Spline1DFitPenalizedW: Y contains infinite or NAN values!");
+            ap.assert(math.isfinite(rho), "Spline1DFitPenalizedW: Rho is infinite!");
+            
+            //
+            // Prepare LambdaV
+            //
+            v = -(Math.Log(math.machineepsilon)/Math.Log(10));
+            if( (double)(rho)<(double)(-v) )
+            {
+                rho = -v;
+            }
+            if( (double)(rho)>(double)(v) )
+            {
+                rho = v;
+            }
+            lambdav = Math.Pow(10, rho);
+            
+            //
+            // Sort X, Y, W
+            //
+            spline1d.heapsortdpoints(ref x, ref y, ref w, n);
+            
+            //
+            // Scale X, Y, XC, YC
+            //
+            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, 0, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
+            
+            //
+            // Allocate space
+            //
+            fmatrix = new double[n, m];
+            amatrix = new double[m, m];
+            d2matrix = new double[m, m];
+            bx = new double[m];
+            by = new double[m];
+            fcolumn = new double[n];
+            nmatrix = new double[m, m];
+            rightpart = new double[m];
+            tmp0 = new double[Math.Max(m, n)];
+            c = new double[m];
+            
+            //
+            // Fill:
+            // * FMatrix by values of basis functions
+            // * TmpAMatrix by second derivatives of I-th function at J-th point
+            // * CMatrix by constraints
+            //
+            fdmax = 0;
+            for(b=0; b<=m-1; b++)
+            {
+                
+                //
+                // Prepare I-th basis function
+                //
+                for(j=0; j<=m-1; j++)
+                {
+                    bx[j] = (double)(2*j)/(double)(m-1)-1;
+                    by[j] = 0;
+                }
+                by[b] = 1;
+                spline1d.spline1dgriddiff2cubic(bx, by, m, 2, 0.0, 2, 0.0, ref bd1, ref bd2);
+                spline1d.spline1dbuildcubic(bx, by, m, 2, 0.0, 2, 0.0, bs);
+                
+                //
+                // Calculate B-th column of FMatrix
+                // Update FDMax (maximum column norm)
+                //
+                spline1d.spline1dconvcubic(bx, by, m, 2, 0.0, 2, 0.0, x, n, ref fcolumn);
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    fmatrix[i_,b] = fcolumn[i_];
+                }
+                v = 0;
+                for(i=0; i<=n-1; i++)
+                {
+                    v = v+math.sqr(w[i]*fcolumn[i]);
+                }
+                fdmax = Math.Max(fdmax, v);
+                
+                //
+                // Fill temporary with second derivatives of basis function
+                //
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    d2matrix[b,i_] = bd2[i_];
+                }
+            }
+            
+            //
+            // * calculate penalty matrix A
+            // * calculate max of diagonal elements of A
+            // * calculate PDecay - coefficient before penalty matrix
+            //
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=i; j<=m-1; j++)
+                {
+                    
+                    //
+                    // calculate integral(B_i''*B_j'') where B_i and B_j are
+                    // i-th and j-th basis splines.
+                    // B_i and B_j are piecewise linear functions.
+                    //
+                    v = 0;
+                    for(b=0; b<=m-2; b++)
+                    {
+                        fa = d2matrix[i,b];
+                        fb = d2matrix[i,b+1];
+                        ga = d2matrix[j,b];
+                        gb = d2matrix[j,b+1];
+                        v = v+(bx[b+1]-bx[b])*(fa*ga+(fa*(gb-ga)+ga*(fb-fa))/2+(fb-fa)*(gb-ga)/3);
+                    }
+                    amatrix[i,j] = v;
+                    amatrix[j,i] = v;
+                }
+            }
+            admax = 0;
+            for(i=0; i<=m-1; i++)
+            {
+                admax = Math.Max(admax, Math.Abs(amatrix[i,i]));
+            }
+            pdecay = lambdav*fdmax/admax;
+            
+            //
+            // Calculate TDecay for Tikhonov regularization
+            //
+            tdecay = fdmax*(1+pdecay)*10*math.machineepsilon;
+            
+            //
+            // Prepare system
+            //
+            // NOTE: FMatrix is spoiled during this process
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                v = w[i];
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    fmatrix[i,i_] = v*fmatrix[i,i_];
+                }
+            }
+            ablas.rmatrixgemm(m, m, n, 1.0, fmatrix, 0, 0, 1, fmatrix, 0, 0, 0, 0.0, ref nmatrix, 0, 0);
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=m-1; j++)
+                {
+                    nmatrix[i,j] = nmatrix[i,j]+pdecay*amatrix[i,j];
+                }
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                nmatrix[i,i] = nmatrix[i,i]+tdecay;
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                rightpart[i] = 0;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                v = y[i]*w[i];
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    rightpart[i_] = rightpart[i_] + v*fmatrix[i,i_];
+                }
+            }
+            
+            //
+            // Solve system
+            //
+            if( !trfac.spdmatrixcholesky(ref nmatrix, m, true) )
+            {
+                info = -4;
+                return;
+            }
+            fbls.fblscholeskysolve(nmatrix, 1.0, m, true, ref rightpart, ref tmp0);
+            for(i_=0; i_<=m-1;i_++)
+            {
+                c[i_] = rightpart[i_];
+            }
+            
+            //
+            // add nodes to force linearity outside of the fitting interval
+            //
+            spline1d.spline1dgriddiffcubic(bx, c, m, 2, 0.0, 2, 0.0, ref bd1);
+            tx = new double[m+2];
+            ty = new double[m+2];
+            td = new double[m+2];
+            i1_ = (0) - (1);
+            for(i_=1; i_<=m;i_++)
+            {
+                tx[i_] = bx[i_+i1_];
+            }
+            i1_ = (0) - (1);
+            for(i_=1; i_<=m;i_++)
+            {
+                ty[i_] = rightpart[i_+i1_];
+            }
+            i1_ = (0) - (1);
+            for(i_=1; i_<=m;i_++)
+            {
+                td[i_] = bd1[i_+i1_];
+            }
+            tx[0] = tx[1]-(tx[2]-tx[1]);
+            ty[0] = ty[1]-td[1]*(tx[2]-tx[1]);
+            td[0] = td[1];
+            tx[m+1] = tx[m]+(tx[m]-tx[m-1]);
+            ty[m+1] = ty[m]+td[m]*(tx[m]-tx[m-1]);
+            td[m+1] = td[m];
+            spline1d.spline1dbuildhermite(tx, ty, td, m+2, s);
+            spline1d.spline1dlintransx(s, 2/(xb-xa), -((xa+xb)/(xb-xa)));
+            spline1d.spline1dlintransy(s, sb-sa, sa);
+            info = 1;
+            
+            //
+            // Fill report
+            //
+            rep.rmserror = 0;
+            rep.avgerror = 0;
+            rep.avgrelerror = 0;
+            rep.maxerror = 0;
+            relcnt = 0;
+            spline1d.spline1dconvcubic(bx, rightpart, m, 2, 0.0, 2, 0.0, x, n, ref fcolumn);
+            for(i=0; i<=n-1; i++)
+            {
+                v = (sb-sa)*fcolumn[i]+sa;
+                rep.rmserror = rep.rmserror+math.sqr(v-yoriginal[i]);
+                rep.avgerror = rep.avgerror+Math.Abs(v-yoriginal[i]);
+                if( (double)(yoriginal[i])!=(double)(0) )
+                {
+                    rep.avgrelerror = rep.avgrelerror+Math.Abs(v-yoriginal[i])/Math.Abs(yoriginal[i]);
+                    relcnt = relcnt+1;
+                }
+                rep.maxerror = Math.Max(rep.maxerror, Math.Abs(v-yoriginal[i]));
+            }
+            rep.rmserror = Math.Sqrt(rep.rmserror/n);
+            rep.avgerror = rep.avgerror/n;
+            if( (double)(relcnt)!=(double)(0) )
+            {
+                rep.avgrelerror = rep.avgrelerror/relcnt;
+            }
+        }
+
+
+        /*************************************************************************
+        Weighted fitting by cubic  spline,  with constraints on function values or
+        derivatives.
+
+        Equidistant grid with M-2 nodes on [min(x,xc),max(x,xc)] is  used to build
+        basis functions. Basis functions are cubic splines with continuous  second
+        derivatives  and  non-fixed first  derivatives  at  interval  ends.  Small
+        regularizing term is used  when  solving  constrained  tasks  (to  improve
+        stability).
+
+        Task is linear, so linear least squares solver is used. Complexity of this
+        computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+        SEE ALSO
+            Spline1DFitHermiteWC()  -   fitting by Hermite splines (more flexible,
+                                        less smooth)
+            Spline1DFitCubic()      -   "lightweight" fitting  by  cubic  splines,
+                                        without invididual weights and constraints
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            W   -   weights, array[0..N-1]
+                    Each summand in square  sum  of  approximation deviations from
+                    given  values  is  multiplied  by  the square of corresponding
+                    weight. Fill it by 1's if you don't  want  to  solve  weighted
+                    task.
+            N   -   number of points (optional):
+                    * N>0
+                    * if given, only first N elements of X/Y/W are processed
+                    * if not given, automatically determined from X/Y/W sizes
+            XC  -   points where spline values/derivatives are constrained,
+                    array[0..K-1].
+            YC  -   values of constraints, array[0..K-1]
+            DC  -   array[0..K-1], types of constraints:
+                    * DC[i]=0   means that S(XC[i])=YC[i]
+                    * DC[i]=1   means that S'(XC[i])=YC[i]
+                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+            K   -   number of constraints (optional):
+                    * 0<=K<M.
+                    * K=0 means no constraints (XC/YC/DC are not used)
+                    * if given, only first K elements of XC/YC/DC are used
+                    * if not given, automatically determined from XC/YC/DC
+            M   -   number of basis functions ( = number_of_nodes+2), M>=4.
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearWC() subroutine.
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+                                -3 means inconsistent constraints
+            S   -   spline interpolant.
+            Rep -   report, same format as in LSFitLinearWC() subroutine.
+                    Following fields are set:
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroitine doesn't calculate task's condition number for K<>0.
+
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+
+        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+        Setting constraints can lead  to undesired  results,  like ill-conditioned
+        behavior, or inconsistency being detected. From the other side,  it allows
+        us to improve quality of the fit. Here we summarize  our  experience  with
+        constrained regression splines:
+        * excessive constraints can be inconsistent. Splines are  piecewise  cubic
+          functions, and it is easy to create an example, where  large  number  of
+          constraints  concentrated  in  small  area will result in inconsistency.
+          Just because spline is not flexible enough to satisfy all of  them.  And
+          same constraints spread across the  [min(x),max(x)]  will  be  perfectly
+          consistent.
+        * the more evenly constraints are spread across [min(x),max(x)],  the more
+          chances that they will be consistent
+        * the  greater  is  M (given  fixed  constraints),  the  more chances that
+          constraints will be consistent
+        * in the general case, consistency of constraints IS NOT GUARANTEED.
+        * in the several special cases, however, we CAN guarantee consistency.
+        * one of this cases is constraints  on  the  function  values  AND/OR  its
+          derivatives at the interval boundaries.
+        * another  special  case  is ONE constraint on the function value (OR, but
+          not AND, derivative) anywhere in the interval
+
+        Our final recommendation is to use constraints  WHEN  AND  ONLY  WHEN  you
+        can't solve your task without them. Anything beyond  special  cases  given
+        above is not guaranteed and may result in inconsistency.
+
+
+          -- ALGLIB PROJECT --
+             Copyright 18.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dfitcubicwc(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            double[] xc,
+            double[] yc,
+            int[] dc,
+            int k,
+            int m,
+            ref int info,
+            spline1d.spline1dinterpolant s,
+            spline1dfitreport rep)
+        {
+            int i = 0;
+
+            info = 0;
+
+            ap.assert(n>=1, "Spline1DFitCubicWC: N<1!");
+            ap.assert(m>=4, "Spline1DFitCubicWC: M<4!");
+            ap.assert(k>=0, "Spline1DFitCubicWC: K<0!");
+            ap.assert(k<m, "Spline1DFitCubicWC: K>=M!");
+            ap.assert(ap.len(x)>=n, "Spline1DFitCubicWC: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DFitCubicWC: Length(Y)<N!");
+            ap.assert(ap.len(w)>=n, "Spline1DFitCubicWC: Length(W)<N!");
+            ap.assert(ap.len(xc)>=k, "Spline1DFitCubicWC: Length(XC)<K!");
+            ap.assert(ap.len(yc)>=k, "Spline1DFitCubicWC: Length(YC)<K!");
+            ap.assert(ap.len(dc)>=k, "Spline1DFitCubicWC: Length(DC)<K!");
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitCubicWC: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitCubicWC: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(w, n), "Spline1DFitCubicWC: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(xc, k), "Spline1DFitCubicWC: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(yc, k), "Spline1DFitCubicWC: Y contains infinite or NAN values!");
+            for(i=0; i<=k-1; i++)
+            {
+                ap.assert(dc[i]==0 | dc[i]==1, "Spline1DFitCubicWC: DC[i] is neither 0 or 1!");
+            }
+            spline1dfitinternal(0, x, y, w, n, xc, yc, dc, k, m, ref info, s, rep);
+        }
+
+
+        /*************************************************************************
+        Weighted  fitting  by Hermite spline,  with constraints on function values
+        or first derivatives.
+
+        Equidistant grid with M nodes on [min(x,xc),max(x,xc)] is  used  to  build
+        basis functions. Basis functions are Hermite splines.  Small  regularizing
+        term is used when solving constrained tasks (to improve stability).
+
+        Task is linear, so linear least squares solver is used. Complexity of this
+        computational scheme is O(N*M^2), mostly dominated by least squares solver
+
+        SEE ALSO
+            Spline1DFitCubicWC()    -   fitting by Cubic splines (less flexible,
+                                        more smooth)
+            Spline1DFitHermite()    -   "lightweight" Hermite fitting, without
+                                        invididual weights and constraints
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            W   -   weights, array[0..N-1]
+                    Each summand in square  sum  of  approximation deviations from
+                    given  values  is  multiplied  by  the square of corresponding
+                    weight. Fill it by 1's if you don't  want  to  solve  weighted
+                    task.
+            N   -   number of points (optional):
+                    * N>0
+                    * if given, only first N elements of X/Y/W are processed
+                    * if not given, automatically determined from X/Y/W sizes
+            XC  -   points where spline values/derivatives are constrained,
+                    array[0..K-1].
+            YC  -   values of constraints, array[0..K-1]
+            DC  -   array[0..K-1], types of constraints:
+                    * DC[i]=0   means that S(XC[i])=YC[i]
+                    * DC[i]=1   means that S'(XC[i])=YC[i]
+                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
+            K   -   number of constraints (optional):
+                    * 0<=K<M.
+                    * K=0 means no constraints (XC/YC/DC are not used)
+                    * if given, only first K elements of XC/YC/DC are used
+                    * if not given, automatically determined from XC/YC/DC
+            M   -   number of basis functions (= 2 * number of nodes),
+                    M>=4,
+                    M IS EVEN!
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearW() subroutine:
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+                                -3 means inconsistent constraints
+                                -2 means odd M was passed (which is not supported)
+                                -1 means another errors in parameters passed
+                                   (N<=0, for example)
+            S   -   spline interpolant.
+            Rep -   report, same format as in LSFitLinearW() subroutine.
+                    Following fields are set:
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroitine doesn't calculate task's condition number for K<>0.
+
+        IMPORTANT:
+            this subroitine supports only even M's
+
+
+        ORDER OF POINTS
+
+        Subroutine automatically sorts points, so caller may pass unsorted array.
+
+        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:
+
+        Setting constraints can lead  to undesired  results,  like ill-conditioned
+        behavior, or inconsistency being detected. From the other side,  it allows
+        us to improve quality of the fit. Here we summarize  our  experience  with
+        constrained regression splines:
+        * excessive constraints can be inconsistent. Splines are  piecewise  cubic
+          functions, and it is easy to create an example, where  large  number  of
+          constraints  concentrated  in  small  area will result in inconsistency.
+          Just because spline is not flexible enough to satisfy all of  them.  And
+          same constraints spread across the  [min(x),max(x)]  will  be  perfectly
+          consistent.
+        * the more evenly constraints are spread across [min(x),max(x)],  the more
+          chances that they will be consistent
+        * the  greater  is  M (given  fixed  constraints),  the  more chances that
+          constraints will be consistent
+        * in the general case, consistency of constraints is NOT GUARANTEED.
+        * in the several special cases, however, we can guarantee consistency.
+        * one of this cases is  M>=4  and   constraints  on   the  function  value
+          (AND/OR its derivative) at the interval boundaries.
+        * another special case is M>=4  and  ONE  constraint on the function value
+          (OR, BUT NOT AND, derivative) anywhere in [min(x),max(x)]
+
+        Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
+        can't solve your task without them. Anything beyond  special  cases  given
+        above is not guaranteed and may result in inconsistency.
+
+          -- ALGLIB PROJECT --
+             Copyright 18.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dfithermitewc(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            double[] xc,
+            double[] yc,
+            int[] dc,
+            int k,
+            int m,
+            ref int info,
+            spline1d.spline1dinterpolant s,
+            spline1dfitreport rep)
+        {
+            int i = 0;
+
+            info = 0;
+
+            ap.assert(n>=1, "Spline1DFitHermiteWC: N<1!");
+            ap.assert(m>=4, "Spline1DFitHermiteWC: M<4!");
+            ap.assert(m%2==0, "Spline1DFitHermiteWC: M is odd!");
+            ap.assert(k>=0, "Spline1DFitHermiteWC: K<0!");
+            ap.assert(k<m, "Spline1DFitHermiteWC: K>=M!");
+            ap.assert(ap.len(x)>=n, "Spline1DFitHermiteWC: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DFitHermiteWC: Length(Y)<N!");
+            ap.assert(ap.len(w)>=n, "Spline1DFitHermiteWC: Length(W)<N!");
+            ap.assert(ap.len(xc)>=k, "Spline1DFitHermiteWC: Length(XC)<K!");
+            ap.assert(ap.len(yc)>=k, "Spline1DFitHermiteWC: Length(YC)<K!");
+            ap.assert(ap.len(dc)>=k, "Spline1DFitHermiteWC: Length(DC)<K!");
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitHermiteWC: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitHermiteWC: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(w, n), "Spline1DFitHermiteWC: Y contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(xc, k), "Spline1DFitHermiteWC: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(yc, k), "Spline1DFitHermiteWC: Y contains infinite or NAN values!");
+            for(i=0; i<=k-1; i++)
+            {
+                ap.assert(dc[i]==0 | dc[i]==1, "Spline1DFitHermiteWC: DC[i] is neither 0 or 1!");
+            }
+            spline1dfitinternal(1, x, y, w, n, xc, yc, dc, k, m, ref info, s, rep);
+        }
+
+
+        /*************************************************************************
+        Least squares fitting by cubic spline.
+
+        This subroutine is "lightweight" alternative for more complex and feature-
+        rich Spline1DFitCubicWC().  See  Spline1DFitCubicWC() for more information
+        about subroutine parameters (we don't duplicate it here because of length)
+
+          -- ALGLIB PROJECT --
+             Copyright 18.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dfitcubic(double[] x,
+            double[] y,
+            int n,
+            int m,
+            ref int info,
+            spline1d.spline1dinterpolant s,
+            spline1dfitreport rep)
+        {
+            int i = 0;
+            double[] w = new double[0];
+            double[] xc = new double[0];
+            double[] yc = new double[0];
+            int[] dc = new int[0];
+
+            info = 0;
+
+            ap.assert(n>=1, "Spline1DFitCubic: N<1!");
+            ap.assert(m>=4, "Spline1DFitCubic: M<4!");
+            ap.assert(ap.len(x)>=n, "Spline1DFitCubic: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DFitCubic: Length(Y)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitCubic: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitCubic: Y contains infinite or NAN values!");
+            w = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = 1;
+            }
+            spline1dfitcubicwc(x, y, w, n, xc, yc, dc, 0, m, ref info, s, rep);
+        }
+
+
+        /*************************************************************************
+        Least squares fitting by Hermite spline.
+
+        This subroutine is "lightweight" alternative for more complex and feature-
+        rich Spline1DFitHermiteWC().  See Spline1DFitHermiteWC()  description  for
+        more information about subroutine parameters (we don't duplicate  it  here
+        because of length).
+
+          -- ALGLIB PROJECT --
+             Copyright 18.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline1dfithermite(double[] x,
+            double[] y,
+            int n,
+            int m,
+            ref int info,
+            spline1d.spline1dinterpolant s,
+            spline1dfitreport rep)
+        {
+            int i = 0;
+            double[] w = new double[0];
+            double[] xc = new double[0];
+            double[] yc = new double[0];
+            int[] dc = new int[0];
+
+            info = 0;
+
+            ap.assert(n>=1, "Spline1DFitHermite: N<1!");
+            ap.assert(m>=4, "Spline1DFitHermite: M<4!");
+            ap.assert(m%2==0, "Spline1DFitHermite: M is odd!");
+            ap.assert(ap.len(x)>=n, "Spline1DFitHermite: Length(X)<N!");
+            ap.assert(ap.len(y)>=n, "Spline1DFitHermite: Length(Y)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "Spline1DFitHermite: X contains infinite or NAN values!");
+            ap.assert(apserv.isfinitevector(y, n), "Spline1DFitHermite: Y contains infinite or NAN values!");
+            w = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = 1;
+            }
+            spline1dfithermitewc(x, y, w, n, xc, yc, dc, 0, m, ref info, s, rep);
+        }
+
+
+        /*************************************************************************
+        Weighted linear least squares fitting.
+
+        QR decomposition is used to reduce task to MxM, then triangular solver  or
+        SVD-based solver is used depending on condition number of the  system.  It
+        allows to maximize speed and retain decent accuracy.
+
+        INPUT PARAMETERS:
+            Y       -   array[0..N-1] Function values in  N  points.
+            W       -   array[0..N-1]  Weights  corresponding to function  values.
+                        Each summand in square  sum  of  approximation  deviations
+                        from  given  values  is  multiplied  by  the   square   of
+                        corresponding weight.
+            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                        FMatrix[I, J] - value of J-th basis function in I-th point.
+            N       -   number of points used. N>=1.
+            M       -   number of basis functions, M>=1.
+
+        OUTPUT PARAMETERS:
+            Info    -   error code:
+                        * -4    internal SVD decomposition subroutine failed (very
+                                rare and for degenerate systems only)
+                        * -1    incorrect N/M were specified
+                        *  1    task is solved
+            C       -   decomposition coefficients, array[0..M-1]
+            Rep     -   fitting report. Following fields are set:
+                        * Rep.TaskRCond     reciprocal of condition number
+                        * RMSError          rms error on the (X,Y).
+                        * AvgError          average error on the (X,Y).
+                        * AvgRelError       average relative error on the non-zero Y
+                        * MaxError          maximum error
+                                            NON-WEIGHTED ERRORS ARE CALCULATED
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitlinearw(double[] y,
+            double[] w,
+            double[,] fmatrix,
+            int n,
+            int m,
+            ref int info,
+            ref double[] c,
+            lsfitreport rep)
+        {
+            info = 0;
+            c = new double[0];
+
+            ap.assert(n>=1, "LSFitLinearW: N<1!");
+            ap.assert(m>=1, "LSFitLinearW: M<1!");
+            ap.assert(ap.len(y)>=n, "LSFitLinearW: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitLinearW: Y contains infinite or NaN values!");
+            ap.assert(ap.len(w)>=n, "LSFitLinearW: length(W)<N!");
+            ap.assert(apserv.isfinitevector(w, n), "LSFitLinearW: W contains infinite or NaN values!");
+            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinearW: rows(FMatrix)<N!");
+            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinearW: cols(FMatrix)<M!");
+            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinearW: FMatrix contains infinite or NaN values!");
+            lsfitlinearinternal(y, w, fmatrix, n, m, ref info, ref c, rep);
+        }
+
+
+        /*************************************************************************
+        Weighted constained linear least squares fitting.
+
+        This  is  variation  of LSFitLinearW(), which searchs for min|A*x=b| given
+        that  K  additional  constaints  C*x=bc are satisfied. It reduces original
+        task to modified one: min|B*y-d| WITHOUT constraints,  then LSFitLinearW()
+        is called.
+
+        INPUT PARAMETERS:
+            Y       -   array[0..N-1] Function values in  N  points.
+            W       -   array[0..N-1]  Weights  corresponding to function  values.
+                        Each summand in square  sum  of  approximation  deviations
+                        from  given  values  is  multiplied  by  the   square   of
+                        corresponding weight.
+            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                        FMatrix[I,J] - value of J-th basis function in I-th point.
+            CMatrix -   a table of constaints, array[0..K-1,0..M].
+                        I-th row of CMatrix corresponds to I-th linear constraint:
+                        CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
+            N       -   number of points used. N>=1.
+            M       -   number of basis functions, M>=1.
+            K       -   number of constraints, 0 <= K < M
+                        K=0 corresponds to absence of constraints.
+
+        OUTPUT PARAMETERS:
+            Info    -   error code:
+                        * -4    internal SVD decomposition subroutine failed (very
+                                rare and for degenerate systems only)
+                        * -3    either   too   many  constraints  (M   or   more),
+                                degenerate  constraints   (some   constraints  are
+                                repetead twice) or inconsistent  constraints  were
+                                specified.
+                        *  1    task is solved
+            C       -   decomposition coefficients, array[0..M-1]
+            Rep     -   fitting report. Following fields are set:
+                        * RMSError          rms error on the (X,Y).
+                        * AvgError          average error on the (X,Y).
+                        * AvgRelError       average relative error on the non-zero Y
+                        * MaxError          maximum error
+                                            NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroitine doesn't calculate task's condition number for K<>0.
+
+          -- ALGLIB --
+             Copyright 07.09.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitlinearwc(double[] y,
+            double[] w,
+            double[,] fmatrix,
+            double[,] cmatrix,
+            int n,
+            int m,
+            int k,
+            ref int info,
+            ref double[] c,
+            lsfitreport rep)
+        {
+            int i = 0;
+            int j = 0;
+            double[] tau = new double[0];
+            double[,] q = new double[0,0];
+            double[,] f2 = new double[0,0];
+            double[] tmp = new double[0];
+            double[] c0 = new double[0];
+            double v = 0;
+            int i_ = 0;
+
+            y = (double[])y.Clone();
+            cmatrix = (double[,])cmatrix.Clone();
+            info = 0;
+            c = new double[0];
+
+            ap.assert(n>=1, "LSFitLinearWC: N<1!");
+            ap.assert(m>=1, "LSFitLinearWC: M<1!");
+            ap.assert(k>=0, "LSFitLinearWC: K<0!");
+            ap.assert(ap.len(y)>=n, "LSFitLinearWC: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitLinearWC: Y contains infinite or NaN values!");
+            ap.assert(ap.len(w)>=n, "LSFitLinearWC: length(W)<N!");
+            ap.assert(apserv.isfinitevector(w, n), "LSFitLinearWC: W contains infinite or NaN values!");
+            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinearWC: rows(FMatrix)<N!");
+            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinearWC: cols(FMatrix)<M!");
+            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinearWC: FMatrix contains infinite or NaN values!");
+            ap.assert(ap.rows(cmatrix)>=k, "LSFitLinearWC: rows(CMatrix)<K!");
+            ap.assert(ap.cols(cmatrix)>=m+1 | k==0, "LSFitLinearWC: cols(CMatrix)<M+1!");
+            ap.assert(apserv.apservisfinitematrix(cmatrix, k, m+1), "LSFitLinearWC: CMatrix contains infinite or NaN values!");
+            if( k>=m )
+            {
+                info = -3;
+                return;
+            }
+            
+            //
+            // Solve
+            //
+            if( k==0 )
+            {
+                
+                //
+                // no constraints
+                //
+                lsfitlinearinternal(y, w, fmatrix, n, m, ref info, ref c, rep);
+            }
+            else
+            {
+                
+                //
+                // First, find general form solution of constraints system:
+                // * factorize C = L*Q
+                // * unpack Q
+                // * fill upper part of C with zeros (for RCond)
+                //
+                // We got C=C0+Q2'*y where Q2 is lower M-K rows of Q.
+                //
+                ortfac.rmatrixlq(ref cmatrix, k, m, ref tau);
+                ortfac.rmatrixlqunpackq(cmatrix, k, m, tau, m, ref q);
+                for(i=0; i<=k-1; i++)
+                {
+                    for(j=i+1; j<=m-1; j++)
+                    {
+                        cmatrix[i,j] = 0.0;
+                    }
+                }
+                if( (double)(rcond.rmatrixlurcondinf(cmatrix, k))<(double)(1000*math.machineepsilon) )
+                {
+                    info = -3;
+                    return;
+                }
+                tmp = new double[k];
+                for(i=0; i<=k-1; i++)
+                {
+                    if( i>0 )
+                    {
+                        v = 0.0;
+                        for(i_=0; i_<=i-1;i_++)
+                        {
+                            v += cmatrix[i,i_]*tmp[i_];
+                        }
+                    }
+                    else
+                    {
+                        v = 0;
+                    }
+                    tmp[i] = (cmatrix[i,m]-v)/cmatrix[i,i];
+                }
+                c0 = new double[m];
+                for(i=0; i<=m-1; i++)
+                {
+                    c0[i] = 0;
+                }
+                for(i=0; i<=k-1; i++)
+                {
+                    v = tmp[i];
+                    for(i_=0; i_<=m-1;i_++)
+                    {
+                        c0[i_] = c0[i_] + v*q[i,i_];
+                    }
+                }
+                
+                //
+                // Second, prepare modified matrix F2 = F*Q2' and solve modified task
+                //
+                tmp = new double[Math.Max(n, m)+1];
+                f2 = new double[n, m-k];
+                blas.matrixvectormultiply(fmatrix, 0, n-1, 0, m-1, false, c0, 0, m-1, -1.0, ref y, 0, n-1, 1.0);
+                blas.matrixmatrixmultiply(fmatrix, 0, n-1, 0, m-1, false, q, k, m-1, 0, m-1, true, 1.0, ref f2, 0, n-1, 0, m-k-1, 0.0, ref tmp);
+                lsfitlinearinternal(y, w, f2, n, m-k, ref info, ref tmp, rep);
+                rep.taskrcond = -1;
+                if( info<=0 )
+                {
+                    return;
+                }
+                
+                //
+                // then, convert back to original answer: C = C0 + Q2'*Y0
+                //
+                c = new double[m];
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    c[i_] = c0[i_];
+                }
+                blas.matrixvectormultiply(q, k, m-1, 0, m-1, true, tmp, 0, m-k-1, 1.0, ref c, 0, m-1, 1.0);
+            }
+        }
+
+
+        /*************************************************************************
+        Linear least squares fitting.
+
+        QR decomposition is used to reduce task to MxM, then triangular solver  or
+        SVD-based solver is used depending on condition number of the  system.  It
+        allows to maximize speed and retain decent accuracy.
+
+        INPUT PARAMETERS:
+            Y       -   array[0..N-1] Function values in  N  points.
+            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                        FMatrix[I, J] - value of J-th basis function in I-th point.
+            N       -   number of points used. N>=1.
+            M       -   number of basis functions, M>=1.
+
+        OUTPUT PARAMETERS:
+            Info    -   error code:
+                        * -4    internal SVD decomposition subroutine failed (very
+                                rare and for degenerate systems only)
+                        *  1    task is solved
+            C       -   decomposition coefficients, array[0..M-1]
+            Rep     -   fitting report. Following fields are set:
+                        * Rep.TaskRCond     reciprocal of condition number
+                        * RMSError          rms error on the (X,Y).
+                        * AvgError          average error on the (X,Y).
+                        * AvgRelError       average relative error on the non-zero Y
+                        * MaxError          maximum error
+                                            NON-WEIGHTED ERRORS ARE CALCULATED
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitlinear(double[] y,
+            double[,] fmatrix,
+            int n,
+            int m,
+            ref int info,
+            ref double[] c,
+            lsfitreport rep)
+        {
+            double[] w = new double[0];
+            int i = 0;
+
+            info = 0;
+            c = new double[0];
+
+            ap.assert(n>=1, "LSFitLinear: N<1!");
+            ap.assert(m>=1, "LSFitLinear: M<1!");
+            ap.assert(ap.len(y)>=n, "LSFitLinear: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitLinear: Y contains infinite or NaN values!");
+            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinear: rows(FMatrix)<N!");
+            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinear: cols(FMatrix)<M!");
+            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinear: FMatrix contains infinite or NaN values!");
+            w = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = 1;
+            }
+            lsfitlinearinternal(y, w, fmatrix, n, m, ref info, ref c, rep);
+        }
+
+
+        /*************************************************************************
+        Constained linear least squares fitting.
+
+        This  is  variation  of LSFitLinear(),  which searchs for min|A*x=b| given
+        that  K  additional  constaints  C*x=bc are satisfied. It reduces original
+        task to modified one: min|B*y-d| WITHOUT constraints,  then  LSFitLinear()
+        is called.
+
+        INPUT PARAMETERS:
+            Y       -   array[0..N-1] Function values in  N  points.
+            FMatrix -   a table of basis functions values, array[0..N-1, 0..M-1].
+                        FMatrix[I,J] - value of J-th basis function in I-th point.
+            CMatrix -   a table of constaints, array[0..K-1,0..M].
+                        I-th row of CMatrix corresponds to I-th linear constraint:
+                        CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M]
+            N       -   number of points used. N>=1.
+            M       -   number of basis functions, M>=1.
+            K       -   number of constraints, 0 <= K < M
+                        K=0 corresponds to absence of constraints.
+
+        OUTPUT PARAMETERS:
+            Info    -   error code:
+                        * -4    internal SVD decomposition subroutine failed (very
+                                rare and for degenerate systems only)
+                        * -3    either   too   many  constraints  (M   or   more),
+                                degenerate  constraints   (some   constraints  are
+                                repetead twice) or inconsistent  constraints  were
+                                specified.
+                        *  1    task is solved
+            C       -   decomposition coefficients, array[0..M-1]
+            Rep     -   fitting report. Following fields are set:
+                        * RMSError          rms error on the (X,Y).
+                        * AvgError          average error on the (X,Y).
+                        * AvgRelError       average relative error on the non-zero Y
+                        * MaxError          maximum error
+                                            NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroitine doesn't calculate task's condition number for K<>0.
+
+          -- ALGLIB --
+             Copyright 07.09.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitlinearc(double[] y,
+            double[,] fmatrix,
+            double[,] cmatrix,
+            int n,
+            int m,
+            int k,
+            ref int info,
+            ref double[] c,
+            lsfitreport rep)
+        {
+            double[] w = new double[0];
+            int i = 0;
+
+            y = (double[])y.Clone();
+            info = 0;
+            c = new double[0];
+
+            ap.assert(n>=1, "LSFitLinearC: N<1!");
+            ap.assert(m>=1, "LSFitLinearC: M<1!");
+            ap.assert(k>=0, "LSFitLinearC: K<0!");
+            ap.assert(ap.len(y)>=n, "LSFitLinearC: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitLinearC: Y contains infinite or NaN values!");
+            ap.assert(ap.rows(fmatrix)>=n, "LSFitLinearC: rows(FMatrix)<N!");
+            ap.assert(ap.cols(fmatrix)>=m, "LSFitLinearC: cols(FMatrix)<M!");
+            ap.assert(apserv.apservisfinitematrix(fmatrix, n, m), "LSFitLinearC: FMatrix contains infinite or NaN values!");
+            ap.assert(ap.rows(cmatrix)>=k, "LSFitLinearC: rows(CMatrix)<K!");
+            ap.assert(ap.cols(cmatrix)>=m+1 | k==0, "LSFitLinearC: cols(CMatrix)<M+1!");
+            ap.assert(apserv.apservisfinitematrix(cmatrix, k, m+1), "LSFitLinearC: CMatrix contains infinite or NaN values!");
+            w = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                w[i] = 1;
+            }
+            lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref c, rep);
+        }
+
+
+        /*************************************************************************
+        Weighted nonlinear least squares fitting using function values only.
+
+        Combination of numerical differentiation and secant updates is used to
+        obtain function Jacobian.
+
+        Nonlinear task min(F(c)) is solved, where
+
+            F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
+
+            * N is a number of points,
+            * M is a dimension of a space points belong to,
+            * K is a dimension of a space of parameters being fitted,
+            * w is an N-dimensional vector of weight coefficients,
+            * x is a set of N points, each of them is an M-dimensional vector,
+            * c is a K-dimensional vector of parameters being fitted
+
+        This subroutine uses only f(c,x[i]).
+
+        INPUT PARAMETERS:
+            X       -   array[0..N-1,0..M-1], points (one row = one point)
+            Y       -   array[0..N-1], function values.
+            W       -   weights, array[0..N-1]
+            C       -   array[0..K-1], initial approximation to the solution,
+            N       -   number of points, N>1
+            M       -   dimension of space
+            K       -   number of parameters being fitted
+            DiffStep-   numerical differentiation step;
+                        should not be very small or large;
+                        large = loss of accuracy
+                        small = growth of round-off errors
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 18.10.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitcreatewf(double[,] x,
+            double[] y,
+            double[] w,
+            double[] c,
+            int n,
+            int m,
+            int k,
+            double diffstep,
+            lsfitstate state)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            ap.assert(n>=1, "LSFitCreateWF: N<1!");
+            ap.assert(m>=1, "LSFitCreateWF: M<1!");
+            ap.assert(k>=1, "LSFitCreateWF: K<1!");
+            ap.assert(ap.len(c)>=k, "LSFitCreateWF: length(C)<K!");
+            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateWF: C contains infinite or NaN values!");
+            ap.assert(ap.len(y)>=n, "LSFitCreateWF: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateWF: Y contains infinite or NaN values!");
+            ap.assert(ap.len(w)>=n, "LSFitCreateWF: length(W)<N!");
+            ap.assert(apserv.isfinitevector(w, n), "LSFitCreateWF: W contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateWF: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateWF: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateWF: X contains infinite or NaN values!");
+            ap.assert(math.isfinite(diffstep), "LSFitCreateWF: DiffStep is not finite!");
+            ap.assert((double)(diffstep)>(double)(0), "LSFitCreateWF: DiffStep<=0!");
+            state.npoints = n;
+            state.nweights = n;
+            state.wkind = 1;
+            state.m = m;
+            state.k = k;
+            lsfitsetcond(state, 0.0, 0.0, 0);
+            lsfitsetstpmax(state, 0.0);
+            lsfitsetxrep(state, false);
+            state.taskx = new double[n, m];
+            state.tasky = new double[n];
+            state.w = new double[n];
+            state.c = new double[k];
+            state.x = new double[m];
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = c[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.w[i_] = w[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.taskx[i,i_] = x[i,i_];
+                }
+                state.tasky[i] = y[i];
+            }
+            state.s = new double[k];
+            state.bndl = new double[k];
+            state.bndu = new double[k];
+            for(i=0; i<=k-1; i++)
+            {
+                state.s[i] = 1.0;
+                state.bndl[i] = Double.NegativeInfinity;
+                state.bndu[i] = Double.PositiveInfinity;
+            }
+            state.optalgo = 0;
+            state.prevnpt = -1;
+            state.prevalgo = -1;
+            minlm.minlmcreatev(k, n, state.c, diffstep, state.optstate);
+            lsfitclearrequestfields(state);
+            state.rstate.ia = new int[4+1];
+            state.rstate.ra = new double[2+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Nonlinear least squares fitting using function values only.
+
+        Combination of numerical differentiation and secant updates is used to
+        obtain function Jacobian.
+
+        Nonlinear task min(F(c)) is solved, where
+
+            F(c) = (f(c,x[0])-y[0])^2 + ... + (f(c,x[n-1])-y[n-1])^2,
+
+            * N is a number of points,
+            * M is a dimension of a space points belong to,
+            * K is a dimension of a space of parameters being fitted,
+            * w is an N-dimensional vector of weight coefficients,
+            * x is a set of N points, each of them is an M-dimensional vector,
+            * c is a K-dimensional vector of parameters being fitted
+
+        This subroutine uses only f(c,x[i]).
+
+        INPUT PARAMETERS:
+            X       -   array[0..N-1,0..M-1], points (one row = one point)
+            Y       -   array[0..N-1], function values.
+            C       -   array[0..K-1], initial approximation to the solution,
+            N       -   number of points, N>1
+            M       -   dimension of space
+            K       -   number of parameters being fitted
+            DiffStep-   numerical differentiation step;
+                        should not be very small or large;
+                        large = loss of accuracy
+                        small = growth of round-off errors
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 18.10.2008 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitcreatef(double[,] x,
+            double[] y,
+            double[] c,
+            int n,
+            int m,
+            int k,
+            double diffstep,
+            lsfitstate state)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            ap.assert(n>=1, "LSFitCreateF: N<1!");
+            ap.assert(m>=1, "LSFitCreateF: M<1!");
+            ap.assert(k>=1, "LSFitCreateF: K<1!");
+            ap.assert(ap.len(c)>=k, "LSFitCreateF: length(C)<K!");
+            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateF: C contains infinite or NaN values!");
+            ap.assert(ap.len(y)>=n, "LSFitCreateF: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateF: Y contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateF: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateF: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateF: X contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateF: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateF: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateF: X contains infinite or NaN values!");
+            ap.assert(math.isfinite(diffstep), "LSFitCreateF: DiffStep is not finite!");
+            ap.assert((double)(diffstep)>(double)(0), "LSFitCreateF: DiffStep<=0!");
+            state.npoints = n;
+            state.wkind = 0;
+            state.m = m;
+            state.k = k;
+            lsfitsetcond(state, 0.0, 0.0, 0);
+            lsfitsetstpmax(state, 0.0);
+            lsfitsetxrep(state, false);
+            state.taskx = new double[n, m];
+            state.tasky = new double[n];
+            state.c = new double[k];
+            state.x = new double[m];
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = c[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.taskx[i,i_] = x[i,i_];
+                }
+                state.tasky[i] = y[i];
+            }
+            state.s = new double[k];
+            state.bndl = new double[k];
+            state.bndu = new double[k];
+            for(i=0; i<=k-1; i++)
+            {
+                state.s[i] = 1.0;
+                state.bndl[i] = Double.NegativeInfinity;
+                state.bndu[i] = Double.PositiveInfinity;
+            }
+            state.optalgo = 0;
+            state.prevnpt = -1;
+            state.prevalgo = -1;
+            minlm.minlmcreatev(k, n, state.c, diffstep, state.optstate);
+            lsfitclearrequestfields(state);
+            state.rstate.ia = new int[4+1];
+            state.rstate.ra = new double[2+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Weighted nonlinear least squares fitting using gradient only.
+
+        Nonlinear task min(F(c)) is solved, where
+
+            F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
+            
+            * N is a number of points,
+            * M is a dimension of a space points belong to,
+            * K is a dimension of a space of parameters being fitted,
+            * w is an N-dimensional vector of weight coefficients,
+            * x is a set of N points, each of them is an M-dimensional vector,
+            * c is a K-dimensional vector of parameters being fitted
+            
+        This subroutine uses only f(c,x[i]) and its gradient.
+            
+        INPUT PARAMETERS:
+            X       -   array[0..N-1,0..M-1], points (one row = one point)
+            Y       -   array[0..N-1], function values.
+            W       -   weights, array[0..N-1]
+            C       -   array[0..K-1], initial approximation to the solution,
+            N       -   number of points, N>1
+            M       -   dimension of space
+            K       -   number of parameters being fitted
+            CheapFG -   boolean flag, which is:
+                        * True  if both function and gradient calculation complexity
+                                are less than O(M^2).  An improved  algorithm  can
+                                be  used  which corresponds  to  FGJ  scheme  from
+                                MINLM unit.
+                        * False otherwise.
+                                Standard Jacibian-bases  Levenberg-Marquardt  algo
+                                will be used (FJ scheme).
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+        See also:
+            LSFitResults
+            LSFitCreateFG (fitting without weights)
+            LSFitCreateWFGH (fitting using Hessian)
+            LSFitCreateFGH (fitting using Hessian, without weights)
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitcreatewfg(double[,] x,
+            double[] y,
+            double[] w,
+            double[] c,
+            int n,
+            int m,
+            int k,
+            bool cheapfg,
+            lsfitstate state)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            ap.assert(n>=1, "LSFitCreateWFG: N<1!");
+            ap.assert(m>=1, "LSFitCreateWFG: M<1!");
+            ap.assert(k>=1, "LSFitCreateWFG: K<1!");
+            ap.assert(ap.len(c)>=k, "LSFitCreateWFG: length(C)<K!");
+            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateWFG: C contains infinite or NaN values!");
+            ap.assert(ap.len(y)>=n, "LSFitCreateWFG: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateWFG: Y contains infinite or NaN values!");
+            ap.assert(ap.len(w)>=n, "LSFitCreateWFG: length(W)<N!");
+            ap.assert(apserv.isfinitevector(w, n), "LSFitCreateWFG: W contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateWFG: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateWFG: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateWFG: X contains infinite or NaN values!");
+            state.npoints = n;
+            state.nweights = n;
+            state.wkind = 1;
+            state.m = m;
+            state.k = k;
+            lsfitsetcond(state, 0.0, 0.0, 0);
+            lsfitsetstpmax(state, 0.0);
+            lsfitsetxrep(state, false);
+            state.taskx = new double[n, m];
+            state.tasky = new double[n];
+            state.w = new double[n];
+            state.c = new double[k];
+            state.x = new double[m];
+            state.g = new double[k];
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = c[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.w[i_] = w[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.taskx[i,i_] = x[i,i_];
+                }
+                state.tasky[i] = y[i];
+            }
+            state.s = new double[k];
+            state.bndl = new double[k];
+            state.bndu = new double[k];
+            for(i=0; i<=k-1; i++)
+            {
+                state.s[i] = 1.0;
+                state.bndl[i] = Double.NegativeInfinity;
+                state.bndu[i] = Double.PositiveInfinity;
+            }
+            state.optalgo = 1;
+            state.prevnpt = -1;
+            state.prevalgo = -1;
+            if( cheapfg )
+            {
+                minlm.minlmcreatevgj(k, n, state.c, state.optstate);
+            }
+            else
+            {
+                minlm.minlmcreatevj(k, n, state.c, state.optstate);
+            }
+            lsfitclearrequestfields(state);
+            state.rstate.ia = new int[4+1];
+            state.rstate.ra = new double[2+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Nonlinear least squares fitting using gradient only, without individual
+        weights.
+
+        Nonlinear task min(F(c)) is solved, where
+
+            F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
+
+            * N is a number of points,
+            * M is a dimension of a space points belong to,
+            * K is a dimension of a space of parameters being fitted,
+            * x is a set of N points, each of them is an M-dimensional vector,
+            * c is a K-dimensional vector of parameters being fitted
+
+        This subroutine uses only f(c,x[i]) and its gradient.
+
+        INPUT PARAMETERS:
+            X       -   array[0..N-1,0..M-1], points (one row = one point)
+            Y       -   array[0..N-1], function values.
+            C       -   array[0..K-1], initial approximation to the solution,
+            N       -   number of points, N>1
+            M       -   dimension of space
+            K       -   number of parameters being fitted
+            CheapFG -   boolean flag, which is:
+                        * True  if both function and gradient calculation complexity
+                                are less than O(M^2).  An improved  algorithm  can
+                                be  used  which corresponds  to  FGJ  scheme  from
+                                MINLM unit.
+                        * False otherwise.
+                                Standard Jacibian-bases  Levenberg-Marquardt  algo
+                                will be used (FJ scheme).
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitcreatefg(double[,] x,
+            double[] y,
+            double[] c,
+            int n,
+            int m,
+            int k,
+            bool cheapfg,
+            lsfitstate state)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            ap.assert(n>=1, "LSFitCreateFG: N<1!");
+            ap.assert(m>=1, "LSFitCreateFG: M<1!");
+            ap.assert(k>=1, "LSFitCreateFG: K<1!");
+            ap.assert(ap.len(c)>=k, "LSFitCreateFG: length(C)<K!");
+            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateFG: C contains infinite or NaN values!");
+            ap.assert(ap.len(y)>=n, "LSFitCreateFG: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateFG: Y contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateFG: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateFG: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFG: X contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateFG: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateFG: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFG: X contains infinite or NaN values!");
+            state.npoints = n;
+            state.wkind = 0;
+            state.m = m;
+            state.k = k;
+            lsfitsetcond(state, 0.0, 0.0, 0);
+            lsfitsetstpmax(state, 0.0);
+            lsfitsetxrep(state, false);
+            state.taskx = new double[n, m];
+            state.tasky = new double[n];
+            state.c = new double[k];
+            state.x = new double[m];
+            state.g = new double[k];
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = c[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.taskx[i,i_] = x[i,i_];
+                }
+                state.tasky[i] = y[i];
+            }
+            state.s = new double[k];
+            state.bndl = new double[k];
+            state.bndu = new double[k];
+            for(i=0; i<=k-1; i++)
+            {
+                state.s[i] = 1.0;
+                state.bndl[i] = Double.NegativeInfinity;
+                state.bndu[i] = Double.PositiveInfinity;
+            }
+            state.optalgo = 1;
+            state.prevnpt = -1;
+            state.prevalgo = -1;
+            if( cheapfg )
+            {
+                minlm.minlmcreatevgj(k, n, state.c, state.optstate);
+            }
+            else
+            {
+                minlm.minlmcreatevj(k, n, state.c, state.optstate);
+            }
+            lsfitclearrequestfields(state);
+            state.rstate.ia = new int[4+1];
+            state.rstate.ra = new double[2+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Weighted nonlinear least squares fitting using gradient/Hessian.
+
+        Nonlinear task min(F(c)) is solved, where
+
+            F(c) = (w[0]*(f(c,x[0])-y[0]))^2 + ... + (w[n-1]*(f(c,x[n-1])-y[n-1]))^2,
+
+            * N is a number of points,
+            * M is a dimension of a space points belong to,
+            * K is a dimension of a space of parameters being fitted,
+            * w is an N-dimensional vector of weight coefficients,
+            * x is a set of N points, each of them is an M-dimensional vector,
+            * c is a K-dimensional vector of parameters being fitted
+
+        This subroutine uses f(c,x[i]), its gradient and its Hessian.
+
+        INPUT PARAMETERS:
+            X       -   array[0..N-1,0..M-1], points (one row = one point)
+            Y       -   array[0..N-1], function values.
+            W       -   weights, array[0..N-1]
+            C       -   array[0..K-1], initial approximation to the solution,
+            N       -   number of points, N>1
+            M       -   dimension of space
+            K       -   number of parameters being fitted
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitcreatewfgh(double[,] x,
+            double[] y,
+            double[] w,
+            double[] c,
+            int n,
+            int m,
+            int k,
+            lsfitstate state)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            ap.assert(n>=1, "LSFitCreateWFGH: N<1!");
+            ap.assert(m>=1, "LSFitCreateWFGH: M<1!");
+            ap.assert(k>=1, "LSFitCreateWFGH: K<1!");
+            ap.assert(ap.len(c)>=k, "LSFitCreateWFGH: length(C)<K!");
+            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateWFGH: C contains infinite or NaN values!");
+            ap.assert(ap.len(y)>=n, "LSFitCreateWFGH: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateWFGH: Y contains infinite or NaN values!");
+            ap.assert(ap.len(w)>=n, "LSFitCreateWFGH: length(W)<N!");
+            ap.assert(apserv.isfinitevector(w, n), "LSFitCreateWFGH: W contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateWFGH: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateWFGH: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateWFGH: X contains infinite or NaN values!");
+            state.npoints = n;
+            state.nweights = n;
+            state.wkind = 1;
+            state.m = m;
+            state.k = k;
+            lsfitsetcond(state, 0.0, 0.0, 0);
+            lsfitsetstpmax(state, 0.0);
+            lsfitsetxrep(state, false);
+            state.taskx = new double[n, m];
+            state.tasky = new double[n];
+            state.w = new double[n];
+            state.c = new double[k];
+            state.h = new double[k, k];
+            state.x = new double[m];
+            state.g = new double[k];
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = c[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.w[i_] = w[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.taskx[i,i_] = x[i,i_];
+                }
+                state.tasky[i] = y[i];
+            }
+            state.s = new double[k];
+            state.bndl = new double[k];
+            state.bndu = new double[k];
+            for(i=0; i<=k-1; i++)
+            {
+                state.s[i] = 1.0;
+                state.bndl[i] = Double.NegativeInfinity;
+                state.bndu[i] = Double.PositiveInfinity;
+            }
+            state.optalgo = 2;
+            state.prevnpt = -1;
+            state.prevalgo = -1;
+            minlm.minlmcreatefgh(k, state.c, state.optstate);
+            lsfitclearrequestfields(state);
+            state.rstate.ia = new int[4+1];
+            state.rstate.ra = new double[2+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Nonlinear least squares fitting using gradient/Hessian, without individial
+        weights.
+
+        Nonlinear task min(F(c)) is solved, where
+
+            F(c) = ((f(c,x[0])-y[0]))^2 + ... + ((f(c,x[n-1])-y[n-1]))^2,
+
+            * N is a number of points,
+            * M is a dimension of a space points belong to,
+            * K is a dimension of a space of parameters being fitted,
+            * x is a set of N points, each of them is an M-dimensional vector,
+            * c is a K-dimensional vector of parameters being fitted
+
+        This subroutine uses f(c,x[i]), its gradient and its Hessian.
+
+        INPUT PARAMETERS:
+            X       -   array[0..N-1,0..M-1], points (one row = one point)
+            Y       -   array[0..N-1], function values.
+            C       -   array[0..K-1], initial approximation to the solution,
+            N       -   number of points, N>1
+            M       -   dimension of space
+            K       -   number of parameters being fitted
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitcreatefgh(double[,] x,
+            double[] y,
+            double[] c,
+            int n,
+            int m,
+            int k,
+            lsfitstate state)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            ap.assert(n>=1, "LSFitCreateFGH: N<1!");
+            ap.assert(m>=1, "LSFitCreateFGH: M<1!");
+            ap.assert(k>=1, "LSFitCreateFGH: K<1!");
+            ap.assert(ap.len(c)>=k, "LSFitCreateFGH: length(C)<K!");
+            ap.assert(apserv.isfinitevector(c, k), "LSFitCreateFGH: C contains infinite or NaN values!");
+            ap.assert(ap.len(y)>=n, "LSFitCreateFGH: length(Y)<N!");
+            ap.assert(apserv.isfinitevector(y, n), "LSFitCreateFGH: Y contains infinite or NaN values!");
+            ap.assert(ap.rows(x)>=n, "LSFitCreateFGH: rows(X)<N!");
+            ap.assert(ap.cols(x)>=m, "LSFitCreateFGH: cols(X)<M!");
+            ap.assert(apserv.apservisfinitematrix(x, n, m), "LSFitCreateFGH: X contains infinite or NaN values!");
+            state.npoints = n;
+            state.wkind = 0;
+            state.m = m;
+            state.k = k;
+            lsfitsetcond(state, 0.0, 0.0, 0);
+            lsfitsetstpmax(state, 0.0);
+            lsfitsetxrep(state, false);
+            state.taskx = new double[n, m];
+            state.tasky = new double[n];
+            state.c = new double[k];
+            state.h = new double[k, k];
+            state.x = new double[m];
+            state.g = new double[k];
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = c[i_];
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.taskx[i,i_] = x[i,i_];
+                }
+                state.tasky[i] = y[i];
+            }
+            state.s = new double[k];
+            state.bndl = new double[k];
+            state.bndu = new double[k];
+            for(i=0; i<=k-1; i++)
+            {
+                state.s[i] = 1.0;
+                state.bndl[i] = Double.NegativeInfinity;
+                state.bndu[i] = Double.PositiveInfinity;
+            }
+            state.optalgo = 2;
+            state.prevnpt = -1;
+            state.prevalgo = -1;
+            minlm.minlmcreatefgh(k, state.c, state.optstate);
+            lsfitclearrequestfields(state);
+            state.rstate.ia = new int[4+1];
+            state.rstate.ra = new double[2+1];
+            state.rstate.stage = -1;
+        }
+
+
+        /*************************************************************************
+        Stopping conditions for nonlinear least squares fitting.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            EpsF    -   stopping criterion. Algorithm stops if
+                        |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
+            EpsX    -   >=0
+                        The subroutine finishes its work if  on  k+1-th  iteration
+                        the condition |v|<=EpsX is fulfilled, where:
+                        * |.| means Euclidian norm
+                        * v - scaled step vector, v[i]=dx[i]/s[i]
+                        * dx - ste pvector, dx=X(k+1)-X(k)
+                        * s - scaling coefficients set by LSFitSetScale()
+            MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
+                        iterations   is    unlimited.   Only   Levenberg-Marquardt
+                        iterations  are  counted  (L-BFGS/CG  iterations  are  NOT
+                        counted because their cost is very low compared to that of
+                        LM).
+
+        NOTE
+
+        Passing EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to automatic
+        stopping criterion selection (according to the scheme used by MINLM unit).
+
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitsetcond(lsfitstate state,
+            double epsf,
+            double epsx,
+            int maxits)
+        {
+            ap.assert(math.isfinite(epsf), "LSFitSetCond: EpsF is not finite!");
+            ap.assert((double)(epsf)>=(double)(0), "LSFitSetCond: negative EpsF!");
+            ap.assert(math.isfinite(epsx), "LSFitSetCond: EpsX is not finite!");
+            ap.assert((double)(epsx)>=(double)(0), "LSFitSetCond: negative EpsX!");
+            ap.assert(maxits>=0, "LSFitSetCond: negative MaxIts!");
+            state.epsf = epsf;
+            state.epsx = epsx;
+            state.maxits = maxits;
+        }
+
+
+        /*************************************************************************
+        This function sets maximum step length
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
+                        want to limit step length.
+
+        Use this subroutine when you optimize target function which contains exp()
+        or  other  fast  growing  functions,  and optimization algorithm makes too
+        large  steps  which  leads  to overflow. This function allows us to reject
+        steps  that  are  too  large  (and  therefore  expose  us  to the possible
+        overflow) without actually calculating function value at the x+stp*d.
+
+        NOTE: non-zero StpMax leads to moderate  performance  degradation  because
+        intermediate  step  of  preconditioned L-BFGS optimization is incompatible
+        with limits on step size.
+
+          -- ALGLIB --
+             Copyright 02.04.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitsetstpmax(lsfitstate state,
+            double stpmax)
+        {
+            ap.assert((double)(stpmax)>=(double)(0), "LSFitSetStpMax: StpMax<0!");
+            state.stpmax = stpmax;
+        }
+
+
+        /*************************************************************************
+        This function turns on/off reporting.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            NeedXRep-   whether iteration reports are needed or not
+            
+        When reports are needed, State.C (current parameters) and State.F (current
+        value of fitting function) are reported.
+
+
+          -- ALGLIB --
+             Copyright 15.08.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitsetxrep(lsfitstate state,
+            bool needxrep)
+        {
+            state.xrep = needxrep;
+        }
+
+
+        /*************************************************************************
+        This function sets scaling coefficients for underlying optimizer.
+
+        ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+        size and gradient are scaled before comparison with tolerances).  Scale of
+        the I-th variable is a translation invariant measure of:
+        a) "how large" the variable is
+        b) how large the step should be to make significant changes in the function
+
+        Generally, scale is NOT considered to be a form of preconditioner.  But LM
+        optimizer is unique in that it uses scaling matrix both  in  the  stopping
+        condition tests and as Marquardt damping factor.
+
+        Proper scaling is very important for the algorithm performance. It is less
+        important for the quality of results, but still has some influence (it  is
+        easier  to  converge  when  variables  are  properly  scaled, so premature
+        stopping is possible when very badly scalled variables are  combined  with
+        relaxed stopping conditions).
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            S       -   array[N], non-zero scaling coefficients
+                        S[i] may be negative, sign doesn't matter.
+
+          -- ALGLIB --
+             Copyright 14.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitsetscale(lsfitstate state,
+            double[] s)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(s)>=state.k, "LSFitSetScale: Length(S)<K");
+            for(i=0; i<=state.k-1; i++)
+            {
+                ap.assert(math.isfinite(s[i]), "LSFitSetScale: S contains infinite or NAN elements");
+                ap.assert((double)(s[i])!=(double)(0), "LSFitSetScale: S contains infinite or NAN elements");
+                state.s[i] = s[i];
+            }
+        }
+
+
+        /*************************************************************************
+        This function sets boundary constraints for underlying optimizer
+
+        Boundary constraints are inactive by default (after initial creation).
+        They are preserved until explicitly turned off with another SetBC() call.
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            BndL    -   lower bounds, array[K].
+                        If some (all) variables are unbounded, you may specify
+                        very small number or -INF (latter is recommended because
+                        it will allow solver to use better algorithm).
+            BndU    -   upper bounds, array[K].
+                        If some (all) variables are unbounded, you may specify
+                        very large number or +INF (latter is recommended because
+                        it will allow solver to use better algorithm).
+
+        NOTE 1: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+        variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+        NOTE 2: unlike other constrained optimization algorithms, this solver  has
+        following useful properties:
+        * bound constraints are always satisfied exactly
+        * function is evaluated only INSIDE area specified by bound constraints
+
+          -- ALGLIB --
+             Copyright 14.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitsetbc(lsfitstate state,
+            double[] bndl,
+            double[] bndu)
+        {
+            int i = 0;
+            int k = 0;
+
+            k = state.k;
+            ap.assert(ap.len(bndl)>=k, "LSFitSetBC: Length(BndL)<K");
+            ap.assert(ap.len(bndu)>=k, "LSFitSetBC: Length(BndU)<K");
+            for(i=0; i<=k-1; i++)
+            {
+                ap.assert(math.isfinite(bndl[i]) | Double.IsNegativeInfinity(bndl[i]), "LSFitSetBC: BndL contains NAN or +INF");
+                ap.assert(math.isfinite(bndu[i]) | Double.IsPositiveInfinity(bndu[i]), "LSFitSetBC: BndU contains NAN or -INF");
+                if( math.isfinite(bndl[i]) & math.isfinite(bndu[i]) )
+                {
+                    ap.assert((double)(bndl[i])<=(double)(bndu[i]), "LSFitSetBC: BndL[i]>BndU[i]");
+                }
+                state.bndl[i] = bndl[i];
+                state.bndu[i] = bndu[i];
+            }
+        }
+
+
+        /*************************************************************************
+        NOTES:
+
+        1. this algorithm is somewhat unusual because it works with  parameterized
+           function f(C,X), where X is a function argument (we  have  many  points
+           which are characterized by different  argument  values),  and  C  is  a
+           parameter to fit.
+
+           For example, if we want to do linear fit by f(c0,c1,x) = c0*x+c1,  then
+           x will be argument, and {c0,c1} will be parameters.
+           
+           It is important to understand that this algorithm finds minimum in  the
+           space of function PARAMETERS (not arguments), so it  needs  derivatives
+           of f() with respect to C, not X.
+           
+           In the example above it will need f=c0*x+c1 and {df/dc0,df/dc1} = {x,1}
+           instead of {df/dx} = {c0}.
+
+        2. Callback functions accept C as the first parameter, and X as the second
+
+        3. If  state  was  created  with  LSFitCreateFG(),  algorithm  needs  just
+           function   and   its   gradient,   but   if   state   was  created with
+           LSFitCreateFGH(), algorithm will need function, gradient and Hessian.
+           
+           According  to  the  said  above,  there  ase  several  versions of this
+           function, which accept different sets of callbacks.
+           
+           This flexibility opens way to subtle errors - you may create state with
+           LSFitCreateFGH() (optimization using Hessian), but call function  which
+           does not accept Hessian. So when algorithm will request Hessian,  there
+           will be no callback to call. In this case exception will be thrown.
+           
+           Be careful to avoid such errors because there is no way to find them at
+           compile time - you can see them at runtime only.
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool lsfititeration(lsfitstate state)
+        {
+            bool result = new bool();
+            int n = 0;
+            int m = 0;
+            int k = 0;
+            int i = 0;
+            int j = 0;
+            double v = 0;
+            double vv = 0;
+            double relcnt = 0;
+            int i_ = 0;
+
+            
+            //
+            // Reverse communication preparations
+            // I know it looks ugly, but it works the same way
+            // anywhere from C++ to Python.
+            //
+            // This code initializes locals by:
+            // * random values determined during code
+            //   generation - on first subroutine call
+            // * values from previous call - on subsequent calls
+            //
+            if( state.rstate.stage>=0 )
+            {
+                n = state.rstate.ia[0];
+                m = state.rstate.ia[1];
+                k = state.rstate.ia[2];
+                i = state.rstate.ia[3];
+                j = state.rstate.ia[4];
+                v = state.rstate.ra[0];
+                vv = state.rstate.ra[1];
+                relcnt = state.rstate.ra[2];
+            }
+            else
+            {
+                n = -983;
+                m = -989;
+                k = -834;
+                i = 900;
+                j = -287;
+                v = 364;
+                vv = 214;
+                relcnt = -338;
+            }
+            if( state.rstate.stage==0 )
+            {
+                goto lbl_0;
+            }
+            if( state.rstate.stage==1 )
+            {
+                goto lbl_1;
+            }
+            if( state.rstate.stage==2 )
+            {
+                goto lbl_2;
+            }
+            if( state.rstate.stage==3 )
+            {
+                goto lbl_3;
+            }
+            if( state.rstate.stage==4 )
+            {
+                goto lbl_4;
+            }
+            if( state.rstate.stage==5 )
+            {
+                goto lbl_5;
+            }
+            if( state.rstate.stage==6 )
+            {
+                goto lbl_6;
+            }
+            
+            //
+            // Routine body
+            //
+            
+            //
+            // init
+            //
+            if( state.wkind==1 )
+            {
+                ap.assert(state.npoints==state.nweights, "LSFitFit: number of points is not equal to the number of weights");
+            }
+            n = state.npoints;
+            m = state.m;
+            k = state.k;
+            minlm.minlmsetcond(state.optstate, 0.0, state.epsf, state.epsx, state.maxits);
+            minlm.minlmsetstpmax(state.optstate, state.stpmax);
+            minlm.minlmsetxrep(state.optstate, state.xrep);
+            minlm.minlmsetscale(state.optstate, state.s);
+            minlm.minlmsetbc(state.optstate, state.bndl, state.bndu);
+            
+            //
+            // Optimize
+            //
+        lbl_7:
+            if( !minlm.minlmiteration(state.optstate) )
+            {
+                goto lbl_8;
+            }
+            if( !state.optstate.needfi )
+            {
+                goto lbl_9;
+            }
+            
+            //
+            // calculate f[] = wi*(f(xi,c)-yi)
+            //
+            i = 0;
+        lbl_11:
+            if( i>n-1 )
+            {
+                goto lbl_13;
+            }
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = state.optstate.x[i_];
+            }
+            for(i_=0; i_<=m-1;i_++)
+            {
+                state.x[i_] = state.taskx[i,i_];
+            }
+            state.pointindex = i;
+            lsfitclearrequestfields(state);
+            state.needf = true;
+            state.rstate.stage = 0;
+            goto lbl_rcomm;
+        lbl_0:
+            state.needf = false;
+            if( state.wkind==1 )
+            {
+                vv = state.w[i];
+            }
+            else
+            {
+                vv = 1.0;
+            }
+            state.optstate.fi[i] = vv*(state.f-state.tasky[i]);
+            i = i+1;
+            goto lbl_11;
+        lbl_13:
+            goto lbl_7;
+        lbl_9:
+            if( !state.optstate.needf )
+            {
+                goto lbl_14;
+            }
+            
+            //
+            // calculate F = sum (wi*(f(xi,c)-yi))^2
+            //
+            state.optstate.f = 0;
+            i = 0;
+        lbl_16:
+            if( i>n-1 )
+            {
+                goto lbl_18;
+            }
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = state.optstate.x[i_];
+            }
+            for(i_=0; i_<=m-1;i_++)
+            {
+                state.x[i_] = state.taskx[i,i_];
+            }
+            state.pointindex = i;
+            lsfitclearrequestfields(state);
+            state.needf = true;
+            state.rstate.stage = 1;
+            goto lbl_rcomm;
+        lbl_1:
+            state.needf = false;
+            if( state.wkind==1 )
+            {
+                vv = state.w[i];
+            }
+            else
+            {
+                vv = 1.0;
+            }
+            state.optstate.f = state.optstate.f+math.sqr(vv*(state.f-state.tasky[i]));
+            i = i+1;
+            goto lbl_16;
+        lbl_18:
+            goto lbl_7;
+        lbl_14:
+            if( !state.optstate.needfg )
+            {
+                goto lbl_19;
+            }
+            
+            //
+            // calculate F/gradF
+            //
+            state.optstate.f = 0;
+            for(i=0; i<=k-1; i++)
+            {
+                state.optstate.g[i] = 0;
+            }
+            i = 0;
+        lbl_21:
+            if( i>n-1 )
+            {
+                goto lbl_23;
+            }
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = state.optstate.x[i_];
+            }
+            for(i_=0; i_<=m-1;i_++)
+            {
+                state.x[i_] = state.taskx[i,i_];
+            }
+            state.pointindex = i;
+            lsfitclearrequestfields(state);
+            state.needfg = true;
+            state.rstate.stage = 2;
+            goto lbl_rcomm;
+        lbl_2:
+            state.needfg = false;
+            if( state.wkind==1 )
+            {
+                vv = state.w[i];
+            }
+            else
+            {
+                vv = 1.0;
+            }
+            state.optstate.f = state.optstate.f+math.sqr(vv*(state.f-state.tasky[i]));
+            v = math.sqr(vv)*2*(state.f-state.tasky[i]);
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.optstate.g[i_] = state.optstate.g[i_] + v*state.g[i_];
+            }
+            i = i+1;
+            goto lbl_21;
+        lbl_23:
+            goto lbl_7;
+        lbl_19:
+            if( !state.optstate.needfij )
+            {
+                goto lbl_24;
+            }
+            
+            //
+            // calculate Fi/jac(Fi)
+            //
+            i = 0;
+        lbl_26:
+            if( i>n-1 )
+            {
+                goto lbl_28;
+            }
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = state.optstate.x[i_];
+            }
+            for(i_=0; i_<=m-1;i_++)
+            {
+                state.x[i_] = state.taskx[i,i_];
+            }
+            state.pointindex = i;
+            lsfitclearrequestfields(state);
+            state.needfg = true;
+            state.rstate.stage = 3;
+            goto lbl_rcomm;
+        lbl_3:
+            state.needfg = false;
+            if( state.wkind==1 )
+            {
+                vv = state.w[i];
+            }
+            else
+            {
+                vv = 1.0;
+            }
+            state.optstate.fi[i] = vv*(state.f-state.tasky[i]);
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.optstate.j[i,i_] = vv*state.g[i_];
+            }
+            i = i+1;
+            goto lbl_26;
+        lbl_28:
+            goto lbl_7;
+        lbl_24:
+            if( !state.optstate.needfgh )
+            {
+                goto lbl_29;
+            }
+            
+            //
+            // calculate F/grad(F)/hess(F)
+            //
+            state.optstate.f = 0;
+            for(i=0; i<=k-1; i++)
+            {
+                state.optstate.g[i] = 0;
+            }
+            for(i=0; i<=k-1; i++)
+            {
+                for(j=0; j<=k-1; j++)
+                {
+                    state.optstate.h[i,j] = 0;
+                }
+            }
+            i = 0;
+        lbl_31:
+            if( i>n-1 )
+            {
+                goto lbl_33;
+            }
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = state.optstate.x[i_];
+            }
+            for(i_=0; i_<=m-1;i_++)
+            {
+                state.x[i_] = state.taskx[i,i_];
+            }
+            state.pointindex = i;
+            lsfitclearrequestfields(state);
+            state.needfgh = true;
+            state.rstate.stage = 4;
+            goto lbl_rcomm;
+        lbl_4:
+            state.needfgh = false;
+            if( state.wkind==1 )
+            {
+                vv = state.w[i];
+            }
+            else
+            {
+                vv = 1.0;
+            }
+            state.optstate.f = state.optstate.f+math.sqr(vv*(state.f-state.tasky[i]));
+            v = math.sqr(vv)*2*(state.f-state.tasky[i]);
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.optstate.g[i_] = state.optstate.g[i_] + v*state.g[i_];
+            }
+            for(j=0; j<=k-1; j++)
+            {
+                v = 2*math.sqr(vv)*state.g[j];
+                for(i_=0; i_<=k-1;i_++)
+                {
+                    state.optstate.h[j,i_] = state.optstate.h[j,i_] + v*state.g[i_];
+                }
+                v = 2*math.sqr(vv)*(state.f-state.tasky[i]);
+                for(i_=0; i_<=k-1;i_++)
+                {
+                    state.optstate.h[j,i_] = state.optstate.h[j,i_] + v*state.h[j,i_];
+                }
+            }
+            i = i+1;
+            goto lbl_31;
+        lbl_33:
+            goto lbl_7;
+        lbl_29:
+            if( !state.optstate.xupdated )
+            {
+                goto lbl_34;
+            }
+            
+            //
+            // Report new iteration
+            //
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = state.optstate.x[i_];
+            }
+            state.f = state.optstate.f;
+            lsfitclearrequestfields(state);
+            state.xupdated = true;
+            state.rstate.stage = 5;
+            goto lbl_rcomm;
+        lbl_5:
+            state.xupdated = false;
+            goto lbl_7;
+        lbl_34:
+            goto lbl_7;
+        lbl_8:
+            minlm.minlmresults(state.optstate, ref state.c, state.optrep);
+            state.repterminationtype = state.optrep.terminationtype;
+            state.repiterationscount = state.optrep.iterationscount;
+            
+            //
+            // calculate errors
+            //
+            if( state.repterminationtype<=0 )
+            {
+                goto lbl_36;
+            }
+            state.reprmserror = 0;
+            state.repwrmserror = 0;
+            state.repavgerror = 0;
+            state.repavgrelerror = 0;
+            state.repmaxerror = 0;
+            relcnt = 0;
+            i = 0;
+        lbl_38:
+            if( i>n-1 )
+            {
+                goto lbl_40;
+            }
+            for(i_=0; i_<=k-1;i_++)
+            {
+                state.c[i_] = state.c[i_];
+            }
+            for(i_=0; i_<=m-1;i_++)
+            {
+                state.x[i_] = state.taskx[i,i_];
+            }
+            state.pointindex = i;
+            lsfitclearrequestfields(state);
+            state.needf = true;
+            state.rstate.stage = 6;
+            goto lbl_rcomm;
+        lbl_6:
+            state.needf = false;
+            v = state.f;
+            if( state.wkind==1 )
+            {
+                vv = state.w[i];
+            }
+            else
+            {
+                vv = 1.0;
+            }
+            state.reprmserror = state.reprmserror+math.sqr(v-state.tasky[i]);
+            state.repwrmserror = state.repwrmserror+math.sqr(vv*(v-state.tasky[i]));
+            state.repavgerror = state.repavgerror+Math.Abs(v-state.tasky[i]);
+            if( (double)(state.tasky[i])!=(double)(0) )
+            {
+                state.repavgrelerror = state.repavgrelerror+Math.Abs(v-state.tasky[i])/Math.Abs(state.tasky[i]);
+                relcnt = relcnt+1;
+            }
+            state.repmaxerror = Math.Max(state.repmaxerror, Math.Abs(v-state.tasky[i]));
+            i = i+1;
+            goto lbl_38;
+        lbl_40:
+            state.reprmserror = Math.Sqrt(state.reprmserror/n);
+            state.repwrmserror = Math.Sqrt(state.repwrmserror/n);
+            state.repavgerror = state.repavgerror/n;
+            if( (double)(relcnt)!=(double)(0) )
+            {
+                state.repavgrelerror = state.repavgrelerror/relcnt;
+            }
+        lbl_36:
+            result = false;
+            return result;
+            
+            //
+            // Saving state
+            //
+        lbl_rcomm:
+            result = true;
+            state.rstate.ia[0] = n;
+            state.rstate.ia[1] = m;
+            state.rstate.ia[2] = k;
+            state.rstate.ia[3] = i;
+            state.rstate.ia[4] = j;
+            state.rstate.ra[0] = v;
+            state.rstate.ra[1] = vv;
+            state.rstate.ra[2] = relcnt;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Nonlinear least squares fitting results.
+
+        Called after return from LSFitFit().
+
+        INPUT PARAMETERS:
+            State   -   algorithm state
+
+        OUTPUT PARAMETERS:
+            Info    -   completetion code:
+                            *  1    relative function improvement is no more than
+                                    EpsF.
+                            *  2    relative step is no more than EpsX.
+                            *  4    gradient norm is no more than EpsG
+                            *  5    MaxIts steps was taken
+                            *  7    stopping conditions are too stringent,
+                                    further improvement is impossible
+            C       -   array[0..K-1], solution
+            Rep     -   optimization report. Following fields are set:
+                        * Rep.TerminationType completetion code:
+                        * RMSError          rms error on the (X,Y).
+                        * AvgError          average error on the (X,Y).
+                        * AvgRelError       average relative error on the non-zero Y
+                        * MaxError          maximum error
+                                            NON-WEIGHTED ERRORS ARE CALCULATED
+                        * WRMSError         weighted rms error on the (X,Y).
+
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitresults(lsfitstate state,
+            ref int info,
+            ref double[] c,
+            lsfitreport rep)
+        {
+            int i_ = 0;
+
+            info = 0;
+            c = new double[0];
+
+            info = state.repterminationtype;
+            if( info>0 )
+            {
+                c = new double[state.k];
+                for(i_=0; i_<=state.k-1;i_++)
+                {
+                    c[i_] = state.c[i_];
+                }
+                rep.rmserror = state.reprmserror;
+                rep.wrmserror = state.repwrmserror;
+                rep.avgerror = state.repavgerror;
+                rep.avgrelerror = state.repavgrelerror;
+                rep.maxerror = state.repmaxerror;
+                rep.iterationscount = state.repiterationscount;
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine: automatic scaling for LLS tasks.
+        NEVER CALL IT DIRECTLY!
+
+        Maps abscissas to [-1,1], standartizes ordinates and correspondingly scales
+        constraints. It also scales weights so that max(W[i])=1
+
+        Transformations performed:
+        * X, XC         [XA,XB] => [-1,+1]
+                        transformation makes min(X)=-1, max(X)=+1
+
+        * Y             [SA,SB] => [0,1]
+                        transformation makes mean(Y)=0, stddev(Y)=1
+                        
+        * YC            transformed accordingly to SA, SB, DC[I]
+
+          -- ALGLIB PROJECT --
+             Copyright 08.09.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void lsfitscalexy(ref double[] x,
+            ref double[] y,
+            ref double[] w,
+            int n,
+            ref double[] xc,
+            ref double[] yc,
+            int[] dc,
+            int k,
+            ref double xa,
+            ref double xb,
+            ref double sa,
+            ref double sb,
+            ref double[] xoriginal,
+            ref double[] yoriginal)
+        {
+            double xmin = 0;
+            double xmax = 0;
+            int i = 0;
+            double mx = 0;
+            int i_ = 0;
+
+            xa = 0;
+            xb = 0;
+            sa = 0;
+            sb = 0;
+            xoriginal = new double[0];
+            yoriginal = new double[0];
+
+            ap.assert(n>=1, "LSFitScaleXY: incorrect N");
+            ap.assert(k>=0, "LSFitScaleXY: incorrect K");
+            
+            //
+            // Calculate xmin/xmax.
+            // Force xmin<>xmax.
+            //
+            xmin = x[0];
+            xmax = x[0];
+            for(i=1; i<=n-1; i++)
+            {
+                xmin = Math.Min(xmin, x[i]);
+                xmax = Math.Max(xmax, x[i]);
+            }
+            for(i=0; i<=k-1; i++)
+            {
+                xmin = Math.Min(xmin, xc[i]);
+                xmax = Math.Max(xmax, xc[i]);
+            }
+            if( (double)(xmin)==(double)(xmax) )
+            {
+                if( (double)(xmin)==(double)(0) )
+                {
+                    xmin = -1;
+                    xmax = 1;
+                }
+                else
+                {
+                    if( (double)(xmin)>(double)(0) )
+                    {
+                        xmin = 0.5*xmin;
+                    }
+                    else
+                    {
+                        xmax = 0.5*xmax;
+                    }
+                }
+            }
+            
+            //
+            // Transform abscissas: map [XA,XB] to [0,1]
+            //
+            // Store old X[] in XOriginal[] (it will be used
+            // to calculate relative error).
+            //
+            xoriginal = new double[n];
+            for(i_=0; i_<=n-1;i_++)
+            {
+                xoriginal[i_] = x[i_];
+            }
+            xa = xmin;
+            xb = xmax;
+            for(i=0; i<=n-1; i++)
+            {
+                x[i] = 2*(x[i]-0.5*(xa+xb))/(xb-xa);
+            }
+            for(i=0; i<=k-1; i++)
+            {
+                ap.assert(dc[i]>=0, "LSFitScaleXY: internal error!");
+                xc[i] = 2*(xc[i]-0.5*(xa+xb))/(xb-xa);
+                yc[i] = yc[i]*Math.Pow(0.5*(xb-xa), dc[i]);
+            }
+            
+            //
+            // Transform function values: map [SA,SB] to [0,1]
+            // SA = mean(Y),
+            // SB = SA+stddev(Y).
+            //
+            // Store old Y[] in YOriginal[] (it will be used
+            // to calculate relative error).
+            //
+            yoriginal = new double[n];
+            for(i_=0; i_<=n-1;i_++)
+            {
+                yoriginal[i_] = y[i_];
+            }
+            sa = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                sa = sa+y[i];
+            }
+            sa = sa/n;
+            sb = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                sb = sb+math.sqr(y[i]-sa);
+            }
+            sb = Math.Sqrt(sb/n)+sa;
+            if( (double)(sb)==(double)(sa) )
+            {
+                sb = 2*sa;
+            }
+            if( (double)(sb)==(double)(sa) )
+            {
+                sb = sa+1;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                y[i] = (y[i]-sa)/(sb-sa);
+            }
+            for(i=0; i<=k-1; i++)
+            {
+                if( dc[i]==0 )
+                {
+                    yc[i] = (yc[i]-sa)/(sb-sa);
+                }
+                else
+                {
+                    yc[i] = yc[i]/(sb-sa);
+                }
+            }
+            
+            //
+            // Scale weights
+            //
+            mx = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                mx = Math.Max(mx, Math.Abs(w[i]));
+            }
+            if( (double)(mx)!=(double)(0) )
+            {
+                for(i=0; i<=n-1; i++)
+                {
+                    w[i] = w[i]/mx;
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Internal spline fitting subroutine
+
+          -- ALGLIB PROJECT --
+             Copyright 08.09.2009 by Bochkanov Sergey
+        *************************************************************************/
+        private static void spline1dfitinternal(int st,
+            double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            double[] xc,
+            double[] yc,
+            int[] dc,
+            int k,
+            int m,
+            ref int info,
+            spline1d.spline1dinterpolant s,
+            spline1dfitreport rep)
+        {
+            double[,] fmatrix = new double[0,0];
+            double[,] cmatrix = new double[0,0];
+            double[] y2 = new double[0];
+            double[] w2 = new double[0];
+            double[] sx = new double[0];
+            double[] sy = new double[0];
+            double[] sd = new double[0];
+            double[] tmp = new double[0];
+            double[] xoriginal = new double[0];
+            double[] yoriginal = new double[0];
+            lsfitreport lrep = new lsfitreport();
+            double v0 = 0;
+            double v1 = 0;
+            double v2 = 0;
+            double mx = 0;
+            spline1d.spline1dinterpolant s2 = new spline1d.spline1dinterpolant();
+            int i = 0;
+            int j = 0;
+            int relcnt = 0;
+            double xa = 0;
+            double xb = 0;
+            double sa = 0;
+            double sb = 0;
+            double bl = 0;
+            double br = 0;
+            double decay = 0;
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            w = (double[])w.Clone();
+            xc = (double[])xc.Clone();
+            yc = (double[])yc.Clone();
+            info = 0;
+
+            ap.assert(st==0 | st==1, "Spline1DFit: internal error!");
+            if( st==0 & m<4 )
+            {
+                info = -1;
+                return;
+            }
+            if( st==1 & m<4 )
+            {
+                info = -1;
+                return;
+            }
+            if( (n<1 | k<0) | k>=m )
+            {
+                info = -1;
+                return;
+            }
+            for(i=0; i<=k-1; i++)
+            {
+                info = 0;
+                if( dc[i]<0 )
+                {
+                    info = -1;
+                }
+                if( dc[i]>1 )
+                {
+                    info = -1;
+                }
+                if( info<0 )
+                {
+                    return;
+                }
+            }
+            if( st==1 & m%2!=0 )
+            {
+                
+                //
+                // Hermite fitter must have even number of basis functions
+                //
+                info = -2;
+                return;
+            }
+            
+            //
+            // weight decay for correct handling of task which becomes
+            // degenerate after constraints are applied
+            //
+            decay = 10000*math.machineepsilon;
+            
+            //
+            // Scale X, Y, XC, YC
+            //
+            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
+            
+            //
+            // allocate space, initialize:
+            // * SX     -   grid for basis functions
+            // * SY     -   values of basis functions at grid points
+            // * FMatrix-   values of basis functions at X[]
+            // * CMatrix-   values (derivatives) of basis functions at XC[]
+            //
+            y2 = new double[n+m];
+            w2 = new double[n+m];
+            fmatrix = new double[n+m, m];
+            if( k>0 )
+            {
+                cmatrix = new double[k, m+1];
+            }
+            if( st==0 )
+            {
+                
+                //
+                // allocate space for cubic spline
+                //
+                sx = new double[m-2];
+                sy = new double[m-2];
+                for(j=0; j<=m-2-1; j++)
+                {
+                    sx[j] = (double)(2*j)/(double)(m-2-1)-1;
+                }
+            }
+            if( st==1 )
+            {
+                
+                //
+                // allocate space for Hermite spline
+                //
+                sx = new double[m/2];
+                sy = new double[m/2];
+                sd = new double[m/2];
+                for(j=0; j<=m/2-1; j++)
+                {
+                    sx[j] = (double)(2*j)/(double)(m/2-1)-1;
+                }
+            }
+            
+            //
+            // Prepare design and constraints matrices:
+            // * fill constraints matrix
+            // * fill first N rows of design matrix with values
+            // * fill next M rows of design matrix with regularizing term
+            // * append M zeros to Y
+            // * append M elements, mean(abs(W)) each, to W
+            //
+            for(j=0; j<=m-1; j++)
+            {
+                
+                //
+                // prepare Jth basis function
+                //
+                if( st==0 )
+                {
+                    
+                    //
+                    // cubic spline basis
+                    //
+                    for(i=0; i<=m-2-1; i++)
+                    {
+                        sy[i] = 0;
+                    }
+                    bl = 0;
+                    br = 0;
+                    if( j<m-2 )
+                    {
+                        sy[j] = 1;
+                    }
+                    if( j==m-2 )
+                    {
+                        bl = 1;
+                    }
+                    if( j==m-1 )
+                    {
+                        br = 1;
+                    }
+                    spline1d.spline1dbuildcubic(sx, sy, m-2, 1, bl, 1, br, s2);
+                }
+                if( st==1 )
+                {
+                    
+                    //
+                    // Hermite basis
+                    //
+                    for(i=0; i<=m/2-1; i++)
+                    {
+                        sy[i] = 0;
+                        sd[i] = 0;
+                    }
+                    if( j%2==0 )
+                    {
+                        sy[j/2] = 1;
+                    }
+                    else
+                    {
+                        sd[j/2] = 1;
+                    }
+                    spline1d.spline1dbuildhermite(sx, sy, sd, m/2, s2);
+                }
+                
+                //
+                // values at X[], XC[]
+                //
+                for(i=0; i<=n-1; i++)
+                {
+                    fmatrix[i,j] = spline1d.spline1dcalc(s2, x[i]);
+                }
+                for(i=0; i<=k-1; i++)
+                {
+                    ap.assert(dc[i]>=0 & dc[i]<=2, "Spline1DFit: internal error!");
+                    spline1d.spline1ddiff(s2, xc[i], ref v0, ref v1, ref v2);
+                    if( dc[i]==0 )
+                    {
+                        cmatrix[i,j] = v0;
+                    }
+                    if( dc[i]==1 )
+                    {
+                        cmatrix[i,j] = v1;
+                    }
+                    if( dc[i]==2 )
+                    {
+                        cmatrix[i,j] = v2;
+                    }
+                }
+            }
+            for(i=0; i<=k-1; i++)
+            {
+                cmatrix[i,m] = yc[i];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=m-1; j++)
+                {
+                    if( i==j )
+                    {
+                        fmatrix[n+i,j] = decay;
+                    }
+                    else
+                    {
+                        fmatrix[n+i,j] = 0;
+                    }
+                }
+            }
+            y2 = new double[n+m];
+            w2 = new double[n+m];
+            for(i_=0; i_<=n-1;i_++)
+            {
+                y2[i_] = y[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                w2[i_] = w[i_];
+            }
+            mx = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                mx = mx+Math.Abs(w[i]);
+            }
+            mx = mx/n;
+            for(i=0; i<=m-1; i++)
+            {
+                y2[n+i] = 0;
+                w2[n+i] = mx;
+            }
+            
+            //
+            // Solve constrained task
+            //
+            if( k>0 )
+            {
+                
+                //
+                // solve using regularization
+                //
+                lsfitlinearwc(y2, w2, fmatrix, cmatrix, n+m, m, k, ref info, ref tmp, lrep);
+            }
+            else
+            {
+                
+                //
+                // no constraints, no regularization needed
+                //
+                lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref tmp, lrep);
+            }
+            if( info<0 )
+            {
+                return;
+            }
+            
+            //
+            // Generate spline and scale it
+            //
+            if( st==0 )
+            {
+                
+                //
+                // cubic spline basis
+                //
+                for(i_=0; i_<=m-2-1;i_++)
+                {
+                    sy[i_] = tmp[i_];
+                }
+                spline1d.spline1dbuildcubic(sx, sy, m-2, 1, tmp[m-2], 1, tmp[m-1], s);
+            }
+            if( st==1 )
+            {
+                
+                //
+                // Hermite basis
+                //
+                for(i=0; i<=m/2-1; i++)
+                {
+                    sy[i] = tmp[2*i];
+                    sd[i] = tmp[2*i+1];
+                }
+                spline1d.spline1dbuildhermite(sx, sy, sd, m/2, s);
+            }
+            spline1d.spline1dlintransx(s, 2/(xb-xa), -((xa+xb)/(xb-xa)));
+            spline1d.spline1dlintransy(s, sb-sa, sa);
+            
+            //
+            // Scale absolute errors obtained from LSFitLinearW.
+            // Relative error should be calculated separately
+            // (because of shifting/scaling of the task)
+            //
+            rep.taskrcond = lrep.taskrcond;
+            rep.rmserror = lrep.rmserror*(sb-sa);
+            rep.avgerror = lrep.avgerror*(sb-sa);
+            rep.maxerror = lrep.maxerror*(sb-sa);
+            rep.avgrelerror = 0;
+            relcnt = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                if( (double)(yoriginal[i])!=(double)(0) )
+                {
+                    rep.avgrelerror = rep.avgrelerror+Math.Abs(spline1d.spline1dcalc(s, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
+                    relcnt = relcnt+1;
+                }
+            }
+            if( relcnt!=0 )
+            {
+                rep.avgrelerror = rep.avgrelerror/relcnt;
+            }
+        }
+
+
+        /*************************************************************************
+        Internal fitting subroutine
+        *************************************************************************/
+        private static void lsfitlinearinternal(double[] y,
+            double[] w,
+            double[,] fmatrix,
+            int n,
+            int m,
+            ref int info,
+            ref double[] c,
+            lsfitreport rep)
+        {
+            double threshold = 0;
+            double[,] ft = new double[0,0];
+            double[,] q = new double[0,0];
+            double[,] l = new double[0,0];
+            double[,] r = new double[0,0];
+            double[] b = new double[0];
+            double[] wmod = new double[0];
+            double[] tau = new double[0];
+            int i = 0;
+            int j = 0;
+            double v = 0;
+            double[] sv = new double[0];
+            double[,] u = new double[0,0];
+            double[,] vt = new double[0,0];
+            double[] tmp = new double[0];
+            double[] utb = new double[0];
+            double[] sutb = new double[0];
+            int relcnt = 0;
+            int i_ = 0;
+
+            info = 0;
+            c = new double[0];
+
+            if( n<1 | m<1 )
+            {
+                info = -1;
+                return;
+            }
+            info = 1;
+            threshold = Math.Sqrt(math.machineepsilon);
+            
+            //
+            // Degenerate case, needs special handling
+            //
+            if( n<m )
+            {
+                
+                //
+                // Create design matrix.
+                //
+                ft = new double[n, m];
+                b = new double[n];
+                wmod = new double[n];
+                for(j=0; j<=n-1; j++)
+                {
+                    v = w[j];
+                    for(i_=0; i_<=m-1;i_++)
+                    {
+                        ft[j,i_] = v*fmatrix[j,i_];
+                    }
+                    b[j] = w[j]*y[j];
+                    wmod[j] = 1;
+                }
+                
+                //
+                // LQ decomposition and reduction to M=N
+                //
+                c = new double[m];
+                for(i=0; i<=m-1; i++)
+                {
+                    c[i] = 0;
+                }
+                rep.taskrcond = 0;
+                ortfac.rmatrixlq(ref ft, n, m, ref tau);
+                ortfac.rmatrixlqunpackq(ft, n, m, tau, n, ref q);
+                ortfac.rmatrixlqunpackl(ft, n, m, ref l);
+                lsfitlinearinternal(b, wmod, l, n, n, ref info, ref tmp, rep);
+                if( info<=0 )
+                {
+                    return;
+                }
+                for(i=0; i<=n-1; i++)
+                {
+                    v = tmp[i];
+                    for(i_=0; i_<=m-1;i_++)
+                    {
+                        c[i_] = c[i_] + v*q[i,i_];
+                    }
+                }
+                return;
+            }
+            
+            //
+            // N>=M. Generate design matrix and reduce to N=M using
+            // QR decomposition.
+            //
+            ft = new double[n, m];
+            b = new double[n];
+            for(j=0; j<=n-1; j++)
+            {
+                v = w[j];
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    ft[j,i_] = v*fmatrix[j,i_];
+                }
+                b[j] = w[j]*y[j];
+            }
+            ortfac.rmatrixqr(ref ft, n, m, ref tau);
+            ortfac.rmatrixqrunpackq(ft, n, m, tau, m, ref q);
+            ortfac.rmatrixqrunpackr(ft, n, m, ref r);
+            tmp = new double[m];
+            for(i=0; i<=m-1; i++)
+            {
+                tmp[i] = 0;
+            }
+            for(i=0; i<=n-1; i++)
+            {
+                v = b[i];
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    tmp[i_] = tmp[i_] + v*q[i,i_];
+                }
+            }
+            b = new double[m];
+            for(i_=0; i_<=m-1;i_++)
+            {
+                b[i_] = tmp[i_];
+            }
+            
+            //
+            // R contains reduced MxM design upper triangular matrix,
+            // B contains reduced Mx1 right part.
+            //
+            // Determine system condition number and decide
+            // should we use triangular solver (faster) or
+            // SVD-based solver (more stable).
+            //
+            // We can use LU-based RCond estimator for this task.
+            //
+            rep.taskrcond = rcond.rmatrixlurcondinf(r, m);
+            if( (double)(rep.taskrcond)>(double)(threshold) )
+            {
+                
+                //
+                // use QR-based solver
+                //
+                c = new double[m];
+                c[m-1] = b[m-1]/r[m-1,m-1];
+                for(i=m-2; i>=0; i--)
+                {
+                    v = 0.0;
+                    for(i_=i+1; i_<=m-1;i_++)
+                    {
+                        v += r[i,i_]*c[i_];
+                    }
+                    c[i] = (b[i]-v)/r[i,i];
+                }
+            }
+            else
+            {
+                
+                //
+                // use SVD-based solver
+                //
+                if( !svd.rmatrixsvd(r, m, m, 1, 1, 2, ref sv, ref u, ref vt) )
+                {
+                    info = -4;
+                    return;
+                }
+                utb = new double[m];
+                sutb = new double[m];
+                for(i=0; i<=m-1; i++)
+                {
+                    utb[i] = 0;
+                }
+                for(i=0; i<=m-1; i++)
+                {
+                    v = b[i];
+                    for(i_=0; i_<=m-1;i_++)
+                    {
+                        utb[i_] = utb[i_] + v*u[i,i_];
+                    }
+                }
+                if( (double)(sv[0])>(double)(0) )
+                {
+                    rep.taskrcond = sv[m-1]/sv[0];
+                    for(i=0; i<=m-1; i++)
+                    {
+                        if( (double)(sv[i])>(double)(threshold*sv[0]) )
+                        {
+                            sutb[i] = utb[i]/sv[i];
+                        }
+                        else
+                        {
+                            sutb[i] = 0;
+                        }
+                    }
+                }
+                else
+                {
+                    rep.taskrcond = 0;
+                    for(i=0; i<=m-1; i++)
+                    {
+                        sutb[i] = 0;
+                    }
+                }
+                c = new double[m];
+                for(i=0; i<=m-1; i++)
+                {
+                    c[i] = 0;
+                }
+                for(i=0; i<=m-1; i++)
+                {
+                    v = sutb[i];
+                    for(i_=0; i_<=m-1;i_++)
+                    {
+                        c[i_] = c[i_] + v*vt[i,i_];
+                    }
+                }
+            }
+            
+            //
+            // calculate errors
+            //
+            rep.rmserror = 0;
+            rep.avgerror = 0;
+            rep.avgrelerror = 0;
+            rep.maxerror = 0;
+            relcnt = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    v += fmatrix[i,i_]*c[i_];
+                }
+                rep.rmserror = rep.rmserror+math.sqr(v-y[i]);
+                rep.avgerror = rep.avgerror+Math.Abs(v-y[i]);
+                if( (double)(y[i])!=(double)(0) )
+                {
+                    rep.avgrelerror = rep.avgrelerror+Math.Abs(v-y[i])/Math.Abs(y[i]);
+                    relcnt = relcnt+1;
+                }
+                rep.maxerror = Math.Max(rep.maxerror, Math.Abs(v-y[i]));
+            }
+            rep.rmserror = Math.Sqrt(rep.rmserror/n);
+            rep.avgerror = rep.avgerror/n;
+            if( relcnt!=0 )
+            {
+                rep.avgrelerror = rep.avgrelerror/relcnt;
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine
+        *************************************************************************/
+        private static void lsfitclearrequestfields(lsfitstate state)
+        {
+            state.needf = false;
+            state.needfg = false;
+            state.needfgh = false;
+            state.xupdated = false;
+        }
+
+
+        /*************************************************************************
+        Internal subroutine, calculates barycentric basis functions.
+        Used for efficient simultaneous calculation of N basis functions.
+
+          -- ALGLIB --
+             Copyright 17.08.2009 by Bochkanov Sergey
+        *************************************************************************/
+        private static void barycentriccalcbasis(ratint.barycentricinterpolant b,
+            double t,
+            ref double[] y)
+        {
+            double s2 = 0;
+            double s = 0;
+            double v = 0;
+            int i = 0;
+            int j = 0;
+            int i_ = 0;
+
+            
+            //
+            // special case: N=1
+            //
+            if( b.n==1 )
+            {
+                y[0] = 1;
+                return;
+            }
+            
+            //
+            // Here we assume that task is normalized, i.e.:
+            // 1. abs(Y[i])<=1
+            // 2. abs(W[i])<=1
+            // 3. X[] is ordered
+            //
+            // First, we decide: should we use "safe" formula (guarded
+            // against overflow) or fast one?
+            //
+            s = Math.Abs(t-b.x[0]);
+            for(i=0; i<=b.n-1; i++)
+            {
+                v = b.x[i];
+                if( (double)(v)==(double)(t) )
+                {
+                    for(j=0; j<=b.n-1; j++)
+                    {
+                        y[j] = 0;
+                    }
+                    y[i] = 1;
+                    return;
+                }
+                v = Math.Abs(t-v);
+                if( (double)(v)<(double)(s) )
+                {
+                    s = v;
+                }
+            }
+            s2 = 0;
+            for(i=0; i<=b.n-1; i++)
+            {
+                v = s/(t-b.x[i]);
+                v = v*b.w[i];
+                y[i] = v;
+                s2 = s2+v;
+            }
+            v = 1/s2;
+            for(i_=0; i_<=b.n-1;i_++)
+            {
+                y[i_] = v*y[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This is internal function for Chebyshev fitting.
+
+        It assumes that input data are normalized:
+        * X/XC belong to [-1,+1],
+        * mean(Y)=0, stddev(Y)=1.
+
+        It does not checks inputs for errors.
+
+        This function is used to fit general (shifted) Chebyshev models, power
+        basis models or barycentric models.
+
+        INPUT PARAMETERS:
+            X   -   points, array[0..N-1].
+            Y   -   function values, array[0..N-1].
+            W   -   weights, array[0..N-1]
+            N   -   number of points, N>0.
+            XC  -   points where polynomial values/derivatives are constrained,
+                    array[0..K-1].
+            YC  -   values of constraints, array[0..K-1]
+            DC  -   array[0..K-1], types of constraints:
+                    * DC[i]=0   means that P(XC[i])=YC[i]
+                    * DC[i]=1   means that P'(XC[i])=YC[i]
+            K   -   number of constraints, 0<=K<M.
+                    K=0 means no constraints (XC/YC/DC are not used in such cases)
+            M   -   number of basis functions (= polynomial_degree + 1), M>=1
+
+        OUTPUT PARAMETERS:
+            Info-   same format as in LSFitLinearW() subroutine:
+                    * Info>0    task is solved
+                    * Info<=0   an error occured:
+                                -4 means inconvergence of internal SVD
+                                -3 means inconsistent constraints
+            C   -   interpolant in Chebyshev form; [-1,+1] is used as base interval
+            Rep -   report, same format as in LSFitLinearW() subroutine.
+                    Following fields are set:
+                    * RMSError      rms error on the (X,Y).
+                    * AvgError      average error on the (X,Y).
+                    * AvgRelError   average relative error on the non-zero Y
+                    * MaxError      maximum error
+                                    NON-WEIGHTED ERRORS ARE CALCULATED
+
+        IMPORTANT:
+            this subroitine doesn't calculate task's condition number for K<>0.
+
+          -- ALGLIB PROJECT --
+             Copyright 10.12.2009 by Bochkanov Sergey
+        *************************************************************************/
+        private static void internalchebyshevfit(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            double[] xc,
+            double[] yc,
+            int[] dc,
+            int k,
+            int m,
+            ref int info,
+            ref double[] c,
+            lsfitreport rep)
+        {
+            double[] y2 = new double[0];
+            double[] w2 = new double[0];
+            double[] tmp = new double[0];
+            double[] tmp2 = new double[0];
+            double[] tmpdiff = new double[0];
+            double[] bx = new double[0];
+            double[] by = new double[0];
+            double[] bw = new double[0];
+            double[,] fmatrix = new double[0,0];
+            double[,] cmatrix = new double[0,0];
+            int i = 0;
+            int j = 0;
+            double mx = 0;
+            double decay = 0;
+            int i_ = 0;
+
+            xc = (double[])xc.Clone();
+            yc = (double[])yc.Clone();
+            info = 0;
+            c = new double[0];
+
+            
+            //
+            // weight decay for correct handling of task which becomes
+            // degenerate after constraints are applied
+            //
+            decay = 10000*math.machineepsilon;
+            
+            //
+            // allocate space, initialize/fill:
+            // * FMatrix-   values of basis functions at X[]
+            // * CMatrix-   values (derivatives) of basis functions at XC[]
+            // * fill constraints matrix
+            // * fill first N rows of design matrix with values
+            // * fill next M rows of design matrix with regularizing term
+            // * append M zeros to Y
+            // * append M elements, mean(abs(W)) each, to W
+            //
+            y2 = new double[n+m];
+            w2 = new double[n+m];
+            tmp = new double[m];
+            tmpdiff = new double[m];
+            fmatrix = new double[n+m, m];
+            if( k>0 )
+            {
+                cmatrix = new double[k, m+1];
+            }
+            
+            //
+            // Fill design matrix, Y2, W2:
+            // * first N rows with basis functions for original points
+            // * next M rows with decay terms
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                
+                //
+                // prepare Ith row
+                // use Tmp for calculations to avoid multidimensional arrays overhead
+                //
+                for(j=0; j<=m-1; j++)
+                {
+                    if( j==0 )
+                    {
+                        tmp[j] = 1;
+                    }
+                    else
+                    {
+                        if( j==1 )
+                        {
+                            tmp[j] = x[i];
+                        }
+                        else
+                        {
+                            tmp[j] = 2*x[i]*tmp[j-1]-tmp[j-2];
+                        }
+                    }
+                }
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    fmatrix[i,i_] = tmp[i_];
+                }
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=m-1; j++)
+                {
+                    if( i==j )
+                    {
+                        fmatrix[n+i,j] = decay;
+                    }
+                    else
+                    {
+                        fmatrix[n+i,j] = 0;
+                    }
+                }
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                y2[i_] = y[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                w2[i_] = w[i_];
+            }
+            mx = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                mx = mx+Math.Abs(w[i]);
+            }
+            mx = mx/n;
+            for(i=0; i<=m-1; i++)
+            {
+                y2[n+i] = 0;
+                w2[n+i] = mx;
+            }
+            
+            //
+            // fill constraints matrix
+            //
+            for(i=0; i<=k-1; i++)
+            {
+                
+                //
+                // prepare Ith row
+                // use Tmp for basis function values,
+                // TmpDiff for basos function derivatives
+                //
+                for(j=0; j<=m-1; j++)
+                {
+                    if( j==0 )
+                    {
+                        tmp[j] = 1;
+                        tmpdiff[j] = 0;
+                    }
+                    else
+                    {
+                        if( j==1 )
+                        {
+                            tmp[j] = xc[i];
+                            tmpdiff[j] = 1;
+                        }
+                        else
+                        {
+                            tmp[j] = 2*xc[i]*tmp[j-1]-tmp[j-2];
+                            tmpdiff[j] = 2*(tmp[j-1]+xc[i]*tmpdiff[j-1])-tmpdiff[j-2];
+                        }
+                    }
+                }
+                if( dc[i]==0 )
+                {
+                    for(i_=0; i_<=m-1;i_++)
+                    {
+                        cmatrix[i,i_] = tmp[i_];
+                    }
+                }
+                if( dc[i]==1 )
+                {
+                    for(i_=0; i_<=m-1;i_++)
+                    {
+                        cmatrix[i,i_] = tmpdiff[i_];
+                    }
+                }
+                cmatrix[i,m] = yc[i];
+            }
+            
+            //
+            // Solve constrained task
+            //
+            if( k>0 )
+            {
+                
+                //
+                // solve using regularization
+                //
+                lsfitlinearwc(y2, w2, fmatrix, cmatrix, n+m, m, k, ref info, ref c, rep);
+            }
+            else
+            {
+                
+                //
+                // no constraints, no regularization needed
+                //
+                lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, 0, ref info, ref c, rep);
+            }
+            if( info<0 )
+            {
+                return;
+            }
+        }
+
+
+        /*************************************************************************
+        Internal Floater-Hormann fitting subroutine for fixed D
+        *************************************************************************/
+        private static void barycentricfitwcfixedd(double[] x,
+            double[] y,
+            double[] w,
+            int n,
+            double[] xc,
+            double[] yc,
+            int[] dc,
+            int k,
+            int m,
+            int d,
+            ref int info,
+            ratint.barycentricinterpolant b,
+            barycentricfitreport rep)
+        {
+            double[,] fmatrix = new double[0,0];
+            double[,] cmatrix = new double[0,0];
+            double[] y2 = new double[0];
+            double[] w2 = new double[0];
+            double[] sx = new double[0];
+            double[] sy = new double[0];
+            double[] sbf = new double[0];
+            double[] xoriginal = new double[0];
+            double[] yoriginal = new double[0];
+            double[] tmp = new double[0];
+            lsfitreport lrep = new lsfitreport();
+            double v0 = 0;
+            double v1 = 0;
+            double mx = 0;
+            ratint.barycentricinterpolant b2 = new ratint.barycentricinterpolant();
+            int i = 0;
+            int j = 0;
+            int relcnt = 0;
+            double xa = 0;
+            double xb = 0;
+            double sa = 0;
+            double sb = 0;
+            double decay = 0;
+            int i_ = 0;
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            w = (double[])w.Clone();
+            xc = (double[])xc.Clone();
+            yc = (double[])yc.Clone();
+            info = 0;
+
+            if( ((n<1 | m<2) | k<0) | k>=m )
+            {
+                info = -1;
+                return;
+            }
+            for(i=0; i<=k-1; i++)
+            {
+                info = 0;
+                if( dc[i]<0 )
+                {
+                    info = -1;
+                }
+                if( dc[i]>1 )
+                {
+                    info = -1;
+                }
+                if( info<0 )
+                {
+                    return;
+                }
+            }
+            
+            //
+            // weight decay for correct handling of task which becomes
+            // degenerate after constraints are applied
+            //
+            decay = 10000*math.machineepsilon;
+            
+            //
+            // Scale X, Y, XC, YC
+            //
+            lsfitscalexy(ref x, ref y, ref w, n, ref xc, ref yc, dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
+            
+            //
+            // allocate space, initialize:
+            // * FMatrix-   values of basis functions at X[]
+            // * CMatrix-   values (derivatives) of basis functions at XC[]
+            //
+            y2 = new double[n+m];
+            w2 = new double[n+m];
+            fmatrix = new double[n+m, m];
+            if( k>0 )
+            {
+                cmatrix = new double[k, m+1];
+            }
+            y2 = new double[n+m];
+            w2 = new double[n+m];
+            
+            //
+            // Prepare design and constraints matrices:
+            // * fill constraints matrix
+            // * fill first N rows of design matrix with values
+            // * fill next M rows of design matrix with regularizing term
+            // * append M zeros to Y
+            // * append M elements, mean(abs(W)) each, to W
+            //
+            sx = new double[m];
+            sy = new double[m];
+            sbf = new double[m];
+            for(j=0; j<=m-1; j++)
+            {
+                sx[j] = (double)(2*j)/(double)(m-1)-1;
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                sy[i] = 1;
+            }
+            ratint.barycentricbuildfloaterhormann(sx, sy, m, d, b2);
+            mx = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                barycentriccalcbasis(b2, x[i], ref sbf);
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    fmatrix[i,i_] = sbf[i_];
+                }
+                y2[i] = y[i];
+                w2[i] = w[i];
+                mx = mx+Math.Abs(w[i])/n;
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=m-1; j++)
+                {
+                    if( i==j )
+                    {
+                        fmatrix[n+i,j] = decay;
+                    }
+                    else
+                    {
+                        fmatrix[n+i,j] = 0;
+                    }
+                }
+                y2[n+i] = 0;
+                w2[n+i] = mx;
+            }
+            if( k>0 )
+            {
+                for(j=0; j<=m-1; j++)
+                {
+                    for(i=0; i<=m-1; i++)
+                    {
+                        sy[i] = 0;
+                    }
+                    sy[j] = 1;
+                    ratint.barycentricbuildfloaterhormann(sx, sy, m, d, b2);
+                    for(i=0; i<=k-1; i++)
+                    {
+                        ap.assert(dc[i]>=0 & dc[i]<=1, "BarycentricFit: internal error!");
+                        ratint.barycentricdiff1(b2, xc[i], ref v0, ref v1);
+                        if( dc[i]==0 )
+                        {
+                            cmatrix[i,j] = v0;
+                        }
+                        if( dc[i]==1 )
+                        {
+                            cmatrix[i,j] = v1;
+                        }
+                    }
+                }
+                for(i=0; i<=k-1; i++)
+                {
+                    cmatrix[i,m] = yc[i];
+                }
+            }
+            
+            //
+            // Solve constrained task
+            //
+            if( k>0 )
+            {
+                
+                //
+                // solve using regularization
+                //
+                lsfitlinearwc(y2, w2, fmatrix, cmatrix, n+m, m, k, ref info, ref tmp, lrep);
+            }
+            else
+            {
+                
+                //
+                // no constraints, no regularization needed
+                //
+                lsfitlinearwc(y, w, fmatrix, cmatrix, n, m, k, ref info, ref tmp, lrep);
+            }
+            if( info<0 )
+            {
+                return;
+            }
+            
+            //
+            // Generate interpolant and scale it
+            //
+            for(i_=0; i_<=m-1;i_++)
+            {
+                sy[i_] = tmp[i_];
+            }
+            ratint.barycentricbuildfloaterhormann(sx, sy, m, d, b);
+            ratint.barycentriclintransx(b, 2/(xb-xa), -((xa+xb)/(xb-xa)));
+            ratint.barycentriclintransy(b, sb-sa, sa);
+            
+            //
+            // Scale absolute errors obtained from LSFitLinearW.
+            // Relative error should be calculated separately
+            // (because of shifting/scaling of the task)
+            //
+            rep.taskrcond = lrep.taskrcond;
+            rep.rmserror = lrep.rmserror*(sb-sa);
+            rep.avgerror = lrep.avgerror*(sb-sa);
+            rep.maxerror = lrep.maxerror*(sb-sa);
+            rep.avgrelerror = 0;
+            relcnt = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                if( (double)(yoriginal[i])!=(double)(0) )
+                {
+                    rep.avgrelerror = rep.avgrelerror+Math.Abs(ratint.barycentriccalc(b, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
+                    relcnt = relcnt+1;
+                }
+            }
+            if( relcnt!=0 )
+            {
+                rep.avgrelerror = rep.avgrelerror/relcnt;
+            }
+        }
+
+
+    }
     public class pspline
     {
@@ -16977,4 +16154,1172 @@
 
     }
+    public class spline2d
+    {
+        /*************************************************************************
+        2-dimensional spline inteprolant
+        *************************************************************************/
+        public class spline2dinterpolant
+        {
+            public int k;
+            public double[] c;
+            public spline2dinterpolant()
+            {
+                c = new double[0];
+            }
+        };
+
+
+
+
+        /*************************************************************************
+        This subroutine builds bilinear spline coefficients table.
+
+        Input parameters:
+            X   -   spline abscissas, array[0..N-1]
+            Y   -   spline ordinates, array[0..M-1]
+            F   -   function values, array[0..M-1,0..N-1]
+            M,N -   grid size, M>=2, N>=2
+
+        Output parameters:
+            C   -   spline interpolant
+
+          -- ALGLIB PROJECT --
+             Copyright 05.07.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dbuildbilinear(double[] x,
+            double[] y,
+            double[,] f,
+            int m,
+            int n,
+            spline2dinterpolant c)
+        {
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int tblsize = 0;
+            int shift = 0;
+            double t = 0;
+            double[,] dx = new double[0,0];
+            double[,] dy = new double[0,0];
+            double[,] dxy = new double[0,0];
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            f = (double[,])f.Clone();
+
+            ap.assert(n>=2 & m>=2, "Spline2DBuildBilinear: N<2 or M<2!");
+            
+            //
+            // Sort points
+            //
+            for(j=0; j<=n-1; j++)
+            {
+                k = j;
+                for(i=j+1; i<=n-1; i++)
+                {
+                    if( (double)(x[i])<(double)(x[k]) )
+                    {
+                        k = i;
+                    }
+                }
+                if( k!=j )
+                {
+                    for(i=0; i<=m-1; i++)
+                    {
+                        t = f[i,j];
+                        f[i,j] = f[i,k];
+                        f[i,k] = t;
+                    }
+                    t = x[j];
+                    x[j] = x[k];
+                    x[k] = t;
+                }
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                k = i;
+                for(j=i+1; j<=m-1; j++)
+                {
+                    if( (double)(y[j])<(double)(y[k]) )
+                    {
+                        k = j;
+                    }
+                }
+                if( k!=i )
+                {
+                    for(j=0; j<=n-1; j++)
+                    {
+                        t = f[i,j];
+                        f[i,j] = f[k,j];
+                        f[k,j] = t;
+                    }
+                    t = y[i];
+                    y[i] = y[k];
+                    y[k] = t;
+                }
+            }
+            
+            //
+            // Fill C:
+            //  C[0]            -   length(C)
+            //  C[1]            -   type(C):
+            //                      -1 = bilinear interpolant
+            //                      -3 = general cubic spline
+            //                           (see BuildBicubicSpline)
+            //  C[2]:
+            //      N (x count)
+            //  C[3]:
+            //      M (y count)
+            //  C[4]...C[4+N-1]:
+            //      x[i], i = 0...N-1
+            //  C[4+N]...C[4+N+M-1]:
+            //      y[i], i = 0...M-1
+            //  C[4+N+M]...C[4+N+M+(N*M-1)]:
+            //      f(i,j) table. f(0,0), f(0, 1), f(0,2) and so on...
+            //
+            c.k = 1;
+            tblsize = 4+n+m+n*m;
+            c.c = new double[tblsize-1+1];
+            c.c[0] = tblsize;
+            c.c[1] = -1;
+            c.c[2] = n;
+            c.c[3] = m;
+            for(i=0; i<=n-1; i++)
+            {
+                c.c[4+i] = x[i];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                c.c[4+n+i] = y[i];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    shift = i*n+j;
+                    c.c[4+n+m+shift] = f[i,j];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine builds bicubic spline coefficients table.
+
+        Input parameters:
+            X   -   spline abscissas, array[0..N-1]
+            Y   -   spline ordinates, array[0..M-1]
+            F   -   function values, array[0..M-1,0..N-1]
+            M,N -   grid size, M>=2, N>=2
+
+        Output parameters:
+            C   -   spline interpolant
+
+          -- ALGLIB PROJECT --
+             Copyright 05.07.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dbuildbicubic(double[] x,
+            double[] y,
+            double[,] f,
+            int m,
+            int n,
+            spline2dinterpolant c)
+        {
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int tblsize = 0;
+            int shift = 0;
+            double t = 0;
+            double[,] dx = new double[0,0];
+            double[,] dy = new double[0,0];
+            double[,] dxy = new double[0,0];
+
+            x = (double[])x.Clone();
+            y = (double[])y.Clone();
+            f = (double[,])f.Clone();
+
+            ap.assert(n>=2 & m>=2, "BuildBicubicSpline: N<2 or M<2!");
+            
+            //
+            // Sort points
+            //
+            for(j=0; j<=n-1; j++)
+            {
+                k = j;
+                for(i=j+1; i<=n-1; i++)
+                {
+                    if( (double)(x[i])<(double)(x[k]) )
+                    {
+                        k = i;
+                    }
+                }
+                if( k!=j )
+                {
+                    for(i=0; i<=m-1; i++)
+                    {
+                        t = f[i,j];
+                        f[i,j] = f[i,k];
+                        f[i,k] = t;
+                    }
+                    t = x[j];
+                    x[j] = x[k];
+                    x[k] = t;
+                }
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                k = i;
+                for(j=i+1; j<=m-1; j++)
+                {
+                    if( (double)(y[j])<(double)(y[k]) )
+                    {
+                        k = j;
+                    }
+                }
+                if( k!=i )
+                {
+                    for(j=0; j<=n-1; j++)
+                    {
+                        t = f[i,j];
+                        f[i,j] = f[k,j];
+                        f[k,j] = t;
+                    }
+                    t = y[i];
+                    y[i] = y[k];
+                    y[k] = t;
+                }
+            }
+            
+            //
+            // Fill C:
+            //  C[0]            -   length(C)
+            //  C[1]            -   type(C):
+            //                      -1 = bilinear interpolant
+            //                           (see BuildBilinearInterpolant)
+            //                      -3 = general cubic spline
+            //  C[2]:
+            //      N (x count)
+            //  C[3]:
+            //      M (y count)
+            //  C[4]...C[4+N-1]:
+            //      x[i], i = 0...N-1
+            //  C[4+N]...C[4+N+M-1]:
+            //      y[i], i = 0...M-1
+            //  C[4+N+M]...C[4+N+M+(N*M-1)]:
+            //      f(i,j) table. f(0,0), f(0, 1), f(0,2) and so on...
+            //  C[4+N+M+N*M]...C[4+N+M+(2*N*M-1)]:
+            //      df(i,j)/dx table.
+            //  C[4+N+M+2*N*M]...C[4+N+M+(3*N*M-1)]:
+            //      df(i,j)/dy table.
+            //  C[4+N+M+3*N*M]...C[4+N+M+(4*N*M-1)]:
+            //      d2f(i,j)/dxdy table.
+            //
+            c.k = 3;
+            tblsize = 4+n+m+4*n*m;
+            c.c = new double[tblsize-1+1];
+            c.c[0] = tblsize;
+            c.c[1] = -3;
+            c.c[2] = n;
+            c.c[3] = m;
+            for(i=0; i<=n-1; i++)
+            {
+                c.c[4+i] = x[i];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                c.c[4+n+i] = y[i];
+            }
+            bicubiccalcderivatives(f, x, y, m, n, ref dx, ref dy, ref dxy);
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    shift = i*n+j;
+                    c.c[4+n+m+shift] = f[i,j];
+                    c.c[4+n+m+n*m+shift] = dx[i,j];
+                    c.c[4+n+m+2*n*m+shift] = dy[i,j];
+                    c.c[4+n+m+3*n*m+shift] = dxy[i,j];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine calculates the value of the bilinear or bicubic spline  at
+        the given point X.
+
+        Input parameters:
+            C   -   coefficients table.
+                    Built by BuildBilinearSpline or BuildBicubicSpline.
+            X, Y-   point
+
+        Result:
+            S(x,y)
+
+          -- ALGLIB PROJECT --
+             Copyright 05.07.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static double spline2dcalc(spline2dinterpolant c,
+            double x,
+            double y)
+        {
+            double result = 0;
+            double v = 0;
+            double vx = 0;
+            double vy = 0;
+            double vxy = 0;
+
+            spline2ddiff(c, x, y, ref v, ref vx, ref vy, ref vxy);
+            result = v;
+            return result;
+        }
+
+
+        /*************************************************************************
+        This subroutine calculates the value of the bilinear or bicubic spline  at
+        the given point X and its derivatives.
+
+        Input parameters:
+            C   -   spline interpolant.
+            X, Y-   point
+
+        Output parameters:
+            F   -   S(x,y)
+            FX  -   dS(x,y)/dX
+            FY  -   dS(x,y)/dY
+            FXY -   d2S(x,y)/dXdY
+
+          -- ALGLIB PROJECT --
+             Copyright 05.07.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2ddiff(spline2dinterpolant c,
+            double x,
+            double y,
+            ref double f,
+            ref double fx,
+            ref double fy,
+            ref double fxy)
+        {
+            int n = 0;
+            int m = 0;
+            double t = 0;
+            double dt = 0;
+            double u = 0;
+            double du = 0;
+            int ix = 0;
+            int iy = 0;
+            int l = 0;
+            int r = 0;
+            int h = 0;
+            int shift1 = 0;
+            int s1 = 0;
+            int s2 = 0;
+            int s3 = 0;
+            int s4 = 0;
+            int sf = 0;
+            int sfx = 0;
+            int sfy = 0;
+            int sfxy = 0;
+            double y1 = 0;
+            double y2 = 0;
+            double y3 = 0;
+            double y4 = 0;
+            double v = 0;
+            double t0 = 0;
+            double t1 = 0;
+            double t2 = 0;
+            double t3 = 0;
+            double u0 = 0;
+            double u1 = 0;
+            double u2 = 0;
+            double u3 = 0;
+
+            f = 0;
+            fx = 0;
+            fy = 0;
+            fxy = 0;
+
+            ap.assert((int)Math.Round(c.c[1])==-1 | (int)Math.Round(c.c[1])==-3, "Spline2DDiff: incorrect C!");
+            n = (int)Math.Round(c.c[2]);
+            m = (int)Math.Round(c.c[3]);
+            
+            //
+            // Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
+            //
+            l = 4;
+            r = 4+n-2+1;
+            while( l!=r-1 )
+            {
+                h = (l+r)/2;
+                if( (double)(c.c[h])>=(double)(x) )
+                {
+                    r = h;
+                }
+                else
+                {
+                    l = h;
+                }
+            }
+            t = (x-c.c[l])/(c.c[l+1]-c.c[l]);
+            dt = 1.0/(c.c[l+1]-c.c[l]);
+            ix = l-4;
+            
+            //
+            // Binary search in the [ y[0], ..., y[m-2] ] (y[m-1] is not included)
+            //
+            l = 4+n;
+            r = 4+n+(m-2)+1;
+            while( l!=r-1 )
+            {
+                h = (l+r)/2;
+                if( (double)(c.c[h])>=(double)(y) )
+                {
+                    r = h;
+                }
+                else
+                {
+                    l = h;
+                }
+            }
+            u = (y-c.c[l])/(c.c[l+1]-c.c[l]);
+            du = 1.0/(c.c[l+1]-c.c[l]);
+            iy = l-(4+n);
+            
+            //
+            // Prepare F, dF/dX, dF/dY, d2F/dXdY
+            //
+            f = 0;
+            fx = 0;
+            fy = 0;
+            fxy = 0;
+            
+            //
+            // Bilinear interpolation
+            //
+            if( (int)Math.Round(c.c[1])==-1 )
+            {
+                shift1 = 4+n+m;
+                y1 = c.c[shift1+n*iy+ix];
+                y2 = c.c[shift1+n*iy+(ix+1)];
+                y3 = c.c[shift1+n*(iy+1)+(ix+1)];
+                y4 = c.c[shift1+n*(iy+1)+ix];
+                f = (1-t)*(1-u)*y1+t*(1-u)*y2+t*u*y3+(1-t)*u*y4;
+                fx = (-((1-u)*y1)+(1-u)*y2+u*y3-u*y4)*dt;
+                fy = (-((1-t)*y1)-t*y2+t*y3+(1-t)*y4)*du;
+                fxy = (y1-y2+y3-y4)*du*dt;
+                return;
+            }
+            
+            //
+            // Bicubic interpolation
+            //
+            if( (int)Math.Round(c.c[1])==-3 )
+            {
+                
+                //
+                // Prepare info
+                //
+                t0 = 1;
+                t1 = t;
+                t2 = math.sqr(t);
+                t3 = t*t2;
+                u0 = 1;
+                u1 = u;
+                u2 = math.sqr(u);
+                u3 = u*u2;
+                sf = 4+n+m;
+                sfx = 4+n+m+n*m;
+                sfy = 4+n+m+2*n*m;
+                sfxy = 4+n+m+3*n*m;
+                s1 = n*iy+ix;
+                s2 = n*iy+(ix+1);
+                s3 = n*(iy+1)+(ix+1);
+                s4 = n*(iy+1)+ix;
+                
+                //
+                // Calculate
+                //
+                v = 1*c.c[sf+s1];
+                f = f+v*t0*u0;
+                v = 1*c.c[sfy+s1]/du;
+                f = f+v*t0*u1;
+                fy = fy+1*v*t0*u0*du;
+                v = -(3*c.c[sf+s1])+3*c.c[sf+s4]-2*c.c[sfy+s1]/du-1*c.c[sfy+s4]/du;
+                f = f+v*t0*u2;
+                fy = fy+2*v*t0*u1*du;
+                v = 2*c.c[sf+s1]-2*c.c[sf+s4]+1*c.c[sfy+s1]/du+1*c.c[sfy+s4]/du;
+                f = f+v*t0*u3;
+                fy = fy+3*v*t0*u2*du;
+                v = 1*c.c[sfx+s1]/dt;
+                f = f+v*t1*u0;
+                fx = fx+1*v*t0*u0*dt;
+                v = 1*c.c[sfxy+s1]/(dt*du);
+                f = f+v*t1*u1;
+                fx = fx+1*v*t0*u1*dt;
+                fy = fy+1*v*t1*u0*du;
+                fxy = fxy+1*v*t0*u0*dt*du;
+                v = -(3*c.c[sfx+s1]/dt)+3*c.c[sfx+s4]/dt-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
+                f = f+v*t1*u2;
+                fx = fx+1*v*t0*u2*dt;
+                fy = fy+2*v*t1*u1*du;
+                fxy = fxy+2*v*t0*u1*dt*du;
+                v = 2*c.c[sfx+s1]/dt-2*c.c[sfx+s4]/dt+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
+                f = f+v*t1*u3;
+                fx = fx+1*v*t0*u3*dt;
+                fy = fy+3*v*t1*u2*du;
+                fxy = fxy+3*v*t0*u2*dt*du;
+                v = -(3*c.c[sf+s1])+3*c.c[sf+s2]-2*c.c[sfx+s1]/dt-1*c.c[sfx+s2]/dt;
+                f = f+v*t2*u0;
+                fx = fx+2*v*t1*u0*dt;
+                v = -(3*c.c[sfy+s1]/du)+3*c.c[sfy+s2]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du);
+                f = f+v*t2*u1;
+                fx = fx+2*v*t1*u1*dt;
+                fy = fy+1*v*t2*u0*du;
+                fxy = fxy+2*v*t1*u0*dt*du;
+                v = 9*c.c[sf+s1]-9*c.c[sf+s2]+9*c.c[sf+s3]-9*c.c[sf+s4]+6*c.c[sfx+s1]/dt+3*c.c[sfx+s2]/dt-3*c.c[sfx+s3]/dt-6*c.c[sfx+s4]/dt+6*c.c[sfy+s1]/du-6*c.c[sfy+s2]/du-3*c.c[sfy+s3]/du+3*c.c[sfy+s4]/du+4*c.c[sfxy+s1]/(dt*du)+2*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+2*c.c[sfxy+s4]/(dt*du);
+                f = f+v*t2*u2;
+                fx = fx+2*v*t1*u2*dt;
+                fy = fy+2*v*t2*u1*du;
+                fxy = fxy+4*v*t1*u1*dt*du;
+                v = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-4*c.c[sfx+s1]/dt-2*c.c[sfx+s2]/dt+2*c.c[sfx+s3]/dt+4*c.c[sfx+s4]/dt-3*c.c[sfy+s1]/du+3*c.c[sfy+s2]/du+3*c.c[sfy+s3]/du-3*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-2*c.c[sfxy+s4]/(dt*du);
+                f = f+v*t2*u3;
+                fx = fx+2*v*t1*u3*dt;
+                fy = fy+3*v*t2*u2*du;
+                fxy = fxy+6*v*t1*u2*dt*du;
+                v = 2*c.c[sf+s1]-2*c.c[sf+s2]+1*c.c[sfx+s1]/dt+1*c.c[sfx+s2]/dt;
+                f = f+v*t3*u0;
+                fx = fx+3*v*t2*u0*dt;
+                v = 2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du);
+                f = f+v*t3*u1;
+                fx = fx+3*v*t2*u1*dt;
+                fy = fy+1*v*t3*u0*du;
+                fxy = fxy+3*v*t2*u0*dt*du;
+                v = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-3*c.c[sfx+s1]/dt-3*c.c[sfx+s2]/dt+3*c.c[sfx+s3]/dt+3*c.c[sfx+s4]/dt-4*c.c[sfy+s1]/du+4*c.c[sfy+s2]/du+2*c.c[sfy+s3]/du-2*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-2*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
+                f = f+v*t3*u2;
+                fx = fx+3*v*t2*u2*dt;
+                fy = fy+2*v*t3*u1*du;
+                fxy = fxy+6*v*t2*u1*dt*du;
+                v = 4*c.c[sf+s1]-4*c.c[sf+s2]+4*c.c[sf+s3]-4*c.c[sf+s4]+2*c.c[sfx+s1]/dt+2*c.c[sfx+s2]/dt-2*c.c[sfx+s3]/dt-2*c.c[sfx+s4]/dt+2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du-2*c.c[sfy+s3]/du+2*c.c[sfy+s4]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
+                f = f+v*t3*u3;
+                fx = fx+3*v*t2*u3*dt;
+                fy = fy+3*v*t3*u2*du;
+                fxy = fxy+9*v*t2*u2*dt*du;
+                return;
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine unpacks two-dimensional spline into the coefficients table
+
+        Input parameters:
+            C   -   spline interpolant.
+
+        Result:
+            M, N-   grid size (x-axis and y-axis)
+            Tbl -   coefficients table, unpacked format,
+                    [0..(N-1)*(M-1)-1, 0..19].
+                    For I = 0...M-2, J=0..N-2:
+                        K =  I*(N-1)+J
+                        Tbl[K,0] = X[j]
+                        Tbl[K,1] = X[j+1]
+                        Tbl[K,2] = Y[i]
+                        Tbl[K,3] = Y[i+1]
+                        Tbl[K,4] = C00
+                        Tbl[K,5] = C01
+                        Tbl[K,6] = C02
+                        Tbl[K,7] = C03
+                        Tbl[K,8] = C10
+                        Tbl[K,9] = C11
+                        ...
+                        Tbl[K,19] = C33
+                    On each grid square spline is equals to:
+                        S(x) = SUM(c[i,j]*(x^i)*(y^j), i=0..3, j=0..3)
+                        t = x-x[j]
+                        u = y-y[i]
+
+          -- ALGLIB PROJECT --
+             Copyright 29.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dunpack(spline2dinterpolant c,
+            ref int m,
+            ref int n,
+            ref double[,] tbl)
+        {
+            int i = 0;
+            int j = 0;
+            int ci = 0;
+            int cj = 0;
+            int k = 0;
+            int p = 0;
+            int shift = 0;
+            int s1 = 0;
+            int s2 = 0;
+            int s3 = 0;
+            int s4 = 0;
+            int sf = 0;
+            int sfx = 0;
+            int sfy = 0;
+            int sfxy = 0;
+            double y1 = 0;
+            double y2 = 0;
+            double y3 = 0;
+            double y4 = 0;
+            double dt = 0;
+            double du = 0;
+
+            m = 0;
+            n = 0;
+            tbl = new double[0,0];
+
+            ap.assert((int)Math.Round(c.c[1])==-3 | (int)Math.Round(c.c[1])==-1, "SplineUnpack2D: incorrect C!");
+            n = (int)Math.Round(c.c[2]);
+            m = (int)Math.Round(c.c[3]);
+            tbl = new double[(n-1)*(m-1)-1+1, 19+1];
+            
+            //
+            // Fill
+            //
+            for(i=0; i<=m-2; i++)
+            {
+                for(j=0; j<=n-2; j++)
+                {
+                    p = i*(n-1)+j;
+                    tbl[p,0] = c.c[4+j];
+                    tbl[p,1] = c.c[4+j+1];
+                    tbl[p,2] = c.c[4+n+i];
+                    tbl[p,3] = c.c[4+n+i+1];
+                    dt = 1/(tbl[p,1]-tbl[p,0]);
+                    du = 1/(tbl[p,3]-tbl[p,2]);
+                    
+                    //
+                    // Bilinear interpolation
+                    //
+                    if( (int)Math.Round(c.c[1])==-1 )
+                    {
+                        for(k=4; k<=19; k++)
+                        {
+                            tbl[p,k] = 0;
+                        }
+                        shift = 4+n+m;
+                        y1 = c.c[shift+n*i+j];
+                        y2 = c.c[shift+n*i+(j+1)];
+                        y3 = c.c[shift+n*(i+1)+(j+1)];
+                        y4 = c.c[shift+n*(i+1)+j];
+                        tbl[p,4] = y1;
+                        tbl[p,4+1*4+0] = y2-y1;
+                        tbl[p,4+0*4+1] = y4-y1;
+                        tbl[p,4+1*4+1] = y3-y2-y4+y1;
+                    }
+                    
+                    //
+                    // Bicubic interpolation
+                    //
+                    if( (int)Math.Round(c.c[1])==-3 )
+                    {
+                        sf = 4+n+m;
+                        sfx = 4+n+m+n*m;
+                        sfy = 4+n+m+2*n*m;
+                        sfxy = 4+n+m+3*n*m;
+                        s1 = n*i+j;
+                        s2 = n*i+(j+1);
+                        s3 = n*(i+1)+(j+1);
+                        s4 = n*(i+1)+j;
+                        tbl[p,4+0*4+0] = 1*c.c[sf+s1];
+                        tbl[p,4+0*4+1] = 1*c.c[sfy+s1]/du;
+                        tbl[p,4+0*4+2] = -(3*c.c[sf+s1])+3*c.c[sf+s4]-2*c.c[sfy+s1]/du-1*c.c[sfy+s4]/du;
+                        tbl[p,4+0*4+3] = 2*c.c[sf+s1]-2*c.c[sf+s4]+1*c.c[sfy+s1]/du+1*c.c[sfy+s4]/du;
+                        tbl[p,4+1*4+0] = 1*c.c[sfx+s1]/dt;
+                        tbl[p,4+1*4+1] = 1*c.c[sfxy+s1]/(dt*du);
+                        tbl[p,4+1*4+2] = -(3*c.c[sfx+s1]/dt)+3*c.c[sfx+s4]/dt-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
+                        tbl[p,4+1*4+3] = 2*c.c[sfx+s1]/dt-2*c.c[sfx+s4]/dt+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
+                        tbl[p,4+2*4+0] = -(3*c.c[sf+s1])+3*c.c[sf+s2]-2*c.c[sfx+s1]/dt-1*c.c[sfx+s2]/dt;
+                        tbl[p,4+2*4+1] = -(3*c.c[sfy+s1]/du)+3*c.c[sfy+s2]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du);
+                        tbl[p,4+2*4+2] = 9*c.c[sf+s1]-9*c.c[sf+s2]+9*c.c[sf+s3]-9*c.c[sf+s4]+6*c.c[sfx+s1]/dt+3*c.c[sfx+s2]/dt-3*c.c[sfx+s3]/dt-6*c.c[sfx+s4]/dt+6*c.c[sfy+s1]/du-6*c.c[sfy+s2]/du-3*c.c[sfy+s3]/du+3*c.c[sfy+s4]/du+4*c.c[sfxy+s1]/(dt*du)+2*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+2*c.c[sfxy+s4]/(dt*du);
+                        tbl[p,4+2*4+3] = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-4*c.c[sfx+s1]/dt-2*c.c[sfx+s2]/dt+2*c.c[sfx+s3]/dt+4*c.c[sfx+s4]/dt-3*c.c[sfy+s1]/du+3*c.c[sfy+s2]/du+3*c.c[sfy+s3]/du-3*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-1*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-2*c.c[sfxy+s4]/(dt*du);
+                        tbl[p,4+3*4+0] = 2*c.c[sf+s1]-2*c.c[sf+s2]+1*c.c[sfx+s1]/dt+1*c.c[sfx+s2]/dt;
+                        tbl[p,4+3*4+1] = 2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du);
+                        tbl[p,4+3*4+2] = -(6*c.c[sf+s1])+6*c.c[sf+s2]-6*c.c[sf+s3]+6*c.c[sf+s4]-3*c.c[sfx+s1]/dt-3*c.c[sfx+s2]/dt+3*c.c[sfx+s3]/dt+3*c.c[sfx+s4]/dt-4*c.c[sfy+s1]/du+4*c.c[sfy+s2]/du+2*c.c[sfy+s3]/du-2*c.c[sfy+s4]/du-2*c.c[sfxy+s1]/(dt*du)-2*c.c[sfxy+s2]/(dt*du)-1*c.c[sfxy+s3]/(dt*du)-1*c.c[sfxy+s4]/(dt*du);
+                        tbl[p,4+3*4+3] = 4*c.c[sf+s1]-4*c.c[sf+s2]+4*c.c[sf+s3]-4*c.c[sf+s4]+2*c.c[sfx+s1]/dt+2*c.c[sfx+s2]/dt-2*c.c[sfx+s3]/dt-2*c.c[sfx+s4]/dt+2*c.c[sfy+s1]/du-2*c.c[sfy+s2]/du-2*c.c[sfy+s3]/du+2*c.c[sfy+s4]/du+1*c.c[sfxy+s1]/(dt*du)+1*c.c[sfxy+s2]/(dt*du)+1*c.c[sfxy+s3]/(dt*du)+1*c.c[sfxy+s4]/(dt*du);
+                    }
+                    
+                    //
+                    // Rescale Cij
+                    //
+                    for(ci=0; ci<=3; ci++)
+                    {
+                        for(cj=0; cj<=3; cj++)
+                        {
+                            tbl[p,4+ci*4+cj] = tbl[p,4+ci*4+cj]*Math.Pow(dt, ci)*Math.Pow(du, cj);
+                        }
+                    }
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine performs linear transformation of the spline argument.
+
+        Input parameters:
+            C       -   spline interpolant
+            AX, BX  -   transformation coefficients: x = A*t + B
+            AY, BY  -   transformation coefficients: y = A*u + B
+        Result:
+            C   -   transformed spline
+
+          -- ALGLIB PROJECT --
+             Copyright 30.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dlintransxy(spline2dinterpolant c,
+            double ax,
+            double bx,
+            double ay,
+            double by)
+        {
+            int i = 0;
+            int j = 0;
+            int n = 0;
+            int m = 0;
+            double v = 0;
+            double[] x = new double[0];
+            double[] y = new double[0];
+            double[,] f = new double[0,0];
+            int typec = 0;
+
+            typec = (int)Math.Round(c.c[1]);
+            ap.assert(typec==-3 | typec==-1, "Spline2DLinTransXY: incorrect C!");
+            n = (int)Math.Round(c.c[2]);
+            m = (int)Math.Round(c.c[3]);
+            x = new double[n-1+1];
+            y = new double[m-1+1];
+            f = new double[m-1+1, n-1+1];
+            for(j=0; j<=n-1; j++)
+            {
+                x[j] = c.c[4+j];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                y[i] = c.c[4+n+i];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    f[i,j] = c.c[4+n+m+i*n+j];
+                }
+            }
+            
+            //
+            // Special case: AX=0 or AY=0
+            //
+            if( (double)(ax)==(double)(0) )
+            {
+                for(i=0; i<=m-1; i++)
+                {
+                    v = spline2dcalc(c, bx, y[i]);
+                    for(j=0; j<=n-1; j++)
+                    {
+                        f[i,j] = v;
+                    }
+                }
+                if( typec==-3 )
+                {
+                    spline2dbuildbicubic(x, y, f, m, n, c);
+                }
+                if( typec==-1 )
+                {
+                    spline2dbuildbilinear(x, y, f, m, n, c);
+                }
+                ax = 1;
+                bx = 0;
+            }
+            if( (double)(ay)==(double)(0) )
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    v = spline2dcalc(c, x[j], by);
+                    for(i=0; i<=m-1; i++)
+                    {
+                        f[i,j] = v;
+                    }
+                }
+                if( typec==-3 )
+                {
+                    spline2dbuildbicubic(x, y, f, m, n, c);
+                }
+                if( typec==-1 )
+                {
+                    spline2dbuildbilinear(x, y, f, m, n, c);
+                }
+                ay = 1;
+                by = 0;
+            }
+            
+            //
+            // General case: AX<>0, AY<>0
+            // Unpack, scale and pack again.
+            //
+            for(j=0; j<=n-1; j++)
+            {
+                x[j] = (x[j]-bx)/ax;
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                y[i] = (y[i]-by)/ay;
+            }
+            if( typec==-3 )
+            {
+                spline2dbuildbicubic(x, y, f, m, n, c);
+            }
+            if( typec==-1 )
+            {
+                spline2dbuildbilinear(x, y, f, m, n, c);
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine performs linear transformation of the spline.
+
+        Input parameters:
+            C   -   spline interpolant.
+            A, B-   transformation coefficients: S2(x,y) = A*S(x,y) + B
+            
+        Output parameters:
+            C   -   transformed spline
+
+          -- ALGLIB PROJECT --
+             Copyright 30.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dlintransf(spline2dinterpolant c,
+            double a,
+            double b)
+        {
+            int i = 0;
+            int j = 0;
+            int n = 0;
+            int m = 0;
+            double[] x = new double[0];
+            double[] y = new double[0];
+            double[,] f = new double[0,0];
+            int typec = 0;
+
+            typec = (int)Math.Round(c.c[1]);
+            ap.assert(typec==-3 | typec==-1, "Spline2DLinTransXY: incorrect C!");
+            n = (int)Math.Round(c.c[2]);
+            m = (int)Math.Round(c.c[3]);
+            x = new double[n-1+1];
+            y = new double[m-1+1];
+            f = new double[m-1+1, n-1+1];
+            for(j=0; j<=n-1; j++)
+            {
+                x[j] = c.c[4+j];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                y[i] = c.c[4+n+i];
+            }
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    f[i,j] = a*c.c[4+n+m+i*n+j]+b;
+                }
+            }
+            if( typec==-3 )
+            {
+                spline2dbuildbicubic(x, y, f, m, n, c);
+            }
+            if( typec==-1 )
+            {
+                spline2dbuildbilinear(x, y, f, m, n, c);
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine makes the copy of the spline model.
+
+        Input parameters:
+            C   -   spline interpolant
+
+        Output parameters:
+            CC  -   spline copy
+
+          -- ALGLIB PROJECT --
+             Copyright 29.06.2007 by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dcopy(spline2dinterpolant c,
+            spline2dinterpolant cc)
+        {
+            int n = 0;
+            int i_ = 0;
+
+            ap.assert(c.k==1 | c.k==3, "Spline2DCopy: incorrect C!");
+            cc.k = c.k;
+            n = (int)Math.Round(c.c[0]);
+            cc.c = new double[n];
+            for(i_=0; i_<=n-1;i_++)
+            {
+                cc.c[i_] = c.c[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        Bicubic spline resampling
+
+        Input parameters:
+            A           -   function values at the old grid,
+                            array[0..OldHeight-1, 0..OldWidth-1]
+            OldHeight   -   old grid height, OldHeight>1
+            OldWidth    -   old grid width, OldWidth>1
+            NewHeight   -   new grid height, NewHeight>1
+            NewWidth    -   new grid width, NewWidth>1
+            
+        Output parameters:
+            B           -   function values at the new grid,
+                            array[0..NewHeight-1, 0..NewWidth-1]
+
+          -- ALGLIB routine --
+             15 May, 2007
+             Copyright by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dresamplebicubic(double[,] a,
+            int oldheight,
+            int oldwidth,
+            ref double[,] b,
+            int newheight,
+            int newwidth)
+        {
+            double[,] buf = new double[0,0];
+            double[] x = new double[0];
+            double[] y = new double[0];
+            spline1d.spline1dinterpolant c = new spline1d.spline1dinterpolant();
+            int i = 0;
+            int j = 0;
+            int mw = 0;
+            int mh = 0;
+
+            b = new double[0,0];
+
+            ap.assert(oldwidth>1 & oldheight>1, "Spline2DResampleBicubic: width/height less than 1");
+            ap.assert(newwidth>1 & newheight>1, "Spline2DResampleBicubic: width/height less than 1");
+            
+            //
+            // Prepare
+            //
+            mw = Math.Max(oldwidth, newwidth);
+            mh = Math.Max(oldheight, newheight);
+            b = new double[newheight-1+1, newwidth-1+1];
+            buf = new double[oldheight-1+1, newwidth-1+1];
+            x = new double[Math.Max(mw, mh)-1+1];
+            y = new double[Math.Max(mw, mh)-1+1];
+            
+            //
+            // Horizontal interpolation
+            //
+            for(i=0; i<=oldheight-1; i++)
+            {
+                
+                //
+                // Fill X, Y
+                //
+                for(j=0; j<=oldwidth-1; j++)
+                {
+                    x[j] = (double)j/(double)(oldwidth-1);
+                    y[j] = a[i,j];
+                }
+                
+                //
+                // Interpolate and place result into temporary matrix
+                //
+                spline1d.spline1dbuildcubic(x, y, oldwidth, 0, 0.0, 0, 0.0, c);
+                for(j=0; j<=newwidth-1; j++)
+                {
+                    buf[i,j] = spline1d.spline1dcalc(c, (double)j/(double)(newwidth-1));
+                }
+            }
+            
+            //
+            // Vertical interpolation
+            //
+            for(j=0; j<=newwidth-1; j++)
+            {
+                
+                //
+                // Fill X, Y
+                //
+                for(i=0; i<=oldheight-1; i++)
+                {
+                    x[i] = (double)i/(double)(oldheight-1);
+                    y[i] = buf[i,j];
+                }
+                
+                //
+                // Interpolate and place result into B
+                //
+                spline1d.spline1dbuildcubic(x, y, oldheight, 0, 0.0, 0, 0.0, c);
+                for(i=0; i<=newheight-1; i++)
+                {
+                    b[i,j] = spline1d.spline1dcalc(c, (double)i/(double)(newheight-1));
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Bilinear spline resampling
+
+        Input parameters:
+            A           -   function values at the old grid,
+                            array[0..OldHeight-1, 0..OldWidth-1]
+            OldHeight   -   old grid height, OldHeight>1
+            OldWidth    -   old grid width, OldWidth>1
+            NewHeight   -   new grid height, NewHeight>1
+            NewWidth    -   new grid width, NewWidth>1
+
+        Output parameters:
+            B           -   function values at the new grid,
+                            array[0..NewHeight-1, 0..NewWidth-1]
+
+          -- ALGLIB routine --
+             09.07.2007
+             Copyright by Bochkanov Sergey
+        *************************************************************************/
+        public static void spline2dresamplebilinear(double[,] a,
+            int oldheight,
+            int oldwidth,
+            ref double[,] b,
+            int newheight,
+            int newwidth)
+        {
+            int i = 0;
+            int j = 0;
+            int l = 0;
+            int c = 0;
+            double t = 0;
+            double u = 0;
+
+            b = new double[0,0];
+
+            b = new double[newheight-1+1, newwidth-1+1];
+            for(i=0; i<=newheight-1; i++)
+            {
+                for(j=0; j<=newwidth-1; j++)
+                {
+                    l = i*(oldheight-1)/(newheight-1);
+                    if( l==oldheight-1 )
+                    {
+                        l = oldheight-2;
+                    }
+                    u = (double)i/(double)(newheight-1)*(oldheight-1)-l;
+                    c = j*(oldwidth-1)/(newwidth-1);
+                    if( c==oldwidth-1 )
+                    {
+                        c = oldwidth-2;
+                    }
+                    t = (double)(j*(oldwidth-1))/(double)(newwidth-1)-c;
+                    b[i,j] = (1-t)*(1-u)*a[l,c]+t*(1-u)*a[l,c+1]+t*u*a[l+1,c+1]+(1-t)*u*a[l+1,c];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Internal subroutine.
+        Calculation of the first derivatives and the cross-derivative.
+        *************************************************************************/
+        private static void bicubiccalcderivatives(double[,] a,
+            double[] x,
+            double[] y,
+            int m,
+            int n,
+            ref double[,] dx,
+            ref double[,] dy,
+            ref double[,] dxy)
+        {
+            int i = 0;
+            int j = 0;
+            double[] xt = new double[0];
+            double[] ft = new double[0];
+            double s = 0;
+            double ds = 0;
+            double d2s = 0;
+            spline1d.spline1dinterpolant c = new spline1d.spline1dinterpolant();
+
+            dx = new double[0,0];
+            dy = new double[0,0];
+            dxy = new double[0,0];
+
+            dx = new double[m, n];
+            dy = new double[m, n];
+            dxy = new double[m, n];
+            
+            //
+            // dF/dX
+            //
+            xt = new double[n];
+            ft = new double[n];
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    xt[j] = x[j];
+                    ft[j] = a[i,j];
+                }
+                spline1d.spline1dbuildcubic(xt, ft, n, 0, 0.0, 0, 0.0, c);
+                for(j=0; j<=n-1; j++)
+                {
+                    spline1d.spline1ddiff(c, x[j], ref s, ref ds, ref d2s);
+                    dx[i,j] = ds;
+                }
+            }
+            
+            //
+            // dF/dY
+            //
+            xt = new double[m];
+            ft = new double[m];
+            for(j=0; j<=n-1; j++)
+            {
+                for(i=0; i<=m-1; i++)
+                {
+                    xt[i] = y[i];
+                    ft[i] = a[i,j];
+                }
+                spline1d.spline1dbuildcubic(xt, ft, m, 0, 0.0, 0, 0.0, c);
+                for(i=0; i<=m-1; i++)
+                {
+                    spline1d.spline1ddiff(c, y[i], ref s, ref ds, ref d2s);
+                    dy[i,j] = ds;
+                }
+            }
+            
+            //
+            // d2F/dXdY
+            //
+            xt = new double[n];
+            ft = new double[n];
+            for(i=0; i<=m-1; i++)
+            {
+                for(j=0; j<=n-1; j++)
+                {
+                    xt[j] = x[j];
+                    ft[j] = dy[i,j];
+                }
+                spline1d.spline1dbuildcubic(xt, ft, n, 0, 0.0, 0, 0.0, c);
+                for(j=0; j<=n-1; j++)
+                {
+                    spline1d.spline1ddiff(c, x[j], ref s, ref ds, ref d2s);
+                    dxy[i,j] = ds;
+                }
+            }
+        }
+
+
+    }
 }
 
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/linalg.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/linalg.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/linalg.cs	(revision 7294)
@@ -1328,9 +1328,9 @@
                     Array whose indexes range within [0..N-1, 0..N-1].
         N       -   size of matrix A.
-        IsUpper -   storage format.
         ZNeeded -   flag controlling whether the eigenvectors are needed or not.
                     If ZNeeded is equal to:
                      * 0, the eigenvectors are not returned;
                      * 1, the eigenvectors are returned.
+        IsUpper -   storage format.
 
     Output parameters:
@@ -10960,9 +10960,9 @@
                         Array whose indexes range within [0..N-1, 0..N-1].
             N       -   size of matrix A.
-            IsUpper -   storage format.
             ZNeeded -   flag controlling whether the eigenvectors are needed or not.
                         If ZNeeded is equal to:
                          * 0, the eigenvectors are not returned;
                          * 1, the eigenvectors are returned.
+            IsUpper -   storage format.
 
         Output parameters:
@@ -17368,4 +17368,17 @@
     public class matgen
     {
+        /*************************************************************************
+        Generation of a random uniformly distributed (Haar) orthogonal matrix
+
+        INPUT PARAMETERS:
+            N   -   matrix size, N>=1
+            
+        OUTPUT PARAMETERS:
+            A   -   orthogonal NxN matrix, array[0..N-1,0..N-1]
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixrndorthogonal(int n,
             ref double[,] a)
@@ -17396,4 +17409,18 @@
 
 
+        /*************************************************************************
+        Generation of random NxN matrix with given condition number and norm2(A)=1
+
+        INPUT PARAMETERS:
+            N   -   matrix size
+            C   -   condition number (in 2-norm)
+
+        OUTPUT PARAMETERS:
+            A   -   random matrix with norm2(A)=1 and cond(A)=C
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixrndcond(int n,
             double c,
@@ -17438,4 +17465,17 @@
 
 
+        /*************************************************************************
+        Generation of a random Haar distributed orthogonal complex matrix
+
+        INPUT PARAMETERS:
+            N   -   matrix size, N>=1
+
+        OUTPUT PARAMETERS:
+            A   -   orthogonal NxN matrix, array[0..N-1,0..N-1]
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void cmatrixrndorthogonal(int n,
             ref complex[,] a)
@@ -17466,4 +17506,19 @@
 
 
+        /*************************************************************************
+        Generation of random NxN complex matrix with given condition number C and
+        norm2(A)=1
+
+        INPUT PARAMETERS:
+            N   -   matrix size
+            C   -   condition number (in 2-norm)
+
+        OUTPUT PARAMETERS:
+            A   -   random matrix with norm2(A)=1 and cond(A)=C
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void cmatrixrndcond(int n,
             double c,
@@ -17512,4 +17567,19 @@
 
 
+        /*************************************************************************
+        Generation of random NxN symmetric matrix with given condition number  and
+        norm2(A)=1
+
+        INPUT PARAMETERS:
+            N   -   matrix size
+            C   -   condition number (in 2-norm)
+
+        OUTPUT PARAMETERS:
+            A   -   random matrix with norm2(A)=1 and cond(A)=C
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void smatrixrndcond(int n,
             double c,
@@ -17561,4 +17631,19 @@
 
 
+        /*************************************************************************
+        Generation of random NxN symmetric positive definite matrix with given
+        condition number and norm2(A)=1
+
+        INPUT PARAMETERS:
+            N   -   matrix size
+            C   -   condition number (in 2-norm)
+
+        OUTPUT PARAMETERS:
+            A   -   random SPD matrix with norm2(A)=1 and cond(A)=C
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void spdmatrixrndcond(int n,
             double c,
@@ -17613,4 +17698,19 @@
 
 
+        /*************************************************************************
+        Generation of random NxN Hermitian matrix with given condition number  and
+        norm2(A)=1
+
+        INPUT PARAMETERS:
+            N   -   matrix size
+            C   -   condition number (in 2-norm)
+
+        OUTPUT PARAMETERS:
+            A   -   random matrix with norm2(A)=1 and cond(A)=C
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void hmatrixrndcond(int n,
             double c,
@@ -17670,4 +17770,19 @@
 
 
+        /*************************************************************************
+        Generation of random NxN Hermitian positive definite matrix with given
+        condition number and norm2(A)=1
+
+        INPUT PARAMETERS:
+            N   -   matrix size
+            C   -   condition number (in 2-norm)
+
+        OUTPUT PARAMETERS:
+            A   -   random HPD matrix with norm2(A)=1 and cond(A)=C
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void hpdmatrixrndcond(int n,
             double c,
@@ -17730,4 +17845,18 @@
 
 
+        /*************************************************************************
+        Multiplication of MxN matrix by NxN random Haar distributed orthogonal matrix
+
+        INPUT PARAMETERS:
+            A   -   matrix, array[0..M-1, 0..N-1]
+            M, N-   matrix size
+
+        OUTPUT PARAMETERS:
+            A   -   A*Q, where Q is random NxN orthogonal matrix
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixrndorthogonalfromtheright(ref double[,] a,
             int m,
@@ -17816,4 +17945,18 @@
 
 
+        /*************************************************************************
+        Multiplication of MxN matrix by MxM random Haar distributed orthogonal matrix
+
+        INPUT PARAMETERS:
+            A   -   matrix, array[0..M-1, 0..N-1]
+            M, N-   matrix size
+
+        OUTPUT PARAMETERS:
+            A   -   Q*A, where Q is random MxM orthogonal matrix
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixrndorthogonalfromtheleft(ref double[,] a,
             int m,
@@ -17903,4 +18046,19 @@
 
 
+        /*************************************************************************
+        Multiplication of MxN complex matrix by NxN random Haar distributed
+        complex orthogonal matrix
+
+        INPUT PARAMETERS:
+            A   -   matrix, array[0..M-1, 0..N-1]
+            M, N-   matrix size
+
+        OUTPUT PARAMETERS:
+            A   -   A*Q, where Q is random NxN orthogonal matrix
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void cmatrixrndorthogonalfromtheright(ref complex[,] a,
             int m,
@@ -17982,4 +18140,19 @@
 
 
+        /*************************************************************************
+        Multiplication of MxN complex matrix by MxM random Haar distributed
+        complex orthogonal matrix
+
+        INPUT PARAMETERS:
+            A   -   matrix, array[0..M-1, 0..N-1]
+            M, N-   matrix size
+
+        OUTPUT PARAMETERS:
+            A   -   Q*A, where Q is random MxM orthogonal matrix
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void cmatrixrndorthogonalfromtheleft(ref complex[,] a,
             int m,
@@ -18062,4 +18235,19 @@
 
 
+        /*************************************************************************
+        Symmetric multiplication of NxN matrix by random Haar distributed
+        orthogonal  matrix
+
+        INPUT PARAMETERS:
+            A   -   matrix, array[0..N-1, 0..N-1]
+            N   -   matrix size
+
+        OUTPUT PARAMETERS:
+            A   -   Q'*A*Q, where Q is random NxN orthogonal matrix
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void smatrixrndmultiply(ref double[,] a,
             int n)
@@ -18137,4 +18325,19 @@
 
 
+        /*************************************************************************
+        Hermitian multiplication of NxN matrix by random Haar distributed
+        complex orthogonal matrix
+
+        INPUT PARAMETERS:
+            A   -   matrix, array[0..N-1, 0..N-1]
+            N   -   matrix size
+
+        OUTPUT PARAMETERS:
+            A   -   Q^H*A*Q, where Q is random NxN orthogonal matrix
+
+          -- ALGLIB routine --
+             04.12.2009
+             Bochkanov Sergey
+        *************************************************************************/
         public static void hmatrixrndmultiply(ref complex[,] a,
             int n)
@@ -24752,4 +24955,82 @@
     public class bdsvd
     {
+        /*************************************************************************
+        Singular value decomposition of a bidiagonal matrix (extended algorithm)
+
+        The algorithm performs the singular value decomposition  of  a  bidiagonal
+        matrix B (upper or lower) representing it as B = Q*S*P^T, where Q and  P -
+        orthogonal matrices, S - diagonal matrix with non-negative elements on the
+        main diagonal, in descending order.
+
+        The  algorithm  finds  singular  values.  In  addition,  the algorithm can
+        calculate  matrices  Q  and P (more precisely, not the matrices, but their
+        product  with  given  matrices U and VT - U*Q and (P^T)*VT)).  Of  course,
+        matrices U and VT can be of any type, including identity. Furthermore, the
+        algorithm can calculate Q'*C (this product is calculated more  effectively
+        than U*Q,  because  this calculation operates with rows instead  of matrix
+        columns).
+
+        The feature of the algorithm is its ability to find  all  singular  values
+        including those which are arbitrarily close to 0  with  relative  accuracy
+        close to  machine precision. If the parameter IsFractionalAccuracyRequired
+        is set to True, all singular values will have high relative accuracy close
+        to machine precision. If the parameter is set to False, only  the  biggest
+        singular value will have relative accuracy  close  to  machine  precision.
+        The absolute error of other singular values is equal to the absolute error
+        of the biggest singular value.
+
+        Input parameters:
+            D       -   main diagonal of matrix B.
+                        Array whose index ranges within [0..N-1].
+            E       -   superdiagonal (or subdiagonal) of matrix B.
+                        Array whose index ranges within [0..N-2].
+            N       -   size of matrix B.
+            IsUpper -   True, if the matrix is upper bidiagonal.
+            IsFractionalAccuracyRequired -
+                        accuracy to search singular values with.
+            U       -   matrix to be multiplied by Q.
+                        Array whose indexes range within [0..NRU-1, 0..N-1].
+                        The matrix can be bigger, in that case only the  submatrix
+                        [0..NRU-1, 0..N-1] will be multiplied by Q.
+            NRU     -   number of rows in matrix U.
+            C       -   matrix to be multiplied by Q'.
+                        Array whose indexes range within [0..N-1, 0..NCC-1].
+                        The matrix can be bigger, in that case only the  submatrix
+                        [0..N-1, 0..NCC-1] will be multiplied by Q'.
+            NCC     -   number of columns in matrix C.
+            VT      -   matrix to be multiplied by P^T.
+                        Array whose indexes range within [0..N-1, 0..NCVT-1].
+                        The matrix can be bigger, in that case only the  submatrix
+                        [0..N-1, 0..NCVT-1] will be multiplied by P^T.
+            NCVT    -   number of columns in matrix VT.
+
+        Output parameters:
+            D       -   singular values of matrix B in descending order.
+            U       -   if NRU>0, contains matrix U*Q.
+            VT      -   if NCVT>0, contains matrix (P^T)*VT.
+            C       -   if NCC>0, contains matrix Q'*C.
+
+        Result:
+            True, if the algorithm has converged.
+            False, if the algorithm hasn't converged (rare case).
+
+        Additional information:
+            The type of convergence is controlled by the internal  parameter  TOL.
+            If the parameter is greater than 0, the singular values will have
+            relative accuracy TOL. If TOL<0, the singular values will have
+            absolute accuracy ABS(TOL)*norm(B).
+            By default, |TOL| falls within the range of 10*Epsilon and 100*Epsilon,
+            where Epsilon is the machine precision. It is not  recommended  to  use
+            TOL less than 10*Epsilon since this will  considerably  slow  down  the
+            algorithm and may not lead to error decreasing.
+        History:
+            * 31 March, 2007.
+                changed MAXITR from 6 to 12.
+
+          -- LAPACK routine (version 3.0) --
+             Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
+             Courant Institute, Argonne National Lab, and Rice University
+             October 31, 1999.
+        *************************************************************************/
         public static bool rmatrixbdsvd(ref double[] d,
             double[] e,
@@ -24818,4 +25099,7 @@
 
 
+        /*************************************************************************
+        Internal working subroutine for bidiagonal decomposition
+        *************************************************************************/
         private static bool bidiagonalsvddecompositioninternal(ref double[] d,
             double[] e,
@@ -28066,4 +28350,25 @@
     public class inverseupdate
     {
+        /*************************************************************************
+        Inverse matrix update by the Sherman-Morrison formula
+
+        The algorithm updates matrix A^-1 when adding a number to an element
+        of matrix A.
+
+        Input parameters:
+            InvA    -   inverse of matrix A.
+                        Array whose indexes range within [0..N-1, 0..N-1].
+            N       -   size of matrix A.
+            UpdRow  -   row where the element to be updated is stored.
+            UpdColumn - column where the element to be updated is stored.
+            UpdVal  -   a number to be added to the element.
+
+
+        Output parameters:
+            InvA    -   inverse of modified matrix A.
+
+          -- ALGLIB --
+             Copyright 2005 by Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixinvupdatesimple(ref double[,] inva,
             int n,
@@ -28120,4 +28425,25 @@
 
 
+        /*************************************************************************
+        Inverse matrix update by the Sherman-Morrison formula
+
+        The algorithm updates matrix A^-1 when adding a vector to a row
+        of matrix A.
+
+        Input parameters:
+            InvA    -   inverse of matrix A.
+                        Array whose indexes range within [0..N-1, 0..N-1].
+            N       -   size of matrix A.
+            UpdRow  -   the row of A whose vector V was added.
+                        0 <= Row <= N-1
+            V       -   the vector to be added to a row.
+                        Array whose index ranges within [0..N-1].
+
+        Output parameters:
+            InvA    -   inverse of modified matrix A.
+
+          -- ALGLIB --
+             Copyright 2005 by Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixinvupdaterow(ref double[,] inva,
             int n,
@@ -28173,4 +28499,25 @@
 
 
+        /*************************************************************************
+        Inverse matrix update by the Sherman-Morrison formula
+
+        The algorithm updates matrix A^-1 when adding a vector to a column
+        of matrix A.
+
+        Input parameters:
+            InvA        -   inverse of matrix A.
+                            Array whose indexes range within [0..N-1, 0..N-1].
+            N           -   size of matrix A.
+            UpdColumn   -   the column of A whose vector U was added.
+                            0 <= UpdColumn <= N-1
+            U           -   the vector to be added to a column.
+                            Array whose index ranges within [0..N-1].
+
+        Output parameters:
+            InvA        -   inverse of modified matrix A.
+
+          -- ALGLIB --
+             Copyright 2005 by Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixinvupdatecolumn(ref double[,] inva,
             int n,
@@ -28225,4 +28572,25 @@
 
 
+        /*************************************************************************
+        Inverse matrix update by the Sherman-Morrison formula
+
+        The algorithm computes the inverse of matrix A+u*v by using the given matrix
+        A^-1 and the vectors u and v.
+
+        Input parameters:
+            InvA    -   inverse of matrix A.
+                        Array whose indexes range within [0..N-1, 0..N-1].
+            N       -   size of matrix A.
+            U       -   the vector modifying the matrix.
+                        Array whose index ranges within [0..N-1].
+            V       -   the vector modifying the matrix.
+                        Array whose index ranges within [0..N-1].
+
+        Output parameters:
+            InvA - inverse of matrix A + u*v'.
+
+          -- ALGLIB --
+             Copyright 2005 by Bochkanov Sergey
+        *************************************************************************/
         public static void rmatrixinvupdateuv(ref double[,] inva,
             int n,
@@ -28290,4 +28658,44 @@
     public class schur
     {
+        /*************************************************************************
+        Subroutine performing the Schur decomposition of a general matrix by using
+        the QR algorithm with multiple shifts.
+
+        The source matrix A is represented as S'*A*S = T, where S is an orthogonal
+        matrix (Schur vectors), T - upper quasi-triangular matrix (with blocks of
+        sizes 1x1 and 2x2 on the main diagonal).
+
+        Input parameters:
+            A   -   matrix to be decomposed.
+                    Array whose indexes range within [0..N-1, 0..N-1].
+            N   -   size of A, N>=0.
+
+
+        Output parameters:
+            A   -   contains matrix T.
+                    Array whose indexes range within [0..N-1, 0..N-1].
+            S   -   contains Schur vectors.
+                    Array whose indexes range within [0..N-1, 0..N-1].
+
+        Note 1:
+            The block structure of matrix T can be easily recognized: since all
+            the elements below the blocks are zeros, the elements a[i+1,i] which
+            are equal to 0 show the block border.
+
+        Note 2:
+            The algorithm performance depends on the value of the internal parameter
+            NS of the InternalSchurDecomposition subroutine which defines the number
+            of shifts in the QR algorithm (similarly to the block width in block-matrix
+            algorithms in linear algebra). If you require maximum performance on
+            your machine, it is recommended to adjust this parameter manually.
+
+        Result:
+            True,
+                if the algorithm has converged and parameters A and S contain the result.
+            False,
+                if the algorithm has not converged.
+
+        Algorithm implemented on the basis of the DHSEQR subroutine (LAPACK 3.0 library).
+        *************************************************************************/
         public static bool rmatrixschur(ref double[,] a,
             int n,
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/optimization.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/optimization.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/optimization.cs	(revision 7294)
@@ -26,4 +26,1386 @@
 
     /*************************************************************************
+    This object stores state of the nonlinear CG optimizer.
+
+    You should use ALGLIB functions to work with this object.
+    *************************************************************************/
+    public class mincgstate
+    {
+        //
+        // Public declarations
+        //
+        public bool needf { get { return _innerobj.needf; } set { _innerobj.needf = value; } }
+        public bool needfg { get { return _innerobj.needfg; } set { _innerobj.needfg = value; } }
+        public bool xupdated { get { return _innerobj.xupdated; } set { _innerobj.xupdated = value; } }
+        public double f { get { return _innerobj.f; } set { _innerobj.f = value; } }
+        public double[] g { get { return _innerobj.g; } }
+        public double[] x { get { return _innerobj.x; } }
+
+        public mincgstate()
+        {
+            _innerobj = new mincg.mincgstate();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private mincg.mincgstate _innerobj;
+        public mincg.mincgstate innerobj { get { return _innerobj; } }
+        public mincgstate(mincg.mincgstate obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+
+    *************************************************************************/
+    public class mincgreport
+    {
+        //
+        // Public declarations
+        //
+        public int iterationscount { get { return _innerobj.iterationscount; } set { _innerobj.iterationscount = value; } }
+        public int nfev { get { return _innerobj.nfev; } set { _innerobj.nfev = value; } }
+        public int terminationtype { get { return _innerobj.terminationtype; } set { _innerobj.terminationtype = value; } }
+
+        public mincgreport()
+        {
+            _innerobj = new mincg.mincgreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private mincg.mincgreport _innerobj;
+        public mincg.mincgreport innerobj { get { return _innerobj; } }
+        public mincgreport(mincg.mincgreport obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+    /*************************************************************************
+            NONLINEAR CONJUGATE GRADIENT METHOD
+
+    DESCRIPTION:
+    The subroutine minimizes function F(x) of N arguments by using one of  the
+    nonlinear conjugate gradient methods.
+
+    These CG methods are globally convergent (even on non-convex functions) as
+    long as grad(f) is Lipschitz continuous in  a  some  neighborhood  of  the
+    L = { x : f(x)<=f(x0) }.
+
+
+    REQUIREMENTS:
+    Algorithm will request following information during its operation:
+    * function value F and its gradient G (simultaneously) at given point X
+
+
+    USAGE:
+    1. User initializes algorithm state with MinCGCreate() call
+    2. User tunes solver parameters with MinCGSetCond(), MinCGSetStpMax() and
+       other functions
+    3. User calls MinCGOptimize() function which takes algorithm  state   and
+       pointer (delegate, etc.) to callback function which calculates F/G.
+    4. User calls MinCGResults() to get solution
+    5. Optionally, user may call MinCGRestartFrom() to solve another  problem
+       with same N but another starting point and/or another function.
+       MinCGRestartFrom() allows to reuse already initialized structure.
+
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>0:
+                    * if given, only leading N elements of X are used
+                    * if not given, automatically determined from size of X
+        X       -   starting point, array[0..N-1].
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 25.03.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgcreate(int n, double[] x, out mincgstate state)
+    {
+        state = new mincgstate();
+        mincg.mincgcreate(n, x, state.innerobj);
+        return;
+    }
+    public static void mincgcreate(double[] x, out mincgstate state)
+    {
+        int n;
+
+        state = new mincgstate();
+        n = ap.len(x);
+        mincg.mincgcreate(n, x, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    The subroutine is finite difference variant of MinCGCreate(). It uses
+    finite differences in order to differentiate target function.
+
+    Description below contains information which is specific to this function
+    only. We recommend to read comments on MinCGCreate() in order to get more
+    information about creation of CG optimizer.
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>0:
+                    * if given, only leading N elements of X are used
+                    * if not given, automatically determined from size of X
+        X       -   starting point, array[0..N-1].
+        DiffStep-   differentiation step, >0
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+    NOTES:
+    1. algorithm uses 4-point central formula for differentiation.
+    2. differentiation step along I-th axis is equal to DiffStep*S[I] where
+       S[] is scaling vector which can be set by MinCGSetScale() call.
+    3. we recommend you to use moderate values of  differentiation  step.  Too
+       large step will result in too large truncation  errors, while too small
+       step will result in too large numerical  errors.  1.0E-6  can  be  good
+       value to start with.
+    4. Numerical  differentiation  is   very   inefficient  -   one   gradient
+       calculation needs 4*N function evaluations. This function will work for
+       any N - either small (1...10), moderate (10...100) or  large  (100...).
+       However, performance penalty will be too severe for any N's except  for
+       small ones.
+       We should also say that code which relies on numerical  differentiation
+       is  less  robust  and  precise.  L-BFGS  needs  exact  gradient values.
+       Imprecise  gradient may slow down  convergence,  especially  on  highly
+       nonlinear problems.
+       Thus  we  recommend to use this function for fast prototyping on small-
+       dimensional problems only, and to implement analytical gradient as soon
+       as possible.
+
+      -- ALGLIB --
+         Copyright 16.05.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgcreatef(int n, double[] x, double diffstep, out mincgstate state)
+    {
+        state = new mincgstate();
+        mincg.mincgcreatef(n, x, diffstep, state.innerobj);
+        return;
+    }
+    public static void mincgcreatef(double[] x, double diffstep, out mincgstate state)
+    {
+        int n;
+
+        state = new mincgstate();
+        n = ap.len(x);
+        mincg.mincgcreatef(n, x, diffstep, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    This function sets stopping conditions for CG optimization algorithm.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        EpsG    -   >=0
+                    The  subroutine  finishes  its  work   if   the  condition
+                    |v|<EpsG is satisfied, where:
+                    * |.| means Euclidian norm
+                    * v - scaled gradient vector, v[i]=g[i]*s[i]
+                    * g - gradient
+                    * s - scaling coefficients set by MinCGSetScale()
+        EpsF    -   >=0
+                    The  subroutine  finishes  its work if on k+1-th iteration
+                    the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
+                    is satisfied.
+        EpsX    -   >=0
+                    The subroutine finishes its work if  on  k+1-th  iteration
+                    the condition |v|<=EpsX is fulfilled, where:
+                    * |.| means Euclidian norm
+                    * v - scaled step vector, v[i]=dx[i]/s[i]
+                    * dx - ste pvector, dx=X(k+1)-X(k)
+                    * s - scaling coefficients set by MinCGSetScale()
+        MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
+                    iterations is unlimited.
+
+    Passing EpsG=0, EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to
+    automatic stopping criterion selection (small EpsX).
+
+      -- ALGLIB --
+         Copyright 02.04.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetcond(mincgstate state, double epsg, double epsf, double epsx, int maxits)
+    {
+
+        mincg.mincgsetcond(state.innerobj, epsg, epsf, epsx, maxits);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets scaling coefficients for CG optimizer.
+
+    ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+    size and gradient are scaled before comparison with tolerances).  Scale of
+    the I-th variable is a translation invariant measure of:
+    a) "how large" the variable is
+    b) how large the step should be to make significant changes in the function
+
+    Scaling is also used by finite difference variant of CG optimizer  -  step
+    along I-th axis is equal to DiffStep*S[I].
+
+    In   most   optimizers  (and  in  the  CG  too)  scaling is NOT a form  of
+    preconditioning. It just  affects  stopping  conditions.  You  should  set
+    preconditioner by separate call to one of the MinCGSetPrec...() functions.
+
+    There  is  special  preconditioning  mode, however,  which  uses   scaling
+    coefficients to form diagonal preconditioning matrix. You  can  turn  this
+    mode on, if you want.   But  you should understand that scaling is not the
+    same thing as preconditioning - these are two different, although  related
+    forms of tuning solver.
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        S       -   array[N], non-zero scaling coefficients
+                    S[i] may be negative, sign doesn't matter.
+
+      -- ALGLIB --
+         Copyright 14.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetscale(mincgstate state, double[] s)
+    {
+
+        mincg.mincgsetscale(state.innerobj, s);
+        return;
+    }
+
+    /*************************************************************************
+    This function turns on/off reporting.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        NeedXRep-   whether iteration reports are needed or not
+
+    If NeedXRep is True, algorithm will call rep() callback function if  it is
+    provided to MinCGOptimize().
+
+      -- ALGLIB --
+         Copyright 02.04.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetxrep(mincgstate state, bool needxrep)
+    {
+
+        mincg.mincgsetxrep(state.innerobj, needxrep);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets CG algorithm.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        CGType  -   algorithm type:
+                    * -1    automatic selection of the best algorithm
+                    * 0     DY (Dai and Yuan) algorithm
+                    * 1     Hybrid DY-HS algorithm
+
+      -- ALGLIB --
+         Copyright 02.04.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetcgtype(mincgstate state, int cgtype)
+    {
+
+        mincg.mincgsetcgtype(state.innerobj, cgtype);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets maximum step length
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
+                    want to limit step length.
+
+    Use this subroutine when you optimize target function which contains exp()
+    or  other  fast  growing  functions,  and optimization algorithm makes too
+    large  steps  which  leads  to overflow. This function allows us to reject
+    steps  that  are  too  large  (and  therefore  expose  us  to the possible
+    overflow) without actually calculating function value at the x+stp*d.
+
+      -- ALGLIB --
+         Copyright 02.04.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetstpmax(mincgstate state, double stpmax)
+    {
+
+        mincg.mincgsetstpmax(state.innerobj, stpmax);
+        return;
+    }
+
+    /*************************************************************************
+    This function allows to suggest initial step length to the CG algorithm.
+
+    Suggested  step  length  is used as starting point for the line search. It
+    can be useful when you have  badly  scaled  problem,  i.e.  when  ||grad||
+    (which is used as initial estimate for the first step) is many  orders  of
+    magnitude different from the desired step.
+
+    Line search  may  fail  on  such problems without good estimate of initial
+    step length. Imagine, for example, problem with ||grad||=10^50 and desired
+    step equal to 0.1 Line  search function will use 10^50  as  initial  step,
+    then  it  will  decrease step length by 2 (up to 20 attempts) and will get
+    10^44, which is still too large.
+
+    This function allows us to tell than line search should  be  started  from
+    some moderate step length, like 1.0, so algorithm will be able  to  detect
+    desired step length in a several searches.
+
+    Default behavior (when no step is suggested) is to use preconditioner,  if
+    it is available, to generate initial estimate of step length.
+
+    This function influences only first iteration of algorithm. It  should  be
+    called between MinCGCreate/MinCGRestartFrom() call and MinCGOptimize call.
+    Suggested step is ignored if you have preconditioner.
+
+    INPUT PARAMETERS:
+        State   -   structure used to store algorithm state.
+        Stp     -   initial estimate of the step length.
+                    Can be zero (no estimate).
+
+      -- ALGLIB --
+         Copyright 30.07.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsuggeststep(mincgstate state, double stp)
+    {
+
+        mincg.mincgsuggeststep(state.innerobj, stp);
+        return;
+    }
+
+    /*************************************************************************
+    Modification of the preconditioner: preconditioning is turned off.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+    NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+    iterations.
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetprecdefault(mincgstate state)
+    {
+
+        mincg.mincgsetprecdefault(state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Modification  of  the  preconditioner:  diagonal of approximate Hessian is
+    used.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        D       -   diagonal of the approximate Hessian, array[0..N-1],
+                    (if larger, only leading N elements are used).
+
+    NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+    iterations.
+
+    NOTE 2: D[i] should be positive. Exception will be thrown otherwise.
+
+    NOTE 3: you should pass diagonal of approximate Hessian - NOT ITS INVERSE.
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetprecdiag(mincgstate state, double[] d)
+    {
+
+        mincg.mincgsetprecdiag(state.innerobj, d);
+        return;
+    }
+
+    /*************************************************************************
+    Modification of the preconditioner: scale-based diagonal preconditioning.
+
+    This preconditioning mode can be useful when you  don't  have  approximate
+    diagonal of Hessian, but you know that your  variables  are  badly  scaled
+    (for  example,  one  variable is in [1,10], and another in [1000,100000]),
+    and most part of the ill-conditioning comes from different scales of vars.
+
+    In this case simple  scale-based  preconditioner,  with H[i] = 1/(s[i]^2),
+    can greatly improve convergence.
+
+    IMPRTANT: you should set scale of your variables with MinCGSetScale() call
+    (before or after MinCGSetPrecScale() call). Without knowledge of the scale
+    of your variables scale-based preconditioner will be just unit matrix.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+    NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+    iterations.
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgsetprecscale(mincgstate state)
+    {
+
+        mincg.mincgsetprecscale(state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This function provides reverse communication interface
+    Reverse communication interface is not documented or recommended to use.
+    See below for functions which provide better documented API
+    *************************************************************************/
+    public static bool mincgiteration(mincgstate state)
+    {
+
+        bool result = mincg.mincgiteration(state.innerobj);
+        return result;
+    }
+    /*************************************************************************
+    This family of functions is used to launcn iterations of nonlinear optimizer
+
+    These functions accept following parameters:
+        func    -   callback which calculates function (or merit function)
+                    value func at given point x
+        grad    -   callback which calculates function (or merit function)
+                    value func and gradient grad at given point x
+        rep     -   optional callback which is called after each iteration
+                    can be null
+        obj     -   optional object which is passed to func/grad/hess/jac/rep
+                    can be null
+
+    NOTES:
+
+    1. This function has two different implementations: one which  uses  exact
+       (analytical) user-supplied  gradient, and one which uses function value
+       only  and  numerically  differentiates  function  in  order  to  obtain
+       gradient.
+
+       Depending  on  the  specific  function  used to create optimizer object
+       (either MinCGCreate()  for analytical gradient  or  MinCGCreateF()  for
+       numerical differentiation) you should  choose  appropriate  variant  of
+       MinCGOptimize() - one which accepts function AND gradient or one  which
+       accepts function ONLY.
+
+       Be careful to choose variant of MinCGOptimize()  which  corresponds  to
+       your optimization scheme! Table below lists different  combinations  of
+       callback (function/gradient) passed  to  MinCGOptimize()  and  specific
+       function used to create optimizer.
+
+
+                      |         USER PASSED TO MinCGOptimize()
+       CREATED WITH   |  function only   |  function and gradient
+       ------------------------------------------------------------
+       MinCGCreateF() |     work                FAIL
+       MinCGCreate()  |     FAIL                work
+
+       Here "FAIL" denotes inappropriate combinations  of  optimizer  creation
+       function and MinCGOptimize() version. Attemps to use  such  combination
+       (for  example,  to create optimizer with  MinCGCreateF()  and  to  pass
+       gradient information to MinCGOptimize()) will lead to  exception  being
+       thrown. Either  you  did  not  pass  gradient when it WAS needed or you
+       passed gradient when it was NOT needed.
+
+      -- ALGLIB --
+         Copyright 20.04.2009 by Bochkanov Sergey
+
+    *************************************************************************/
+    public static void mincgoptimize(mincgstate state, ndimensional_func func, ndimensional_rep rep, object obj)
+    {
+        if( func==null )
+            throw new alglibexception("ALGLIB: error in 'mincgoptimize()' (func is null)");
+        while( alglib.mincgiteration(state) )
+        {
+            if( state.needf )
+            {
+                func(state.x, ref state.innerobj.f, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.x, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'mincgoptimize' (some derivatives were not provided?)");
+        }
+    }
+
+
+    public static void mincgoptimize(mincgstate state, ndimensional_grad grad, ndimensional_rep rep, object obj)
+    {
+        if( grad==null )
+            throw new alglibexception("ALGLIB: error in 'mincgoptimize()' (grad is null)");
+        while( alglib.mincgiteration(state) )
+        {
+            if( state.needfg )
+            {
+                grad(state.x, ref state.innerobj.f, state.innerobj.g, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.x, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'mincgoptimize' (some derivatives were not provided?)");
+        }
+    }
+
+
+
+    /*************************************************************************
+    Conjugate gradient results
+
+    INPUT PARAMETERS:
+        State   -   algorithm state
+
+    OUTPUT PARAMETERS:
+        X       -   array[0..N-1], solution
+        Rep     -   optimization report:
+                    * Rep.TerminationType completetion code:
+                        *  1    relative function improvement is no more than
+                                EpsF.
+                        *  2    relative step is no more than EpsX.
+                        *  4    gradient norm is no more than EpsG
+                        *  5    MaxIts steps was taken
+                        *  7    stopping conditions are too stringent,
+                                further improvement is impossible,
+                                we return best X found so far
+                        *  8    terminated by user
+                    * Rep.IterationsCount contains iterations count
+                    * NFEV countains number of function calculations
+
+      -- ALGLIB --
+         Copyright 20.04.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgresults(mincgstate state, out double[] x, out mincgreport rep)
+    {
+        x = new double[0];
+        rep = new mincgreport();
+        mincg.mincgresults(state.innerobj, ref x, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Conjugate gradient results
+
+    Buffered implementation of MinCGResults(), which uses pre-allocated buffer
+    to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
+    intended to be used in the inner cycles of performance critical algorithms
+    where array reallocation penalty is too large to be ignored.
+
+      -- ALGLIB --
+         Copyright 20.04.2009 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgresultsbuf(mincgstate state, ref double[] x, mincgreport rep)
+    {
+
+        mincg.mincgresultsbuf(state.innerobj, ref x, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This  subroutine  restarts  CG  algorithm from new point. All optimization
+    parameters are left unchanged.
+
+    This  function  allows  to  solve multiple  optimization  problems  (which
+    must have same number of dimensions) without object reallocation penalty.
+
+    INPUT PARAMETERS:
+        State   -   structure used to store algorithm state.
+        X       -   new starting point.
+
+      -- ALGLIB --
+         Copyright 30.07.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void mincgrestartfrom(mincgstate state, double[] x)
+    {
+
+        mincg.mincgrestartfrom(state.innerobj, x);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    This object stores nonlinear optimizer state.
+    You should use functions provided by MinBLEIC subpackage to work with this
+    object
+    *************************************************************************/
+    public class minbleicstate
+    {
+        //
+        // Public declarations
+        //
+        public bool needf { get { return _innerobj.needf; } set { _innerobj.needf = value; } }
+        public bool needfg { get { return _innerobj.needfg; } set { _innerobj.needfg = value; } }
+        public bool xupdated { get { return _innerobj.xupdated; } set { _innerobj.xupdated = value; } }
+        public double f { get { return _innerobj.f; } set { _innerobj.f = value; } }
+        public double[] g { get { return _innerobj.g; } }
+        public double[] x { get { return _innerobj.x; } }
+
+        public minbleicstate()
+        {
+            _innerobj = new minbleic.minbleicstate();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private minbleic.minbleicstate _innerobj;
+        public minbleic.minbleicstate innerobj { get { return _innerobj; } }
+        public minbleicstate(minbleic.minbleicstate obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    This structure stores optimization report:
+    * InnerIterationsCount      number of inner iterations
+    * OuterIterationsCount      number of outer iterations
+    * NFEV                      number of gradient evaluations
+    * TerminationType           termination type (see below)
+
+    TERMINATION CODES
+
+    TerminationType field contains completion code, which can be:
+      -10   unsupported combination of algorithm settings:
+            1) StpMax is set to non-zero value,
+            AND 2) non-default preconditioner is used.
+            You can't use both features at the same moment,
+            so you have to choose one of them (and to turn
+            off another one).
+      -3    inconsistent constraints. Feasible point is
+            either nonexistent or too hard to find. Try to
+            restart optimizer with better initial
+            approximation
+       4    conditions on constraints are fulfilled
+            with error less than or equal to EpsC
+       5    MaxIts steps was taken
+       7    stopping conditions are too stringent,
+            further improvement is impossible,
+            X contains best point found so far.
+
+    ADDITIONAL FIELDS
+
+    There are additional fields which can be used for debugging:
+    * DebugEqErr                error in the equality constraints (2-norm)
+    * DebugFS                   f, calculated at projection of initial point
+                                to the feasible set
+    * DebugFF                   f, calculated at the final point
+    * DebugDX                   |X_start-X_final|
+    *************************************************************************/
+    public class minbleicreport
+    {
+        //
+        // Public declarations
+        //
+        public int inneriterationscount { get { return _innerobj.inneriterationscount; } set { _innerobj.inneriterationscount = value; } }
+        public int outeriterationscount { get { return _innerobj.outeriterationscount; } set { _innerobj.outeriterationscount = value; } }
+        public int nfev { get { return _innerobj.nfev; } set { _innerobj.nfev = value; } }
+        public int terminationtype { get { return _innerobj.terminationtype; } set { _innerobj.terminationtype = value; } }
+        public double debugeqerr { get { return _innerobj.debugeqerr; } set { _innerobj.debugeqerr = value; } }
+        public double debugfs { get { return _innerobj.debugfs; } set { _innerobj.debugfs = value; } }
+        public double debugff { get { return _innerobj.debugff; } set { _innerobj.debugff = value; } }
+        public double debugdx { get { return _innerobj.debugdx; } set { _innerobj.debugdx = value; } }
+
+        public minbleicreport()
+        {
+            _innerobj = new minbleic.minbleicreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private minbleic.minbleicreport _innerobj;
+        public minbleic.minbleicreport innerobj { get { return _innerobj; } }
+        public minbleicreport(minbleic.minbleicreport obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+    /*************************************************************************
+                         BOUND CONSTRAINED OPTIMIZATION
+           WITH ADDITIONAL LINEAR EQUALITY AND INEQUALITY CONSTRAINTS
+
+    DESCRIPTION:
+    The  subroutine  minimizes  function   F(x)  of N arguments subject to any
+    combination of:
+    * bound constraints
+    * linear inequality constraints
+    * linear equality constraints
+
+    REQUIREMENTS:
+    * user must provide function value and gradient
+    * starting point X0 must be feasible or
+      not too far away from the feasible set
+    * grad(f) must be Lipschitz continuous on a level set:
+      L = { x : f(x)<=f(x0) }
+    * function must be defined everywhere on the feasible set F
+
+    USAGE:
+
+    Constrained optimization if far more complex than the unconstrained one.
+    Here we give very brief outline of the BLEIC optimizer. We strongly recommend
+    you to read examples in the ALGLIB Reference Manual and to read ALGLIB User Guide
+    on optimization, which is available at http://www.alglib.net/optimization/
+
+    1. User initializes algorithm state with MinBLEICCreate() call
+
+    2. USer adds boundary and/or linear constraints by calling
+       MinBLEICSetBC() and MinBLEICSetLC() functions.
+
+    3. User sets stopping conditions for underlying unconstrained solver
+       with MinBLEICSetInnerCond() call.
+       This function controls accuracy of underlying optimization algorithm.
+
+    4. User sets stopping conditions for outer iteration by calling
+       MinBLEICSetOuterCond() function.
+       This function controls handling of boundary and inequality constraints.
+
+    5. Additionally, user may set limit on number of internal iterations
+       by MinBLEICSetMaxIts() call.
+       This function allows to prevent algorithm from looping forever.
+
+    6. User calls MinBLEICOptimize() function which takes algorithm  state and
+       pointer (delegate, etc.) to callback function which calculates F/G.
+
+    7. User calls MinBLEICResults() to get solution
+
+    8. Optionally user may call MinBLEICRestartFrom() to solve another problem
+       with same N but another starting point.
+       MinBLEICRestartFrom() allows to reuse already initialized structure.
+
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>0:
+                    * if given, only leading N elements of X are used
+                    * if not given, automatically determined from size ofX
+        X       -   starting point, array[N]:
+                    * it is better to set X to a feasible point
+                    * but X can be infeasible, in which case algorithm will try
+                      to find feasible point first, using X as initial
+                      approximation.
+
+    OUTPUT PARAMETERS:
+        State   -   structure stores algorithm state
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleiccreate(int n, double[] x, out minbleicstate state)
+    {
+        state = new minbleicstate();
+        minbleic.minbleiccreate(n, x, state.innerobj);
+        return;
+    }
+    public static void minbleiccreate(double[] x, out minbleicstate state)
+    {
+        int n;
+
+        state = new minbleicstate();
+        n = ap.len(x);
+        minbleic.minbleiccreate(n, x, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    The subroutine is finite difference variant of MinBLEICCreate().  It  uses
+    finite differences in order to differentiate target function.
+
+    Description below contains information which is specific to  this function
+    only. We recommend to read comments on MinBLEICCreate() in  order  to  get
+    more information about creation of BLEIC optimizer.
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>0:
+                    * if given, only leading N elements of X are used
+                    * if not given, automatically determined from size of X
+        X       -   starting point, array[0..N-1].
+        DiffStep-   differentiation step, >0
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+    NOTES:
+    1. algorithm uses 4-point central formula for differentiation.
+    2. differentiation step along I-th axis is equal to DiffStep*S[I] where
+       S[] is scaling vector which can be set by MinBLEICSetScale() call.
+    3. we recommend you to use moderate values of  differentiation  step.  Too
+       large step will result in too large truncation  errors, while too small
+       step will result in too large numerical  errors.  1.0E-6  can  be  good
+       value to start with.
+    4. Numerical  differentiation  is   very   inefficient  -   one   gradient
+       calculation needs 4*N function evaluations. This function will work for
+       any N - either small (1...10), moderate (10...100) or  large  (100...).
+       However, performance penalty will be too severe for any N's except  for
+       small ones.
+       We should also say that code which relies on numerical  differentiation
+       is  less  robust and precise. CG needs exact gradient values. Imprecise
+       gradient may slow  down  convergence, especially  on  highly  nonlinear
+       problems.
+       Thus  we  recommend to use this function for fast prototyping on small-
+       dimensional problems only, and to implement analytical gradient as soon
+       as possible.
+
+      -- ALGLIB --
+         Copyright 16.05.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleiccreatef(int n, double[] x, double diffstep, out minbleicstate state)
+    {
+        state = new minbleicstate();
+        minbleic.minbleiccreatef(n, x, diffstep, state.innerobj);
+        return;
+    }
+    public static void minbleiccreatef(double[] x, double diffstep, out minbleicstate state)
+    {
+        int n;
+
+        state = new minbleicstate();
+        n = ap.len(x);
+        minbleic.minbleiccreatef(n, x, diffstep, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
+    This function sets boundary constraints for BLEIC optimizer.
+
+    Boundary constraints are inactive by default (after initial creation).
+    They are preserved after algorithm restart with MinBLEICRestartFrom().
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        BndL    -   lower bounds, array[N].
+                    If some (all) variables are unbounded, you may specify
+                    very small number or -INF.
+        BndU    -   upper bounds, array[N].
+                    If some (all) variables are unbounded, you may specify
+                    very large number or +INF.
+
+    NOTE 1: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+    variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+    NOTE 2: this solver has following useful properties:
+    * bound constraints are always satisfied exactly
+    * function is evaluated only INSIDE area specified by  bound  constraints,
+      even  when  numerical  differentiation is used (algorithm adjusts  nodes
+      according to boundary constraints)
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetbc(minbleicstate state, double[] bndl, double[] bndu)
+    {
+
+        minbleic.minbleicsetbc(state.innerobj, bndl, bndu);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets linear constraints for BLEIC optimizer.
+
+    Linear constraints are inactive by default (after initial creation).
+    They are preserved after algorithm restart with MinBLEICRestartFrom().
+
+    INPUT PARAMETERS:
+        State   -   structure previously allocated with MinBLEICCreate call.
+        C       -   linear constraints, array[K,N+1].
+                    Each row of C represents one constraint, either equality
+                    or inequality (see below):
+                    * first N elements correspond to coefficients,
+                    * last element corresponds to the right part.
+                    All elements of C (including right part) must be finite.
+        CT      -   type of constraints, array[K]:
+                    * if CT[i]>0, then I-th constraint is C[i,*]*x >= C[i,n+1]
+                    * if CT[i]=0, then I-th constraint is C[i,*]*x  = C[i,n+1]
+                    * if CT[i]<0, then I-th constraint is C[i,*]*x <= C[i,n+1]
+        K       -   number of equality/inequality constraints, K>=0:
+                    * if given, only leading K elements of C/CT are used
+                    * if not given, automatically determined from sizes of C/CT
+
+    NOTE 1: linear (non-bound) constraints are satisfied only approximately:
+    * there always exists some minor violation (about Epsilon in magnitude)
+      due to rounding errors
+    * numerical differentiation, if used, may  lead  to  function  evaluations
+      outside  of the feasible  area,   because   algorithm  does  NOT  change
+      numerical differentiation formula according to linear constraints.
+    If you want constraints to be  satisfied  exactly, try to reformulate your
+    problem  in  such  manner  that  all constraints will become boundary ones
+    (this kind of constraints is always satisfied exactly, both in  the  final
+    solution and in all intermediate points).
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetlc(minbleicstate state, double[,] c, int[] ct, int k)
+    {
+
+        minbleic.minbleicsetlc(state.innerobj, c, ct, k);
+        return;
+    }
+    public static void minbleicsetlc(minbleicstate state, double[,] c, int[] ct)
+    {
+        int k;
+        if( (ap.rows(c)!=ap.len(ct)))
+            throw new alglibexception("Error while calling 'minbleicsetlc': looks like one of arguments has wrong size");
+
+        k = ap.rows(c);
+        minbleic.minbleicsetlc(state.innerobj, c, ct, k);
+
+        return;
+    }
+
+    /*************************************************************************
+    This function sets stopping conditions for the underlying nonlinear CG
+    optimizer. It controls overall accuracy of solution. These conditions
+    should be strict enough in order for algorithm to converge.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        EpsG    -   >=0
+                    The  subroutine  finishes  its  work   if   the  condition
+                    |v|<EpsG is satisfied, where:
+                    * |.| means Euclidian norm
+                    * v - scaled gradient vector, v[i]=g[i]*s[i]
+                    * g - gradient
+                    * s - scaling coefficients set by MinBLEICSetScale()
+        EpsF    -   >=0
+                    The  subroutine  finishes  its work if on k+1-th iteration
+                    the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
+                    is satisfied.
+        EpsX    -   >=0
+                    The subroutine finishes its work if  on  k+1-th  iteration
+                    the condition |v|<=EpsX is fulfilled, where:
+                    * |.| means Euclidian norm
+                    * v - scaled step vector, v[i]=dx[i]/s[i]
+                    * dx - ste pvector, dx=X(k+1)-X(k)
+                    * s - scaling coefficients set by MinBLEICSetScale()
+
+    Passing EpsG=0, EpsF=0 and EpsX=0 (simultaneously) will lead to
+    automatic stopping criterion selection.
+
+    These conditions are used to terminate inner iterations. However, you
+    need to tune termination conditions for outer iterations too.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetinnercond(minbleicstate state, double epsg, double epsf, double epsx)
+    {
+
+        minbleic.minbleicsetinnercond(state.innerobj, epsg, epsf, epsx);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets stopping conditions for outer iteration of BLEIC algo.
+
+    These conditions control accuracy of constraint handling and amount of
+    infeasibility allowed in the solution.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        EpsX    -   >0, stopping condition on outer iteration step length
+        EpsI    -   >0, stopping condition on infeasibility
+
+    Both EpsX and EpsI must be non-zero.
+
+    MEANING OF EpsX
+
+    EpsX  is  a  stopping  condition for outer iterations. Algorithm will stop
+    when  solution  of  the  current  modified  subproblem will be within EpsX
+    (using 2-norm) of the previous solution.
+
+    MEANING OF EpsI
+
+    EpsI controls feasibility properties -  algorithm  won't  stop  until  all
+    inequality constraints will be satisfied with error (distance from current
+    point to the feasible area) at most EpsI.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetoutercond(minbleicstate state, double epsx, double epsi)
+    {
+
+        minbleic.minbleicsetoutercond(state.innerobj, epsx, epsi);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets scaling coefficients for BLEIC optimizer.
+
+    ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+    size and gradient are scaled before comparison with tolerances).  Scale of
+    the I-th variable is a translation invariant measure of:
+    a) "how large" the variable is
+    b) how large the step should be to make significant changes in the function
+
+    Scaling is also used by finite difference variant of the optimizer  - step
+    along I-th axis is equal to DiffStep*S[I].
+
+    In  most  optimizers  (and  in  the  BLEIC  too)  scaling is NOT a form of
+    preconditioning. It just  affects  stopping  conditions.  You  should  set
+    preconditioner  by  separate  call  to  one  of  the  MinBLEICSetPrec...()
+    functions.
+
+    There is a special  preconditioning  mode, however,  which  uses   scaling
+    coefficients to form diagonal preconditioning matrix. You  can  turn  this
+    mode on, if you want.   But  you should understand that scaling is not the
+    same thing as preconditioning - these are two different, although  related
+    forms of tuning solver.
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        S       -   array[N], non-zero scaling coefficients
+                    S[i] may be negative, sign doesn't matter.
+
+      -- ALGLIB --
+         Copyright 14.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetscale(minbleicstate state, double[] s)
+    {
+
+        minbleic.minbleicsetscale(state.innerobj, s);
+        return;
+    }
+
+    /*************************************************************************
+    Modification of the preconditioner: preconditioning is turned off.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetprecdefault(minbleicstate state)
+    {
+
+        minbleic.minbleicsetprecdefault(state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Modification  of  the  preconditioner:  diagonal of approximate Hessian is
+    used.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        D       -   diagonal of the approximate Hessian, array[0..N-1],
+                    (if larger, only leading N elements are used).
+
+    NOTE 1: D[i] should be positive. Exception will be thrown otherwise.
+
+    NOTE 2: you should pass diagonal of approximate Hessian - NOT ITS INVERSE.
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetprecdiag(minbleicstate state, double[] d)
+    {
+
+        minbleic.minbleicsetprecdiag(state.innerobj, d);
+        return;
+    }
+
+    /*************************************************************************
+    Modification of the preconditioner: scale-based diagonal preconditioning.
+
+    This preconditioning mode can be useful when you  don't  have  approximate
+    diagonal of Hessian, but you know that your  variables  are  badly  scaled
+    (for  example,  one  variable is in [1,10], and another in [1000,100000]),
+    and most part of the ill-conditioning comes from different scales of vars.
+
+    In this case simple  scale-based  preconditioner,  with H[i] = 1/(s[i]^2),
+    can greatly improve convergence.
+
+    IMPRTANT: you should set scale of your variables  with  MinBLEICSetScale()
+    call  (before  or after MinBLEICSetPrecScale() call). Without knowledge of
+    the scale of your variables scale-based preconditioner will be  just  unit
+    matrix.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetprecscale(minbleicstate state)
+    {
+
+        minbleic.minbleicsetprecscale(state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This function allows to stop algorithm after specified number of inner
+    iterations.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        MaxIts  -   maximum number of inner iterations.
+                    If MaxIts=0, the number of iterations is unlimited.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetmaxits(minbleicstate state, int maxits)
+    {
+
+        minbleic.minbleicsetmaxits(state.innerobj, maxits);
+        return;
+    }
+
+    /*************************************************************************
+    This function turns on/off reporting.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        NeedXRep-   whether iteration reports are needed or not
+
+    If NeedXRep is True, algorithm will call rep() callback function if  it is
+    provided to MinBLEICOptimize().
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetxrep(minbleicstate state, bool needxrep)
+    {
+
+        minbleic.minbleicsetxrep(state.innerobj, needxrep);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets maximum step length
+
+    IMPORTANT: this feature is hard to combine with preconditioning. You can't
+    set upper limit on step length, when you solve optimization  problem  with
+    linear (non-boundary) constraints AND preconditioner turned on.
+
+    When  non-boundary  constraints  are  present,  you  have to either a) use
+    preconditioner, or b) use upper limit on step length.  YOU CAN'T USE BOTH!
+    In this case algorithm will terminate with appropriate error code.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
+                    want to limit step length.
+
+    Use this subroutine when you optimize target function which contains exp()
+    or  other  fast  growing  functions,  and optimization algorithm makes too
+    large  steps  which  lead   to overflow. This function allows us to reject
+    steps  that  are  too  large  (and  therefore  expose  us  to the possible
+    overflow) without actually calculating function value at the x+stp*d.
+
+      -- ALGLIB --
+         Copyright 02.04.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetstpmax(minbleicstate state, double stpmax)
+    {
+
+        minbleic.minbleicsetstpmax(state.innerobj, stpmax);
+        return;
+    }
+
+    /*************************************************************************
+    This function provides reverse communication interface
+    Reverse communication interface is not documented or recommended to use.
+    See below for functions which provide better documented API
+    *************************************************************************/
+    public static bool minbleiciteration(minbleicstate state)
+    {
+
+        bool result = minbleic.minbleiciteration(state.innerobj);
+        return result;
+    }
+    /*************************************************************************
+    This family of functions is used to launcn iterations of nonlinear optimizer
+
+    These functions accept following parameters:
+        func    -   callback which calculates function (or merit function)
+                    value func at given point x
+        grad    -   callback which calculates function (or merit function)
+                    value func and gradient grad at given point x
+        rep     -   optional callback which is called after each iteration
+                    can be null
+        obj     -   optional object which is passed to func/grad/hess/jac/rep
+                    can be null
+
+    NOTES:
+
+    1. This function has two different implementations: one which  uses  exact
+       (analytical) user-supplied gradient,  and one which uses function value
+       only  and  numerically  differentiates  function  in  order  to  obtain
+       gradient.
+
+       Depending  on  the  specific  function  used to create optimizer object
+       (either  MinBLEICCreate() for analytical gradient or  MinBLEICCreateF()
+       for numerical differentiation) you should choose appropriate variant of
+       MinBLEICOptimize() - one  which  accepts  function  AND gradient or one
+       which accepts function ONLY.
+
+       Be careful to choose variant of MinBLEICOptimize() which corresponds to
+       your optimization scheme! Table below lists different  combinations  of
+       callback (function/gradient) passed to MinBLEICOptimize()  and specific
+       function used to create optimizer.
+
+
+                         |         USER PASSED TO MinBLEICOptimize()
+       CREATED WITH      |  function only   |  function and gradient
+       ------------------------------------------------------------
+       MinBLEICCreateF() |     work                FAIL
+       MinBLEICCreate()  |     FAIL                work
+
+       Here "FAIL" denotes inappropriate combinations  of  optimizer  creation
+       function  and  MinBLEICOptimize()  version.   Attemps   to   use   such
+       combination (for  example,  to  create optimizer with MinBLEICCreateF()
+       and  to  pass  gradient  information  to  MinCGOptimize()) will lead to
+       exception being thrown. Either  you  did  not pass gradient when it WAS
+       needed or you passed gradient when it was NOT needed.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+
+    *************************************************************************/
+    public static void minbleicoptimize(minbleicstate state, ndimensional_func func, ndimensional_rep rep, object obj)
+    {
+        if( func==null )
+            throw new alglibexception("ALGLIB: error in 'minbleicoptimize()' (func is null)");
+        while( alglib.minbleiciteration(state) )
+        {
+            if( state.needf )
+            {
+                func(state.x, ref state.innerobj.f, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.x, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'minbleicoptimize' (some derivatives were not provided?)");
+        }
+    }
+
+
+    public static void minbleicoptimize(minbleicstate state, ndimensional_grad grad, ndimensional_rep rep, object obj)
+    {
+        if( grad==null )
+            throw new alglibexception("ALGLIB: error in 'minbleicoptimize()' (grad is null)");
+        while( alglib.minbleiciteration(state) )
+        {
+            if( state.needfg )
+            {
+                grad(state.x, ref state.innerobj.f, state.innerobj.g, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.x, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'minbleicoptimize' (some derivatives were not provided?)");
+        }
+    }
+
+
+
+    /*************************************************************************
+    BLEIC results
+
+    INPUT PARAMETERS:
+        State   -   algorithm state
+
+    OUTPUT PARAMETERS:
+        X       -   array[0..N-1], solution
+        Rep     -   optimization report. You should check Rep.TerminationType
+                    in  order  to  distinguish  successful  termination  from
+                    unsuccessful one.
+                    More information about fields of this  structure  can  be
+                    found in the comments on MinBLEICReport datatype.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicresults(minbleicstate state, out double[] x, out minbleicreport rep)
+    {
+        x = new double[0];
+        rep = new minbleicreport();
+        minbleic.minbleicresults(state.innerobj, ref x, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    BLEIC results
+
+    Buffered implementation of MinBLEICResults() which uses pre-allocated buffer
+    to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
+    intended to be used in the inner cycles of performance critical algorithms
+    where array reallocation penalty is too large to be ignored.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicresultsbuf(minbleicstate state, ref double[] x, minbleicreport rep)
+    {
+
+        minbleic.minbleicresultsbuf(state.innerobj, ref x, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This subroutine restarts algorithm from new point.
+    All optimization parameters (including constraints) are left unchanged.
+
+    This  function  allows  to  solve multiple  optimization  problems  (which
+    must have  same number of dimensions) without object reallocation penalty.
+
+    INPUT PARAMETERS:
+        State   -   structure previously allocated with MinBLEICCreate call.
+        X       -   new starting point.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicrestartfrom(minbleicstate state, double[] x)
+    {
+
+        minbleic.minbleicrestartfrom(state.innerobj, x);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
 
     *************************************************************************/
@@ -33,4 +1415,5 @@
         // Public declarations
         //
+        public bool needf { get { return _innerobj.needf; } set { _innerobj.needf = value; } }
         public bool needfg { get { return _innerobj.needfg; } set { _innerobj.needfg = value; } }
         public bool xupdated { get { return _innerobj.xupdated; } set { _innerobj.xupdated = value; } }
@@ -159,4 +1542,68 @@
 
     /*************************************************************************
+    The subroutine is finite difference variant of MinLBFGSCreate().  It  uses
+    finite differences in order to differentiate target function.
+
+    Description below contains information which is specific to  this function
+    only. We recommend to read comments on MinLBFGSCreate() in  order  to  get
+    more information about creation of LBFGS optimizer.
+
+    INPUT PARAMETERS:
+        N       -   problem dimension, N>0:
+                    * if given, only leading N elements of X are used
+                    * if not given, automatically determined from size of X
+        M       -   number of corrections in the BFGS scheme of Hessian
+                    approximation update. Recommended value:  3<=M<=7. The smaller
+                    value causes worse convergence, the bigger will  not  cause  a
+                    considerably better convergence, but will cause a fall in  the
+                    performance. M<=N.
+        X       -   starting point, array[0..N-1].
+        DiffStep-   differentiation step, >0
+
+    OUTPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+    NOTES:
+    1. algorithm uses 4-point central formula for differentiation.
+    2. differentiation step along I-th axis is equal to DiffStep*S[I] where
+       S[] is scaling vector which can be set by MinLBFGSSetScale() call.
+    3. we recommend you to use moderate values of  differentiation  step.  Too
+       large step will result in too large truncation  errors, while too small
+       step will result in too large numerical  errors.  1.0E-6  can  be  good
+       value to start with.
+    4. Numerical  differentiation  is   very   inefficient  -   one   gradient
+       calculation needs 4*N function evaluations. This function will work for
+       any N - either small (1...10), moderate (10...100) or  large  (100...).
+       However, performance penalty will be too severe for any N's except  for
+       small ones.
+       We should also say that code which relies on numerical  differentiation
+       is   less  robust  and  precise.  LBFGS  needs  exact  gradient values.
+       Imprecise gradient may slow  down  convergence,  especially  on  highly
+       nonlinear problems.
+       Thus  we  recommend to use this function for fast prototyping on small-
+       dimensional problems only, and to implement analytical gradient as soon
+       as possible.
+
+      -- ALGLIB --
+         Copyright 16.05.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlbfgscreatef(int n, int m, double[] x, double diffstep, out minlbfgsstate state)
+    {
+        state = new minlbfgsstate();
+        minlbfgs.minlbfgscreatef(n, m, x, diffstep, state.innerobj);
+        return;
+    }
+    public static void minlbfgscreatef(int m, double[] x, double diffstep, out minlbfgsstate state)
+    {
+        int n;
+
+        state = new minlbfgsstate();
+        n = ap.len(x);
+        minlbfgs.minlbfgscreatef(n, m, x, diffstep, state.innerobj);
+
+        return;
+    }
+
+    /*************************************************************************
     This function sets stopping conditions for L-BFGS optimization algorithm.
 
@@ -165,6 +1612,9 @@
         EpsG    -   >=0
                     The  subroutine  finishes  its  work   if   the  condition
-                    ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                    G - gradient.
+                    |v|<EpsG is satisfied, where:
+                    * |.| means Euclidian norm
+                    * v - scaled gradient vector, v[i]=g[i]*s[i]
+                    * g - gradient
+                    * s - scaling coefficients set by MinLBFGSSetScale()
         EpsF    -   >=0
                     The  subroutine  finishes  its work if on k+1-th iteration
@@ -173,5 +1623,9 @@
         EpsX    -   >=0
                     The subroutine finishes its work if  on  k+1-th  iteration
-                    the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
+                    the condition |v|<=EpsX is fulfilled, where:
+                    * |.| means Euclidian norm
+                    * v - scaled step vector, v[i]=dx[i]/s[i]
+                    * dx - ste pvector, dx=X(k+1)-X(k)
+                    * s - scaling coefficients set by MinLBFGSSetScale()
         MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
                     iterations is unlimited.
@@ -236,11 +1690,47 @@
 
     /*************************************************************************
-    Modification of the preconditioner:
-    default preconditioner (simple scaling) is used.
+    This function sets scaling coefficients for LBFGS optimizer.
+
+    ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+    size and gradient are scaled before comparison with tolerances).  Scale of
+    the I-th variable is a translation invariant measure of:
+    a) "how large" the variable is
+    b) how large the step should be to make significant changes in the function
+
+    Scaling is also used by finite difference variant of the optimizer  - step
+    along I-th axis is equal to DiffStep*S[I].
+
+    In  most  optimizers  (and  in  the  LBFGS  too)  scaling is NOT a form of
+    preconditioning. It just  affects  stopping  conditions.  You  should  set
+    preconditioner  by  separate  call  to  one  of  the  MinLBFGSSetPrec...()
+    functions.
+
+    There  is  special  preconditioning  mode, however,  which  uses   scaling
+    coefficients to form diagonal preconditioning matrix. You  can  turn  this
+    mode on, if you want.   But  you should understand that scaling is not the
+    same thing as preconditioning - these are two different, although  related
+    forms of tuning solver.
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        S       -   array[N], non-zero scaling coefficients
+                    S[i] may be negative, sign doesn't matter.
+
+      -- ALGLIB --
+         Copyright 14.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlbfgssetscale(minlbfgsstate state, double[] s)
+    {
+
+        minlbfgs.minlbfgssetscale(state.innerobj, s);
+        return;
+    }
+
+    /*************************************************************************
+    Modification  of  the  preconditioner:  default  preconditioner    (simple
+    scaling, same for all elements of X) is used.
 
     INPUT PARAMETERS:
         State   -   structure which stores algorithm state
-
-    After call to this function preconditioner is changed to the default one.
 
     NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
@@ -250,14 +1740,14 @@
          Copyright 13.10.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void minlbfgssetdefaultpreconditioner(minlbfgsstate state)
+    public static void minlbfgssetprecdefault(minlbfgsstate state)
     {
 
-        minlbfgs.minlbfgssetdefaultpreconditioner(state.innerobj);
+        minlbfgs.minlbfgssetprecdefault(state.innerobj);
         return;
     }
 
     /*************************************************************************
-    Modification of the preconditioner:
-    Cholesky factorization of approximate Hessian is used.
+    Modification of the preconditioner: Cholesky factorization of  approximate
+    Hessian is used.
 
     INPUT PARAMETERS:
@@ -275,15 +1765,68 @@
     iterations.
 
-    NOTE 2:  P  should  be nonsingular. Exception will be thrown otherwise. It
-    also should be well conditioned, although only strict  non-singularity  is
-    tested.
+    NOTE 2:  P  should  be nonsingular. Exception will be thrown otherwise.
 
       -- ALGLIB --
          Copyright 13.10.2010 by Bochkanov Sergey
     *************************************************************************/
-    public static void minlbfgssetcholeskypreconditioner(minlbfgsstate state, double[,] p, bool isupper)
+    public static void minlbfgssetpreccholesky(minlbfgsstate state, double[,] p, bool isupper)
     {
 
-        minlbfgs.minlbfgssetcholeskypreconditioner(state.innerobj, p, isupper);
+        minlbfgs.minlbfgssetpreccholesky(state.innerobj, p, isupper);
+        return;
+    }
+
+    /*************************************************************************
+    Modification  of  the  preconditioner:  diagonal of approximate Hessian is
+    used.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        D       -   diagonal of the approximate Hessian, array[0..N-1],
+                    (if larger, only leading N elements are used).
+
+    NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+    iterations.
+
+    NOTE 2: D[i] should be positive. Exception will be thrown otherwise.
+
+    NOTE 3: you should pass diagonal of approximate Hessian - NOT ITS INVERSE.
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlbfgssetprecdiag(minlbfgsstate state, double[] d)
+    {
+
+        minlbfgs.minlbfgssetprecdiag(state.innerobj, d);
+        return;
+    }
+
+    /*************************************************************************
+    Modification of the preconditioner: scale-based diagonal preconditioning.
+
+    This preconditioning mode can be useful when you  don't  have  approximate
+    diagonal of Hessian, but you know that your  variables  are  badly  scaled
+    (for  example,  one  variable is in [1,10], and another in [1000,100000]),
+    and most part of the ill-conditioning comes from different scales of vars.
+
+    In this case simple  scale-based  preconditioner,  with H[i] = 1/(s[i]^2),
+    can greatly improve convergence.
+
+    IMPRTANT: you should set scale of your variables  with  MinLBFGSSetScale()
+    call  (before  or after MinLBFGSSetPrecScale() call). Without knowledge of
+    the scale of your variables scale-based preconditioner will be  just  unit
+    matrix.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlbfgssetprecscale(minlbfgsstate state)
+    {
+
+        minlbfgs.minlbfgssetprecscale(state.innerobj);
         return;
     }
@@ -304,4 +1847,6 @@
 
     These functions accept following parameters:
+        func    -   callback which calculates function (or merit function)
+                    value func at given point x
         grad    -   callback which calculates function (or merit function)
                     value func and gradient grad at given point x
@@ -311,4 +1856,35 @@
                     can be null
 
+    NOTES:
+
+    1. This function has two different implementations: one which  uses  exact
+       (analytical) user-supplied gradient,  and one which uses function value
+       only  and  numerically  differentiates  function  in  order  to  obtain
+       gradient.
+
+       Depending  on  the  specific  function  used to create optimizer object
+       (either MinLBFGSCreate() for analytical gradient  or  MinLBFGSCreateF()
+       for numerical differentiation) you should choose appropriate variant of
+       MinLBFGSOptimize() - one  which  accepts  function  AND gradient or one
+       which accepts function ONLY.
+
+       Be careful to choose variant of MinLBFGSOptimize() which corresponds to
+       your optimization scheme! Table below lists different  combinations  of
+       callback (function/gradient) passed to MinLBFGSOptimize()  and specific
+       function used to create optimizer.
+
+
+                         |         USER PASSED TO MinLBFGSOptimize()
+       CREATED WITH      |  function only   |  function and gradient
+       ------------------------------------------------------------
+       MinLBFGSCreateF() |     work                FAIL
+       MinLBFGSCreate()  |     FAIL                work
+
+       Here "FAIL" denotes inappropriate combinations  of  optimizer  creation
+       function  and  MinLBFGSOptimize()  version.   Attemps   to   use   such
+       combination (for example, to create optimizer with MinLBFGSCreateF() and
+       to pass gradient information to MinCGOptimize()) will lead to exception
+       being thrown. Either  you  did  not pass gradient when it WAS needed or
+       you passed gradient when it was NOT needed.
 
       -- ALGLIB --
@@ -316,4 +1892,26 @@
 
     *************************************************************************/
+    public static void minlbfgsoptimize(minlbfgsstate state, ndimensional_func func, ndimensional_rep rep, object obj)
+    {
+        if( func==null )
+            throw new alglibexception("ALGLIB: error in 'minlbfgsoptimize()' (func is null)");
+        while( alglib.minlbfgsiteration(state) )
+        {
+            if( state.needf )
+            {
+                func(state.x, ref state.innerobj.f, obj);
+                continue;
+            }
+            if( state.innerobj.xupdated )
+            {
+                if( rep!=null )
+                    rep(state.innerobj.x, state.innerobj.f, obj);
+                continue;
+            }
+            throw new alglibexception("ALGLIB: error in 'minlbfgsoptimize' (some derivatives were not provided?)");
+        }
+    }
+
+
     public static void minlbfgsoptimize(minlbfgsstate state, ndimensional_grad grad, ndimensional_rep rep, object obj)
     {
@@ -409,4 +2007,338 @@
 
         minlbfgs.minlbfgsrestartfrom(state.innerobj, x);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    This object stores nonlinear optimizer state.
+    You should use functions provided by MinQP subpackage to work with this
+    object
+    *************************************************************************/
+    public class minqpstate
+    {
+        //
+        // Public declarations
+        //
+
+        public minqpstate()
+        {
+            _innerobj = new minqp.minqpstate();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private minqp.minqpstate _innerobj;
+        public minqp.minqpstate innerobj { get { return _innerobj; } }
+        public minqpstate(minqp.minqpstate obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+
+    /*************************************************************************
+    This structure stores optimization report:
+    * InnerIterationsCount      number of inner iterations
+    * OuterIterationsCount      number of outer iterations
+    * NCholesky                 number of Cholesky decomposition
+    * NMV                       number of matrix-vector products
+                                (only products calculated as part of iterative
+                                process are counted)
+    * TerminationType           completion code (see below)
+
+    Completion codes:
+    * -5    inappropriate solver was used:
+            * Cholesky solver for semidefinite or indefinite problems
+            * Cholesky solver for problems with non-boundary constraints
+    * -3    inconsistent constraints (or, maybe, feasible point is
+            too hard to find). If you are sure that constraints are feasible,
+            try to restart optimizer with better initial approximation.
+    *  4    successful completion
+    *  5    MaxIts steps was taken
+    *  7    stopping conditions are too stringent,
+            further improvement is impossible,
+            X contains best point found so far.
+    *************************************************************************/
+    public class minqpreport
+    {
+        //
+        // Public declarations
+        //
+        public int inneriterationscount { get { return _innerobj.inneriterationscount; } set { _innerobj.inneriterationscount = value; } }
+        public int outeriterationscount { get { return _innerobj.outeriterationscount; } set { _innerobj.outeriterationscount = value; } }
+        public int nmv { get { return _innerobj.nmv; } set { _innerobj.nmv = value; } }
+        public int ncholesky { get { return _innerobj.ncholesky; } set { _innerobj.ncholesky = value; } }
+        public int terminationtype { get { return _innerobj.terminationtype; } set { _innerobj.terminationtype = value; } }
+
+        public minqpreport()
+        {
+            _innerobj = new minqp.minqpreport();
+        }
+
+        //
+        // Although some of declarations below are public, you should not use them
+        // They are intended for internal use only
+        //
+        private minqp.minqpreport _innerobj;
+        public minqp.minqpreport innerobj { get { return _innerobj; } }
+        public minqpreport(minqp.minqpreport obj)
+        {
+            _innerobj = obj;
+        }
+    }
+
+    /*************************************************************************
+                        CONSTRAINED QUADRATIC PROGRAMMING
+
+    The subroutine creates QP optimizer. After initial creation,  it  contains
+    default optimization problem with zero quadratic and linear terms  and  no
+    constraints. You should set quadratic/linear terms with calls to functions
+    provided by MinQP subpackage.
+
+    INPUT PARAMETERS:
+        N       -   problem size
+
+    OUTPUT PARAMETERS:
+        State   -   optimizer with zero quadratic/linear terms
+                    and no constraints
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpcreate(int n, out minqpstate state)
+    {
+        state = new minqpstate();
+        minqp.minqpcreate(n, state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets linear term for QP solver.
+
+    By default, linear term is zero.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        B       -   linear term, array[N].
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpsetlinearterm(minqpstate state, double[] b)
+    {
+
+        minqp.minqpsetlinearterm(state.innerobj, b);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets quadratic term for QP solver.
+
+    By default quadratic term is zero.
+
+    IMPORTANT: this solver minimizes following  function:
+        f(x) = 0.5*x'*A*x + b'*x.
+    Note that quadratic term has 0.5 before it. So if  you  want  to  minimize
+        f(x) = x^2 + x
+    you should rewrite your problem as follows:
+        f(x) = 0.5*(2*x^2) + x
+    and your matrix A will be equal to [[2.0]], not to [[1.0]]
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        A       -   matrix, array[N,N]
+        IsUpper -   (optional) storage type:
+                    * if True, symmetric matrix  A  is  given  by  its  upper
+                      triangle, and the lower triangle isnt used
+                    * if False, symmetric matrix  A  is  given  by  its lower
+                      triangle, and the upper triangle isnt used
+                    * if not given, both lower and upper  triangles  must  be
+                      filled.
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpsetquadraticterm(minqpstate state, double[,] a, bool isupper)
+    {
+
+        minqp.minqpsetquadraticterm(state.innerobj, a, isupper);
+        return;
+    }
+    public static void minqpsetquadraticterm(minqpstate state, double[,] a)
+    {
+        bool isupper;
+        if( !alglib.ap.issymmetric(a) )
+            throw new alglibexception("'a' parameter is not symmetric matrix");
+
+        isupper = false;
+        minqp.minqpsetquadraticterm(state.innerobj, a, isupper);
+
+        return;
+    }
+
+    /*************************************************************************
+    This function sets starting point for QP solver. It is useful to have
+    good initial approximation to the solution, because it will increase
+    speed of convergence and identification of active constraints.
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        X       -   starting point, array[N].
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpsetstartingpoint(minqpstate state, double[] x)
+    {
+
+        minqp.minqpsetstartingpoint(state.innerobj, x);
+        return;
+    }
+
+    /*************************************************************************
+    This  function sets origin for QP solver. By default, following QP program
+    is solved:
+
+        min(0.5*x'*A*x+b'*x)
+
+    This function allows to solve different problem:
+
+        min(0.5*(x-x_origin)'*A*(x-x_origin)+b'*(x-x_origin))
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+        XOrigin -   origin, array[N].
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpsetorigin(minqpstate state, double[] xorigin)
+    {
+
+        minqp.minqpsetorigin(state.innerobj, xorigin);
+        return;
+    }
+
+    /*************************************************************************
+    This function tells solver to use Cholesky-based algorithm.
+
+    Cholesky-based algorithm can be used when:
+    * problem is convex
+    * there is no constraints or only boundary constraints are present
+
+    This algorithm has O(N^3) complexity for unconstrained problem and  is  up
+    to several times slower on bound constrained  problems  (these  additional
+    iterations are needed to identify active constraints).
+
+    INPUT PARAMETERS:
+        State   -   structure which stores algorithm state
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpsetalgocholesky(minqpstate state)
+    {
+
+        minqp.minqpsetalgocholesky(state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets boundary constraints for QP solver
+
+    Boundary constraints are inactive by default (after initial creation).
+    After  being  set,  they  are  preserved  until explicitly turned off with
+    another SetBC() call.
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        BndL    -   lower bounds, array[N].
+                    If some (all) variables are unbounded, you may specify
+                    very small number or -INF (latter is recommended because
+                    it will allow solver to use better algorithm).
+        BndU    -   upper bounds, array[N].
+                    If some (all) variables are unbounded, you may specify
+                    very large number or +INF (latter is recommended because
+                    it will allow solver to use better algorithm).
+
+    NOTE: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+    variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpsetbc(minqpstate state, double[] bndl, double[] bndu)
+    {
+
+        minqp.minqpsetbc(state.innerobj, bndl, bndu);
+        return;
+    }
+
+    /*************************************************************************
+    This function solves quadratic programming problem.
+    You should call it after setting solver options with MinQPSet...() calls.
+
+    INPUT PARAMETERS:
+        State   -   algorithm state
+
+    You should use MinQPResults() function to access results after calls
+    to this function.
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpoptimize(minqpstate state)
+    {
+
+        minqp.minqpoptimize(state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    QP solver results
+
+    INPUT PARAMETERS:
+        State   -   algorithm state
+
+    OUTPUT PARAMETERS:
+        X       -   array[0..N-1], solution
+        Rep     -   optimization report. You should check Rep.TerminationType,
+                    which contains completion code, and you may check  another
+                    fields which contain another information  about  algorithm
+                    functioning.
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpresults(minqpstate state, out double[] x, out minqpreport rep)
+    {
+        x = new double[0];
+        rep = new minqpreport();
+        minqp.minqpresults(state.innerobj, ref x, rep.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    QP results
+
+    Buffered implementation of MinQPResults() which uses pre-allocated  buffer
+    to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
+    intended to be used in the inner cycles of performance critical algorithms
+    where array reallocation penalty is too large to be ignored.
+
+      -- ALGLIB --
+         Copyright 11.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minqpresultsbuf(minqpstate state, ref double[] x, minqpreport rep)
+    {
+
+        minqp.minqpresultsbuf(state.innerobj, ref x, rep.innerobj);
         return;
     }
@@ -748,161 +2680,4 @@
 
     /*************************************************************************
-                    IMPROVED LEVENBERG-MARQUARDT METHOD FOR
-                     NON-LINEAR LEAST SQUARES OPTIMIZATION
-
-    DESCRIPTION:
-    This function is used to find minimum of function which is represented  as
-    sum of squares:
-        F(x) = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
-    using:
-    * value of function vector f[]
-    * value of Jacobian of f[]
-    * gradient of merit function F(x)
-
-    This function creates optimizer which uses acceleration strategy 2.  Cheap
-    gradient of merit function (which is twice the product of function  vector
-    and Jacobian) is used for accelerated iterations (see User Guide for  more
-    info on this subject).
-
-    REQUIREMENTS:
-    This algorithm will request following information during its operation:
-
-    * function vector f[] at given point X
-    * function vector f[] and Jacobian of f[] (simultaneously) at given point
-    * gradient of
-
-    There are several overloaded versions of  MinLMOptimize()  function  which
-    correspond  to  different LM-like optimization algorithms provided by this
-    unit. You should choose version which accepts  fvec(),  jac()  and  grad()
-    callbacks. First one is used to calculate f[] at given point,  second  one
-    calculates f[] and Jacobian df[i]/dx[j], last one calculates  gradient  of
-    merit function F(x).
-
-    You can try to initialize MinLMState structure with VJ  function and  then
-    use incorrect version  of  MinLMOptimize()  (for  example,  version  which
-    works  with  general  form function and does not provide Jacobian), but it
-    will  lead  to  exception  being  thrown  after first attempt to calculate
-    Jacobian.
-
-
-    USAGE:
-    1. User initializes algorithm state with MinLMCreateVGJ() call
-    2. User tunes solver parameters with MinLMSetCond(),  MinLMSetStpMax() and
-       other functions
-    3. User calls MinLMOptimize() function which  takes algorithm  state   and
-       callback functions.
-    4. User calls MinLMResults() to get solution
-    5. Optionally, user may call MinLMRestartFrom() to solve  another  problem
-       with same N/M but another starting point and/or another function.
-       MinLMRestartFrom() allows to reuse already initialized structure.
-
-
-    INPUT PARAMETERS:
-        N       -   dimension, N>1
-                    * if given, only leading N elements of X are used
-                    * if not given, automatically determined from size of X
-        M       -   number of functions f[i]
-        X       -   initial solution, array[0..N-1]
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-    NOTES:
-    1. you may tune stopping conditions with MinLMSetCond() function
-    2. if target function contains exp() or other fast growing functions,  and
-       optimization algorithm makes too large steps which leads  to  overflow,
-       use MinLMSetStpMax() function to bound algorithm's steps.
-
-      -- ALGLIB --
-         Copyright 30.03.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void minlmcreatevgj(int n, int m, double[] x, out minlmstate state)
-    {
-        state = new minlmstate();
-        minlm.minlmcreatevgj(n, m, x, state.innerobj);
-        return;
-    }
-    public static void minlmcreatevgj(int m, double[] x, out minlmstate state)
-    {
-        int n;
-
-        state = new minlmstate();
-        n = ap.len(x);
-        minlm.minlmcreatevgj(n, m, x, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-                       LEVENBERG-MARQUARDT-LIKE METHOD FOR
-                      NON-LINEAR LEAST SQUARES OPTIMIZATION
-
-    DESCRIPTION:
-
-    This function is used to find minimum of function which is represented  as
-    sum of squares:
-        F(x) = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
-    using value of F(), gradient of F(), function vector f[]  and  Jacobian of
-    f[].
-
-    This function is considered obsolete since ALGLIB 3.1.0 and is present for
-    backward  compatibility  only.  We  recommend to use MinLMCreateVGJ, which
-    provides similar, but more consistent interface.
-
-      -- ALGLIB --
-         Copyright 30.03.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void minlmcreatefgj(int n, int m, double[] x, out minlmstate state)
-    {
-        state = new minlmstate();
-        minlm.minlmcreatefgj(n, m, x, state.innerobj);
-        return;
-    }
-    public static void minlmcreatefgj(int m, double[] x, out minlmstate state)
-    {
-        int n;
-
-        state = new minlmstate();
-        n = ap.len(x);
-        minlm.minlmcreatefgj(n, m, x, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
-        CLASSIC LEVENBERG-MARQUARDT METHOD FOR NON-LINEAR OPTIMIZATION
-
-    DESCRIPTION:
-    This function is used to find minimum of function which is represented  as
-    sum of squares:
-        F(x) = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
-    using  value  of  F(),  function  vector  f[] and Jacobian of f[]. Classic
-    Levenberg-Marquardt method is used.
-
-    This function is considered obsolete since ALGLIB 3.1.0 and is present for
-    backward  compatibility  only.  We  recommend  to use MinLMCreateVJ, which
-    provides similar, but more consistent and feature-rich interface.
-
-      -- ALGLIB --
-         Copyright 30.03.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void minlmcreatefj(int n, int m, double[] x, out minlmstate state)
-    {
-        state = new minlmstate();
-        minlm.minlmcreatefj(n, m, x, state.innerobj);
-        return;
-    }
-    public static void minlmcreatefj(int m, double[] x, out minlmstate state)
-    {
-        int n;
-
-        state = new minlmstate();
-        n = ap.len(x);
-        minlm.minlmcreatefj(n, m, x, state.innerobj);
-
-        return;
-    }
-
-    /*************************************************************************
     This function sets stopping conditions for Levenberg-Marquardt optimization
     algorithm.
@@ -912,6 +2687,9 @@
         EpsG    -   >=0
                     The  subroutine  finishes  its  work   if   the  condition
-                    ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                    G - gradient.
+                    |v|<EpsG is satisfied, where:
+                    * |.| means Euclidian norm
+                    * v - scaled gradient vector, v[i]=g[i]*s[i]
+                    * g - gradient
+                    * s - scaling coefficients set by MinLMSetScale()
         EpsF    -   >=0
                     The  subroutine  finishes  its work if on k+1-th iteration
@@ -920,5 +2698,9 @@
         EpsX    -   >=0
                     The subroutine finishes its work if  on  k+1-th  iteration
-                    the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
+                    the condition |v|<=EpsX is fulfilled, where:
+                    * |.| means Euclidian norm
+                    * v - scaled step vector, v[i]=dx[i]/s[i]
+                    * dx - ste pvector, dx=X(k+1)-X(k)
+                    * s - scaling coefficients set by MinLMSetScale()
         MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
                     iterations   is    unlimited.   Only   Levenberg-Marquardt
@@ -986,4 +2768,73 @@
 
         minlm.minlmsetstpmax(state.innerobj, stpmax);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets scaling coefficients for LM optimizer.
+
+    ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+    size and gradient are scaled before comparison with tolerances).  Scale of
+    the I-th variable is a translation invariant measure of:
+    a) "how large" the variable is
+    b) how large the step should be to make significant changes in the function
+
+    Generally, scale is NOT considered to be a form of preconditioner.  But LM
+    optimizer is unique in that it uses scaling matrix both  in  the  stopping
+    condition tests and as Marquardt damping factor.
+
+    Proper scaling is very important for the algorithm performance. It is less
+    important for the quality of results, but still has some influence (it  is
+    easier  to  converge  when  variables  are  properly  scaled, so premature
+    stopping is possible when very badly scalled variables are  combined  with
+    relaxed stopping conditions).
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        S       -   array[N], non-zero scaling coefficients
+                    S[i] may be negative, sign doesn't matter.
+
+      -- ALGLIB --
+         Copyright 14.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlmsetscale(minlmstate state, double[] s)
+    {
+
+        minlm.minlmsetscale(state.innerobj, s);
+        return;
+    }
+
+    /*************************************************************************
+    This function sets boundary constraints for LM optimizer
+
+    Boundary constraints are inactive by default (after initial creation).
+    They are preserved until explicitly turned off with another SetBC() call.
+
+    INPUT PARAMETERS:
+        State   -   structure stores algorithm state
+        BndL    -   lower bounds, array[N].
+                    If some (all) variables are unbounded, you may specify
+                    very small number or -INF (latter is recommended because
+                    it will allow solver to use better algorithm).
+        BndU    -   upper bounds, array[N].
+                    If some (all) variables are unbounded, you may specify
+                    very large number or +INF (latter is recommended because
+                    it will allow solver to use better algorithm).
+
+    NOTE 1: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+    variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+    NOTE 2: this solver has following useful properties:
+    * bound constraints are always satisfied exactly
+    * function is evaluated only INSIDE area specified by bound constraints
+      or at its boundary
+
+      -- ALGLIB --
+         Copyright 14.01.2011 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlmsetbc(minlmstate state, double[] bndl, double[] bndu)
+    {
+
+        minlm.minlmsetbc(state.innerobj, bndl, bndu);
         return;
     }
@@ -1000,6 +2851,4 @@
       settings: about 3 iterations - when we have analytic Jacobian, up to 2*N
       iterations - when we use finite differences to calculate Jacobian.
-    * AccType=2, after quadratic model is built and LM step is made, we use it
-      as preconditioner for several (5-10) iterations of L-BFGS algorithm.
 
     AccType=1 is recommended when Jacobian  calculation  cost  is  prohibitive
@@ -1009,9 +2858,4 @@
     approximation takes too much time to compute.
 
-    AccType=2 is recommended when Jacobian is cheap - much more  cheaper  than
-    one  Cholesky  factorization.   We   can   reduce   number   of   Cholesky
-    factorizations at the cost of increased number of  Jacobian  calculations.
-    Sometimes it helps.
-
     Table below list  optimization  protocols  (XYZ  protocol  corresponds  to
     MinLMCreateXYZ) and acceleration types they support (and use by  default).
@@ -1019,21 +2863,15 @@
     ACCELERATION TYPES SUPPORTED BY OPTIMIZATION PROTOCOLS:
 
-    protocol    0   1   2   comment
+    protocol    0   1   comment
     V           +   +
-    VJ          +   +   +
-    FGH         +       +
-    VGJ         +   +   +   special protocol, not for widespread use
-    FJ          +       +   obsolete protocol, not recommended
-    FGJ         +       +   obsolete protocol, not recommended
+    VJ          +   +
+    FGH         +
 
     DAFAULT VALUES:
 
-    protocol    0   1   2   comment
-    V               x       without acceleration it is so slooooooooow
+    protocol    0   1   comment
+    V               x   without acceleration it is so slooooooooow
     VJ          x
     FGH         x
-    VGJ                 x   we've implicitly turned (2) by passing gradient
-    FJ          x           obsolete protocol, not recommended
-    FGJ                 x   obsolete protocol, not recommended
 
     NOTE: this  function should be called before optimization. Attempt to call
@@ -1317,124 +3155,25 @@
     }
 
-}
-public partial class alglib
-{
-
-
     /*************************************************************************
-
+    This is obsolete function.
+
+    Since ALGLIB 3.3 it is equivalent to MinLMCreateVJ().
+
+      -- ALGLIB --
+         Copyright 30.03.2009 by Bochkanov Sergey
     *************************************************************************/
-    public class mincgstate
+    public static void minlmcreatevgj(int n, int m, double[] x, out minlmstate state)
     {
-        //
-        // Public declarations
-        //
-        public bool needfg { get { return _innerobj.needfg; } set { _innerobj.needfg = value; } }
-        public bool xupdated { get { return _innerobj.xupdated; } set { _innerobj.xupdated = value; } }
-        public double f { get { return _innerobj.f; } set { _innerobj.f = value; } }
-        public double[] g { get { return _innerobj.g; } }
-        public double[] x { get { return _innerobj.x; } }
-
-        public mincgstate()
-        {
-            _innerobj = new mincg.mincgstate();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private mincg.mincgstate _innerobj;
-        public mincg.mincgstate innerobj { get { return _innerobj; } }
-        public mincgstate(mincg.mincgstate obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-
-    /*************************************************************************
-
-    *************************************************************************/
-    public class mincgreport
-    {
-        //
-        // Public declarations
-        //
-        public int iterationscount { get { return _innerobj.iterationscount; } set { _innerobj.iterationscount = value; } }
-        public int nfev { get { return _innerobj.nfev; } set { _innerobj.nfev = value; } }
-        public int terminationtype { get { return _innerobj.terminationtype; } set { _innerobj.terminationtype = value; } }
-
-        public mincgreport()
-        {
-            _innerobj = new mincg.mincgreport();
-        }
-
-        //
-        // Although some of declarations below are public, you should not use them
-        // They are intended for internal use only
-        //
-        private mincg.mincgreport _innerobj;
-        public mincg.mincgreport innerobj { get { return _innerobj; } }
-        public mincgreport(mincg.mincgreport obj)
-        {
-            _innerobj = obj;
-        }
-    }
-
-    /*************************************************************************
-            NONLINEAR CONJUGATE GRADIENT METHOD
-
-    DESCRIPTION:
-    The subroutine minimizes function F(x) of N arguments by using one of  the
-    nonlinear conjugate gradient methods.
-
-    These CG methods are globally convergent (even on non-convex functions) as
-    long as grad(f) is Lipschitz continuous in  a  some  neighborhood  of  the
-    L = { x : f(x)<=f(x0) }.
-
-
-    REQUIREMENTS:
-    Algorithm will request following information during its operation:
-    * function value F and its gradient G (simultaneously) at given point X
-
-
-    USAGE:
-    1. User initializes algorithm state with MinCGCreate() call
-    2. User tunes solver parameters with MinCGSetCond(), MinCGSetStpMax() and
-       other functions
-    3. User calls MinCGOptimize() function which takes algorithm  state   and
-       pointer (delegate, etc.) to callback function which calculates F/G.
-    4. User calls MinCGResults() to get solution
-    5. Optionally, user may call MinCGRestartFrom() to solve another  problem
-       with same N but another starting point and/or another function.
-       MinCGRestartFrom() allows to reuse already initialized structure.
-
-
-    INPUT PARAMETERS:
-        N       -   problem dimension, N>0:
-                    * if given, only leading N elements of X are used
-                    * if not given, automatically determined from size of X
-        X       -   starting point, array[0..N-1].
-
-    OUTPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-
-      -- ALGLIB --
-         Copyright 25.03.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mincgcreate(int n, double[] x, out mincgstate state)
-    {
-        state = new mincgstate();
-        mincg.mincgcreate(n, x, state.innerobj);
+        state = new minlmstate();
+        minlm.minlmcreatevgj(n, m, x, state.innerobj);
         return;
     }
-    public static void mincgcreate(double[] x, out mincgstate state)
+    public static void minlmcreatevgj(int m, double[] x, out minlmstate state)
     {
         int n;
 
-        state = new mincgstate();
+        state = new minlmstate();
         n = ap.len(x);
-        mincg.mincgcreate(n, x, state.innerobj);
+        minlm.minlmcreatevgj(n, m, x, state.innerobj);
 
         return;
@@ -1442,219 +3181,50 @@
 
     /*************************************************************************
-    This function sets stopping conditions for CG optimization algorithm.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        EpsG    -   >=0
-                    The  subroutine  finishes  its  work   if   the  condition
-                    ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                    G - gradient.
-        EpsF    -   >=0
-                    The  subroutine  finishes  its work if on k+1-th iteration
-                    the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
-                    is satisfied.
-        EpsX    -   >=0
-                    The subroutine finishes its work if  on  k+1-th  iteration
-                    the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
-        MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
-                    iterations is unlimited.
-
-    Passing EpsG=0, EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to
-    automatic stopping criterion selection (small EpsX).
+    This is obsolete function.
+
+    Since ALGLIB 3.3 it is equivalent to MinLMCreateFJ().
 
       -- ALGLIB --
-         Copyright 02.04.2010 by Bochkanov Sergey
+         Copyright 30.03.2009 by Bochkanov Sergey
     *************************************************************************/
-    public static void mincgsetcond(mincgstate state, double epsg, double epsf, double epsx, int maxits)
+    public static void minlmcreatefgj(int n, int m, double[] x, out minlmstate state)
     {
-
-        mincg.mincgsetcond(state.innerobj, epsg, epsf, epsx, maxits);
+        state = new minlmstate();
+        minlm.minlmcreatefgj(n, m, x, state.innerobj);
         return;
     }
-
-    /*************************************************************************
-    This function turns on/off reporting.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        NeedXRep-   whether iteration reports are needed or not
-
-    If NeedXRep is True, algorithm will call rep() callback function if  it is
-    provided to MinCGOptimize().
-
-      -- ALGLIB --
-         Copyright 02.04.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mincgsetxrep(mincgstate state, bool needxrep)
+    public static void minlmcreatefgj(int m, double[] x, out minlmstate state)
     {
-
-        mincg.mincgsetxrep(state.innerobj, needxrep);
+        int n;
+
+        state = new minlmstate();
+        n = ap.len(x);
+        minlm.minlmcreatefgj(n, m, x, state.innerobj);
+
         return;
     }
 
     /*************************************************************************
-    This function sets CG algorithm.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        CGType  -   algorithm type:
-                    * -1    automatic selection of the best algorithm
-                    * 0     DY (Dai and Yuan) algorithm
-                    * 1     Hybrid DY-HS algorithm
+    This function is considered obsolete since ALGLIB 3.1.0 and is present for
+    backward  compatibility  only.  We  recommend  to use MinLMCreateVJ, which
+    provides similar, but more consistent and feature-rich interface.
 
       -- ALGLIB --
-         Copyright 02.04.2010 by Bochkanov Sergey
+         Copyright 30.03.2009 by Bochkanov Sergey
     *************************************************************************/
-    public static void mincgsetcgtype(mincgstate state, int cgtype)
+    public static void minlmcreatefj(int n, int m, double[] x, out minlmstate state)
     {
-
-        mincg.mincgsetcgtype(state.innerobj, cgtype);
+        state = new minlmstate();
+        minlm.minlmcreatefj(n, m, x, state.innerobj);
         return;
     }
-
-    /*************************************************************************
-    This function sets maximum step length
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
-                    want to limit step length.
-
-    Use this subroutine when you optimize target function which contains exp()
-    or  other  fast  growing  functions,  and optimization algorithm makes too
-    large  steps  which  leads  to overflow. This function allows us to reject
-    steps  that  are  too  large  (and  therefore  expose  us  to the possible
-    overflow) without actually calculating function value at the x+stp*d.
-
-      -- ALGLIB --
-         Copyright 02.04.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mincgsetstpmax(mincgstate state, double stpmax)
+    public static void minlmcreatefj(int m, double[] x, out minlmstate state)
     {
-
-        mincg.mincgsetstpmax(state.innerobj, stpmax);
-        return;
-    }
-
-    /*************************************************************************
-    This function provides reverse communication interface
-    Reverse communication interface is not documented or recommended to use.
-    See below for functions which provide better documented API
-    *************************************************************************/
-    public static bool mincgiteration(mincgstate state)
-    {
-
-        bool result = mincg.mincgiteration(state.innerobj);
-        return result;
-    }
-    /*************************************************************************
-    This family of functions is used to launcn iterations of nonlinear optimizer
-
-    These functions accept following parameters:
-        grad    -   callback which calculates function (or merit function)
-                    value func and gradient grad at given point x
-        rep     -   optional callback which is called after each iteration
-                    can be null
-        obj     -   optional object which is passed to func/grad/hess/jac/rep
-                    can be null
-
-
-      -- ALGLIB --
-         Copyright 20.04.2009 by Bochkanov Sergey
-
-    *************************************************************************/
-    public static void mincgoptimize(mincgstate state, ndimensional_grad grad, ndimensional_rep rep, object obj)
-    {
-        if( grad==null )
-            throw new alglibexception("ALGLIB: error in 'mincgoptimize()' (grad is null)");
-        while( alglib.mincgiteration(state) )
-        {
-            if( state.needfg )
-            {
-                grad(state.x, ref state.innerobj.f, state.innerobj.g, obj);
-                continue;
-            }
-            if( state.innerobj.xupdated )
-            {
-                if( rep!=null )
-                    rep(state.innerobj.x, state.innerobj.f, obj);
-                continue;
-            }
-            throw new alglibexception("ALGLIB: error in 'mincgoptimize' (some derivatives were not provided?)");
-        }
-    }
-
-
-
-    /*************************************************************************
-    Conjugate gradient results
-
-    INPUT PARAMETERS:
-        State   -   algorithm state
-
-    OUTPUT PARAMETERS:
-        X       -   array[0..N-1], solution
-        Rep     -   optimization report:
-                    * Rep.TerminationType completetion code:
-                        * -2    rounding errors prevent further improvement.
-                                X contains best point found.
-                        * -1    incorrect parameters were specified
-                        *  1    relative function improvement is no more than
-                                EpsF.
-                        *  2    relative step is no more than EpsX.
-                        *  4    gradient norm is no more than EpsG
-                        *  5    MaxIts steps was taken
-                        *  7    stopping conditions are too stringent,
-                                further improvement is impossible
-                    * Rep.IterationsCount contains iterations count
-                    * NFEV countains number of function calculations
-
-      -- ALGLIB --
-         Copyright 20.04.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mincgresults(mincgstate state, out double[] x, out mincgreport rep)
-    {
-        x = new double[0];
-        rep = new mincgreport();
-        mincg.mincgresults(state.innerobj, ref x, rep.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    Conjugate gradient results
-
-    Buffered implementation of MinCGResults(), which uses pre-allocated buffer
-    to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
-    intended to be used in the inner cycles of performance critical algorithms
-    where array reallocation penalty is too large to be ignored.
-
-      -- ALGLIB --
-         Copyright 20.04.2009 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mincgresultsbuf(mincgstate state, ref double[] x, mincgreport rep)
-    {
-
-        mincg.mincgresultsbuf(state.innerobj, ref x, rep.innerobj);
-        return;
-    }
-
-    /*************************************************************************
-    This  subroutine  restarts  CG  algorithm from new point. All optimization
-    parameters are left unchanged.
-
-    This  function  allows  to  solve multiple  optimization  problems  (which
-    must have same number of dimensions) without object reallocation penalty.
-
-    INPUT PARAMETERS:
-        State   -   structure used to store algorithm state.
-        X       -   new starting point.
-
-      -- ALGLIB --
-         Copyright 30.07.2010 by Bochkanov Sergey
-    *************************************************************************/
-    public static void mincgrestartfrom(mincgstate state, double[] x)
-    {
-
-        mincg.mincgrestartfrom(state.innerobj, x);
+        int n;
+
+        state = new minlmstate();
+        n = ap.len(x);
+        minlm.minlmcreatefj(n, m, x, state.innerobj);
+
         return;
     }
@@ -1681,5 +3251,5 @@
         public minasastate()
         {
-            _innerobj = new minasa.minasastate();
+            _innerobj = new mincomp.minasastate();
         }
 
@@ -1688,7 +3258,7 @@
         // They are intended for internal use only
         //
-        private minasa.minasastate _innerobj;
-        public minasa.minasastate innerobj { get { return _innerobj; } }
-        public minasastate(minasa.minasastate obj)
+        private mincomp.minasastate _innerobj;
+        public mincomp.minasastate innerobj { get { return _innerobj; } }
+        public minasastate(mincomp.minasastate obj)
         {
             _innerobj = obj;
@@ -1712,5 +3282,5 @@
         public minasareport()
         {
-            _innerobj = new minasa.minasareport();
+            _innerobj = new mincomp.minasareport();
         }
 
@@ -1719,7 +3289,7 @@
         // They are intended for internal use only
         //
-        private minasa.minasareport _innerobj;
-        public minasa.minasareport innerobj { get { return _innerobj; } }
-        public minasareport(minasa.minasareport obj)
+        private mincomp.minasareport _innerobj;
+        public mincomp.minasareport innerobj { get { return _innerobj; } }
+        public minasareport(mincomp.minasareport obj)
         {
             _innerobj = obj;
@@ -1728,60 +3298,60 @@
 
     /*************************************************************************
-                  NONLINEAR BOUND CONSTRAINED OPTIMIZATION USING
-                          MODIFIED ACTIVE SET ALGORITHM
-                       WILLIAM W. HAGER AND HONGCHAO ZHANG
-
-    DESCRIPTION:
-    The  subroutine  minimizes  function  F(x)  of  N  arguments  with   bound
-    constraints: BndL[i] <= x[i] <= BndU[i]
-
-    This method is  globally  convergent  as  long  as  grad(f)  is  Lipschitz
-    continuous on a level set: L = { x : f(x)<=f(x0) }.
-
-
-    REQUIREMENTS:
-    Algorithm will request following information during its operation:
-    * function value F and its gradient G (simultaneously) at given point X
-
-
-    USAGE:
-    1. User initializes algorithm state with MinASACreate() call
-    2. User tunes solver parameters with MinASASetCond() MinASASetStpMax() and
-       other functions
-    3. User calls MinASAOptimize() function which takes algorithm  state   and
-       pointer (delegate, etc.) to callback function which calculates F/G.
-    4. User calls MinASAResults() to get solution
-    5. Optionally, user may call MinASARestartFrom() to solve another  problem
-       with same N but another starting point and/or another function.
-       MinASARestartFrom() allows to reuse already initialized structure.
-
-
-    INPUT PARAMETERS:
-        N       -   problem dimension, N>0:
-                    * if given, only leading N elements of X are used
-                    * if not given, automatically determined from sizes of
-                      X/BndL/BndU.
-        X       -   starting point, array[0..N-1].
-        BndL    -   lower bounds, array[0..N-1].
-                    all elements MUST be specified,  i.e.  all  variables  are
-                    bounded. However, if some (all) variables  are  unbounded,
-                    you may specify very small number as bound: -1000,  -1.0E6
-                    or -1.0E300, or something like that.
-        BndU    -   upper bounds, array[0..N-1].
-                    all elements MUST be specified,  i.e.  all  variables  are
-                    bounded. However, if some (all) variables  are  unbounded,
-                    you may specify very large number as bound: +1000,  +1.0E6
-                    or +1.0E300, or something like that.
-
-    OUTPUT PARAMETERS:
-        State   -   structure stores algorithm state
-
-    NOTES:
-
-    1. you may tune stopping conditions with MinASASetCond() function
-    2. if target function contains exp() or other fast growing functions,  and
-       optimization algorithm makes too large steps which leads  to  overflow,
-       use MinASASetStpMax() function to bound algorithm's steps.
-    3. this function does NOT support infinite/NaN values in X, BndL, BndU.
+    Obsolete function, use MinLBFGSSetPrecDefault() instead.
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlbfgssetdefaultpreconditioner(minlbfgsstate state)
+    {
+
+        mincomp.minlbfgssetdefaultpreconditioner(state.innerobj);
+        return;
+    }
+
+    /*************************************************************************
+    Obsolete function, use MinLBFGSSetCholeskyPreconditioner() instead.
+
+      -- ALGLIB --
+         Copyright 13.10.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minlbfgssetcholeskypreconditioner(minlbfgsstate state, double[,] p, bool isupper)
+    {
+
+        mincomp.minlbfgssetcholeskypreconditioner(state.innerobj, p, isupper);
+        return;
+    }
+
+    /*************************************************************************
+    This is obsolete function which was used by previous version of the  BLEIC
+    optimizer. It does nothing in the current version of BLEIC.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetbarrierwidth(minbleicstate state, double mu)
+    {
+
+        mincomp.minbleicsetbarrierwidth(state.innerobj, mu);
+        return;
+    }
+
+    /*************************************************************************
+    This is obsolete function which was used by previous version of the  BLEIC
+    optimizer. It does nothing in the current version of BLEIC.
+
+      -- ALGLIB --
+         Copyright 28.11.2010 by Bochkanov Sergey
+    *************************************************************************/
+    public static void minbleicsetbarrierdecay(minbleicstate state, double mudecay)
+    {
+
+        mincomp.minbleicsetbarrierdecay(state.innerobj, mudecay);
+        return;
+    }
+
+    /*************************************************************************
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -1791,5 +3361,5 @@
     {
         state = new minasastate();
-        minasa.minasacreate(n, x, bndl, bndu, state.innerobj);
+        mincomp.minasacreate(n, x, bndl, bndu, state.innerobj);
         return;
     }
@@ -1801,5 +3371,5 @@
         state = new minasastate();
         n = ap.len(x);
-        minasa.minasacreate(n, x, bndl, bndu, state.innerobj);
+        mincomp.minasacreate(n, x, bndl, bndu, state.innerobj);
 
         return;
@@ -1807,24 +3377,6 @@
 
     /*************************************************************************
-    This function sets stopping conditions for the ASA optimization algorithm.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        EpsG    -   >=0
-                    The  subroutine  finishes  its  work   if   the  condition
-                    ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                    G - gradient.
-        EpsF    -   >=0
-                    The  subroutine  finishes  its work if on k+1-th iteration
-                    the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
-                    is satisfied.
-        EpsX    -   >=0
-                    The subroutine finishes its work if  on  k+1-th  iteration
-                    the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
-        MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
-                    iterations is unlimited.
-
-    Passing EpsG=0, EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to
-    automatic stopping criterion selection (small EpsX).
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -1834,17 +3386,11 @@
     {
 
-        minasa.minasasetcond(state.innerobj, epsg, epsf, epsx, maxits);
+        mincomp.minasasetcond(state.innerobj, epsg, epsf, epsx, maxits);
         return;
     }
 
     /*************************************************************************
-    This function turns on/off reporting.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        NeedXRep-   whether iteration reports are needed or not
-
-    If NeedXRep is True, algorithm will call rep() callback function if  it is
-    provided to MinASAOptimize().
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -1854,17 +3400,11 @@
     {
 
-        minasa.minasasetxrep(state.innerobj, needxrep);
+        mincomp.minasasetxrep(state.innerobj, needxrep);
         return;
     }
 
     /*************************************************************************
-    This function sets optimization algorithm.
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm stat
-        UAType  -   algorithm type:
-                    * -1    automatic selection of the best algorithm
-                    * 0     DY (Dai and Yuan) algorithm
-                    * 1     Hybrid DY-HS algorithm
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -1874,21 +3414,11 @@
     {
 
-        minasa.minasasetalgorithm(state.innerobj, algotype);
+        mincomp.minasasetalgorithm(state.innerobj, algotype);
         return;
     }
 
     /*************************************************************************
-    This function sets maximum step length
-
-    INPUT PARAMETERS:
-        State   -   structure which stores algorithm state
-        StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
-                    want to limit step length (zero by default).
-
-    Use this subroutine when you optimize target function which contains exp()
-    or  other  fast  growing  functions,  and optimization algorithm makes too
-    large  steps  which  leads  to overflow. This function allows us to reject
-    steps  that  are  too  large  (and  therefore  expose  us  to the possible
-    overflow) without actually calculating function value at the x+stp*d.
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -1898,5 +3428,5 @@
     {
 
-        minasa.minasasetstpmax(state.innerobj, stpmax);
+        mincomp.minasasetstpmax(state.innerobj, stpmax);
         return;
     }
@@ -1910,5 +3440,5 @@
     {
 
-        bool result = minasa.minasaiteration(state.innerobj);
+        bool result = mincomp.minasaiteration(state.innerobj);
         return result;
     }
@@ -1953,26 +3483,6 @@
 
     /*************************************************************************
-    ASA results
-
-    INPUT PARAMETERS:
-        State   -   algorithm state
-
-    OUTPUT PARAMETERS:
-        X       -   array[0..N-1], solution
-        Rep     -   optimization report:
-                    * Rep.TerminationType completetion code:
-                        * -2    rounding errors prevent further improvement.
-                                X contains best point found.
-                        * -1    incorrect parameters were specified
-                        *  1    relative function improvement is no more than
-                                EpsF.
-                        *  2    relative step is no more than EpsX.
-                        *  4    gradient norm is no more than EpsG
-                        *  5    MaxIts steps was taken
-                        *  7    stopping conditions are too stringent,
-                                further improvement is impossible
-                    * Rep.IterationsCount contains iterations count
-                    * NFEV countains number of function calculations
-                    * ActiveConstraints contains number of active constraints
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -1983,15 +3493,11 @@
         x = new double[0];
         rep = new minasareport();
-        minasa.minasaresults(state.innerobj, ref x, rep.innerobj);
+        mincomp.minasaresults(state.innerobj, ref x, rep.innerobj);
         return;
     }
 
     /*************************************************************************
-    ASA results
-
-    Buffered implementation of MinASAResults() which uses pre-allocated buffer
-    to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
-    intended to be used in the inner cycles of performance critical algorithms
-    where array reallocation penalty is too large to be ignored.
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -2001,20 +3507,11 @@
     {
 
-        minasa.minasaresultsbuf(state.innerobj, ref x, rep.innerobj);
+        mincomp.minasaresultsbuf(state.innerobj, ref x, rep.innerobj);
         return;
     }
 
     /*************************************************************************
-    This  subroutine  restarts  CG  algorithm from new point. All optimization
-    parameters are left unchanged.
-
-    This  function  allows  to  solve multiple  optimization  problems  (which
-    must have same number of dimensions) without object reallocation penalty.
-
-    INPUT PARAMETERS:
-        State   -   structure previously allocated with MinCGCreate call.
-        X       -   new starting point.
-        BndL    -   new lower bounds
-        BndU    -   new upper bounds
+    Obsolete optimization algorithm.
+    Was replaced by MinBLEIC subpackage.
 
       -- ALGLIB --
@@ -2024,5 +3521,5 @@
     {
 
-        minasa.minasarestartfrom(state.innerobj, x, bndl, bndu);
+        mincomp.minasarestartfrom(state.innerobj, x, bndl, bndu);
         return;
     }
@@ -2031,4 +3528,4111 @@
 public partial class alglib
 {
+    public class mincg
+    {
+        /*************************************************************************
+        This object stores state of the nonlinear CG optimizer.
+
+        You should use ALGLIB functions to work with this object.
+        *************************************************************************/
+        public class mincgstate
+        {
+            public int n;
+            public double epsg;
+            public double epsf;
+            public double epsx;
+            public int maxits;
+            public double stpmax;
+            public double suggestedstep;
+            public bool xrep;
+            public bool drep;
+            public int cgtype;
+            public int prectype;
+            public double[] diagh;
+            public double[] diaghl2;
+            public double[,] vcorr;
+            public int vcnt;
+            public double[] s;
+            public double diffstep;
+            public int nfev;
+            public int mcstage;
+            public int k;
+            public double[] xk;
+            public double[] dk;
+            public double[] xn;
+            public double[] dn;
+            public double[] d;
+            public double fold;
+            public double stp;
+            public double curstpmax;
+            public double[] yk;
+            public double laststep;
+            public double lastscaledstep;
+            public int mcinfo;
+            public bool innerresetneeded;
+            public bool terminationneeded;
+            public double trimthreshold;
+            public int rstimer;
+            public double[] x;
+            public double f;
+            public double[] g;
+            public bool needf;
+            public bool needfg;
+            public bool xupdated;
+            public bool algpowerup;
+            public bool lsstart;
+            public bool lsend;
+            public rcommstate rstate;
+            public int repiterationscount;
+            public int repnfev;
+            public int repterminationtype;
+            public int debugrestartscount;
+            public linmin.linminstate lstate;
+            public double fbase;
+            public double fm2;
+            public double fm1;
+            public double fp1;
+            public double fp2;
+            public double betahs;
+            public double betady;
+            public double[] work0;
+            public double[] work1;
+            public mincgstate()
+            {
+                diagh = new double[0];
+                diaghl2 = new double[0];
+                vcorr = new double[0,0];
+                s = new double[0];
+                xk = new double[0];
+                dk = new double[0];
+                xn = new double[0];
+                dn = new double[0];
+                d = new double[0];
+                yk = new double[0];
+                x = new double[0];
+                g = new double[0];
+                rstate = new rcommstate();
+                lstate = new linmin.linminstate();
+                work0 = new double[0];
+                work1 = new double[0];
+            }
+        };
+
+
+        public class mincgreport
+        {
+            public int iterationscount;
+            public int nfev;
+            public int terminationtype;
+        };
+
+
+
+
+        public const int rscountdownlen = 10;
+        public const double gtol = 0.3;
+
+
+        /*************************************************************************
+                NONLINEAR CONJUGATE GRADIENT METHOD
+
+        DESCRIPTION:
+        The subroutine minimizes function F(x) of N arguments by using one of  the
+        nonlinear conjugate gradient methods.
+
+        These CG methods are globally convergent (even on non-convex functions) as
+        long as grad(f) is Lipschitz continuous in  a  some  neighborhood  of  the
+        L = { x : f(x)<=f(x0) }.
+
+
+        REQUIREMENTS:
+        Algorithm will request following information during its operation:
+        * function value F and its gradient G (simultaneously) at given point X
+
+
+        USAGE:
+        1. User initializes algorithm state with MinCGCreate() call
+        2. User tunes solver parameters with MinCGSetCond(), MinCGSetStpMax() and
+           other functions
+        3. User calls MinCGOptimize() function which takes algorithm  state   and
+           pointer (delegate, etc.) to callback function which calculates F/G.
+        4. User calls MinCGResults() to get solution
+        5. Optionally, user may call MinCGRestartFrom() to solve another  problem
+           with same N but another starting point and/or another function.
+           MinCGRestartFrom() allows to reuse already initialized structure.
+
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>0:
+                        * if given, only leading N elements of X are used
+                        * if not given, automatically determined from size of X
+            X       -   starting point, array[0..N-1].
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 25.03.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgcreate(int n,
+            double[] x,
+            mincgstate state)
+        {
+            ap.assert(n>=1, "MinCGCreate: N too small!");
+            ap.assert(ap.len(x)>=n, "MinCGCreate: Length(X)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "MinCGCreate: X contains infinite or NaN values!");
+            mincginitinternal(n, 0.0, state);
+            mincgrestartfrom(state, x);
+        }
+
+
+        /*************************************************************************
+        The subroutine is finite difference variant of MinCGCreate(). It uses
+        finite differences in order to differentiate target function.
+
+        Description below contains information which is specific to this function
+        only. We recommend to read comments on MinCGCreate() in order to get more
+        information about creation of CG optimizer.
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>0:
+                        * if given, only leading N elements of X are used
+                        * if not given, automatically determined from size of X
+            X       -   starting point, array[0..N-1].
+            DiffStep-   differentiation step, >0
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+        NOTES:
+        1. algorithm uses 4-point central formula for differentiation.
+        2. differentiation step along I-th axis is equal to DiffStep*S[I] where
+           S[] is scaling vector which can be set by MinCGSetScale() call.
+        3. we recommend you to use moderate values of  differentiation  step.  Too
+           large step will result in too large truncation  errors, while too small
+           step will result in too large numerical  errors.  1.0E-6  can  be  good
+           value to start with.
+        4. Numerical  differentiation  is   very   inefficient  -   one   gradient
+           calculation needs 4*N function evaluations. This function will work for
+           any N - either small (1...10), moderate (10...100) or  large  (100...).
+           However, performance penalty will be too severe for any N's except  for
+           small ones.
+           We should also say that code which relies on numerical  differentiation
+           is  less  robust  and  precise.  L-BFGS  needs  exact  gradient values.
+           Imprecise  gradient may slow down  convergence,  especially  on  highly
+           nonlinear problems.
+           Thus  we  recommend to use this function for fast prototyping on small-
+           dimensional problems only, and to implement analytical gradient as soon
+           as possible.
+
+          -- ALGLIB --
+             Copyright 16.05.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgcreatef(int n,
+            double[] x,
+            double diffstep,
+            mincgstate state)
+        {
+            ap.assert(n>=1, "MinCGCreateF: N too small!");
+            ap.assert(ap.len(x)>=n, "MinCGCreateF: Length(X)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "MinCGCreateF: X contains infinite or NaN values!");
+            ap.assert(math.isfinite(diffstep), "MinCGCreateF: DiffStep is infinite or NaN!");
+            ap.assert((double)(diffstep)>(double)(0), "MinCGCreateF: DiffStep is non-positive!");
+            mincginitinternal(n, diffstep, state);
+            mincgrestartfrom(state, x);
+        }
+
+
+        /*************************************************************************
+        This function sets stopping conditions for CG optimization algorithm.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            EpsG    -   >=0
+                        The  subroutine  finishes  its  work   if   the  condition
+                        |v|<EpsG is satisfied, where:
+                        * |.| means Euclidian norm
+                        * v - scaled gradient vector, v[i]=g[i]*s[i]
+                        * g - gradient
+                        * s - scaling coefficients set by MinCGSetScale()
+            EpsF    -   >=0
+                        The  subroutine  finishes  its work if on k+1-th iteration
+                        the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
+                        is satisfied.
+            EpsX    -   >=0
+                        The subroutine finishes its work if  on  k+1-th  iteration
+                        the condition |v|<=EpsX is fulfilled, where:
+                        * |.| means Euclidian norm
+                        * v - scaled step vector, v[i]=dx[i]/s[i]
+                        * dx - ste pvector, dx=X(k+1)-X(k)
+                        * s - scaling coefficients set by MinCGSetScale()
+            MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
+                        iterations is unlimited.
+
+        Passing EpsG=0, EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to
+        automatic stopping criterion selection (small EpsX).
+
+          -- ALGLIB --
+             Copyright 02.04.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetcond(mincgstate state,
+            double epsg,
+            double epsf,
+            double epsx,
+            int maxits)
+        {
+            ap.assert(math.isfinite(epsg), "MinCGSetCond: EpsG is not finite number!");
+            ap.assert((double)(epsg)>=(double)(0), "MinCGSetCond: negative EpsG!");
+            ap.assert(math.isfinite(epsf), "MinCGSetCond: EpsF is not finite number!");
+            ap.assert((double)(epsf)>=(double)(0), "MinCGSetCond: negative EpsF!");
+            ap.assert(math.isfinite(epsx), "MinCGSetCond: EpsX is not finite number!");
+            ap.assert((double)(epsx)>=(double)(0), "MinCGSetCond: negative EpsX!");
+            ap.assert(maxits>=0, "MinCGSetCond: negative MaxIts!");
+            if( (((double)(epsg)==(double)(0) & (double)(epsf)==(double)(0)) & (double)(epsx)==(double)(0)) & maxits==0 )
+            {
+                epsx = 1.0E-6;
+            }
+            state.epsg = epsg;
+            state.epsf = epsf;
+            state.epsx = epsx;
+            state.maxits = maxits;
+        }
+
+
+        /*************************************************************************
+        This function sets scaling coefficients for CG optimizer.
+
+        ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+        size and gradient are scaled before comparison with tolerances).  Scale of
+        the I-th variable is a translation invariant measure of:
+        a) "how large" the variable is
+        b) how large the step should be to make significant changes in the function
+
+        Scaling is also used by finite difference variant of CG optimizer  -  step
+        along I-th axis is equal to DiffStep*S[I].
+
+        In   most   optimizers  (and  in  the  CG  too)  scaling is NOT a form  of
+        preconditioning. It just  affects  stopping  conditions.  You  should  set
+        preconditioner by separate call to one of the MinCGSetPrec...() functions.
+
+        There  is  special  preconditioning  mode, however,  which  uses   scaling
+        coefficients to form diagonal preconditioning matrix. You  can  turn  this
+        mode on, if you want.   But  you should understand that scaling is not the
+        same thing as preconditioning - these are two different, although  related
+        forms of tuning solver.
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            S       -   array[N], non-zero scaling coefficients
+                        S[i] may be negative, sign doesn't matter.
+
+          -- ALGLIB --
+             Copyright 14.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetscale(mincgstate state,
+            double[] s)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(s)>=state.n, "MinCGSetScale: Length(S)<N");
+            for(i=0; i<=state.n-1; i++)
+            {
+                ap.assert(math.isfinite(s[i]), "MinCGSetScale: S contains infinite or NAN elements");
+                ap.assert((double)(s[i])!=(double)(0), "MinCGSetScale: S contains zero elements");
+                state.s[i] = Math.Abs(s[i]);
+            }
+        }
+
+
+        /*************************************************************************
+        This function turns on/off reporting.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            NeedXRep-   whether iteration reports are needed or not
+
+        If NeedXRep is True, algorithm will call rep() callback function if  it is
+        provided to MinCGOptimize().
+
+          -- ALGLIB --
+             Copyright 02.04.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetxrep(mincgstate state,
+            bool needxrep)
+        {
+            state.xrep = needxrep;
+        }
+
+
+        /*************************************************************************
+        This function turns on/off line search reports.
+        These reports are described in more details in developer-only  comments on
+        MinCGState object.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            NeedDRep-   whether line search reports are needed or not
+
+        This function is intended for private use only. Turning it on artificially
+        may cause program failure.
+
+          -- ALGLIB --
+             Copyright 02.04.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetdrep(mincgstate state,
+            bool needdrep)
+        {
+            state.drep = needdrep;
+        }
+
+
+        /*************************************************************************
+        This function sets CG algorithm.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            CGType  -   algorithm type:
+                        * -1    automatic selection of the best algorithm
+                        * 0     DY (Dai and Yuan) algorithm
+                        * 1     Hybrid DY-HS algorithm
+
+          -- ALGLIB --
+             Copyright 02.04.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetcgtype(mincgstate state,
+            int cgtype)
+        {
+            ap.assert(cgtype>=-1 & cgtype<=1, "MinCGSetCGType: incorrect CGType!");
+            if( cgtype==-1 )
+            {
+                cgtype = 1;
+            }
+            state.cgtype = cgtype;
+        }
+
+
+        /*************************************************************************
+        This function sets maximum step length
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
+                        want to limit step length.
+
+        Use this subroutine when you optimize target function which contains exp()
+        or  other  fast  growing  functions,  and optimization algorithm makes too
+        large  steps  which  leads  to overflow. This function allows us to reject
+        steps  that  are  too  large  (and  therefore  expose  us  to the possible
+        overflow) without actually calculating function value at the x+stp*d.
+
+          -- ALGLIB --
+             Copyright 02.04.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetstpmax(mincgstate state,
+            double stpmax)
+        {
+            ap.assert(math.isfinite(stpmax), "MinCGSetStpMax: StpMax is not finite!");
+            ap.assert((double)(stpmax)>=(double)(0), "MinCGSetStpMax: StpMax<0!");
+            state.stpmax = stpmax;
+        }
+
+
+        /*************************************************************************
+        This function allows to suggest initial step length to the CG algorithm.
+
+        Suggested  step  length  is used as starting point for the line search. It
+        can be useful when you have  badly  scaled  problem,  i.e.  when  ||grad||
+        (which is used as initial estimate for the first step) is many  orders  of
+        magnitude different from the desired step.
+
+        Line search  may  fail  on  such problems without good estimate of initial
+        step length. Imagine, for example, problem with ||grad||=10^50 and desired
+        step equal to 0.1 Line  search function will use 10^50  as  initial  step,
+        then  it  will  decrease step length by 2 (up to 20 attempts) and will get
+        10^44, which is still too large.
+
+        This function allows us to tell than line search should  be  started  from
+        some moderate step length, like 1.0, so algorithm will be able  to  detect
+        desired step length in a several searches.
+
+        Default behavior (when no step is suggested) is to use preconditioner,  if
+        it is available, to generate initial estimate of step length.
+
+        This function influences only first iteration of algorithm. It  should  be
+        called between MinCGCreate/MinCGRestartFrom() call and MinCGOptimize call.
+        Suggested step is ignored if you have preconditioner.
+
+        INPUT PARAMETERS:
+            State   -   structure used to store algorithm state.
+            Stp     -   initial estimate of the step length.
+                        Can be zero (no estimate).
+
+          -- ALGLIB --
+             Copyright 30.07.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsuggeststep(mincgstate state,
+            double stp)
+        {
+            ap.assert(math.isfinite(stp), "MinCGSuggestStep: Stp is infinite or NAN");
+            ap.assert((double)(stp)>=(double)(0), "MinCGSuggestStep: Stp<0");
+            state.suggestedstep = stp;
+        }
+
+
+        /*************************************************************************
+        Modification of the preconditioner: preconditioning is turned off.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+        NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+        iterations.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetprecdefault(mincgstate state)
+        {
+            state.prectype = 0;
+            state.innerresetneeded = true;
+        }
+
+
+        /*************************************************************************
+        Modification  of  the  preconditioner:  diagonal of approximate Hessian is
+        used.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            D       -   diagonal of the approximate Hessian, array[0..N-1],
+                        (if larger, only leading N elements are used).
+
+        NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+        iterations.
+
+        NOTE 2: D[i] should be positive. Exception will be thrown otherwise.
+
+        NOTE 3: you should pass diagonal of approximate Hessian - NOT ITS INVERSE.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetprecdiag(mincgstate state,
+            double[] d)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(d)>=state.n, "MinCGSetPrecDiag: D is too short");
+            for(i=0; i<=state.n-1; i++)
+            {
+                ap.assert(math.isfinite(d[i]), "MinCGSetPrecDiag: D contains infinite or NAN elements");
+                ap.assert((double)(d[i])>(double)(0), "MinCGSetPrecDiag: D contains non-positive elements");
+            }
+            mincgsetprecdiagfast(state, d);
+        }
+
+
+        /*************************************************************************
+        Modification of the preconditioner: scale-based diagonal preconditioning.
+
+        This preconditioning mode can be useful when you  don't  have  approximate
+        diagonal of Hessian, but you know that your  variables  are  badly  scaled
+        (for  example,  one  variable is in [1,10], and another in [1000,100000]),
+        and most part of the ill-conditioning comes from different scales of vars.
+
+        In this case simple  scale-based  preconditioner,  with H[i] = 1/(s[i]^2),
+        can greatly improve convergence.
+
+        IMPRTANT: you should set scale of your variables with MinCGSetScale() call
+        (before or after MinCGSetPrecScale() call). Without knowledge of the scale
+        of your variables scale-based preconditioner will be just unit matrix.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+        NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+        iterations.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetprecscale(mincgstate state)
+        {
+            state.prectype = 3;
+            state.innerresetneeded = true;
+        }
+
+
+        /*************************************************************************
+        NOTES:
+
+        1. This function has two different implementations: one which  uses  exact
+           (analytical) user-supplied  gradient, and one which uses function value
+           only  and  numerically  differentiates  function  in  order  to  obtain
+           gradient.
+           
+           Depending  on  the  specific  function  used to create optimizer object
+           (either MinCGCreate()  for analytical gradient  or  MinCGCreateF()  for
+           numerical differentiation) you should  choose  appropriate  variant  of
+           MinCGOptimize() - one which accepts function AND gradient or one  which
+           accepts function ONLY.
+
+           Be careful to choose variant of MinCGOptimize()  which  corresponds  to
+           your optimization scheme! Table below lists different  combinations  of
+           callback (function/gradient) passed  to  MinCGOptimize()  and  specific
+           function used to create optimizer.
+           
+
+                          |         USER PASSED TO MinCGOptimize()
+           CREATED WITH   |  function only   |  function and gradient
+           ------------------------------------------------------------
+           MinCGCreateF() |     work                FAIL
+           MinCGCreate()  |     FAIL                work
+
+           Here "FAIL" denotes inappropriate combinations  of  optimizer  creation
+           function and MinCGOptimize() version. Attemps to use  such  combination
+           (for  example,  to create optimizer with  MinCGCreateF()  and  to  pass
+           gradient information to MinCGOptimize()) will lead to  exception  being
+           thrown. Either  you  did  not  pass  gradient when it WAS needed or you
+           passed gradient when it was NOT needed.
+
+          -- ALGLIB --
+             Copyright 20.04.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool mincgiteration(mincgstate state)
+        {
+            bool result = new bool();
+            int n = 0;
+            int i = 0;
+            double betak = 0;
+            double v = 0;
+            double vv = 0;
+            int i_ = 0;
+
+            
+            //
+            // Reverse communication preparations
+            // I know it looks ugly, but it works the same way
+            // anywhere from C++ to Python.
+            //
+            // This code initializes locals by:
+            // * random values determined during code
+            //   generation - on first subroutine call
+            // * values from previous call - on subsequent calls
+            //
+            if( state.rstate.stage>=0 )
+            {
+                n = state.rstate.ia[0];
+                i = state.rstate.ia[1];
+                betak = state.rstate.ra[0];
+                v = state.rstate.ra[1];
+                vv = state.rstate.ra[2];
+            }
+            else
+            {
+                n = -983;
+                i = -989;
+                betak = -834;
+                v = 900;
+                vv = -287;
+            }
+            if( state.rstate.stage==0 )
+            {
+                goto lbl_0;
+            }
+            if( state.rstate.stage==1 )
+            {
+                goto lbl_1;
+            }
+            if( state.rstate.stage==2 )
+            {
+                goto lbl_2;
+            }
+            if( state.rstate.stage==3 )
+            {
+                goto lbl_3;
+            }
+            if( state.rstate.stage==4 )
+            {
+                goto lbl_4;
+            }
+            if( state.rstate.stage==5 )
+            {
+                goto lbl_5;
+            }
+            if( state.rstate.stage==6 )
+            {
+                goto lbl_6;
+            }
+            if( state.rstate.stage==7 )
+            {
+                goto lbl_7;
+            }
+            if( state.rstate.stage==8 )
+            {
+                goto lbl_8;
+            }
+            if( state.rstate.stage==9 )
+            {
+                goto lbl_9;
+            }
+            if( state.rstate.stage==10 )
+            {
+                goto lbl_10;
+            }
+            if( state.rstate.stage==11 )
+            {
+                goto lbl_11;
+            }
+            if( state.rstate.stage==12 )
+            {
+                goto lbl_12;
+            }
+            if( state.rstate.stage==13 )
+            {
+                goto lbl_13;
+            }
+            if( state.rstate.stage==14 )
+            {
+                goto lbl_14;
+            }
+            if( state.rstate.stage==15 )
+            {
+                goto lbl_15;
+            }
+            if( state.rstate.stage==16 )
+            {
+                goto lbl_16;
+            }
+            
+            //
+            // Routine body
+            //
+            
+            //
+            // Prepare
+            //
+            n = state.n;
+            state.repterminationtype = 0;
+            state.repiterationscount = 0;
+            state.repnfev = 0;
+            state.debugrestartscount = 0;
+            
+            //
+            // Preparations continue:
+            // * set XK
+            // * calculate F/G
+            // * set DK to -G
+            // * powerup algo (it may change preconditioner)
+            // * apply preconditioner to DK
+            // * report update of X
+            // * check stopping conditions for G
+            //
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.xk[i_] = state.x[i_];
+            }
+            state.terminationneeded = false;
+            clearrequestfields(state);
+            if( (double)(state.diffstep)!=(double)(0) )
+            {
+                goto lbl_17;
+            }
+            state.needfg = true;
+            state.rstate.stage = 0;
+            goto lbl_rcomm;
+        lbl_0:
+            state.needfg = false;
+            goto lbl_18;
+        lbl_17:
+            state.needf = true;
+            state.rstate.stage = 1;
+            goto lbl_rcomm;
+        lbl_1:
+            state.fbase = state.f;
+            i = 0;
+        lbl_19:
+            if( i>n-1 )
+            {
+                goto lbl_21;
+            }
+            v = state.x[i];
+            state.x[i] = v-state.diffstep*state.s[i];
+            state.rstate.stage = 2;
+            goto lbl_rcomm;
+        lbl_2:
+            state.fm2 = state.f;
+            state.x[i] = v-0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 3;
+            goto lbl_rcomm;
+        lbl_3:
+            state.fm1 = state.f;
+            state.x[i] = v+0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 4;
+            goto lbl_rcomm;
+        lbl_4:
+            state.fp1 = state.f;
+            state.x[i] = v+state.diffstep*state.s[i];
+            state.rstate.stage = 5;
+            goto lbl_rcomm;
+        lbl_5:
+            state.fp2 = state.f;
+            state.x[i] = v;
+            state.g[i] = (8*(state.fp1-state.fm1)-(state.fp2-state.fm2))/(6*state.diffstep*state.s[i]);
+            i = i+1;
+            goto lbl_19;
+        lbl_21:
+            state.f = state.fbase;
+            state.needf = false;
+        lbl_18:
+            if( !state.drep )
+            {
+                goto lbl_22;
+            }
+            
+            //
+            // Report algorithm powerup (if needed)
+            //
+            clearrequestfields(state);
+            state.algpowerup = true;
+            state.rstate.stage = 6;
+            goto lbl_rcomm;
+        lbl_6:
+            state.algpowerup = false;
+        lbl_22:
+            optserv.trimprepare(state.f, ref state.trimthreshold);
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.dk[i_] = -state.g[i_];
+            }
+            preconditionedmultiply(state, ref state.dk, ref state.work0, ref state.work1);
+            if( !state.xrep )
+            {
+                goto lbl_24;
+            }
+            clearrequestfields(state);
+            state.xupdated = true;
+            state.rstate.stage = 7;
+            goto lbl_rcomm;
+        lbl_7:
+            state.xupdated = false;
+        lbl_24:
+            if( state.terminationneeded )
+            {
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.xn[i_] = state.xk[i_];
+                }
+                state.repterminationtype = 8;
+                result = false;
+                return result;
+            }
+            v = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                v = v+math.sqr(state.g[i]*state.s[i]);
+            }
+            if( (double)(Math.Sqrt(v))<=(double)(state.epsg) )
+            {
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.xn[i_] = state.xk[i_];
+                }
+                state.repterminationtype = 4;
+                result = false;
+                return result;
+            }
+            state.repnfev = 1;
+            state.k = 0;
+            state.fold = state.f;
+            
+            //
+            // Choose initial step.
+            // Apply preconditioner, if we have something other than default.
+            //
+            if( state.prectype==2 | state.prectype==3 )
+            {
+                
+                //
+                // because we use preconditioner, step length must be equal
+                // to the norm of DK
+                //
+                v = 0.0;
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    v += state.dk[i_]*state.dk[i_];
+                }
+                state.laststep = Math.Sqrt(v);
+            }
+            else
+            {
+                
+                //
+                // No preconditioner is used, we try to use suggested step
+                //
+                if( (double)(state.suggestedstep)>(double)(0) )
+                {
+                    state.laststep = state.suggestedstep;
+                }
+                else
+                {
+                    v = 0.0;
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        v += state.g[i_]*state.g[i_];
+                    }
+                    v = Math.Sqrt(v);
+                    if( (double)(state.stpmax)==(double)(0) )
+                    {
+                        state.laststep = Math.Min(1.0/v, 1);
+                    }
+                    else
+                    {
+                        state.laststep = Math.Min(1.0/v, state.stpmax);
+                    }
+                }
+            }
+            
+            //
+            // Main cycle
+            //
+            state.rstimer = rscountdownlen;
+        lbl_26:
+            if( false )
+            {
+                goto lbl_27;
+            }
+            
+            //
+            // * clear reset flag
+            // * clear termination flag
+            // * store G[k] for later calculation of Y[k]
+            // * prepare starting point and direction and step length for line search
+            //
+            state.innerresetneeded = false;
+            state.terminationneeded = false;
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.yk[i_] = -state.g[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.d[i_] = state.dk[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.x[i_] = state.xk[i_];
+            }
+            state.mcstage = 0;
+            state.stp = 1.0;
+            linmin.linminnormalized(ref state.d, ref state.stp, n);
+            if( (double)(state.laststep)!=(double)(0) )
+            {
+                state.stp = state.laststep;
+            }
+            state.curstpmax = state.stpmax;
+            
+            //
+            // Report beginning of line search (if needed)
+            // Terminate algorithm, if user request was detected
+            //
+            if( !state.drep )
+            {
+                goto lbl_28;
+            }
+            clearrequestfields(state);
+            state.lsstart = true;
+            state.rstate.stage = 8;
+            goto lbl_rcomm;
+        lbl_8:
+            state.lsstart = false;
+        lbl_28:
+            if( state.terminationneeded )
+            {
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.xn[i_] = state.x[i_];
+                }
+                state.repterminationtype = 8;
+                result = false;
+                return result;
+            }
+            
+            //
+            // Minimization along D
+            //
+            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.curstpmax, gtol, ref state.mcinfo, ref state.nfev, ref state.work0, state.lstate, ref state.mcstage);
+        lbl_30:
+            if( state.mcstage==0 )
+            {
+                goto lbl_31;
+            }
+            
+            //
+            // Calculate function/gradient using either
+            // analytical gradient supplied by user
+            // or finite difference approximation.
+            //
+            // "Trim" function in order to handle near-singularity points.
+            //
+            clearrequestfields(state);
+            if( (double)(state.diffstep)!=(double)(0) )
+            {
+                goto lbl_32;
+            }
+            state.needfg = true;
+            state.rstate.stage = 9;
+            goto lbl_rcomm;
+        lbl_9:
+            state.needfg = false;
+            goto lbl_33;
+        lbl_32:
+            state.needf = true;
+            state.rstate.stage = 10;
+            goto lbl_rcomm;
+        lbl_10:
+            state.fbase = state.f;
+            i = 0;
+        lbl_34:
+            if( i>n-1 )
+            {
+                goto lbl_36;
+            }
+            v = state.x[i];
+            state.x[i] = v-state.diffstep*state.s[i];
+            state.rstate.stage = 11;
+            goto lbl_rcomm;
+        lbl_11:
+            state.fm2 = state.f;
+            state.x[i] = v-0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 12;
+            goto lbl_rcomm;
+        lbl_12:
+            state.fm1 = state.f;
+            state.x[i] = v+0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 13;
+            goto lbl_rcomm;
+        lbl_13:
+            state.fp1 = state.f;
+            state.x[i] = v+state.diffstep*state.s[i];
+            state.rstate.stage = 14;
+            goto lbl_rcomm;
+        lbl_14:
+            state.fp2 = state.f;
+            state.x[i] = v;
+            state.g[i] = (8*(state.fp1-state.fm1)-(state.fp2-state.fm2))/(6*state.diffstep*state.s[i]);
+            i = i+1;
+            goto lbl_34;
+        lbl_36:
+            state.f = state.fbase;
+            state.needf = false;
+        lbl_33:
+            optserv.trimfunction(ref state.f, ref state.g, n, state.trimthreshold);
+            
+            //
+            // Call MCSRCH again
+            //
+            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.curstpmax, gtol, ref state.mcinfo, ref state.nfev, ref state.work0, state.lstate, ref state.mcstage);
+            goto lbl_30;
+        lbl_31:
+            
+            //
+            // * report end of line search
+            // * store current point to XN
+            // * report iteration
+            // * terminate algorithm if user request was detected
+            //
+            if( !state.drep )
+            {
+                goto lbl_37;
+            }
+            
+            //
+            // Report end of line search (if needed)
+            //
+            clearrequestfields(state);
+            state.lsend = true;
+            state.rstate.stage = 15;
+            goto lbl_rcomm;
+        lbl_15:
+            state.lsend = false;
+        lbl_37:
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.xn[i_] = state.x[i_];
+            }
+            if( !state.xrep )
+            {
+                goto lbl_39;
+            }
+            clearrequestfields(state);
+            state.xupdated = true;
+            state.rstate.stage = 16;
+            goto lbl_rcomm;
+        lbl_16:
+            state.xupdated = false;
+        lbl_39:
+            if( state.terminationneeded )
+            {
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.xn[i_] = state.x[i_];
+                }
+                state.repterminationtype = 8;
+                result = false;
+                return result;
+            }
+            
+            //
+            // Line search is finished.
+            // * calculate BetaK
+            // * calculate DN
+            // * update timers
+            // * calculate step length
+            //
+            if( state.mcinfo==1 & !state.innerresetneeded )
+            {
+                
+                //
+                // Standard Wolfe conditions hold
+                // Calculate Y[K] and D[K]'*Y[K]
+                //
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.yk[i_] = state.yk[i_] + state.g[i_];
+                }
+                vv = 0.0;
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    vv += state.yk[i_]*state.dk[i_];
+                }
+                
+                //
+                // Calculate BetaK according to DY formula
+                //
+                v = preconditionedmultiply2(state, ref state.g, ref state.g, ref state.work0, ref state.work1);
+                state.betady = v/vv;
+                
+                //
+                // Calculate BetaK according to HS formula
+                //
+                v = preconditionedmultiply2(state, ref state.g, ref state.yk, ref state.work0, ref state.work1);
+                state.betahs = v/vv;
+                
+                //
+                // Choose BetaK
+                //
+                if( state.cgtype==0 )
+                {
+                    betak = state.betady;
+                }
+                if( state.cgtype==1 )
+                {
+                    betak = Math.Max(0, Math.Min(state.betady, state.betahs));
+                }
+            }
+            else
+            {
+                
+                //
+                // Something is wrong (may be function is too wild or too flat)
+                // or we just have to restart algo.
+                //
+                // We'll set BetaK=0, which will restart CG algorithm.
+                // We can stop later (during normal checks) if stopping conditions are met.
+                //
+                betak = 0;
+                state.debugrestartscount = state.debugrestartscount+1;
+            }
+            if( state.repiterationscount>0 & state.repiterationscount%(3+n)==0 )
+            {
+                
+                //
+                // clear Beta every N iterations
+                //
+                betak = 0;
+            }
+            if( state.mcinfo==1 | state.mcinfo==5 )
+            {
+                state.rstimer = rscountdownlen;
+            }
+            else
+            {
+                state.rstimer = state.rstimer-1;
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.dn[i_] = -state.g[i_];
+            }
+            preconditionedmultiply(state, ref state.dn, ref state.work0, ref state.work1);
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.dn[i_] = state.dn[i_] + betak*state.dk[i_];
+            }
+            state.laststep = 0;
+            state.lastscaledstep = 0.0;
+            for(i=0; i<=n-1; i++)
+            {
+                state.laststep = state.laststep+math.sqr(state.d[i]);
+                state.lastscaledstep = state.lastscaledstep+math.sqr(state.d[i]/state.s[i]);
+            }
+            state.laststep = state.stp*Math.Sqrt(state.laststep);
+            state.lastscaledstep = state.stp*Math.Sqrt(state.lastscaledstep);
+            
+            //
+            // Update information.
+            // Check stopping conditions.
+            //
+            state.repnfev = state.repnfev+state.nfev;
+            state.repiterationscount = state.repiterationscount+1;
+            if( state.repiterationscount>=state.maxits & state.maxits>0 )
+            {
+                
+                //
+                // Too many iterations
+                //
+                state.repterminationtype = 5;
+                result = false;
+                return result;
+            }
+            v = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                v = v+math.sqr(state.g[i]*state.s[i]);
+            }
+            if( (double)(Math.Sqrt(v))<=(double)(state.epsg) )
+            {
+                
+                //
+                // Gradient is small enough
+                //
+                state.repterminationtype = 4;
+                result = false;
+                return result;
+            }
+            if( !state.innerresetneeded )
+            {
+                
+                //
+                // These conditions are checked only when no inner reset was requested by user
+                //
+                if( (double)(state.fold-state.f)<=(double)(state.epsf*Math.Max(Math.Abs(state.fold), Math.Max(Math.Abs(state.f), 1.0))) )
+                {
+                    
+                    //
+                    // F(k+1)-F(k) is small enough
+                    //
+                    state.repterminationtype = 1;
+                    result = false;
+                    return result;
+                }
+                if( (double)(state.lastscaledstep)<=(double)(state.epsx) )
+                {
+                    
+                    //
+                    // X(k+1)-X(k) is small enough
+                    //
+                    state.repterminationtype = 2;
+                    result = false;
+                    return result;
+                }
+            }
+            if( state.rstimer<=0 )
+            {
+                
+                //
+                // Too many subsequent restarts
+                //
+                state.repterminationtype = 7;
+                result = false;
+                return result;
+            }
+            
+            //
+            // Shift Xk/Dk, update other information
+            //
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.xk[i_] = state.xn[i_];
+            }
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.dk[i_] = state.dn[i_];
+            }
+            state.fold = state.f;
+            state.k = state.k+1;
+            goto lbl_26;
+        lbl_27:
+            result = false;
+            return result;
+            
+            //
+            // Saving state
+            //
+        lbl_rcomm:
+            result = true;
+            state.rstate.ia[0] = n;
+            state.rstate.ia[1] = i;
+            state.rstate.ra[0] = betak;
+            state.rstate.ra[1] = v;
+            state.rstate.ra[2] = vv;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Conjugate gradient results
+
+        INPUT PARAMETERS:
+            State   -   algorithm state
+
+        OUTPUT PARAMETERS:
+            X       -   array[0..N-1], solution
+            Rep     -   optimization report:
+                        * Rep.TerminationType completetion code:
+                            *  1    relative function improvement is no more than
+                                    EpsF.
+                            *  2    relative step is no more than EpsX.
+                            *  4    gradient norm is no more than EpsG
+                            *  5    MaxIts steps was taken
+                            *  7    stopping conditions are too stringent,
+                                    further improvement is impossible,
+                                    we return best X found so far
+                            *  8    terminated by user
+                        * Rep.IterationsCount contains iterations count
+                        * NFEV countains number of function calculations
+
+          -- ALGLIB --
+             Copyright 20.04.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgresults(mincgstate state,
+            ref double[] x,
+            mincgreport rep)
+        {
+            x = new double[0];
+
+            mincgresultsbuf(state, ref x, rep);
+        }
+
+
+        /*************************************************************************
+        Conjugate gradient results
+
+        Buffered implementation of MinCGResults(), which uses pre-allocated buffer
+        to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
+        intended to be used in the inner cycles of performance critical algorithms
+        where array reallocation penalty is too large to be ignored.
+
+          -- ALGLIB --
+             Copyright 20.04.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgresultsbuf(mincgstate state,
+            ref double[] x,
+            mincgreport rep)
+        {
+            int i_ = 0;
+
+            if( ap.len(x)<state.n )
+            {
+                x = new double[state.n];
+            }
+            for(i_=0; i_<=state.n-1;i_++)
+            {
+                x[i_] = state.xn[i_];
+            }
+            rep.iterationscount = state.repiterationscount;
+            rep.nfev = state.repnfev;
+            rep.terminationtype = state.repterminationtype;
+        }
+
+
+        /*************************************************************************
+        This  subroutine  restarts  CG  algorithm from new point. All optimization
+        parameters are left unchanged.
+
+        This  function  allows  to  solve multiple  optimization  problems  (which
+        must have same number of dimensions) without object reallocation penalty.
+
+        INPUT PARAMETERS:
+            State   -   structure used to store algorithm state.
+            X       -   new starting point.
+
+          -- ALGLIB --
+             Copyright 30.07.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgrestartfrom(mincgstate state,
+            double[] x)
+        {
+            int i_ = 0;
+
+            ap.assert(ap.len(x)>=state.n, "MinCGRestartFrom: Length(X)<N!");
+            ap.assert(apserv.isfinitevector(x, state.n), "MinCGCreate: X contains infinite or NaN values!");
+            for(i_=0; i_<=state.n-1;i_++)
+            {
+                state.x[i_] = x[i_];
+            }
+            mincgsuggeststep(state, 0.0);
+            state.rstate.ia = new int[1+1];
+            state.rstate.ra = new double[2+1];
+            state.rstate.stage = -1;
+            clearrequestfields(state);
+        }
+
+
+        /*************************************************************************
+        Faster version of MinCGSetPrecDiag(), for time-critical parts of code,
+        without safety checks.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetprecdiagfast(mincgstate state,
+            double[] d)
+        {
+            int i = 0;
+
+            apserv.rvectorsetlengthatleast(ref state.diagh, state.n);
+            apserv.rvectorsetlengthatleast(ref state.diaghl2, state.n);
+            state.prectype = 2;
+            state.vcnt = 0;
+            state.innerresetneeded = true;
+            for(i=0; i<=state.n-1; i++)
+            {
+                state.diagh[i] = d[i];
+                state.diaghl2[i] = 0.0;
+            }
+        }
+
+
+        /*************************************************************************
+        This function sets low-rank preconditioner for Hessian matrix  H=D+V'*C*V,
+        where:
+        * H is a Hessian matrix, which is approximated by D/V/C
+        * D=D1+D2 is a diagonal matrix, which includes two positive definite terms:
+          * constant term D1 (is not updated or infrequently updated)
+          * variable term D2 (can be cheaply updated from iteration to iteration)
+        * V is a low-rank correction
+        * C is a diagonal factor of low-rank correction
+
+        Preconditioner P is calculated using approximate Woodburry formula:
+            P  = D^(-1) - D^(-1)*V'*(C^(-1)+V*D1^(-1)*V')^(-1)*V*D^(-1)
+               = D^(-1) - D^(-1)*VC'*VC*D^(-1),
+        where
+            VC = sqrt(B)*V
+            B  = (C^(-1)+V*D1^(-1)*V')^(-1)
+            
+        Note that B is calculated using constant term (D1) only,  which  allows us
+        to update D2 without recalculation of B or   VC.  Such  preconditioner  is
+        exact when D2 is zero. When D2 is non-zero, it is only approximation,  but
+        very good and cheap one.
+
+        This function accepts D1, V, C.
+        D2 is set to zero by default.
+
+        Cost of this update is O(N*VCnt*VCnt), but D2 can be updated in just O(N)
+        by MinCGSetPrecVarPart.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetpreclowrankfast(mincgstate state,
+            double[] d1,
+            double[] c,
+            double[,] v,
+            int vcnt)
+        {
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int n = 0;
+            double t = 0;
+            double[,] b = new double[0,0];
+            int i_ = 0;
+
+            if( vcnt==0 )
+            {
+                mincgsetprecdiagfast(state, d1);
+                return;
+            }
+            n = state.n;
+            b = new double[vcnt, vcnt];
+            apserv.rvectorsetlengthatleast(ref state.diagh, n);
+            apserv.rvectorsetlengthatleast(ref state.diaghl2, n);
+            apserv.rmatrixsetlengthatleast(ref state.vcorr, vcnt, n);
+            state.prectype = 2;
+            state.vcnt = vcnt;
+            state.innerresetneeded = true;
+            for(i=0; i<=n-1; i++)
+            {
+                state.diagh[i] = d1[i];
+                state.diaghl2[i] = 0.0;
+            }
+            for(i=0; i<=vcnt-1; i++)
+            {
+                for(j=i; j<=vcnt-1; j++)
+                {
+                    t = 0;
+                    for(k=0; k<=n-1; k++)
+                    {
+                        t = t+v[i,k]*v[j,k]/d1[k];
+                    }
+                    b[i,j] = t;
+                }
+                b[i,i] = b[i,i]+1.0/c[i];
+            }
+            if( !trfac.spdmatrixcholeskyrec(ref b, 0, vcnt, true, ref state.work0) )
+            {
+                state.vcnt = 0;
+                return;
+            }
+            for(i=0; i<=vcnt-1; i++)
+            {
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.vcorr[i,i_] = v[i,i_];
+                }
+                for(j=0; j<=i-1; j++)
+                {
+                    t = b[j,i];
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.vcorr[i,i_] = state.vcorr[i,i_] - t*state.vcorr[j,i_];
+                    }
+                }
+                t = 1/b[i,i];
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.vcorr[i,i_] = t*state.vcorr[i,i_];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function updates variable part (diagonal matrix D2)
+        of low-rank preconditioner.
+
+        This update is very cheap and takes just O(N) time.
+
+        It has no effect with default preconditioner.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void mincgsetprecvarpart(mincgstate state,
+            double[] d2)
+        {
+            int i = 0;
+            int n = 0;
+
+            n = state.n;
+            for(i=0; i<=n-1; i++)
+            {
+                state.diaghl2[i] = d2[i];
+            }
+        }
+
+
+        /*************************************************************************
+        Clears request fileds (to be sure that we don't forgot to clear something)
+        *************************************************************************/
+        private static void clearrequestfields(mincgstate state)
+        {
+            state.needf = false;
+            state.needfg = false;
+            state.xupdated = false;
+            state.lsstart = false;
+            state.lsend = false;
+            state.algpowerup = false;
+        }
+
+
+        /*************************************************************************
+        This function calculates preconditioned product H^(-1)*x and stores result
+        back into X. Work0[] and Work1[] are used as temporaries (size must be at
+        least N; this function doesn't allocate arrays).
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        private static void preconditionedmultiply(mincgstate state,
+            ref double[] x,
+            ref double[] work0,
+            ref double[] work1)
+        {
+            int i = 0;
+            int n = 0;
+            int vcnt = 0;
+            double v = 0;
+            int i_ = 0;
+
+            n = state.n;
+            vcnt = state.vcnt;
+            if( state.prectype==0 )
+            {
+                return;
+            }
+            if( state.prectype==3 )
+            {
+                for(i=0; i<=n-1; i++)
+                {
+                    x[i] = x[i]*state.s[i]*state.s[i];
+                }
+                return;
+            }
+            ap.assert(state.prectype==2, "MinCG: internal error (unexpected PrecType)");
+            
+            //
+            // handle part common for VCnt=0 and VCnt<>0
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                x[i] = x[i]/(state.diagh[i]+state.diaghl2[i]);
+            }
+            
+            //
+            // if VCnt>0
+            //
+            if( vcnt>0 )
+            {
+                for(i=0; i<=vcnt-1; i++)
+                {
+                    v = 0.0;
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        v += state.vcorr[i,i_]*x[i_];
+                    }
+                    work0[i] = v;
+                }
+                for(i=0; i<=n-1; i++)
+                {
+                    work1[i] = 0;
+                }
+                for(i=0; i<=vcnt-1; i++)
+                {
+                    v = work0[i];
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.work1[i_] = state.work1[i_] + v*state.vcorr[i,i_];
+                    }
+                }
+                for(i=0; i<=n-1; i++)
+                {
+                    x[i] = x[i]-state.work1[i]/(state.diagh[i]+state.diaghl2[i]);
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function calculates preconditioned product x'*H^(-1)*y. Work0[] and
+        Work1[] are used as temporaries (size must be at least N; this function
+        doesn't allocate arrays).
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        private static double preconditionedmultiply2(mincgstate state,
+            ref double[] x,
+            ref double[] y,
+            ref double[] work0,
+            ref double[] work1)
+        {
+            double result = 0;
+            int i = 0;
+            int n = 0;
+            int vcnt = 0;
+            double v0 = 0;
+            double v1 = 0;
+            int i_ = 0;
+
+            n = state.n;
+            vcnt = state.vcnt;
+            
+            //
+            // no preconditioning
+            //
+            if( state.prectype==0 )
+            {
+                v0 = 0.0;
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    v0 += x[i_]*y[i_];
+                }
+                result = v0;
+                return result;
+            }
+            if( state.prectype==3 )
+            {
+                result = 0;
+                for(i=0; i<=n-1; i++)
+                {
+                    result = result+x[i]*state.s[i]*state.s[i]*y[i];
+                }
+                return result;
+            }
+            ap.assert(state.prectype==2, "MinCG: internal error (unexpected PrecType)");
+            
+            //
+            // low rank preconditioning
+            //
+            result = 0.0;
+            for(i=0; i<=n-1; i++)
+            {
+                result = result+x[i]*y[i]/(state.diagh[i]+state.diaghl2[i]);
+            }
+            if( vcnt>0 )
+            {
+                for(i=0; i<=n-1; i++)
+                {
+                    work0[i] = x[i]/(state.diagh[i]+state.diaghl2[i]);
+                    work1[i] = y[i]/(state.diagh[i]+state.diaghl2[i]);
+                }
+                for(i=0; i<=vcnt-1; i++)
+                {
+                    v0 = 0.0;
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        v0 += work0[i_]*state.vcorr[i,i_];
+                    }
+                    v1 = 0.0;
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        v1 += work1[i_]*state.vcorr[i,i_];
+                    }
+                    result = result-v0*v1;
+                }
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Internal initialization subroutine
+
+          -- ALGLIB --
+             Copyright 16.05.2011 by Bochkanov Sergey
+        *************************************************************************/
+        private static void mincginitinternal(int n,
+            double diffstep,
+            mincgstate state)
+        {
+            int i = 0;
+
+            state.n = n;
+            state.diffstep = diffstep;
+            mincgsetcond(state, 0, 0, 0, 0);
+            mincgsetxrep(state, false);
+            mincgsetdrep(state, false);
+            mincgsetstpmax(state, 0);
+            mincgsetcgtype(state, -1);
+            mincgsetprecdefault(state);
+            state.xk = new double[n];
+            state.dk = new double[n];
+            state.xn = new double[n];
+            state.dn = new double[n];
+            state.x = new double[n];
+            state.d = new double[n];
+            state.g = new double[n];
+            state.work0 = new double[n];
+            state.work1 = new double[n];
+            state.yk = new double[n];
+            state.s = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                state.s[i] = 1.0;
+            }
+        }
+
+
+    }
+    public class minbleic
+    {
+        /*************************************************************************
+        This object stores nonlinear optimizer state.
+        You should use functions provided by MinBLEIC subpackage to work with this
+        object
+        *************************************************************************/
+        public class minbleicstate
+        {
+            public int nmain;
+            public int nslack;
+            public double innerepsg;
+            public double innerepsf;
+            public double innerepsx;
+            public double outerepsx;
+            public double outerepsi;
+            public int maxits;
+            public bool xrep;
+            public double stpmax;
+            public double diffstep;
+            public int prectype;
+            public double[] diaghoriginal;
+            public double[] diagh;
+            public double[] x;
+            public double f;
+            public double[] g;
+            public bool needf;
+            public bool needfg;
+            public bool xupdated;
+            public rcommstate rstate;
+            public int repinneriterationscount;
+            public int repouteriterationscount;
+            public int repnfev;
+            public int repterminationtype;
+            public double repdebugeqerr;
+            public double repdebugfs;
+            public double repdebugff;
+            public double repdebugdx;
+            public double[] xcur;
+            public double[] xprev;
+            public double[] xstart;
+            public int itsleft;
+            public double[] xend;
+            public double[] lastg;
+            public double trimthreshold;
+            public double[,] ceoriginal;
+            public double[,] ceeffective;
+            public double[,] cecurrent;
+            public int[] ct;
+            public int cecnt;
+            public int cedim;
+            public double[] xe;
+            public bool[] hasbndl;
+            public bool[] hasbndu;
+            public double[] bndloriginal;
+            public double[] bnduoriginal;
+            public double[] bndleffective;
+            public double[] bndueffective;
+            public bool[] activeconstraints;
+            public double[] constrainedvalues;
+            public double[] transforms;
+            public double[] seffective;
+            public double[] soriginal;
+            public double[] w;
+            public double[] tmp0;
+            public double[] tmp1;
+            public double[] tmp2;
+            public double[] r;
+            public double[,] lmmatrix;
+            public double v0;
+            public double v1;
+            public double v2;
+            public double t;
+            public double errfeas;
+            public double gnorm;
+            public double mpgnorm;
+            public double mba;
+            public int variabletofreeze;
+            public double valuetofreeze;
+            public double fbase;
+            public double fm2;
+            public double fm1;
+            public double fp1;
+            public double fp2;
+            public double xm1;
+            public double xp1;
+            public mincg.mincgstate cgstate;
+            public mincg.mincgreport cgrep;
+            public int optdim;
+            public minbleicstate()
+            {
+                diaghoriginal = new double[0];
+                diagh = new double[0];
+                x = new double[0];
+                g = new double[0];
+                rstate = new rcommstate();
+                xcur = new double[0];
+                xprev = new double[0];
+                xstart = new double[0];
+                xend = new double[0];
+                lastg = new double[0];
+                ceoriginal = new double[0,0];
+                ceeffective = new double[0,0];
+                cecurrent = new double[0,0];
+                ct = new int[0];
+                xe = new double[0];
+                hasbndl = new bool[0];
+                hasbndu = new bool[0];
+                bndloriginal = new double[0];
+                bnduoriginal = new double[0];
+                bndleffective = new double[0];
+                bndueffective = new double[0];
+                activeconstraints = new bool[0];
+                constrainedvalues = new double[0];
+                transforms = new double[0];
+                seffective = new double[0];
+                soriginal = new double[0];
+                w = new double[0];
+                tmp0 = new double[0];
+                tmp1 = new double[0];
+                tmp2 = new double[0];
+                r = new double[0];
+                lmmatrix = new double[0,0];
+                cgstate = new mincg.mincgstate();
+                cgrep = new mincg.mincgreport();
+            }
+        };
+
+
+        /*************************************************************************
+        This structure stores optimization report:
+        * InnerIterationsCount      number of inner iterations
+        * OuterIterationsCount      number of outer iterations
+        * NFEV                      number of gradient evaluations
+        * TerminationType           termination type (see below)
+
+        TERMINATION CODES
+
+        TerminationType field contains completion code, which can be:
+          -10   unsupported combination of algorithm settings:
+                1) StpMax is set to non-zero value,
+                AND 2) non-default preconditioner is used.
+                You can't use both features at the same moment,
+                so you have to choose one of them (and to turn
+                off another one).
+          -3    inconsistent constraints. Feasible point is
+                either nonexistent or too hard to find. Try to
+                restart optimizer with better initial
+                approximation
+           4    conditions on constraints are fulfilled
+                with error less than or equal to EpsC
+           5    MaxIts steps was taken
+           7    stopping conditions are too stringent,
+                further improvement is impossible,
+                X contains best point found so far.
+
+        ADDITIONAL FIELDS
+
+        There are additional fields which can be used for debugging:
+        * DebugEqErr                error in the equality constraints (2-norm)
+        * DebugFS                   f, calculated at projection of initial point
+                                    to the feasible set
+        * DebugFF                   f, calculated at the final point
+        * DebugDX                   |X_start-X_final|
+        *************************************************************************/
+        public class minbleicreport
+        {
+            public int inneriterationscount;
+            public int outeriterationscount;
+            public int nfev;
+            public int terminationtype;
+            public double debugeqerr;
+            public double debugfs;
+            public double debugff;
+            public double debugdx;
+        };
+
+
+
+
+        public const double svdtol = 100;
+        public const double maxouterits = 20;
+
+
+        /*************************************************************************
+                             BOUND CONSTRAINED OPTIMIZATION
+               WITH ADDITIONAL LINEAR EQUALITY AND INEQUALITY CONSTRAINTS
+
+        DESCRIPTION:
+        The  subroutine  minimizes  function   F(x)  of N arguments subject to any
+        combination of:
+        * bound constraints
+        * linear inequality constraints
+        * linear equality constraints
+
+        REQUIREMENTS:
+        * user must provide function value and gradient
+        * starting point X0 must be feasible or
+          not too far away from the feasible set
+        * grad(f) must be Lipschitz continuous on a level set:
+          L = { x : f(x)<=f(x0) }
+        * function must be defined everywhere on the feasible set F
+
+        USAGE:
+
+        Constrained optimization if far more complex than the unconstrained one.
+        Here we give very brief outline of the BLEIC optimizer. We strongly recommend
+        you to read examples in the ALGLIB Reference Manual and to read ALGLIB User Guide
+        on optimization, which is available at http://www.alglib.net/optimization/
+
+        1. User initializes algorithm state with MinBLEICCreate() call
+
+        2. USer adds boundary and/or linear constraints by calling
+           MinBLEICSetBC() and MinBLEICSetLC() functions.
+
+        3. User sets stopping conditions for underlying unconstrained solver
+           with MinBLEICSetInnerCond() call.
+           This function controls accuracy of underlying optimization algorithm.
+
+        4. User sets stopping conditions for outer iteration by calling
+           MinBLEICSetOuterCond() function.
+           This function controls handling of boundary and inequality constraints.
+
+        5. Additionally, user may set limit on number of internal iterations
+           by MinBLEICSetMaxIts() call.
+           This function allows to prevent algorithm from looping forever.
+
+        6. User calls MinBLEICOptimize() function which takes algorithm  state and
+           pointer (delegate, etc.) to callback function which calculates F/G.
+
+        7. User calls MinBLEICResults() to get solution
+
+        8. Optionally user may call MinBLEICRestartFrom() to solve another problem
+           with same N but another starting point.
+           MinBLEICRestartFrom() allows to reuse already initialized structure.
+
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>0:
+                        * if given, only leading N elements of X are used
+                        * if not given, automatically determined from size ofX
+            X       -   starting point, array[N]:
+                        * it is better to set X to a feasible point
+                        * but X can be infeasible, in which case algorithm will try
+                          to find feasible point first, using X as initial
+                          approximation.
+
+        OUTPUT PARAMETERS:
+            State   -   structure stores algorithm state
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleiccreate(int n,
+            double[] x,
+            minbleicstate state)
+        {
+            double[,] c = new double[0,0];
+            int[] ct = new int[0];
+
+            ap.assert(n>=1, "MinBLEICCreate: N<1");
+            ap.assert(ap.len(x)>=n, "MinBLEICCreate: Length(X)<N");
+            ap.assert(apserv.isfinitevector(x, n), "MinBLEICCreate: X contains infinite or NaN values!");
+            minbleicinitinternal(n, x, 0.0, state);
+        }
+
+
+        /*************************************************************************
+        The subroutine is finite difference variant of MinBLEICCreate().  It  uses
+        finite differences in order to differentiate target function.
+
+        Description below contains information which is specific to  this function
+        only. We recommend to read comments on MinBLEICCreate() in  order  to  get
+        more information about creation of BLEIC optimizer.
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>0:
+                        * if given, only leading N elements of X are used
+                        * if not given, automatically determined from size of X
+            X       -   starting point, array[0..N-1].
+            DiffStep-   differentiation step, >0
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+        NOTES:
+        1. algorithm uses 4-point central formula for differentiation.
+        2. differentiation step along I-th axis is equal to DiffStep*S[I] where
+           S[] is scaling vector which can be set by MinBLEICSetScale() call.
+        3. we recommend you to use moderate values of  differentiation  step.  Too
+           large step will result in too large truncation  errors, while too small
+           step will result in too large numerical  errors.  1.0E-6  can  be  good
+           value to start with.
+        4. Numerical  differentiation  is   very   inefficient  -   one   gradient
+           calculation needs 4*N function evaluations. This function will work for
+           any N - either small (1...10), moderate (10...100) or  large  (100...).
+           However, performance penalty will be too severe for any N's except  for
+           small ones.
+           We should also say that code which relies on numerical  differentiation
+           is  less  robust and precise. CG needs exact gradient values. Imprecise
+           gradient may slow  down  convergence, especially  on  highly  nonlinear
+           problems.
+           Thus  we  recommend to use this function for fast prototyping on small-
+           dimensional problems only, and to implement analytical gradient as soon
+           as possible.
+
+          -- ALGLIB --
+             Copyright 16.05.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleiccreatef(int n,
+            double[] x,
+            double diffstep,
+            minbleicstate state)
+        {
+            double[,] c = new double[0,0];
+            int[] ct = new int[0];
+
+            ap.assert(n>=1, "MinBLEICCreateF: N<1");
+            ap.assert(ap.len(x)>=n, "MinBLEICCreateF: Length(X)<N");
+            ap.assert(apserv.isfinitevector(x, n), "MinBLEICCreateF: X contains infinite or NaN values!");
+            ap.assert(math.isfinite(diffstep), "MinBLEICCreateF: DiffStep is infinite or NaN!");
+            ap.assert((double)(diffstep)>(double)(0), "MinBLEICCreateF: DiffStep is non-positive!");
+            minbleicinitinternal(n, x, diffstep, state);
+        }
+
+
+        /*************************************************************************
+        This function sets boundary constraints for BLEIC optimizer.
+
+        Boundary constraints are inactive by default (after initial creation).
+        They are preserved after algorithm restart with MinBLEICRestartFrom().
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            BndL    -   lower bounds, array[N].
+                        If some (all) variables are unbounded, you may specify
+                        very small number or -INF.
+            BndU    -   upper bounds, array[N].
+                        If some (all) variables are unbounded, you may specify
+                        very large number or +INF.
+
+        NOTE 1: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+        variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+        NOTE 2: this solver has following useful properties:
+        * bound constraints are always satisfied exactly
+        * function is evaluated only INSIDE area specified by  bound  constraints,
+          even  when  numerical  differentiation is used (algorithm adjusts  nodes
+          according to boundary constraints)
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetbc(minbleicstate state,
+            double[] bndl,
+            double[] bndu)
+        {
+            int i = 0;
+            int n = 0;
+
+            n = state.nmain;
+            ap.assert(ap.len(bndl)>=n, "MinBLEICSetBC: Length(BndL)<N");
+            ap.assert(ap.len(bndu)>=n, "MinBLEICSetBC: Length(BndU)<N");
+            for(i=0; i<=n-1; i++)
+            {
+                ap.assert(math.isfinite(bndl[i]) | Double.IsNegativeInfinity(bndl[i]), "MinBLEICSetBC: BndL contains NAN or +INF");
+                ap.assert(math.isfinite(bndu[i]) | Double.IsPositiveInfinity(bndu[i]), "MinBLEICSetBC: BndL contains NAN or -INF");
+                state.bndloriginal[i] = bndl[i];
+                state.hasbndl[i] = math.isfinite(bndl[i]);
+                state.bnduoriginal[i] = bndu[i];
+                state.hasbndu[i] = math.isfinite(bndu[i]);
+            }
+        }
+
+
+        /*************************************************************************
+        This function sets linear constraints for BLEIC optimizer.
+
+        Linear constraints are inactive by default (after initial creation).
+        They are preserved after algorithm restart with MinBLEICRestartFrom().
+
+        INPUT PARAMETERS:
+            State   -   structure previously allocated with MinBLEICCreate call.
+            C       -   linear constraints, array[K,N+1].
+                        Each row of C represents one constraint, either equality
+                        or inequality (see below):
+                        * first N elements correspond to coefficients,
+                        * last element corresponds to the right part.
+                        All elements of C (including right part) must be finite.
+            CT      -   type of constraints, array[K]:
+                        * if CT[i]>0, then I-th constraint is C[i,*]*x >= C[i,n+1]
+                        * if CT[i]=0, then I-th constraint is C[i,*]*x  = C[i,n+1]
+                        * if CT[i]<0, then I-th constraint is C[i,*]*x <= C[i,n+1]
+            K       -   number of equality/inequality constraints, K>=0:
+                        * if given, only leading K elements of C/CT are used
+                        * if not given, automatically determined from sizes of C/CT
+
+        NOTE 1: linear (non-bound) constraints are satisfied only approximately:
+        * there always exists some minor violation (about Epsilon in magnitude)
+          due to rounding errors
+        * numerical differentiation, if used, may  lead  to  function  evaluations
+          outside  of the feasible  area,   because   algorithm  does  NOT  change
+          numerical differentiation formula according to linear constraints.
+        If you want constraints to be  satisfied  exactly, try to reformulate your
+        problem  in  such  manner  that  all constraints will become boundary ones
+        (this kind of constraints is always satisfied exactly, both in  the  final
+        solution and in all intermediate points).
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetlc(minbleicstate state,
+            double[,] c,
+            int[] ct,
+            int k)
+        {
+            int nmain = 0;
+            int i = 0;
+            int i_ = 0;
+
+            nmain = state.nmain;
+            
+            //
+            // First, check for errors in the inputs
+            //
+            ap.assert(k>=0, "MinBLEICSetLC: K<0");
+            ap.assert(ap.cols(c)>=nmain+1 | k==0, "MinBLEICSetLC: Cols(C)<N+1");
+            ap.assert(ap.rows(c)>=k, "MinBLEICSetLC: Rows(C)<K");
+            ap.assert(ap.len(ct)>=k, "MinBLEICSetLC: Length(CT)<K");
+            ap.assert(apserv.apservisfinitematrix(c, k, nmain+1), "MinBLEICSetLC: C contains infinite or NaN values!");
+            
+            //
+            // Determine number of constraints,
+            // allocate space and copy
+            //
+            state.cecnt = k;
+            apserv.rmatrixsetlengthatleast(ref state.ceoriginal, state.cecnt, nmain+1);
+            apserv.ivectorsetlengthatleast(ref state.ct, state.cecnt);
+            for(i=0; i<=k-1; i++)
+            {
+                state.ct[i] = ct[i];
+                for(i_=0; i_<=nmain;i_++)
+                {
+                    state.ceoriginal[i,i_] = c[i,i_];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function sets stopping conditions for the underlying nonlinear CG
+        optimizer. It controls overall accuracy of solution. These conditions
+        should be strict enough in order for algorithm to converge.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            EpsG    -   >=0
+                        The  subroutine  finishes  its  work   if   the  condition
+                        |v|<EpsG is satisfied, where:
+                        * |.| means Euclidian norm
+                        * v - scaled gradient vector, v[i]=g[i]*s[i]
+                        * g - gradient
+                        * s - scaling coefficients set by MinBLEICSetScale()
+            EpsF    -   >=0
+                        The  subroutine  finishes  its work if on k+1-th iteration
+                        the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
+                        is satisfied.
+            EpsX    -   >=0
+                        The subroutine finishes its work if  on  k+1-th  iteration
+                        the condition |v|<=EpsX is fulfilled, where:
+                        * |.| means Euclidian norm
+                        * v - scaled step vector, v[i]=dx[i]/s[i]
+                        * dx - ste pvector, dx=X(k+1)-X(k)
+                        * s - scaling coefficients set by MinBLEICSetScale()
+
+        Passing EpsG=0, EpsF=0 and EpsX=0 (simultaneously) will lead to
+        automatic stopping criterion selection.
+
+        These conditions are used to terminate inner iterations. However, you
+        need to tune termination conditions for outer iterations too.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetinnercond(minbleicstate state,
+            double epsg,
+            double epsf,
+            double epsx)
+        {
+            ap.assert(math.isfinite(epsg), "MinBLEICSetInnerCond: EpsG is not finite number");
+            ap.assert((double)(epsg)>=(double)(0), "MinBLEICSetInnerCond: negative EpsG");
+            ap.assert(math.isfinite(epsf), "MinBLEICSetInnerCond: EpsF is not finite number");
+            ap.assert((double)(epsf)>=(double)(0), "MinBLEICSetInnerCond: negative EpsF");
+            ap.assert(math.isfinite(epsx), "MinBLEICSetInnerCond: EpsX is not finite number");
+            ap.assert((double)(epsx)>=(double)(0), "MinBLEICSetInnerCond: negative EpsX");
+            state.innerepsg = epsg;
+            state.innerepsf = epsf;
+            state.innerepsx = epsx;
+        }
+
+
+        /*************************************************************************
+        This function sets stopping conditions for outer iteration of BLEIC algo.
+
+        These conditions control accuracy of constraint handling and amount of
+        infeasibility allowed in the solution.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            EpsX    -   >0, stopping condition on outer iteration step length
+            EpsI    -   >0, stopping condition on infeasibility
+            
+        Both EpsX and EpsI must be non-zero.
+
+        MEANING OF EpsX
+
+        EpsX  is  a  stopping  condition for outer iterations. Algorithm will stop
+        when  solution  of  the  current  modified  subproblem will be within EpsX
+        (using 2-norm) of the previous solution.
+
+        MEANING OF EpsI
+
+        EpsI controls feasibility properties -  algorithm  won't  stop  until  all
+        inequality constraints will be satisfied with error (distance from current
+        point to the feasible area) at most EpsI.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetoutercond(minbleicstate state,
+            double epsx,
+            double epsi)
+        {
+            ap.assert(math.isfinite(epsx), "MinBLEICSetOuterCond: EpsX is not finite number");
+            ap.assert((double)(epsx)>(double)(0), "MinBLEICSetOuterCond: non-positive EpsX");
+            ap.assert(math.isfinite(epsi), "MinBLEICSetOuterCond: EpsI is not finite number");
+            ap.assert((double)(epsi)>(double)(0), "MinBLEICSetOuterCond: non-positive EpsI");
+            state.outerepsx = epsx;
+            state.outerepsi = epsi;
+        }
+
+
+        /*************************************************************************
+        This function sets scaling coefficients for BLEIC optimizer.
+
+        ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+        size and gradient are scaled before comparison with tolerances).  Scale of
+        the I-th variable is a translation invariant measure of:
+        a) "how large" the variable is
+        b) how large the step should be to make significant changes in the function
+
+        Scaling is also used by finite difference variant of the optimizer  - step
+        along I-th axis is equal to DiffStep*S[I].
+
+        In  most  optimizers  (and  in  the  BLEIC  too)  scaling is NOT a form of
+        preconditioning. It just  affects  stopping  conditions.  You  should  set
+        preconditioner  by  separate  call  to  one  of  the  MinBLEICSetPrec...()
+        functions.
+
+        There is a special  preconditioning  mode, however,  which  uses   scaling
+        coefficients to form diagonal preconditioning matrix. You  can  turn  this
+        mode on, if you want.   But  you should understand that scaling is not the
+        same thing as preconditioning - these are two different, although  related
+        forms of tuning solver.
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            S       -   array[N], non-zero scaling coefficients
+                        S[i] may be negative, sign doesn't matter.
+
+          -- ALGLIB --
+             Copyright 14.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetscale(minbleicstate state,
+            double[] s)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(s)>=state.nmain, "MinBLEICSetScale: Length(S)<N");
+            for(i=0; i<=state.nmain-1; i++)
+            {
+                ap.assert(math.isfinite(s[i]), "MinBLEICSetScale: S contains infinite or NAN elements");
+                ap.assert((double)(s[i])!=(double)(0), "MinBLEICSetScale: S contains zero elements");
+                state.soriginal[i] = Math.Abs(s[i]);
+            }
+        }
+
+
+        /*************************************************************************
+        Modification of the preconditioner: preconditioning is turned off.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetprecdefault(minbleicstate state)
+        {
+            state.prectype = 0;
+        }
+
+
+        /*************************************************************************
+        Modification  of  the  preconditioner:  diagonal of approximate Hessian is
+        used.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            D       -   diagonal of the approximate Hessian, array[0..N-1],
+                        (if larger, only leading N elements are used).
+
+        NOTE 1: D[i] should be positive. Exception will be thrown otherwise.
+
+        NOTE 2: you should pass diagonal of approximate Hessian - NOT ITS INVERSE.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetprecdiag(minbleicstate state,
+            double[] d)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(d)>=state.nmain, "MinBLEICSetPrecDiag: D is too short");
+            for(i=0; i<=state.nmain-1; i++)
+            {
+                ap.assert(math.isfinite(d[i]), "MinBLEICSetPrecDiag: D contains infinite or NAN elements");
+                ap.assert((double)(d[i])>(double)(0), "MinBLEICSetPrecDiag: D contains non-positive elements");
+            }
+            apserv.rvectorsetlengthatleast(ref state.diaghoriginal, state.nmain);
+            state.prectype = 2;
+            for(i=0; i<=state.nmain-1; i++)
+            {
+                state.diaghoriginal[i] = d[i];
+            }
+        }
+
+
+        /*************************************************************************
+        Modification of the preconditioner: scale-based diagonal preconditioning.
+
+        This preconditioning mode can be useful when you  don't  have  approximate
+        diagonal of Hessian, but you know that your  variables  are  badly  scaled
+        (for  example,  one  variable is in [1,10], and another in [1000,100000]),
+        and most part of the ill-conditioning comes from different scales of vars.
+
+        In this case simple  scale-based  preconditioner,  with H[i] = 1/(s[i]^2),
+        can greatly improve convergence.
+
+        IMPRTANT: you should set scale of your variables  with  MinBLEICSetScale()
+        call  (before  or after MinBLEICSetPrecScale() call). Without knowledge of
+        the scale of your variables scale-based preconditioner will be  just  unit
+        matrix.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetprecscale(minbleicstate state)
+        {
+            state.prectype = 3;
+        }
+
+
+        /*************************************************************************
+        This function allows to stop algorithm after specified number of inner
+        iterations.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            MaxIts  -   maximum number of inner iterations.
+                        If MaxIts=0, the number of iterations is unlimited.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetmaxits(minbleicstate state,
+            int maxits)
+        {
+            ap.assert(maxits>=0, "MinBLEICSetMaxIts: negative MaxIts!");
+            state.maxits = maxits;
+        }
+
+
+        /*************************************************************************
+        This function turns on/off reporting.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            NeedXRep-   whether iteration reports are needed or not
+
+        If NeedXRep is True, algorithm will call rep() callback function if  it is
+        provided to MinBLEICOptimize().
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetxrep(minbleicstate state,
+            bool needxrep)
+        {
+            state.xrep = needxrep;
+        }
+
+
+        /*************************************************************************
+        This function sets maximum step length
+
+        IMPORTANT: this feature is hard to combine with preconditioning. You can't
+        set upper limit on step length, when you solve optimization  problem  with
+        linear (non-boundary) constraints AND preconditioner turned on.
+
+        When  non-boundary  constraints  are  present,  you  have to either a) use
+        preconditioner, or b) use upper limit on step length.  YOU CAN'T USE BOTH!
+        In this case algorithm will terminate with appropriate error code.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
+                        want to limit step length.
+
+        Use this subroutine when you optimize target function which contains exp()
+        or  other  fast  growing  functions,  and optimization algorithm makes too
+        large  steps  which  lead   to overflow. This function allows us to reject
+        steps  that  are  too  large  (and  therefore  expose  us  to the possible
+        overflow) without actually calculating function value at the x+stp*d.
+
+          -- ALGLIB --
+             Copyright 02.04.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetstpmax(minbleicstate state,
+            double stpmax)
+        {
+            ap.assert(math.isfinite(stpmax), "MinBLEICSetStpMax: StpMax is not finite!");
+            ap.assert((double)(stpmax)>=(double)(0), "MinBLEICSetStpMax: StpMax<0!");
+            state.stpmax = stpmax;
+        }
+
+
+        /*************************************************************************
+        NOTES:
+
+        1. This function has two different implementations: one which  uses  exact
+           (analytical) user-supplied gradient,  and one which uses function value
+           only  and  numerically  differentiates  function  in  order  to  obtain
+           gradient.
+
+           Depending  on  the  specific  function  used to create optimizer object
+           (either  MinBLEICCreate() for analytical gradient or  MinBLEICCreateF()
+           for numerical differentiation) you should choose appropriate variant of
+           MinBLEICOptimize() - one  which  accepts  function  AND gradient or one
+           which accepts function ONLY.
+
+           Be careful to choose variant of MinBLEICOptimize() which corresponds to
+           your optimization scheme! Table below lists different  combinations  of
+           callback (function/gradient) passed to MinBLEICOptimize()  and specific
+           function used to create optimizer.
+
+
+                             |         USER PASSED TO MinBLEICOptimize()
+           CREATED WITH      |  function only   |  function and gradient
+           ------------------------------------------------------------
+           MinBLEICCreateF() |     work                FAIL
+           MinBLEICCreate()  |     FAIL                work
+
+           Here "FAIL" denotes inappropriate combinations  of  optimizer  creation
+           function  and  MinBLEICOptimize()  version.   Attemps   to   use   such
+           combination (for  example,  to  create optimizer with MinBLEICCreateF()
+           and  to  pass  gradient  information  to  MinCGOptimize()) will lead to
+           exception being thrown. Either  you  did  not pass gradient when it WAS
+           needed or you passed gradient when it was NOT needed.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static bool minbleiciteration(minbleicstate state)
+        {
+            bool result = new bool();
+            int nmain = 0;
+            int nslack = 0;
+            int m = 0;
+            int i = 0;
+            int j = 0;
+            double v = 0;
+            double vv = 0;
+            bool b = new bool();
+            int i_ = 0;
+
+            
+            //
+            // Reverse communication preparations
+            // I know it looks ugly, but it works the same way
+            // anywhere from C++ to Python.
+            //
+            // This code initializes locals by:
+            // * random values determined during code
+            //   generation - on first subroutine call
+            // * values from previous call - on subsequent calls
+            //
+            if( state.rstate.stage>=0 )
+            {
+                nmain = state.rstate.ia[0];
+                nslack = state.rstate.ia[1];
+                m = state.rstate.ia[2];
+                i = state.rstate.ia[3];
+                j = state.rstate.ia[4];
+                b = state.rstate.ba[0];
+                v = state.rstate.ra[0];
+                vv = state.rstate.ra[1];
+            }
+            else
+            {
+                nmain = -983;
+                nslack = -989;
+                m = -834;
+                i = 900;
+                j = -287;
+                b = false;
+                v = 214;
+                vv = -338;
+            }
+            if( state.rstate.stage==0 )
+            {
+                goto lbl_0;
+            }
+            if( state.rstate.stage==1 )
+            {
+                goto lbl_1;
+            }
+            if( state.rstate.stage==2 )
+            {
+                goto lbl_2;
+            }
+            if( state.rstate.stage==3 )
+            {
+                goto lbl_3;
+            }
+            if( state.rstate.stage==4 )
+            {
+                goto lbl_4;
+            }
+            if( state.rstate.stage==5 )
+            {
+                goto lbl_5;
+            }
+            if( state.rstate.stage==6 )
+            {
+                goto lbl_6;
+            }
+            if( state.rstate.stage==7 )
+            {
+                goto lbl_7;
+            }
+            if( state.rstate.stage==8 )
+            {
+                goto lbl_8;
+            }
+            if( state.rstate.stage==9 )
+            {
+                goto lbl_9;
+            }
+            if( state.rstate.stage==10 )
+            {
+                goto lbl_10;
+            }
+            if( state.rstate.stage==11 )
+            {
+                goto lbl_11;
+            }
+            if( state.rstate.stage==12 )
+            {
+                goto lbl_12;
+            }
+            
+            //
+            // Routine body
+            //
+            
+            //
+            // Prepare:
+            // * calculate number of slack variables
+            // * initialize locals
+            // * initialize debug fields
+            // * make quick check
+            //
+            nmain = state.nmain;
+            nslack = 0;
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                if( state.ct[i]!=0 )
+                {
+                    nslack = nslack+1;
+                }
+            }
+            state.nslack = nslack;
+            state.repterminationtype = 0;
+            state.repinneriterationscount = 0;
+            state.repouteriterationscount = 0;
+            state.repnfev = 0;
+            state.repdebugeqerr = 0.0;
+            state.repdebugfs = Double.NaN;
+            state.repdebugff = Double.NaN;
+            state.repdebugdx = Double.NaN;
+            if( (double)(state.stpmax)!=(double)(0) & state.prectype!=0 )
+            {
+                state.repterminationtype = -10;
+                result = false;
+                return result;
+            }
+            
+            //
+            // allocate
+            //
+            apserv.rvectorsetlengthatleast(ref state.r, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.diagh, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.tmp0, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.tmp1, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.tmp2, nmain+nslack);
+            apserv.rmatrixsetlengthatleast(ref state.cecurrent, state.cecnt, nmain+nslack+1);
+            apserv.bvectorsetlengthatleast(ref state.activeconstraints, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.constrainedvalues, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.lastg, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.xe, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.xcur, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.xprev, nmain+nslack);
+            apserv.rvectorsetlengthatleast(ref state.xend, nmain);
+            
+            //
+            // Create/restart optimizer.
+            //
+            // State.OptDim is used to determine current state of optimizer.
+            //
+            if( state.optdim!=nmain+nslack )
+            {
+                for(i=0; i<=nmain+nslack-1; i++)
+                {
+                    state.tmp1[i] = 0.0;
+                }
+                mincg.mincgcreate(nmain+nslack, state.tmp1, state.cgstate);
+                state.optdim = nmain+nslack;
+            }
+            
+            //
+            // Prepare transformation.
+            //
+            // MinBLEIC's handling of preconditioner matrix is somewhat unusual -
+            // instead of incorporating it into algorithm and making implicit
+            // scaling (as most optimizers do) BLEIC optimizer uses explicit
+            // scaling - it solves problem in the scaled parameters space S,
+            // making transition between scaled (S) and unscaled (X) variables
+            // every time we ask for function value.
+            //
+            // Following fields are calculated here:
+            // * TransformS         X[i] = TransformS[i]*S[i], array[NMain]
+            // * SEffective         "effective" scale of the variables after
+            //                      transformation, array[NMain+NSlack]
+            //
+            apserv.rvectorsetlengthatleast(ref state.transforms, nmain);
+            for(i=0; i<=nmain-1; i++)
+            {
+                if( state.prectype==2 )
+                {
+                    state.transforms[i] = 1/Math.Sqrt(state.diaghoriginal[i]);
+                    continue;
+                }
+                if( state.prectype==3 )
+                {
+                    state.transforms[i] = state.soriginal[i];
+                    continue;
+                }
+                state.transforms[i] = 1;
+            }
+            apserv.rvectorsetlengthatleast(ref state.seffective, nmain+nslack);
+            for(i=0; i<=nmain-1; i++)
+            {
+                state.seffective[i] = state.soriginal[i]/state.transforms[i];
+            }
+            for(i=0; i<=nslack-1; i++)
+            {
+                state.seffective[nmain+i] = 1;
+            }
+            mincg.mincgsetscale(state.cgstate, state.seffective);
+            
+            //
+            // Pre-process constraints
+            // * check consistency of bound constraints
+            // * add slack vars, convert problem to the bound/equality
+            //   constrained one
+            //
+            // We calculate here:
+            // * BndLEffective - lower bounds after transformation of variables (see above)
+            // * BndUEffective - upper bounds after transformation of variables (see above)
+            // * CEEffective - matrix of equality constraints for transformed variables
+            //
+            for(i=0; i<=nmain-1; i++)
+            {
+                if( state.hasbndl[i] )
+                {
+                    state.bndleffective[i] = state.bndloriginal[i]/state.transforms[i];
+                }
+                if( state.hasbndu[i] )
+                {
+                    state.bndueffective[i] = state.bnduoriginal[i]/state.transforms[i];
+                }
+            }
+            for(i=0; i<=nmain-1; i++)
+            {
+                if( state.hasbndl[i] & state.hasbndu[i] )
+                {
+                    if( (double)(state.bndleffective[i])>(double)(state.bndueffective[i]) )
+                    {
+                        state.repterminationtype = -3;
+                        result = false;
+                        return result;
+                    }
+                }
+            }
+            apserv.rmatrixsetlengthatleast(ref state.ceeffective, state.cecnt, nmain+nslack+1);
+            m = 0;
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                
+                //
+                // NOTE: when we add slack variable, we use V = max(abs(CE[i,...])) as
+                // coefficient before it in order to make linear equations better
+                // conditioned.
+                //
+                v = 0;
+                for(j=0; j<=nmain-1; j++)
+                {
+                    state.ceeffective[i,j] = state.ceoriginal[i,j]*state.transforms[j];
+                    v = Math.Max(v, Math.Abs(state.ceeffective[i,j]));
+                }
+                if( (double)(v)==(double)(0) )
+                {
+                    v = 1;
+                }
+                for(j=0; j<=nslack-1; j++)
+                {
+                    state.ceeffective[i,nmain+j] = 0.0;
+                }
+                state.ceeffective[i,nmain+nslack] = state.ceoriginal[i,nmain];
+                if( state.ct[i]<0 )
+                {
+                    state.ceeffective[i,nmain+m] = v;
+                    m = m+1;
+                }
+                if( state.ct[i]>0 )
+                {
+                    state.ceeffective[i,nmain+m] = -v;
+                    m = m+1;
+                }
+            }
+            
+            //
+            // Find feasible point.
+            //
+            // 0. Convert from unscaled values (as stored in XStart) to scaled
+            //    ones
+            // 1. calculate values of slack variables such that starting
+            //    point satisfies inequality constraints (after conversion to
+            //    equality ones) as much as possible.
+            // 2. use PrepareConstraintMatrix() function, which forces X
+            //    to be strictly feasible.
+            //
+            for(i=0; i<=nmain-1; i++)
+            {
+                state.tmp0[i] = state.xstart[i]/state.transforms[i];
+            }
+            m = 0;
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=nmain-1;i_++)
+                {
+                    v += state.ceeffective[i,i_]*state.tmp0[i_];
+                }
+                if( state.ct[i]<0 )
+                {
+                    state.tmp0[nmain+m] = state.ceeffective[i,nmain+nslack]-v;
+                    m = m+1;
+                }
+                if( state.ct[i]>0 )
+                {
+                    state.tmp0[nmain+m] = v-state.ceeffective[i,nmain+nslack];
+                    m = m+1;
+                }
+            }
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                state.tmp1[i] = 0;
+            }
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                state.activeconstraints[i] = false;
+            }
+            b = prepareconstraintmatrix(state, state.tmp0, state.tmp1, ref state.xcur, ref state.tmp2);
+            state.repdebugeqerr = 0.0;
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    v += state.ceeffective[i,i_]*state.xcur[i_];
+                }
+                state.repdebugeqerr = state.repdebugeqerr+math.sqr(v-state.ceeffective[i,nmain+nslack]);
+            }
+            state.repdebugeqerr = Math.Sqrt(state.repdebugeqerr);
+            if( !b )
+            {
+                state.repterminationtype = -3;
+                result = false;
+                return result;
+            }
+            
+            //
+            // Initialize RepDebugFS with function value at initial point
+            //
+            unscalepoint(state, state.xcur, ref state.x);
+            clearrequestfields(state);
+            if( (double)(state.diffstep)!=(double)(0) )
+            {
+                goto lbl_13;
+            }
+            state.needfg = true;
+            state.rstate.stage = 0;
+            goto lbl_rcomm;
+        lbl_0:
+            state.needfg = false;
+            goto lbl_14;
+        lbl_13:
+            state.needf = true;
+            state.rstate.stage = 1;
+            goto lbl_rcomm;
+        lbl_1:
+            state.needf = false;
+        lbl_14:
+            optserv.trimprepare(state.f, ref state.trimthreshold);
+            state.repnfev = state.repnfev+1;
+            state.repdebugfs = state.f;
+            
+            //
+            // Outer cycle
+            //
+            state.itsleft = state.maxits;
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                state.xprev[i_] = state.xcur[i_];
+            }
+        lbl_15:
+            if( false )
+            {
+                goto lbl_16;
+            }
+            ap.assert(state.prectype==0 | (double)(state.stpmax)==(double)(0), "MinBLEIC: internal error (-10)");
+            
+            //
+            // Inner cycle: CG with projections and penalty functions
+            //
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                state.tmp0[i_] = state.xcur[i_];
+            }
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                state.tmp1[i] = 0;
+                state.activeconstraints[i] = false;
+            }
+            if( !prepareconstraintmatrix(state, state.tmp0, state.tmp1, ref state.xcur, ref state.tmp2) )
+            {
+                state.repterminationtype = -3;
+                result = false;
+                return result;
+            }
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                state.activeconstraints[i] = false;
+            }
+            rebuildcexe(state);
+            mincg.mincgrestartfrom(state.cgstate, state.xcur);
+            mincg.mincgsetcond(state.cgstate, state.innerepsg, state.innerepsf, state.innerepsx, state.itsleft);
+            mincg.mincgsetxrep(state.cgstate, state.xrep);
+            mincg.mincgsetdrep(state.cgstate, true);
+            mincg.mincgsetstpmax(state.cgstate, state.stpmax);
+        lbl_17:
+            if( !mincg.mincgiteration(state.cgstate) )
+            {
+                goto lbl_18;
+            }
+            
+            //
+            // process different requests/reports of inner optimizer
+            //
+            if( state.cgstate.algpowerup )
+            {
+                for(i=0; i<=nmain+nslack-1; i++)
+                {
+                    state.activeconstraints[i] = false;
+                }
+                do
+                {
+                    rebuildcexe(state);
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        state.tmp1[i_] = state.cgstate.g[i_];
+                    }
+                    makegradientprojection(state, ref state.tmp1);
+                    b = false;
+                    for(i=0; i<=nmain-1; i++)
+                    {
+                        if( !state.activeconstraints[i] )
+                        {
+                            if( state.hasbndl[i] )
+                            {
+                                if( (double)(state.cgstate.x[i])==(double)(state.bndleffective[i]) & (double)(state.tmp1[i])>=(double)(0) )
+                                {
+                                    state.activeconstraints[i] = true;
+                                    state.constrainedvalues[i] = state.bndleffective[i];
+                                    b = true;
+                                }
+                            }
+                            if( state.hasbndu[i] )
+                            {
+                                if( (double)(state.cgstate.x[i])==(double)(state.bndueffective[i]) & (double)(state.tmp1[i])<=(double)(0) )
+                                {
+                                    state.activeconstraints[i] = true;
+                                    state.constrainedvalues[i] = state.bndueffective[i];
+                                    b = true;
+                                }
+                            }
+                        }
+                    }
+                    for(i=0; i<=nslack-1; i++)
+                    {
+                        if( !state.activeconstraints[nmain+i] )
+                        {
+                            if( (double)(state.cgstate.x[nmain+i])==(double)(0) & (double)(state.tmp1[nmain+i])>=(double)(0) )
+                            {
+                                state.activeconstraints[nmain+i] = true;
+                                state.constrainedvalues[nmain+i] = 0;
+                                b = true;
+                            }
+                        }
+                    }
+                }
+                while( b );
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    state.cgstate.g[i_] = state.tmp1[i_];
+                }
+                goto lbl_17;
+            }
+            if( state.cgstate.lsstart )
+            {
+                
+                //
+                // Beginning of the line search: set upper limit on step size
+                // to prevent algo from leaving feasible area.
+                //
+                state.variabletofreeze = -1;
+                if( (double)(state.cgstate.curstpmax)==(double)(0) )
+                {
+                    state.cgstate.curstpmax = 1.0E50;
+                }
+                for(i=0; i<=nmain-1; i++)
+                {
+                    if( state.hasbndl[i] & (double)(state.cgstate.d[i])<(double)(0) )
+                    {
+                        v = state.cgstate.curstpmax;
+                        vv = state.cgstate.x[i]-state.bndleffective[i];
+                        if( (double)(vv)<(double)(0) )
+                        {
+                            vv = 0;
+                        }
+                        state.cgstate.curstpmax = apserv.safeminposrv(vv, -state.cgstate.d[i], state.cgstate.curstpmax);
+                        if( (double)(state.cgstate.curstpmax)<(double)(v) )
+                        {
+                            state.variabletofreeze = i;
+                            state.valuetofreeze = state.bndleffective[i];
+                        }
+                    }
+                    if( state.hasbndu[i] & (double)(state.cgstate.d[i])>(double)(0) )
+                    {
+                        v = state.cgstate.curstpmax;
+                        vv = state.bndueffective[i]-state.cgstate.x[i];
+                        if( (double)(vv)<(double)(0) )
+                        {
+                            vv = 0;
+                        }
+                        state.cgstate.curstpmax = apserv.safeminposrv(vv, state.cgstate.d[i], state.cgstate.curstpmax);
+                        if( (double)(state.cgstate.curstpmax)<(double)(v) )
+                        {
+                            state.variabletofreeze = i;
+                            state.valuetofreeze = state.bndueffective[i];
+                        }
+                    }
+                }
+                for(i=0; i<=nslack-1; i++)
+                {
+                    if( (double)(state.cgstate.d[nmain+i])<(double)(0) )
+                    {
+                        v = state.cgstate.curstpmax;
+                        vv = state.cgstate.x[nmain+i];
+                        if( (double)(vv)<(double)(0) )
+                        {
+                            vv = 0;
+                        }
+                        state.cgstate.curstpmax = apserv.safeminposrv(vv, -state.cgstate.d[nmain+i], state.cgstate.curstpmax);
+                        if( (double)(state.cgstate.curstpmax)<(double)(v) )
+                        {
+                            state.variabletofreeze = nmain+i;
+                            state.valuetofreeze = 0;
+                        }
+                    }
+                }
+                if( (double)(state.cgstate.curstpmax)==(double)(0) )
+                {
+                    state.activeconstraints[state.variabletofreeze] = true;
+                    state.constrainedvalues[state.variabletofreeze] = state.valuetofreeze;
+                    state.cgstate.x[state.variabletofreeze] = state.valuetofreeze;
+                    state.cgstate.terminationneeded = true;
+                }
+                goto lbl_17;
+            }
+            if( state.cgstate.lsend )
+            {
+                
+                //
+                // Line search just finished.
+                // Maybe we should activate some constraints?
+                //
+                b = (double)(state.cgstate.stp)>=(double)(state.cgstate.curstpmax) & state.variabletofreeze>=0;
+                if( b )
+                {
+                    state.activeconstraints[state.variabletofreeze] = true;
+                    state.constrainedvalues[state.variabletofreeze] = state.valuetofreeze;
+                }
+                
+                //
+                // Additional activation of constraints
+                //
+                b = b | additionalcheckforconstraints(state, state.cgstate.x);
+                
+                //
+                // If at least one constraint was activated we have to rebuild constraint matrices
+                //
+                if( b )
+                {
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        state.tmp0[i_] = state.cgstate.x[i_];
+                    }
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        state.tmp1[i_] = state.lastg[i_];
+                    }
+                    if( !prepareconstraintmatrix(state, state.tmp0, state.tmp1, ref state.cgstate.x, ref state.cgstate.g) )
+                    {
+                        state.repterminationtype = -3;
+                        result = false;
+                        return result;
+                    }
+                    state.cgstate.innerresetneeded = true;
+                }
+                goto lbl_17;
+            }
+            if( !state.cgstate.needfg )
+            {
+                goto lbl_19;
+            }
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                state.tmp1[i_] = state.cgstate.x[i_];
+            }
+            projectpointandunscale(state, ref state.tmp1, ref state.x, ref state.r, ref vv);
+            clearrequestfields(state);
+            if( (double)(state.diffstep)!=(double)(0) )
+            {
+                goto lbl_21;
+            }
+            state.needfg = true;
+            state.rstate.stage = 2;
+            goto lbl_rcomm;
+        lbl_2:
+            state.needfg = false;
+            goto lbl_22;
+        lbl_21:
+            state.needf = true;
+            state.rstate.stage = 3;
+            goto lbl_rcomm;
+        lbl_3:
+            state.fbase = state.f;
+            i = 0;
+        lbl_23:
+            if( i>nmain-1 )
+            {
+                goto lbl_25;
+            }
+            v = state.x[i];
+            b = false;
+            if( state.hasbndl[i] )
+            {
+                b = b | (double)(v-state.diffstep*state.soriginal[i])<(double)(state.bndloriginal[i]);
+            }
+            if( state.hasbndu[i] )
+            {
+                b = b | (double)(v+state.diffstep*state.soriginal[i])>(double)(state.bnduoriginal[i]);
+            }
+            if( b )
+            {
+                goto lbl_26;
+            }
+            state.x[i] = v-state.diffstep*state.soriginal[i];
+            state.rstate.stage = 4;
+            goto lbl_rcomm;
+        lbl_4:
+            state.fm2 = state.f;
+            state.x[i] = v-0.5*state.diffstep*state.soriginal[i];
+            state.rstate.stage = 5;
+            goto lbl_rcomm;
+        lbl_5:
+            state.fm1 = state.f;
+            state.x[i] = v+0.5*state.diffstep*state.soriginal[i];
+            state.rstate.stage = 6;
+            goto lbl_rcomm;
+        lbl_6:
+            state.fp1 = state.f;
+            state.x[i] = v+state.diffstep*state.soriginal[i];
+            state.rstate.stage = 7;
+            goto lbl_rcomm;
+        lbl_7:
+            state.fp2 = state.f;
+            state.g[i] = (8*(state.fp1-state.fm1)-(state.fp2-state.fm2))/(6*state.diffstep*state.soriginal[i]);
+            goto lbl_27;
+        lbl_26:
+            state.xm1 = Math.Max(v-state.diffstep*state.soriginal[i], state.bndloriginal[i]);
+            state.x[i] = state.xm1;
+            state.rstate.stage = 8;
+            goto lbl_rcomm;
+        lbl_8:
+            state.fm1 = state.f;
+            state.xp1 = Math.Min(v+state.diffstep*state.soriginal[i], state.bnduoriginal[i]);
+            state.x[i] = state.xp1;
+            state.rstate.stage = 9;
+            goto lbl_rcomm;
+        lbl_9:
+            state.fp1 = state.f;
+            state.g[i] = (state.fp1-state.fm1)/(state.xp1-state.xm1);
+        lbl_27:
+            state.x[i] = v;
+            i = i+1;
+            goto lbl_23;
+        lbl_25:
+            state.f = state.fbase;
+            state.needf = false;
+        lbl_22:
+            if( (double)(state.f)<(double)(state.trimthreshold) )
+            {
+                
+                //
+                // normal processing
+                //
+                state.cgstate.f = state.f;
+                scalegradientandexpand(state, state.g, ref state.cgstate.g);
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    state.lastg[i_] = state.cgstate.g[i_];
+                }
+                modifytargetfunction(state, state.tmp1, state.r, vv, ref state.cgstate.f, ref state.cgstate.g, ref state.gnorm, ref state.mpgnorm);
+            }
+            else
+            {
+                
+                //
+                // function value is too high, trim it
+                //
+                state.cgstate.f = state.trimthreshold;
+                for(i=0; i<=nmain+nslack-1; i++)
+                {
+                    state.cgstate.g[i] = 0.0;
+                }
+            }
+            goto lbl_17;
+        lbl_19:
+            if( !state.cgstate.xupdated )
+            {
+                goto lbl_28;
+            }
+            
+            //
+            // Report
+            //
+            unscalepoint(state, state.cgstate.x, ref state.x);
+            state.f = state.cgstate.f;
+            clearrequestfields(state);
+            state.xupdated = true;
+            state.rstate.stage = 10;
+            goto lbl_rcomm;
+        lbl_10:
+            state.xupdated = false;
+            goto lbl_17;
+        lbl_28:
+            goto lbl_17;
+        lbl_18:
+            mincg.mincgresults(state.cgstate, ref state.xcur, state.cgrep);
+            unscalepoint(state, state.xcur, ref state.xend);
+            state.repinneriterationscount = state.repinneriterationscount+state.cgrep.iterationscount;
+            state.repouteriterationscount = state.repouteriterationscount+1;
+            state.repnfev = state.repnfev+state.cgrep.nfev;
+            
+            //
+            // Update RepDebugFF with function value at current point
+            //
+            unscalepoint(state, state.xcur, ref state.x);
+            clearrequestfields(state);
+            if( (double)(state.diffstep)!=(double)(0) )
+            {
+                goto lbl_30;
+            }
+            state.needfg = true;
+            state.rstate.stage = 11;
+            goto lbl_rcomm;
+        lbl_11:
+            state.needfg = false;
+            goto lbl_31;
+        lbl_30:
+            state.needf = true;
+            state.rstate.stage = 12;
+            goto lbl_rcomm;
+        lbl_12:
+            state.needf = false;
+        lbl_31:
+            state.repnfev = state.repnfev+1;
+            state.repdebugff = state.f;
+            
+            //
+            // Check for stopping:
+            // * "normal", outer step size is small enough, infeasibility is within bounds
+            // * "inconsistent",  if Lagrange multipliers increased beyond threshold given by MaxLagrangeMul
+            // * "too stringent", in other cases
+            //
+            v = 0;
+            for(i=0; i<=nmain-1; i++)
+            {
+                v = v+math.sqr((state.xcur[i]-state.xprev[i])/state.seffective[i]);
+            }
+            v = Math.Sqrt(v);
+            if( (double)(v)<=(double)(state.outerepsx) )
+            {
+                state.repterminationtype = 4;
+                goto lbl_16;
+            }
+            if( state.maxits>0 )
+            {
+                state.itsleft = state.itsleft-state.cgrep.iterationscount;
+                if( state.itsleft<=0 )
+                {
+                    state.repterminationtype = 5;
+                    goto lbl_16;
+                }
+            }
+            if( (double)(state.repouteriterationscount)>=(double)(maxouterits) )
+            {
+                state.repterminationtype = 5;
+                goto lbl_16;
+            }
+            
+            //
+            // Next iteration
+            //
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                state.xprev[i_] = state.xcur[i_];
+            }
+            goto lbl_15;
+        lbl_16:
+            
+            //
+            // We've stopped, fill debug information
+            //
+            state.repdebugeqerr = 0.0;
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    v += state.ceeffective[i,i_]*state.xcur[i_];
+                }
+                state.repdebugeqerr = state.repdebugeqerr+math.sqr(v-state.ceeffective[i,nmain+nslack]);
+            }
+            state.repdebugeqerr = Math.Sqrt(state.repdebugeqerr);
+            state.repdebugdx = 0;
+            for(i=0; i<=nmain-1; i++)
+            {
+                state.repdebugdx = state.repdebugdx+math.sqr(state.xcur[i]-state.xstart[i]);
+            }
+            state.repdebugdx = Math.Sqrt(state.repdebugdx);
+            result = false;
+            return result;
+            
+            //
+            // Saving state
+            //
+        lbl_rcomm:
+            result = true;
+            state.rstate.ia[0] = nmain;
+            state.rstate.ia[1] = nslack;
+            state.rstate.ia[2] = m;
+            state.rstate.ia[3] = i;
+            state.rstate.ia[4] = j;
+            state.rstate.ba[0] = b;
+            state.rstate.ra[0] = v;
+            state.rstate.ra[1] = vv;
+            return result;
+        }
+
+
+        /*************************************************************************
+        BLEIC results
+
+        INPUT PARAMETERS:
+            State   -   algorithm state
+
+        OUTPUT PARAMETERS:
+            X       -   array[0..N-1], solution
+            Rep     -   optimization report. You should check Rep.TerminationType
+                        in  order  to  distinguish  successful  termination  from
+                        unsuccessful one.
+                        More information about fields of this  structure  can  be
+                        found in the comments on MinBLEICReport datatype.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicresults(minbleicstate state,
+            ref double[] x,
+            minbleicreport rep)
+        {
+            x = new double[0];
+
+            minbleicresultsbuf(state, ref x, rep);
+        }
+
+
+        /*************************************************************************
+        BLEIC results
+
+        Buffered implementation of MinBLEICResults() which uses pre-allocated buffer
+        to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
+        intended to be used in the inner cycles of performance critical algorithms
+        where array reallocation penalty is too large to be ignored.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicresultsbuf(minbleicstate state,
+            ref double[] x,
+            minbleicreport rep)
+        {
+            int i = 0;
+            int i_ = 0;
+
+            if( ap.len(x)<state.nmain )
+            {
+                x = new double[state.nmain];
+            }
+            rep.inneriterationscount = state.repinneriterationscount;
+            rep.outeriterationscount = state.repouteriterationscount;
+            rep.nfev = state.repnfev;
+            rep.terminationtype = state.repterminationtype;
+            if( state.repterminationtype>0 )
+            {
+                for(i_=0; i_<=state.nmain-1;i_++)
+                {
+                    x[i_] = state.xend[i_];
+                }
+            }
+            else
+            {
+                for(i=0; i<=state.nmain-1; i++)
+                {
+                    x[i] = Double.NaN;
+                }
+            }
+            rep.debugeqerr = state.repdebugeqerr;
+            rep.debugfs = state.repdebugfs;
+            rep.debugff = state.repdebugff;
+            rep.debugdx = state.repdebugdx;
+        }
+
+
+        /*************************************************************************
+        This subroutine restarts algorithm from new point.
+        All optimization parameters (including constraints) are left unchanged.
+
+        This  function  allows  to  solve multiple  optimization  problems  (which
+        must have  same number of dimensions) without object reallocation penalty.
+
+        INPUT PARAMETERS:
+            State   -   structure previously allocated with MinBLEICCreate call.
+            X       -   new starting point.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicrestartfrom(minbleicstate state,
+            double[] x)
+        {
+            int n = 0;
+            int i_ = 0;
+
+            n = state.nmain;
+            
+            //
+            // First, check for errors in the inputs
+            //
+            ap.assert(ap.len(x)>=n, "MinBLEICRestartFrom: Length(X)<N");
+            ap.assert(apserv.isfinitevector(x, n), "MinBLEICRestartFrom: X contains infinite or NaN values!");
+            
+            //
+            // Set XC
+            //
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.xstart[i_] = x[i_];
+            }
+            
+            //
+            // prepare RComm facilities
+            //
+            state.rstate.ia = new int[4+1];
+            state.rstate.ba = new bool[0+1];
+            state.rstate.ra = new double[1+1];
+            state.rstate.stage = -1;
+            clearrequestfields(state);
+        }
+
+
+        /*************************************************************************
+        Clears request fileds (to be sure that we don't forget to clear something)
+        *************************************************************************/
+        private static void clearrequestfields(minbleicstate state)
+        {
+            state.needf = false;
+            state.needfg = false;
+            state.xupdated = false;
+        }
+
+
+        /*************************************************************************
+        This functions "unscales" point, i.e. it makes transformation  from scaled
+        variables to unscaled ones. Only leading NMain variables are copied from
+        XUnscaled to XScaled.
+        *************************************************************************/
+        private static void unscalepoint(minbleicstate state,
+            double[] xscaled,
+            ref double[] xunscaled)
+        {
+            int i = 0;
+            double v = 0;
+
+            for(i=0; i<=state.nmain-1; i++)
+            {
+                v = xscaled[i]*state.transforms[i];
+                if( state.hasbndl[i] )
+                {
+                    if( (double)(v)<(double)(state.bndloriginal[i]) )
+                    {
+                        v = state.bndloriginal[i];
+                    }
+                }
+                if( state.hasbndu[i] )
+                {
+                    if( (double)(v)>(double)(state.bnduoriginal[i]) )
+                    {
+                        v = state.bnduoriginal[i];
+                    }
+                }
+                xunscaled[i] = v;
+            }
+        }
+
+
+        /*************************************************************************
+        This function:
+        1. makes projection of XScaled into equality constrained subspace
+           (X is modified in-place)
+        2. stores residual from the projection into R
+        3. unscales projected XScaled and stores result into XUnscaled with
+           additional enforcement
+        It calculates set of additional values which are used later for
+        modification of the target function F.
+
+        INPUT PARAMETERS:
+            State   -   optimizer state (we use its fields to get information
+                        about constraints)
+            X       -   vector being projected
+            R       -   preallocated buffer, used to store residual from projection
+
+        OUTPUT PARAMETERS:
+            X       -   projection of input X
+            R       -   residual
+            RNorm   -   residual norm squared, used later to modify target function
+        *************************************************************************/
+        private static void projectpointandunscale(minbleicstate state,
+            ref double[] xscaled,
+            ref double[] xunscaled,
+            ref double[] rscaled,
+            ref double rnorm2)
+        {
+            double v = 0;
+            int i = 0;
+            int nmain = 0;
+            int nslack = 0;
+            int i_ = 0;
+
+            rnorm2 = 0;
+
+            nmain = state.nmain;
+            nslack = state.nslack;
+            
+            //
+            // * subtract XE from XScaled
+            // * project XScaled
+            // * calculate norm of deviation from null space, store it in RNorm2
+            // * calculate residual from projection, store it in R
+            // * add XE to XScaled
+            // * unscale variables
+            //
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                xscaled[i_] = xscaled[i_] - state.xe[i_];
+            }
+            rnorm2 = 0;
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                rscaled[i] = 0;
+            }
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                if( state.activeconstraints[i] )
+                {
+                    v = xscaled[i];
+                    xscaled[i] = 0;
+                    rscaled[i] = rscaled[i]+v;
+                    rnorm2 = rnorm2+math.sqr(v);
+                }
+            }
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    v += xscaled[i_]*state.cecurrent[i,i_];
+                }
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    xscaled[i_] = xscaled[i_] - v*state.cecurrent[i,i_];
+                }
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    rscaled[i_] = rscaled[i_] + v*state.cecurrent[i,i_];
+                }
+                rnorm2 = rnorm2+math.sqr(v);
+            }
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                xscaled[i_] = xscaled[i_] + state.xe[i_];
+            }
+            unscalepoint(state, xscaled, ref xunscaled);
+        }
+
+
+        /*************************************************************************
+        This function scales and copies NMain elements of GUnscaled into GScaled.
+        Other NSlack components of GScaled are set to zero.
+        *************************************************************************/
+        private static void scalegradientandexpand(minbleicstate state,
+            double[] gunscaled,
+            ref double[] gscaled)
+        {
+            int i = 0;
+
+            for(i=0; i<=state.nmain-1; i++)
+            {
+                gscaled[i] = gunscaled[i]*state.transforms[i];
+            }
+            for(i=0; i<=state.nslack-1; i++)
+            {
+                gscaled[state.nmain+i] = 0;
+            }
+        }
+
+
+        /*************************************************************************
+        This subroutine applies modifications to the target function given by
+        its value F and gradient G at the projected point X which lies in the
+        equality constrained subspace.
+
+        Following modifications are applied:
+        * modified barrier functions to handle inequality constraints
+          (both F and G are modified)
+        * projection of gradient into equality constrained subspace
+          (only G is modified)
+        * quadratic penalty for deviations from equality constrained subspace
+          (both F and G are modified)
+
+        It also calculates gradient norm (three different norms for three
+        different types of gradient), feasibility and complementary slackness
+        errors.
+
+        INPUT PARAMETERS:
+            State   -   optimizer state (we use its fields to get information
+                        about constraints)
+            X       -   point (projected into equality constrained subspace)
+            R       -   residual from projection
+            RNorm2  -   residual norm squared
+            F       -   function value at X
+            G       -   function gradient at X
+
+        OUTPUT PARAMETERS:
+            F       -   modified function value at X
+            G       -   modified function gradient at X
+            GNorm   -   2-norm of unmodified G
+            MPGNorm -   2-norm of modified G
+            MBA     -   minimum argument of barrier functions.
+                        If X is strictly feasible, it is greater than zero.
+                        If X lies on a boundary, it is zero.
+                        It is negative for infeasible X.
+            FIErr   -   2-norm of feasibility error with respect to
+                        inequality/bound constraints
+            CSErr   -   2-norm of complementarity slackness error
+        *************************************************************************/
+        private static void modifytargetfunction(minbleicstate state,
+            double[] x,
+            double[] r,
+            double rnorm2,
+            ref double f,
+            ref double[] g,
+            ref double gnorm,
+            ref double mpgnorm)
+        {
+            double v = 0;
+            int i = 0;
+            int nmain = 0;
+            int nslack = 0;
+            bool hasconstraints = new bool();
+            int i_ = 0;
+
+            gnorm = 0;
+            mpgnorm = 0;
+
+            nmain = state.nmain;
+            nslack = state.nslack;
+            hasconstraints = false;
+            
+            //
+            // GNorm
+            //
+            v = 0.0;
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                v += g[i_]*g[i_];
+            }
+            gnorm = Math.Sqrt(v);
+            
+            //
+            // Process equality constraints:
+            // * modify F to handle penalty term for equality constraints
+            // * project gradient on null space of equality constraints
+            // * add penalty term for equality constraints to gradient
+            //
+            f = f+rnorm2;
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                if( state.activeconstraints[i] )
+                {
+                    g[i] = 0;
+                }
+            }
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    v += g[i_]*state.cecurrent[i,i_];
+                }
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    g[i_] = g[i_] - v*state.cecurrent[i,i_];
+                }
+            }
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                g[i_] = g[i_] + 2*r[i_];
+            }
+            
+            //
+            // MPGNorm
+            //
+            v = 0.0;
+            for(i_=0; i_<=nmain+nslack-1;i_++)
+            {
+                v += g[i_]*g[i_];
+            }
+            mpgnorm = Math.Sqrt(v);
+        }
+
+
+        /*************************************************************************
+        This function makes additional check for constraints which can be activated.
+
+        We try activate constraints one by one, but it is possible that several
+        constraints should be activated during one iteration. It this case only
+        one of them (probably last) will be activated. This function will fix it -
+        it will pass through constraints and activate those which are at the boundary
+        or beyond it.
+
+        It will return True, if at least one constraint was activated by this function.
+        *************************************************************************/
+        private static bool additionalcheckforconstraints(minbleicstate state,
+            double[] x)
+        {
+            bool result = new bool();
+            int i = 0;
+            int nmain = 0;
+            int nslack = 0;
+
+            result = false;
+            nmain = state.nmain;
+            nslack = state.nslack;
+            for(i=0; i<=nmain-1; i++)
+            {
+                if( !state.activeconstraints[i] )
+                {
+                    if( state.hasbndl[i] )
+                    {
+                        if( (double)(x[i])<=(double)(state.bndleffective[i]) )
+                        {
+                            state.activeconstraints[i] = true;
+                            state.constrainedvalues[i] = state.bndleffective[i];
+                            result = true;
+                        }
+                    }
+                    if( state.hasbndu[i] )
+                    {
+                        if( (double)(x[i])>=(double)(state.bndueffective[i]) )
+                        {
+                            state.activeconstraints[i] = true;
+                            state.constrainedvalues[i] = state.bndueffective[i];
+                            result = true;
+                        }
+                    }
+                }
+            }
+            for(i=0; i<=nslack-1; i++)
+            {
+                if( !state.activeconstraints[nmain+i] )
+                {
+                    if( (double)(x[nmain+i])<=(double)(0) )
+                    {
+                        state.activeconstraints[nmain+i] = true;
+                        state.constrainedvalues[nmain+i] = 0;
+                        result = true;
+                    }
+                }
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        This function rebuilds CECurrent and XE according to current set of
+        active bound constraints.
+        *************************************************************************/
+        private static void rebuildcexe(minbleicstate state)
+        {
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int nmain = 0;
+            int nslack = 0;
+            double v = 0;
+            int i_ = 0;
+
+            nmain = state.nmain;
+            nslack = state.nslack;
+            ablas.rmatrixcopy(state.cecnt, nmain+nslack+1, state.ceeffective, 0, 0, ref state.cecurrent, 0, 0);
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                
+                //
+                // "Subtract" active bound constraints from I-th linear constraint
+                //
+                for(j=0; j<=nmain+nslack-1; j++)
+                {
+                    if( state.activeconstraints[j] )
+                    {
+                        state.cecurrent[i,nmain+nslack] = state.cecurrent[i,nmain+nslack]-state.cecurrent[i,j]*state.constrainedvalues[j];
+                        state.cecurrent[i,j] = 0.0;
+                    }
+                }
+                
+                //
+                // Reorthogonalize I-th constraint with respect to previous ones
+                // NOTE: we also update right part, which is CECurrent[...,NMain+NSlack].
+                //
+                for(k=0; k<=i-1; k++)
+                {
+                    v = 0.0;
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        v += state.cecurrent[k,i_]*state.cecurrent[i,i_];
+                    }
+                    for(i_=0; i_<=nmain+nslack;i_++)
+                    {
+                        state.cecurrent[i,i_] = state.cecurrent[i,i_] - v*state.cecurrent[k,i_];
+                    }
+                }
+                
+                //
+                // Calculate norm of I-th row of CECurrent. Fill by zeros, if it is
+                // too small. Normalize otherwise.
+                //
+                // NOTE: we also scale last column of CECurrent (right part)
+                //
+                v = 0.0;
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    v += state.cecurrent[i,i_]*state.cecurrent[i,i_];
+                }
+                v = Math.Sqrt(v);
+                if( (double)(v)>(double)(10000*math.machineepsilon) )
+                {
+                    v = 1/v;
+                    for(i_=0; i_<=nmain+nslack;i_++)
+                    {
+                        state.cecurrent[i,i_] = v*state.cecurrent[i,i_];
+                    }
+                }
+                else
+                {
+                    for(j=0; j<=nmain+nslack; j++)
+                    {
+                        state.cecurrent[i,j] = 0;
+                    }
+                }
+            }
+            for(j=0; j<=nmain+nslack-1; j++)
+            {
+                state.xe[j] = 0;
+            }
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                if( state.activeconstraints[i] )
+                {
+                    state.xe[i] = state.xe[i]+state.constrainedvalues[i];
+                }
+            }
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = state.cecurrent[i,nmain+nslack];
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    state.xe[i_] = state.xe[i_] + v*state.cecurrent[i,i_];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function projects gradient onto equality constrained subspace
+        *************************************************************************/
+        private static void makegradientprojection(minbleicstate state,
+            ref double[] pg)
+        {
+            int i = 0;
+            int nmain = 0;
+            int nslack = 0;
+            double v = 0;
+            int i_ = 0;
+
+            nmain = state.nmain;
+            nslack = state.nslack;
+            for(i=0; i<=nmain+nslack-1; i++)
+            {
+                if( state.activeconstraints[i] )
+                {
+                    pg[i] = 0;
+                }
+            }
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    v += pg[i_]*state.cecurrent[i,i_];
+                }
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    pg[i_] = pg[i_] - v*state.cecurrent[i,i_];
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        This function prepares equality constrained subproblem:
+
+        1. X is used to activate constraints (if there are ones which are still
+           inactive, but should be activated).
+        2. constraints matrix CEOrt is copied to CECurrent and modified  according
+           to the list of active bound constraints (corresponding elements are
+           filled by zeros and reorthogonalized).
+        3. XE - least squares solution of equality constraints - is recalculated
+        4. X is copied to PX and projected onto equality constrained subspace
+        5. inactive constraints are checked against PX - if there is at least one
+           which should be activated, we activate it and move back to (2)
+        6. as result, PX is feasible with respect to bound constraints - step (5)
+           guarantees it. But PX can be infeasible with respect to equality ones,
+           because step (2) is done without checks for consistency. As the final
+           step, we check that PX is feasible. If not, we return False. True is
+           returned otherwise.
+
+        If this algorithm returned True, then:
+        * X is not changed
+        * PX contains projection of X onto constrained subspace
+        * G is not changed
+        * PG contains projection of G onto constrained subspace
+        * PX is feasible with respect to all constraints
+        * all constraints which are active at PX, are activated
+        *************************************************************************/
+        private static bool prepareconstraintmatrix(minbleicstate state,
+            double[] x,
+            double[] g,
+            ref double[] px,
+            ref double[] pg)
+        {
+            bool result = new bool();
+            int i = 0;
+            int nmain = 0;
+            int nslack = 0;
+            double v = 0;
+            double ferr = 0;
+            int i_ = 0;
+
+            nmain = state.nmain;
+            nslack = state.nslack;
+            result = true;
+            
+            //
+            // Step 1
+            //
+            additionalcheckforconstraints(state, x);
+            
+            //
+            // Steps 2-5
+            //
+            do
+            {
+                
+                //
+                // Steps 2-3
+                //
+                rebuildcexe(state);
+                
+                //
+                // Step 4
+                //
+                // Calculate PX, PG
+                //
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    px[i_] = x[i_];
+                }
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    px[i_] = px[i_] - state.xe[i_];
+                }
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    pg[i_] = g[i_];
+                }
+                for(i=0; i<=nmain+nslack-1; i++)
+                {
+                    if( state.activeconstraints[i] )
+                    {
+                        px[i] = 0;
+                        pg[i] = 0;
+                    }
+                }
+                for(i=0; i<=state.cecnt-1; i++)
+                {
+                    v = 0.0;
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        v += px[i_]*state.cecurrent[i,i_];
+                    }
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        px[i_] = px[i_] - v*state.cecurrent[i,i_];
+                    }
+                    v = 0.0;
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        v += pg[i_]*state.cecurrent[i,i_];
+                    }
+                    for(i_=0; i_<=nmain+nslack-1;i_++)
+                    {
+                        pg[i_] = pg[i_] - v*state.cecurrent[i,i_];
+                    }
+                }
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    px[i_] = px[i_] + state.xe[i_];
+                }
+                
+                //
+                // Step 5 (loop condition below)
+                //
+            }
+            while( additionalcheckforconstraints(state, px) );
+            
+            //
+            // Step 6
+            //
+            ferr = 0;
+            for(i=0; i<=state.cecnt-1; i++)
+            {
+                v = 0.0;
+                for(i_=0; i_<=nmain+nslack-1;i_++)
+                {
+                    v += px[i_]*state.ceeffective[i,i_];
+                }
+                v = v-state.ceeffective[i,nmain+nslack];
+                ferr = Math.Max(ferr, Math.Abs(v));
+            }
+            result = (double)(ferr)<=(double)(state.outerepsi);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Internal initialization subroutine
+        *************************************************************************/
+        private static void minbleicinitinternal(int n,
+            double[] x,
+            double diffstep,
+            minbleicstate state)
+        {
+            int i = 0;
+            double[,] c = new double[0,0];
+            int[] ct = new int[0];
+
+            state.nmain = n;
+            state.optdim = 0;
+            state.diffstep = diffstep;
+            state.bndloriginal = new double[n];
+            state.bndleffective = new double[n];
+            state.hasbndl = new bool[n];
+            state.bnduoriginal = new double[n];
+            state.bndueffective = new double[n];
+            state.hasbndu = new bool[n];
+            state.xstart = new double[n];
+            state.soriginal = new double[n];
+            state.x = new double[n];
+            state.g = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                state.bndloriginal[i] = Double.NegativeInfinity;
+                state.hasbndl[i] = false;
+                state.bnduoriginal[i] = Double.PositiveInfinity;
+                state.hasbndu[i] = false;
+                state.soriginal[i] = 1.0;
+            }
+            minbleicsetlc(state, c, ct, 0);
+            minbleicsetinnercond(state, 0.0, 0.0, 0.0);
+            minbleicsetoutercond(state, 1.0E-6, 1.0E-6);
+            minbleicsetmaxits(state, 0);
+            minbleicsetxrep(state, false);
+            minbleicsetstpmax(state, 0.0);
+            minbleicsetprecdefault(state);
+            minbleicrestartfrom(state, x);
+        }
+
+
+    }
     public class minlbfgs
     {
@@ -2041,7 +7645,8 @@
             public double epsx;
             public int maxits;
-            public int flags;
             public bool xrep;
             public double stpmax;
+            public double[] s;
+            public double diffstep;
             public int nfev;
             public int mcstage;
@@ -2050,6 +7655,6 @@
             public int p;
             public double[] rho;
-            public double[,] y;
-            public double[,] s;
+            public double[,] yk;
+            public double[,] sk;
             public double[] theta;
             public double[] d;
@@ -2057,11 +7662,19 @@
             public double[] work;
             public double fold;
+            public double trimthreshold;
             public int prectype;
             public double gammak;
             public double[,] denseh;
+            public double[] diagh;
+            public double fbase;
+            public double fm2;
+            public double fm1;
+            public double fp1;
+            public double fp2;
             public double[] autobuf;
             public double[] x;
             public double f;
             public double[] g;
+            public bool needf;
             public bool needfg;
             public bool xupdated;
@@ -2073,11 +7686,13 @@
             public minlbfgsstate()
             {
+                s = new double[0];
                 rho = new double[0];
-                y = new double[0,0];
-                s = new double[0,0];
+                yk = new double[0,0];
+                sk = new double[0,0];
                 theta = new double[0];
                 d = new double[0];
                 work = new double[0];
                 denseh = new double[0,0];
+                diagh = new double[0];
                 autobuf = new double[0];
                 x = new double[0];
@@ -2097,4 +7712,7 @@
 
 
+
+
+        public const double gtol = 0.4;
 
 
@@ -2164,5 +7782,69 @@
             ap.assert(ap.len(x)>=n, "MinLBFGSCreate: Length(X)<N!");
             ap.assert(apserv.isfinitevector(x, n), "MinLBFGSCreate: X contains infinite or NaN values!");
-            minlbfgscreatex(n, m, x, 0, state);
+            minlbfgscreatex(n, m, x, 0, 0.0, state);
+        }
+
+
+        /*************************************************************************
+        The subroutine is finite difference variant of MinLBFGSCreate().  It  uses
+        finite differences in order to differentiate target function.
+
+        Description below contains information which is specific to  this function
+        only. We recommend to read comments on MinLBFGSCreate() in  order  to  get
+        more information about creation of LBFGS optimizer.
+
+        INPUT PARAMETERS:
+            N       -   problem dimension, N>0:
+                        * if given, only leading N elements of X are used
+                        * if not given, automatically determined from size of X
+            M       -   number of corrections in the BFGS scheme of Hessian
+                        approximation update. Recommended value:  3<=M<=7. The smaller
+                        value causes worse convergence, the bigger will  not  cause  a
+                        considerably better convergence, but will cause a fall in  the
+                        performance. M<=N.
+            X       -   starting point, array[0..N-1].
+            DiffStep-   differentiation step, >0
+
+        OUTPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+        NOTES:
+        1. algorithm uses 4-point central formula for differentiation.
+        2. differentiation step along I-th axis is equal to DiffStep*S[I] where
+           S[] is scaling vector which can be set by MinLBFGSSetScale() call.
+        3. we recommend you to use moderate values of  differentiation  step.  Too
+           large step will result in too large truncation  errors, while too small
+           step will result in too large numerical  errors.  1.0E-6  can  be  good
+           value to start with.
+        4. Numerical  differentiation  is   very   inefficient  -   one   gradient
+           calculation needs 4*N function evaluations. This function will work for
+           any N - either small (1...10), moderate (10...100) or  large  (100...).
+           However, performance penalty will be too severe for any N's except  for
+           small ones.
+           We should also say that code which relies on numerical  differentiation
+           is   less  robust  and  precise.  LBFGS  needs  exact  gradient values.
+           Imprecise gradient may slow  down  convergence,  especially  on  highly
+           nonlinear problems.
+           Thus  we  recommend to use this function for fast prototyping on small-
+           dimensional problems only, and to implement analytical gradient as soon
+           as possible.
+
+          -- ALGLIB --
+             Copyright 16.05.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlbfgscreatef(int n,
+            int m,
+            double[] x,
+            double diffstep,
+            minlbfgsstate state)
+        {
+            ap.assert(n>=1, "MinLBFGSCreateF: N too small!");
+            ap.assert(m>=1, "MinLBFGSCreateF: M<1");
+            ap.assert(m<=n, "MinLBFGSCreateF: M>N");
+            ap.assert(ap.len(x)>=n, "MinLBFGSCreateF: Length(X)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "MinLBFGSCreateF: X contains infinite or NaN values!");
+            ap.assert(math.isfinite(diffstep), "MinLBFGSCreateF: DiffStep is infinite or NaN!");
+            ap.assert((double)(diffstep)>(double)(0), "MinLBFGSCreateF: DiffStep is non-positive!");
+            minlbfgscreatex(n, m, x, 0, diffstep, state);
         }
 
@@ -2175,6 +7857,9 @@
             EpsG    -   >=0
                         The  subroutine  finishes  its  work   if   the  condition
-                        ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                        G - gradient.
+                        |v|<EpsG is satisfied, where:
+                        * |.| means Euclidian norm
+                        * v - scaled gradient vector, v[i]=g[i]*s[i]
+                        * g - gradient
+                        * s - scaling coefficients set by MinLBFGSSetScale()
             EpsF    -   >=0
                         The  subroutine  finishes  its work if on k+1-th iteration
@@ -2183,5 +7868,9 @@
             EpsX    -   >=0
                         The subroutine finishes its work if  on  k+1-th  iteration
-                        the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
+                        the condition |v|<=EpsX is fulfilled, where:
+                        * |.| means Euclidian norm
+                        * v - scaled step vector, v[i]=dx[i]/s[i]
+                        * dx - ste pvector, dx=X(k+1)-X(k)
+                        * s - scaling coefficients set by MinLBFGSSetScale()
             MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
                         iterations is unlimited.
@@ -2265,4 +7954,50 @@
 
         /*************************************************************************
+        This function sets scaling coefficients for LBFGS optimizer.
+
+        ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+        size and gradient are scaled before comparison with tolerances).  Scale of
+        the I-th variable is a translation invariant measure of:
+        a) "how large" the variable is
+        b) how large the step should be to make significant changes in the function
+
+        Scaling is also used by finite difference variant of the optimizer  - step
+        along I-th axis is equal to DiffStep*S[I].
+
+        In  most  optimizers  (and  in  the  LBFGS  too)  scaling is NOT a form of
+        preconditioning. It just  affects  stopping  conditions.  You  should  set
+        preconditioner  by  separate  call  to  one  of  the  MinLBFGSSetPrec...()
+        functions.
+
+        There  is  special  preconditioning  mode, however,  which  uses   scaling
+        coefficients to form diagonal preconditioning matrix. You  can  turn  this
+        mode on, if you want.   But  you should understand that scaling is not the
+        same thing as preconditioning - these are two different, although  related
+        forms of tuning solver.
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            S       -   array[N], non-zero scaling coefficients
+                        S[i] may be negative, sign doesn't matter.
+
+          -- ALGLIB --
+             Copyright 14.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlbfgssetscale(minlbfgsstate state,
+            double[] s)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(s)>=state.n, "MinLBFGSSetScale: Length(S)<N");
+            for(i=0; i<=state.n-1; i++)
+            {
+                ap.assert(math.isfinite(s[i]), "MinLBFGSSetScale: S contains infinite or NAN elements");
+                ap.assert((double)(s[i])!=(double)(0), "MinLBFGSSetScale: S contains zero elements");
+                state.s[i] = Math.Abs(s[i]);
+            }
+        }
+
+
+        /*************************************************************************
         Extended subroutine for internal use only.
 
@@ -2276,4 +8011,5 @@
                                     subsequent  calls   of   MinLBFGS   with   same
                                     MinLBFGSState structure can set Flags to 1.
+            DiffStep - numerical differentiation step
 
           -- ALGLIB --
@@ -2284,7 +8020,9 @@
             double[] x,
             int flags,
+            double diffstep,
             minlbfgsstate state)
         {
             bool allocatemem = new bool();
+            int i = 0;
 
             ap.assert(n>=1, "MinLBFGS: N too small!");
@@ -2295,19 +8033,20 @@
             // Initialize
             //
+            state.diffstep = diffstep;
             state.n = n;
             state.m = m;
-            state.flags = flags;
             allocatemem = flags%2==0;
             flags = flags/2;
             if( allocatemem )
             {
-                state.rho = new double[m-1+1];
-                state.theta = new double[m-1+1];
-                state.y = new double[m-1+1, n-1+1];
-                state.s = new double[m-1+1, n-1+1];
-                state.d = new double[n-1+1];
-                state.x = new double[n-1+1];
-                state.g = new double[n-1+1];
-                state.work = new double[n-1+1];
+                state.rho = new double[m];
+                state.theta = new double[m];
+                state.yk = new double[m, n];
+                state.sk = new double[m, n];
+                state.d = new double[n];
+                state.x = new double[n];
+                state.s = new double[n];
+                state.g = new double[n];
+                state.work = new double[n];
             }
             minlbfgssetcond(state, 0, 0, 0, 0);
@@ -2315,4 +8054,8 @@
             minlbfgssetstpmax(state, 0);
             minlbfgsrestartfrom(state, x);
+            for(i=0; i<=n-1; i++)
+            {
+                state.s[i] = 1.0;
+            }
             state.prectype = 0;
         }
@@ -2320,11 +8063,9 @@
 
         /*************************************************************************
-        Modification of the preconditioner:
-        default preconditioner (simple scaling) is used.
+        Modification  of  the  preconditioner:  default  preconditioner    (simple
+        scaling, same for all elements of X) is used.
 
         INPUT PARAMETERS:
             State   -   structure which stores algorithm state
-
-        After call to this function preconditioner is changed to the default one.
 
         NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
@@ -2334,5 +8075,5 @@
              Copyright 13.10.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void minlbfgssetdefaultpreconditioner(minlbfgsstate state)
+        public static void minlbfgssetprecdefault(minlbfgsstate state)
         {
             state.prectype = 0;
@@ -2341,6 +8082,6 @@
 
         /*************************************************************************
-        Modification of the preconditioner:
-        Cholesky factorization of approximate Hessian is used.
+        Modification of the preconditioner: Cholesky factorization of  approximate
+        Hessian is used.
 
         INPUT PARAMETERS:
@@ -2358,12 +8099,10 @@
         iterations.
 
-        NOTE 2:  P  should  be nonsingular. Exception will be thrown otherwise. It
-        also should be well conditioned, although only strict  non-singularity  is
-        tested.
+        NOTE 2:  P  should  be nonsingular. Exception will be thrown otherwise.
 
           -- ALGLIB --
              Copyright 13.10.2010 by Bochkanov Sergey
         *************************************************************************/
-        public static void minlbfgssetcholeskypreconditioner(minlbfgsstate state,
+        public static void minlbfgssetpreccholesky(minlbfgsstate state,
             double[,] p,
             bool isupper)
@@ -2372,5 +8111,5 @@
             double mx = 0;
 
-            ap.assert(apserv.isfinitertrmatrix(p, state.n, isupper), "MinLBFGSSetCholeskyPreconditioner: P contains infinite or NAN values!");
+            ap.assert(apserv.isfinitertrmatrix(p, state.n, isupper), "MinLBFGSSetPrecCholesky: P contains infinite or NAN values!");
             mx = 0;
             for(i=0; i<=state.n-1; i++)
@@ -2378,5 +8117,5 @@
                 mx = Math.Max(mx, Math.Abs(p[i,i]));
             }
-            ap.assert((double)(mx)>(double)(0), "MinLBFGSSetCholeskyPreconditioner: P is strictly singular!");
+            ap.assert((double)(mx)>(double)(0), "MinLBFGSSetPrecCholesky: P is strictly singular!");
             if( ap.rows(state.denseh)<state.n | ap.cols(state.denseh)<state.n )
             {
@@ -2396,4 +8135,102 @@
 
         /*************************************************************************
+        Modification  of  the  preconditioner:  diagonal of approximate Hessian is
+        used.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            D       -   diagonal of the approximate Hessian, array[0..N-1],
+                        (if larger, only leading N elements are used).
+
+        NOTE:  you  can  change  preconditioner  "on  the  fly",  during algorithm
+        iterations.
+
+        NOTE 2: D[i] should be positive. Exception will be thrown otherwise.
+
+        NOTE 3: you should pass diagonal of approximate Hessian - NOT ITS INVERSE.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlbfgssetprecdiag(minlbfgsstate state,
+            double[] d)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(d)>=state.n, "MinLBFGSSetPrecDiag: D is too short");
+            for(i=0; i<=state.n-1; i++)
+            {
+                ap.assert(math.isfinite(d[i]), "MinLBFGSSetPrecDiag: D contains infinite or NAN elements");
+                ap.assert((double)(d[i])>(double)(0), "MinLBFGSSetPrecDiag: D contains non-positive elements");
+            }
+            apserv.rvectorsetlengthatleast(ref state.diagh, state.n);
+            state.prectype = 2;
+            for(i=0; i<=state.n-1; i++)
+            {
+                state.diagh[i] = d[i];
+            }
+        }
+
+
+        /*************************************************************************
+        Modification of the preconditioner: scale-based diagonal preconditioning.
+
+        This preconditioning mode can be useful when you  don't  have  approximate
+        diagonal of Hessian, but you know that your  variables  are  badly  scaled
+        (for  example,  one  variable is in [1,10], and another in [1000,100000]),
+        and most part of the ill-conditioning comes from different scales of vars.
+
+        In this case simple  scale-based  preconditioner,  with H[i] = 1/(s[i]^2),
+        can greatly improve convergence.
+
+        IMPRTANT: you should set scale of your variables  with  MinLBFGSSetScale()
+        call  (before  or after MinLBFGSSetPrecScale() call). Without knowledge of
+        the scale of your variables scale-based preconditioner will be  just  unit
+        matrix.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlbfgssetprecscale(minlbfgsstate state)
+        {
+            state.prectype = 3;
+        }
+
+
+        /*************************************************************************
+        NOTES:
+
+        1. This function has two different implementations: one which  uses  exact
+           (analytical) user-supplied gradient,  and one which uses function value
+           only  and  numerically  differentiates  function  in  order  to  obtain
+           gradient.
+
+           Depending  on  the  specific  function  used to create optimizer object
+           (either MinLBFGSCreate() for analytical gradient  or  MinLBFGSCreateF()
+           for numerical differentiation) you should choose appropriate variant of
+           MinLBFGSOptimize() - one  which  accepts  function  AND gradient or one
+           which accepts function ONLY.
+
+           Be careful to choose variant of MinLBFGSOptimize() which corresponds to
+           your optimization scheme! Table below lists different  combinations  of
+           callback (function/gradient) passed to MinLBFGSOptimize()  and specific
+           function used to create optimizer.
+
+
+                             |         USER PASSED TO MinLBFGSOptimize()
+           CREATED WITH      |  function only   |  function and gradient
+           ------------------------------------------------------------
+           MinLBFGSCreateF() |     work                FAIL
+           MinLBFGSCreate()  |     FAIL                work
+
+           Here "FAIL" denotes inappropriate combinations  of  optimizer  creation
+           function  and  MinLBFGSOptimize()  version.   Attemps   to   use   such
+           combination (for example, to create optimizer with MinLBFGSCreateF() and
+           to pass gradient information to MinCGOptimize()) will lead to exception
+           being thrown. Either  you  did  not pass gradient when it WAS needed or
+           you passed gradient when it was NOT needed.
 
           -- ALGLIB --
@@ -2405,8 +8242,4 @@
             int n = 0;
             int m = 0;
-            int maxits = 0;
-            double epsf = 0;
-            double epsg = 0;
-            double epsx = 0;
             int i = 0;
             int j = 0;
@@ -2432,14 +8265,10 @@
                 n = state.rstate.ia[0];
                 m = state.rstate.ia[1];
-                maxits = state.rstate.ia[2];
-                i = state.rstate.ia[3];
-                j = state.rstate.ia[4];
-                ic = state.rstate.ia[5];
-                mcinfo = state.rstate.ia[6];
-                epsf = state.rstate.ra[0];
-                epsg = state.rstate.ra[1];
-                epsx = state.rstate.ra[2];
-                v = state.rstate.ra[3];
-                vv = state.rstate.ra[4];
+                i = state.rstate.ia[2];
+                j = state.rstate.ia[3];
+                ic = state.rstate.ia[4];
+                mcinfo = state.rstate.ia[5];
+                v = state.rstate.ra[0];
+                vv = state.rstate.ra[1];
             }
             else
@@ -2447,14 +8276,10 @@
                 n = -983;
                 m = -989;
-                maxits = -834;
-                i = 900;
-                j = -287;
-                ic = 364;
-                mcinfo = 214;
-                epsf = -338;
-                epsg = -686;
-                epsx = 912;
-                v = 585;
-                vv = 497;
+                i = -834;
+                j = 900;
+                ic = -287;
+                mcinfo = 364;
+                v = 214;
+                vv = -338;
             }
             if( state.rstate.stage==0 )
@@ -2473,4 +8298,44 @@
             {
                 goto lbl_3;
+            }
+            if( state.rstate.stage==4 )
+            {
+                goto lbl_4;
+            }
+            if( state.rstate.stage==5 )
+            {
+                goto lbl_5;
+            }
+            if( state.rstate.stage==6 )
+            {
+                goto lbl_6;
+            }
+            if( state.rstate.stage==7 )
+            {
+                goto lbl_7;
+            }
+            if( state.rstate.stage==8 )
+            {
+                goto lbl_8;
+            }
+            if( state.rstate.stage==9 )
+            {
+                goto lbl_9;
+            }
+            if( state.rstate.stage==10 )
+            {
+                goto lbl_10;
+            }
+            if( state.rstate.stage==11 )
+            {
+                goto lbl_11;
+            }
+            if( state.rstate.stage==12 )
+            {
+                goto lbl_12;
+            }
+            if( state.rstate.stage==13 )
+            {
+                goto lbl_13;
             }
             
@@ -2485,8 +8350,4 @@
             n = state.n;
             m = state.m;
-            epsg = state.epsg;
-            epsf = state.epsf;
-            epsx = state.epsx;
-            maxits = state.maxits;
             state.repterminationtype = 0;
             state.repiterationscount = 0;
@@ -2497,4 +8358,8 @@
             //
             clearrequestfields(state);
+            if( (double)(state.diffstep)!=(double)(0) )
+            {
+                goto lbl_14;
+            }
             state.needfg = true;
             state.rstate.stage = 0;
@@ -2502,24 +8367,66 @@
         lbl_0:
             state.needfg = false;
-            if( !state.xrep )
-            {
-                goto lbl_4;
-            }
-            clearrequestfields(state);
-            state.xupdated = true;
+            goto lbl_15;
+        lbl_14:
+            state.needf = true;
             state.rstate.stage = 1;
             goto lbl_rcomm;
         lbl_1:
+            state.fbase = state.f;
+            i = 0;
+        lbl_16:
+            if( i>n-1 )
+            {
+                goto lbl_18;
+            }
+            v = state.x[i];
+            state.x[i] = v-state.diffstep*state.s[i];
+            state.rstate.stage = 2;
+            goto lbl_rcomm;
+        lbl_2:
+            state.fm2 = state.f;
+            state.x[i] = v-0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 3;
+            goto lbl_rcomm;
+        lbl_3:
+            state.fm1 = state.f;
+            state.x[i] = v+0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 4;
+            goto lbl_rcomm;
+        lbl_4:
+            state.fp1 = state.f;
+            state.x[i] = v+state.diffstep*state.s[i];
+            state.rstate.stage = 5;
+            goto lbl_rcomm;
+        lbl_5:
+            state.fp2 = state.f;
+            state.x[i] = v;
+            state.g[i] = (8*(state.fp1-state.fm1)-(state.fp2-state.fm2))/(6*state.diffstep*state.s[i]);
+            i = i+1;
+            goto lbl_16;
+        lbl_18:
+            state.f = state.fbase;
+            state.needf = false;
+        lbl_15:
+            optserv.trimprepare(state.f, ref state.trimthreshold);
+            if( !state.xrep )
+            {
+                goto lbl_19;
+            }
+            clearrequestfields(state);
+            state.xupdated = true;
+            state.rstate.stage = 6;
+            goto lbl_rcomm;
+        lbl_6:
             state.xupdated = false;
-        lbl_4:
+        lbl_19:
             state.repnfev = 1;
             state.fold = state.f;
-            v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.g[i_]*state.g[i_];
-            }
-            v = Math.Sqrt(v);
-            if( (double)(v)<=(double)(epsg) )
+            v = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                v = v+math.sqr(state.g[i]*state.s[i]);
+            }
+            if( (double)(Math.Sqrt(v))<=(double)(state.epsg) )
             {
                 state.repterminationtype = 4;
@@ -2532,15 +8439,28 @@
             // Apply preconditioner, if we have something other than default.
             //
-            if( (double)(state.stpmax)==(double)(0) )
-            {
-                state.stp = Math.Min(1.0/v, 1);
-            }
-            else
-            {
-                state.stp = Math.Min(1.0/v, state.stpmax);
-            }
             for(i_=0; i_<=n-1;i_++)
             {
                 state.d[i_] = -state.g[i_];
+            }
+            if( state.prectype==0 )
+            {
+                
+                //
+                // Default preconditioner is used, but we can't use it before iterations will start
+                //
+                v = 0.0;
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    v += state.g[i_]*state.g[i_];
+                }
+                v = Math.Sqrt(v);
+                if( (double)(state.stpmax)==(double)(0) )
+                {
+                    state.stp = Math.Min(1.0/v, 1);
+                }
+                else
+                {
+                    state.stp = Math.Min(1.0/v, state.stpmax);
+                }
             }
             if( state.prectype==1 )
@@ -2551,4 +8471,29 @@
                 //
                 fbls.fblscholeskysolve(state.denseh, 1.0, n, true, ref state.d, ref state.autobuf);
+                state.stp = 1;
+            }
+            if( state.prectype==2 )
+            {
+                
+                //
+                // diagonal approximation is used
+                //
+                for(i=0; i<=n-1; i++)
+                {
+                    state.d[i] = state.d[i]/state.diagh[i];
+                }
+                state.stp = 1;
+            }
+            if( state.prectype==3 )
+            {
+                
+                //
+                // scale-based preconditioner is used
+                //
+                for(i=0; i<=n-1; i++)
+                {
+                    state.d[i] = state.d[i]*state.s[i]*state.s[i];
+                }
+                state.stp = 1;
             }
             
@@ -2557,8 +8502,8 @@
             //
             state.k = 0;
-        lbl_6:
+        lbl_21:
             if( false )
             {
-                goto lbl_7;
+                goto lbl_22;
             }
             
@@ -2574,9 +8519,9 @@
             for(i_=0; i_<=n-1;i_++)
             {
-                state.s[state.p,i_] = -state.x[i_];
+                state.sk[state.p,i_] = -state.x[i_];
             }
             for(i_=0; i_<=n-1;i_++)
             {
-                state.y[state.p,i_] = -state.g[i_];
+                state.yk[state.p,i_] = -state.g[i_];
             }
             
@@ -2591,22 +8536,69 @@
             }
             linmin.linminnormalized(ref state.d, ref state.stp, n);
-            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.stpmax, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
+            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.stpmax, gtol, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
+        lbl_23:
+            if( state.mcstage==0 )
+            {
+                goto lbl_24;
+            }
+            clearrequestfields(state);
+            if( (double)(state.diffstep)!=(double)(0) )
+            {
+                goto lbl_25;
+            }
+            state.needfg = true;
+            state.rstate.stage = 7;
+            goto lbl_rcomm;
+        lbl_7:
+            state.needfg = false;
+            goto lbl_26;
+        lbl_25:
+            state.needf = true;
+            state.rstate.stage = 8;
+            goto lbl_rcomm;
         lbl_8:
-            if( state.mcstage==0 )
-            {
-                goto lbl_9;
-            }
-            clearrequestfields(state);
-            state.needfg = true;
-            state.rstate.stage = 2;
+            state.fbase = state.f;
+            i = 0;
+        lbl_27:
+            if( i>n-1 )
+            {
+                goto lbl_29;
+            }
+            v = state.x[i];
+            state.x[i] = v-state.diffstep*state.s[i];
+            state.rstate.stage = 9;
             goto lbl_rcomm;
-        lbl_2:
-            state.needfg = false;
-            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.stpmax, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
-            goto lbl_8;
         lbl_9:
+            state.fm2 = state.f;
+            state.x[i] = v-0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 10;
+            goto lbl_rcomm;
+        lbl_10:
+            state.fm1 = state.f;
+            state.x[i] = v+0.5*state.diffstep*state.s[i];
+            state.rstate.stage = 11;
+            goto lbl_rcomm;
+        lbl_11:
+            state.fp1 = state.f;
+            state.x[i] = v+state.diffstep*state.s[i];
+            state.rstate.stage = 12;
+            goto lbl_rcomm;
+        lbl_12:
+            state.fp2 = state.f;
+            state.x[i] = v;
+            state.g[i] = (8*(state.fp1-state.fm1)-(state.fp2-state.fm2))/(6*state.diffstep*state.s[i]);
+            i = i+1;
+            goto lbl_27;
+        lbl_29:
+            state.f = state.fbase;
+            state.needf = false;
+        lbl_26:
+            optserv.trimfunction(ref state.f, ref state.g, n, state.trimthreshold);
+            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.stpmax, gtol, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
+            goto lbl_23;
+        lbl_24:
             if( !state.xrep )
             {
-                goto lbl_10;
+                goto lbl_30;
             }
             
@@ -2616,18 +8608,18 @@
             clearrequestfields(state);
             state.xupdated = true;
-            state.rstate.stage = 3;
+            state.rstate.stage = 13;
             goto lbl_rcomm;
-        lbl_3:
+        lbl_13:
             state.xupdated = false;
-        lbl_10:
+        lbl_30:
             state.repnfev = state.repnfev+state.nfev;
             state.repiterationscount = state.repiterationscount+1;
             for(i_=0; i_<=n-1;i_++)
             {
-                state.s[state.p,i_] = state.s[state.p,i_] + state.x[i_];
+                state.sk[state.p,i_] = state.sk[state.p,i_] + state.x[i_];
             }
             for(i_=0; i_<=n-1;i_++)
             {
-                state.y[state.p,i_] = state.y[state.p,i_] + state.g[i_];
+                state.yk[state.p,i_] = state.yk[state.p,i_] + state.g[i_];
             }
             
@@ -2635,5 +8627,5 @@
             // Stopping conditions
             //
-            if( state.repiterationscount>=maxits & maxits>0 )
+            if( state.repiterationscount>=state.maxits & state.maxits>0 )
             {
                 
@@ -2645,10 +8637,10 @@
                 return result;
             }
-            v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.g[i_]*state.g[i_];
-            }
-            if( (double)(Math.Sqrt(v))<=(double)(epsg) )
+            v = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                v = v+math.sqr(state.g[i]*state.s[i]);
+            }
+            if( (double)(Math.Sqrt(v))<=(double)(state.epsg) )
             {
                 
@@ -2660,5 +8652,5 @@
                 return result;
             }
-            if( (double)(state.fold-state.f)<=(double)(epsf*Math.Max(Math.Abs(state.fold), Math.Max(Math.Abs(state.f), 1.0))) )
+            if( (double)(state.fold-state.f)<=(double)(state.epsf*Math.Max(Math.Abs(state.fold), Math.Max(Math.Abs(state.f), 1.0))) )
             {
                 
@@ -2670,10 +8662,10 @@
                 return result;
             }
-            v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.s[state.p,i_]*state.s[state.p,i_];
-            }
-            if( (double)(Math.Sqrt(v))<=(double)(epsx) )
+            v = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                v = v+math.sqr(state.sk[state.p,i]/state.s[i]);
+            }
+            if( (double)(Math.Sqrt(v))<=(double)(state.epsx) )
             {
                 
@@ -2718,10 +8710,10 @@
                 for(i_=0; i_<=n-1;i_++)
                 {
-                    v += state.y[state.p,i_]*state.s[state.p,i_];
+                    v += state.yk[state.p,i_]*state.sk[state.p,i_];
                 }
                 vv = 0.0;
                 for(i_=0; i_<=n-1;i_++)
                 {
-                    vv += state.y[state.p,i_]*state.y[state.p,i_];
+                    vv += state.yk[state.p,i_]*state.yk[state.p,i_];
                 }
                 if( (double)(v)==(double)(0) | (double)(vv)==(double)(0) )
@@ -2762,5 +8754,5 @@
                     for(i_=0; i_<=n-1;i_++)
                     {
-                        v += state.s[ic,i_]*state.work[i_];
+                        v += state.sk[ic,i_]*state.work[i_];
                     }
                     state.theta[ic] = v;
@@ -2768,5 +8760,5 @@
                     for(i_=0; i_<=n-1;i_++)
                     {
-                        state.work[i_] = state.work[i_] - vv*state.y[ic,i_];
+                        state.work[i_] = state.work[i_] - vv*state.yk[ic,i_];
                     }
                 }
@@ -2791,4 +8783,26 @@
                     fbls.fblscholeskysolve(state.denseh, 1, n, true, ref state.work, ref state.autobuf);
                 }
+                if( state.prectype==2 )
+                {
+                    
+                    //
+                    // diagonal approximation is used
+                    //
+                    for(i=0; i<=n-1; i++)
+                    {
+                        state.work[i] = state.work[i]/state.diagh[i];
+                    }
+                }
+                if( state.prectype==3 )
+                {
+                    
+                    //
+                    // scale-based preconditioner is used
+                    //
+                    for(i=0; i<=n-1; i++)
+                    {
+                        state.work[i] = state.work[i]*state.s[i]*state.s[i];
+                    }
+                }
                 for(i=state.k-state.q; i<=state.k; i++)
                 {
@@ -2797,10 +8811,10 @@
                     for(i_=0; i_<=n-1;i_++)
                     {
-                        v += state.y[ic,i_]*state.work[i_];
+                        v += state.yk[ic,i_]*state.work[i_];
                     }
                     vv = state.rho[ic]*(-v+state.theta[ic]);
                     for(i_=0; i_<=n-1;i_++)
                     {
-                        state.work[i_] = state.work[i_] + vv*state.s[ic,i_];
+                        state.work[i_] = state.work[i_] + vv*state.sk[ic,i_];
                     }
                 }
@@ -2816,6 +8830,6 @@
                 state.k = state.k+1;
             }
-            goto lbl_6;
-        lbl_7:
+            goto lbl_21;
+        lbl_22:
             result = false;
             return result;
@@ -2828,14 +8842,10 @@
             state.rstate.ia[0] = n;
             state.rstate.ia[1] = m;
-            state.rstate.ia[2] = maxits;
-            state.rstate.ia[3] = i;
-            state.rstate.ia[4] = j;
-            state.rstate.ia[5] = ic;
-            state.rstate.ia[6] = mcinfo;
-            state.rstate.ra[0] = epsf;
-            state.rstate.ra[1] = epsg;
-            state.rstate.ra[2] = epsx;
-            state.rstate.ra[3] = v;
-            state.rstate.ra[4] = vv;
+            state.rstate.ia[2] = i;
+            state.rstate.ia[3] = j;
+            state.rstate.ia[4] = ic;
+            state.rstate.ia[5] = mcinfo;
+            state.rstate.ra[0] = v;
+            state.rstate.ra[1] = vv;
             return result;
         }
@@ -2934,6 +8944,6 @@
                 state.x[i_] = x[i_];
             }
-            state.rstate.ia = new int[6+1];
-            state.rstate.ra = new double[4+1];
+            state.rstate.ia = new int[5+1];
+            state.rstate.ra = new double[1+1];
             state.rstate.stage = -1;
             clearrequestfields(state);
@@ -2946,6 +8956,1201 @@
         private static void clearrequestfields(minlbfgsstate state)
         {
+            state.needf = false;
             state.needfg = false;
             state.xupdated = false;
+        }
+
+
+    }
+    public class minqp
+    {
+        /*************************************************************************
+        This object stores nonlinear optimizer state.
+        You should use functions provided by MinQP subpackage to work with this
+        object
+        *************************************************************************/
+        public class minqpstate
+        {
+            public int n;
+            public int algokind;
+            public int akind;
+            public double[,] densea;
+            public double[] diaga;
+            public double[] b;
+            public double[] bndl;
+            public double[] bndu;
+            public bool[] havebndl;
+            public bool[] havebndu;
+            public double[] xorigin;
+            public double[] startx;
+            public bool havex;
+            public double[] xc;
+            public double[] gc;
+            public int[] activeconstraints;
+            public int[] prevactiveconstraints;
+            public double constterm;
+            public double[] workbndl;
+            public double[] workbndu;
+            public int repinneriterationscount;
+            public int repouteriterationscount;
+            public int repncholesky;
+            public int repnmv;
+            public int repterminationtype;
+            public double[] tmp0;
+            public double[] tmp1;
+            public int[] itmp0;
+            public int[] p2;
+            public double[,] bufa;
+            public double[] bufb;
+            public double[] bufx;
+            public apserv.apbuffers buf;
+            public minqpstate()
+            {
+                densea = new double[0,0];
+                diaga = new double[0];
+                b = new double[0];
+                bndl = new double[0];
+                bndu = new double[0];
+                havebndl = new bool[0];
+                havebndu = new bool[0];
+                xorigin = new double[0];
+                startx = new double[0];
+                xc = new double[0];
+                gc = new double[0];
+                activeconstraints = new int[0];
+                prevactiveconstraints = new int[0];
+                workbndl = new double[0];
+                workbndu = new double[0];
+                tmp0 = new double[0];
+                tmp1 = new double[0];
+                itmp0 = new int[0];
+                p2 = new int[0];
+                bufa = new double[0,0];
+                bufb = new double[0];
+                bufx = new double[0];
+                buf = new apserv.apbuffers();
+            }
+        };
+
+
+        /*************************************************************************
+        This structure stores optimization report:
+        * InnerIterationsCount      number of inner iterations
+        * OuterIterationsCount      number of outer iterations
+        * NCholesky                 number of Cholesky decomposition
+        * NMV                       number of matrix-vector products
+                                    (only products calculated as part of iterative
+                                    process are counted)
+        * TerminationType           completion code (see below)
+
+        Completion codes:
+        * -5    inappropriate solver was used:
+                * Cholesky solver for semidefinite or indefinite problems
+                * Cholesky solver for problems with non-boundary constraints
+        * -3    inconsistent constraints (or, maybe, feasible point is
+                too hard to find). If you are sure that constraints are feasible,
+                try to restart optimizer with better initial approximation.
+        *  4    successful completion
+        *  5    MaxIts steps was taken
+        *  7    stopping conditions are too stringent,
+                further improvement is impossible,
+                X contains best point found so far.
+        *************************************************************************/
+        public class minqpreport
+        {
+            public int inneriterationscount;
+            public int outeriterationscount;
+            public int nmv;
+            public int ncholesky;
+            public int terminationtype;
+        };
+
+
+
+
+        /*************************************************************************
+                            CONSTRAINED QUADRATIC PROGRAMMING
+
+        The subroutine creates QP optimizer. After initial creation,  it  contains
+        default optimization problem with zero quadratic and linear terms  and  no
+        constraints. You should set quadratic/linear terms with calls to functions
+        provided by MinQP subpackage.
+
+        INPUT PARAMETERS:
+            N       -   problem size
+            
+        OUTPUT PARAMETERS:
+            State   -   optimizer with zero quadratic/linear terms
+                        and no constraints
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpcreate(int n,
+            minqpstate state)
+        {
+            int i = 0;
+
+            ap.assert(n>=1, "MinQPCreate: N<1");
+            
+            //
+            // initialize QP solver
+            //
+            state.n = n;
+            state.akind = -1;
+            state.repterminationtype = 0;
+            state.b = new double[n];
+            state.bndl = new double[n];
+            state.bndu = new double[n];
+            state.workbndl = new double[n];
+            state.workbndu = new double[n];
+            state.havebndl = new bool[n];
+            state.havebndu = new bool[n];
+            state.startx = new double[n];
+            state.xorigin = new double[n];
+            state.xc = new double[n];
+            state.gc = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                state.b[i] = 0.0;
+                state.workbndl[i] = Double.NegativeInfinity;
+                state.workbndu[i] = Double.PositiveInfinity;
+                state.havebndl[i] = false;
+                state.havebndu[i] = false;
+                state.startx[i] = 0.0;
+                state.xorigin[i] = 0.0;
+            }
+            state.havex = false;
+            minqpsetalgocholesky(state);
+        }
+
+
+        /*************************************************************************
+        This function sets linear term for QP solver.
+
+        By default, linear term is zero.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            B       -   linear term, array[N].
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetlinearterm(minqpstate state,
+            double[] b)
+        {
+            int n = 0;
+
+            n = state.n;
+            ap.assert(ap.len(b)>=n, "MinQPSetLinearTerm: Length(B)<N");
+            ap.assert(apserv.isfinitevector(b, n), "MinQPSetLinearTerm: B contains infinite or NaN elements");
+            minqpsetlineartermfast(state, b);
+        }
+
+
+        /*************************************************************************
+        This function sets quadratic term for QP solver.
+
+        By default quadratic term is zero.
+
+        IMPORTANT: this solver minimizes following  function:
+            f(x) = 0.5*x'*A*x + b'*x.
+        Note that quadratic term has 0.5 before it. So if  you  want  to  minimize
+            f(x) = x^2 + x
+        you should rewrite your problem as follows:
+            f(x) = 0.5*(2*x^2) + x
+        and your matrix A will be equal to [[2.0]], not to [[1.0]]
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            A       -   matrix, array[N,N]
+            IsUpper -   (optional) storage type:
+                        * if True, symmetric matrix  A  is  given  by  its  upper
+                          triangle, and the lower triangle isnt used
+                        * if False, symmetric matrix  A  is  given  by  its lower
+                          triangle, and the upper triangle isnt used
+                        * if not given, both lower and upper  triangles  must  be
+                          filled.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetquadraticterm(minqpstate state,
+            double[,] a,
+            bool isupper)
+        {
+            int n = 0;
+
+            n = state.n;
+            ap.assert(ap.rows(a)>=n, "MinQPSetQuadraticTerm: Rows(A)<N");
+            ap.assert(ap.cols(a)>=n, "MinQPSetQuadraticTerm: Cols(A)<N");
+            ap.assert(apserv.isfinitertrmatrix(a, n, isupper), "MinQPSetQuadraticTerm: A contains infinite or NaN elements");
+            minqpsetquadratictermfast(state, a, isupper, 0.0);
+        }
+
+
+        /*************************************************************************
+        This function sets starting point for QP solver. It is useful to have
+        good initial approximation to the solution, because it will increase
+        speed of convergence and identification of active constraints.
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            X       -   starting point, array[N].
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetstartingpoint(minqpstate state,
+            double[] x)
+        {
+            int n = 0;
+
+            n = state.n;
+            ap.assert(ap.len(x)>=n, "MinQPSetStartingPoint: Length(B)<N");
+            ap.assert(apserv.isfinitevector(x, n), "MinQPSetStartingPoint: X contains infinite or NaN elements");
+            minqpsetstartingpointfast(state, x);
+        }
+
+
+        /*************************************************************************
+        This  function sets origin for QP solver. By default, following QP program
+        is solved:
+
+            min(0.5*x'*A*x+b'*x)
+            
+        This function allows to solve different problem:
+
+            min(0.5*(x-x_origin)'*A*(x-x_origin)+b'*(x-x_origin))
+            
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+            XOrigin -   origin, array[N].
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetorigin(minqpstate state,
+            double[] xorigin)
+        {
+            int n = 0;
+
+            n = state.n;
+            ap.assert(ap.len(xorigin)>=n, "MinQPSetOrigin: Length(B)<N");
+            ap.assert(apserv.isfinitevector(xorigin, n), "MinQPSetOrigin: B contains infinite or NaN elements");
+            minqpsetoriginfast(state, xorigin);
+        }
+
+
+        /*************************************************************************
+        This function tells solver to use Cholesky-based algorithm.
+
+        Cholesky-based algorithm can be used when:
+        * problem is convex
+        * there is no constraints or only boundary constraints are present
+
+        This algorithm has O(N^3) complexity for unconstrained problem and  is  up
+        to several times slower on bound constrained  problems  (these  additional
+        iterations are needed to identify active constraints).
+
+        INPUT PARAMETERS:
+            State   -   structure which stores algorithm state
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetalgocholesky(minqpstate state)
+        {
+            state.algokind = 1;
+        }
+
+
+        /*************************************************************************
+        This function sets boundary constraints for QP solver
+
+        Boundary constraints are inactive by default (after initial creation).
+        After  being  set,  they  are  preserved  until explicitly turned off with
+        another SetBC() call.
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            BndL    -   lower bounds, array[N].
+                        If some (all) variables are unbounded, you may specify
+                        very small number or -INF (latter is recommended because
+                        it will allow solver to use better algorithm).
+            BndU    -   upper bounds, array[N].
+                        If some (all) variables are unbounded, you may specify
+                        very large number or +INF (latter is recommended because
+                        it will allow solver to use better algorithm).
+                        
+        NOTE: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+        variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetbc(minqpstate state,
+            double[] bndl,
+            double[] bndu)
+        {
+            int i = 0;
+            int n = 0;
+
+            n = state.n;
+            ap.assert(ap.len(bndl)>=n, "MinQPSetBC: Length(BndL)<N");
+            ap.assert(ap.len(bndu)>=n, "MinQPSetBC: Length(BndU)<N");
+            for(i=0; i<=n-1; i++)
+            {
+                ap.assert(math.isfinite(bndl[i]) | Double.IsNegativeInfinity(bndl[i]), "MinQPSetBC: BndL contains NAN or +INF");
+                ap.assert(math.isfinite(bndu[i]) | Double.IsPositiveInfinity(bndu[i]), "MinQPSetBC: BndU contains NAN or -INF");
+                state.bndl[i] = bndl[i];
+                state.havebndl[i] = math.isfinite(bndl[i]);
+                state.bndu[i] = bndu[i];
+                state.havebndu[i] = math.isfinite(bndu[i]);
+            }
+        }
+
+
+        /*************************************************************************
+        This function solves quadratic programming problem.
+        You should call it after setting solver options with MinQPSet...() calls.
+
+        INPUT PARAMETERS:
+            State   -   algorithm state
+
+        You should use MinQPResults() function to access results after calls
+        to this function.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpoptimize(minqpstate state)
+        {
+            int n = 0;
+            int i = 0;
+            int j = 0;
+            int k = 0;
+            int nbc = 0;
+            int nlc = 0;
+            int nactive = 0;
+            int nfree = 0;
+            double f = 0;
+            double fprev = 0;
+            double v = 0;
+            bool b = new bool();
+            int i_ = 0;
+
+            n = state.n;
+            state.repterminationtype = -5;
+            state.repinneriterationscount = 0;
+            state.repouteriterationscount = 0;
+            state.repncholesky = 0;
+            state.repnmv = 0;
+            
+            //
+            // check correctness of constraints
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                if( state.havebndl[i] & state.havebndu[i] )
+                {
+                    if( (double)(state.bndl[i])>(double)(state.bndu[i]) )
+                    {
+                        state.repterminationtype = -3;
+                        return;
+                    }
+                }
+            }
+            
+            //
+            // count number of bound and linear constraints
+            //
+            nbc = 0;
+            nlc = 0;
+            for(i=0; i<=n-1; i++)
+            {
+                if( state.havebndl[i] )
+                {
+                    nbc = nbc+1;
+                }
+                if( state.havebndu[i] )
+                {
+                    nbc = nbc+1;
+                }
+            }
+            
+            //
+            // Our formulation of quadratic problem includes origin point,
+            // i.e. we have F(x-x_origin) which is minimized subject to
+            // constraints on x, instead of having simply F(x).
+            //
+            // Here we make transition from non-zero origin to zero one.
+            // In order to make such transition we have to:
+            // 1. subtract x_origin from x_start
+            // 2. modify constraints
+            // 3. solve problem
+            // 4. add x_origin to solution
+            //
+            // There is alternate solution - to modify quadratic function
+            // by expansion of multipliers containing (x-x_origin), but
+            // we prefer to modify constraints, because it is a) more precise
+            // and b) easier to to.
+            //
+            // Parts (1)-(2) are done here. After this block is over,
+            // we have:
+            // * XC, which stores shifted XStart (if we don't have XStart,
+            //   value of XC will be ignored later)
+            // * WorkBndL, WorkBndU, which store modified boundary constraints.
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                state.xc[i] = state.startx[i]-state.xorigin[i];
+                if( state.havebndl[i] )
+                {
+                    state.workbndl[i] = state.bndl[i]-state.xorigin[i];
+                }
+                if( state.havebndu[i] )
+                {
+                    state.workbndu[i] = state.bndu[i]-state.xorigin[i];
+                }
+            }
+            
+            //
+            // modify starting point XC according to boundary constraints
+            //
+            if( state.havex )
+            {
+                
+                //
+                // We have starting point in XC, so we just have to bound it
+                //
+                for(i=0; i<=n-1; i++)
+                {
+                    if( state.havebndl[i] )
+                    {
+                        if( (double)(state.xc[i])<(double)(state.workbndl[i]) )
+                        {
+                            state.xc[i] = state.workbndl[i];
+                        }
+                    }
+                    if( state.havebndu[i] )
+                    {
+                        if( (double)(state.xc[i])>(double)(state.workbndu[i]) )
+                        {
+                            state.xc[i] = state.workbndu[i];
+                        }
+                    }
+                }
+            }
+            else
+            {
+                
+                //
+                // We don't have starting point, so we deduce it from
+                // constraints (if they are present).
+                //
+                // NOTE: XC contains some meaningless values from previous block
+                // which are ignored by code below.
+                //
+                for(i=0; i<=n-1; i++)
+                {
+                    if( state.havebndl[i] & state.havebndu[i] )
+                    {
+                        state.xc[i] = 0.5*(state.workbndl[i]+state.workbndu[i]);
+                        if( (double)(state.xc[i])<(double)(state.workbndl[i]) )
+                        {
+                            state.xc[i] = state.workbndl[i];
+                        }
+                        if( (double)(state.xc[i])>(double)(state.workbndu[i]) )
+                        {
+                            state.xc[i] = state.workbndu[i];
+                        }
+                        continue;
+                    }
+                    if( state.havebndl[i] )
+                    {
+                        state.xc[i] = state.workbndl[i];
+                        continue;
+                    }
+                    if( state.havebndu[i] )
+                    {
+                        state.xc[i] = state.workbndu[i];
+                        continue;
+                    }
+                    state.xc[i] = 0;
+                }
+            }
+            
+            //
+            // Select algo
+            //
+            if( state.algokind==1 & state.akind==0 )
+            {
+                
+                //
+                // Cholesky-based algorithm for dense bound constrained problems.
+                //
+                // This algorithm exists in two variants:
+                // * unconstrained one, which can solve problem using only one NxN
+                //   double matrix
+                // * bound constrained one, which needs two NxN matrices
+                //
+                // We will try to solve problem using unconstrained algorithm,
+                // and will use bound constrained version only when constraints
+                // are actually present
+                //
+                if( nbc==0 & nlc==0 )
+                {
+                    
+                    //
+                    // "Simple" unconstrained version
+                    //
+                    apserv.rvectorsetlengthatleast(ref state.tmp0, n);
+                    apserv.rvectorsetlengthatleast(ref state.bufb, n);
+                    state.densea[0,0] = state.diaga[0];
+                    for(k=1; k<=n-1; k++)
+                    {
+                        for(i_=0; i_<=k-1;i_++)
+                        {
+                            state.densea[i_,k] = state.densea[k,i_];
+                        }
+                        state.densea[k,k] = state.diaga[k];
+                    }
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.bufb[i_] = state.b[i_];
+                    }
+                    state.repncholesky = 1;
+                    if( !trfac.spdmatrixcholeskyrec(ref state.densea, 0, n, true, ref state.tmp0) )
+                    {
+                        state.repterminationtype = -5;
+                        return;
+                    }
+                    fbls.fblscholeskysolve(state.densea, 1.0, n, true, ref state.bufb, ref state.tmp0);
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.xc[i_] = -state.bufb[i_];
+                    }
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.xc[i_] = state.xc[i_] + state.xorigin[i_];
+                    }
+                    state.repouteriterationscount = 1;
+                    state.repterminationtype = 4;
+                    return;
+                }
+                
+                //
+                // General bound constrained algo
+                //
+                apserv.rmatrixsetlengthatleast(ref state.bufa, n, n);
+                apserv.rvectorsetlengthatleast(ref state.bufb, n);
+                apserv.rvectorsetlengthatleast(ref state.bufx, n);
+                apserv.ivectorsetlengthatleast(ref state.activeconstraints, n);
+                apserv.ivectorsetlengthatleast(ref state.prevactiveconstraints, n);
+                apserv.rvectorsetlengthatleast(ref state.tmp0, n);
+                
+                //
+                // Prepare constraints vectors:
+                // * ActiveConstraints - constraints active at current step
+                // * PrevActiveConstraints - constraints which were active at previous step
+                //
+                // Elements of constraints vectors can be:
+                // *  0 - inactive
+                // *  1 - active
+                // * -1 - undefined (used to initialize PrevActiveConstraints before first iteration)
+                //
+                for(i=0; i<=n-1; i++)
+                {
+                    state.prevactiveconstraints[i] = -1;
+                }
+                
+                //
+                // Main cycle
+                //
+                fprev = math.maxrealnumber;
+                while( true )
+                {
+                    
+                    //
+                    // * calculate gradient at XC
+                    // * determine active constraints
+                    // * break if there is no free variables or
+                    //   there were no changes in the list of active constraints
+                    //
+                    minqpgrad(state);
+                    nactive = 0;
+                    for(i=0; i<=n-1; i++)
+                    {
+                        state.activeconstraints[i] = 0;
+                        if( state.havebndl[i] )
+                        {
+                            if( (double)(state.xc[i])<=(double)(state.workbndl[i]) & (double)(state.gc[i])>=(double)(0) )
+                            {
+                                state.activeconstraints[i] = 1;
+                            }
+                        }
+                        if( state.havebndu[i] )
+                        {
+                            if( (double)(state.xc[i])>=(double)(state.workbndu[i]) & (double)(state.gc[i])<=(double)(0) )
+                            {
+                                state.activeconstraints[i] = 1;
+                            }
+                        }
+                        if( state.havebndl[i] & state.havebndu[i] )
+                        {
+                            if( (double)(state.workbndl[i])==(double)(state.workbndu[i]) )
+                            {
+                                state.activeconstraints[i] = 1;
+                            }
+                        }
+                        if( state.activeconstraints[i]>0 )
+                        {
+                            nactive = nactive+1;
+                        }
+                    }
+                    nfree = n-nactive;
+                    if( nfree==0 )
+                    {
+                        break;
+                    }
+                    b = false;
+                    for(i=0; i<=n-1; i++)
+                    {
+                        if( state.activeconstraints[i]!=state.prevactiveconstraints[i] )
+                        {
+                            b = true;
+                        }
+                    }
+                    if( !b )
+                    {
+                        break;
+                    }
+                    
+                    //
+                    // * copy A, B and X to buffer
+                    // * rearrange BufA, BufB and BufX, in such way that active variables come first,
+                    //   inactive are moved to the tail. We use sorting subroutine
+                    //   to solve this problem.
+                    //
+                    state.bufa[0,0] = state.diaga[0];
+                    for(k=1; k<=n-1; k++)
+                    {
+                        for(i_=0; i_<=k-1;i_++)
+                        {
+                            state.bufa[k,i_] = state.densea[k,i_];
+                        }
+                        for(i_=0; i_<=k-1;i_++)
+                        {
+                            state.bufa[i_,k] = state.densea[k,i_];
+                        }
+                        state.bufa[k,k] = state.diaga[k];
+                    }
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.bufb[i_] = state.b[i_];
+                    }
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.bufx[i_] = state.xc[i_];
+                    }
+                    for(i=0; i<=n-1; i++)
+                    {
+                        state.tmp0[i] = state.activeconstraints[i];
+                    }
+                    tsort.tagsortbuf(ref state.tmp0, n, ref state.itmp0, ref state.p2, state.buf);
+                    for(k=0; k<=n-1; k++)
+                    {
+                        if( state.p2[k]!=k )
+                        {
+                            v = state.bufb[k];
+                            state.bufb[k] = state.bufb[state.p2[k]];
+                            state.bufb[state.p2[k]] = v;
+                            v = state.bufx[k];
+                            state.bufx[k] = state.bufx[state.p2[k]];
+                            state.bufx[state.p2[k]] = v;
+                        }
+                    }
+                    for(i=0; i<=n-1; i++)
+                    {
+                        for(i_=0; i_<=n-1;i_++)
+                        {
+                            state.tmp0[i_] = state.bufa[i,i_];
+                        }
+                        for(k=0; k<=n-1; k++)
+                        {
+                            if( state.p2[k]!=k )
+                            {
+                                v = state.tmp0[k];
+                                state.tmp0[k] = state.tmp0[state.p2[k]];
+                                state.tmp0[state.p2[k]] = v;
+                            }
+                        }
+                        for(i_=0; i_<=n-1;i_++)
+                        {
+                            state.bufa[i,i_] = state.tmp0[i_];
+                        }
+                    }
+                    for(i=0; i<=n-1; i++)
+                    {
+                        if( state.p2[i]!=i )
+                        {
+                            for(i_=0; i_<=n-1;i_++)
+                            {
+                                state.tmp0[i_] = state.bufa[i,i_];
+                            }
+                            for(i_=0; i_<=n-1;i_++)
+                            {
+                                state.bufa[i,i_] = state.bufa[state.p2[i],i_];
+                            }
+                            for(i_=0; i_<=n-1;i_++)
+                            {
+                                state.bufa[state.p2[i],i_] = state.tmp0[i_];
+                            }
+                        }
+                    }
+                    
+                    //
+                    // Now we have A and B in BufA and BufB, variables are rearranged
+                    // into two groups: Xf - free variables, Xc - active (fixed) variables,
+                    // and our quadratic problem can be written as
+                    //
+                    //                           ( Af  Ac  )   ( Xf )                 ( Xf )
+                    // F(X) = 0.5* ( Xf' Xc' ) * (         ) * (    ) + ( Bf' Bc' ) * (    )
+                    //                           ( Ac' Acc )   ( Xc )                 ( Xc )
+                    //
+                    // we want to convert to the optimization with respect to Xf,
+                    // treating Xc as constant term. After expansion of expression above
+                    // we get
+                    //
+                    // F(Xf) = 0.5*Xf'*Af*Xf + (Bf+Ac*Xc)'*Xf + 0.5*Xc'*Acc*Xc
+                    //
+                    // We will update BufB using this expression and calculate
+                    // constant term.
+                    //
+                    ablas.rmatrixmv(nfree, nactive, state.bufa, 0, nfree, 0, state.bufx, nfree, ref state.tmp0, 0);
+                    for(i_=0; i_<=nfree-1;i_++)
+                    {
+                        state.bufb[i_] = state.bufb[i_] + state.tmp0[i_];
+                    }
+                    state.constterm = 0.0;
+                    for(i=nfree; i<=n-1; i++)
+                    {
+                        state.constterm = state.constterm+0.5*state.bufx[i]*state.bufa[i,i]*state.bufx[i];
+                        for(j=i+1; j<=n-1; j++)
+                        {
+                            state.constterm = state.constterm+state.bufx[i]*state.bufa[i,j]*state.bufx[j];
+                        }
+                    }
+                    
+                    //
+                    // Now we are ready to minimize F(Xf)...
+                    //
+                    state.repncholesky = state.repncholesky+1;
+                    if( !trfac.spdmatrixcholeskyrec(ref state.bufa, 0, nfree, true, ref state.tmp0) )
+                    {
+                        state.repterminationtype = -5;
+                        return;
+                    }
+                    fbls.fblscholeskysolve(state.bufa, 1.0, nfree, true, ref state.bufb, ref state.tmp0);
+                    for(i_=0; i_<=nfree-1;i_++)
+                    {
+                        state.bufx[i_] = -state.bufb[i_];
+                    }
+                    
+                    //
+                    // ...and to copy results back to XC.
+                    //
+                    // It is done in several steps:
+                    // * original order of variables is restored
+                    // * result is copied back to XC
+                    // * XC is bounded with respect to bound constraints
+                    //
+                    for(k=n-1; k>=0; k--)
+                    {
+                        if( state.p2[k]!=k )
+                        {
+                            v = state.bufx[k];
+                            state.bufx[k] = state.bufx[state.p2[k]];
+                            state.bufx[state.p2[k]] = v;
+                        }
+                    }
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        state.xc[i_] = state.bufx[i_];
+                    }
+                    for(i=0; i<=n-1; i++)
+                    {
+                        if( state.havebndl[i] )
+                        {
+                            if( (double)(state.xc[i])<(double)(state.workbndl[i]) )
+                            {
+                                state.xc[i] = state.workbndl[i];
+                            }
+                        }
+                        if( state.havebndu[i] )
+                        {
+                            if( (double)(state.xc[i])>(double)(state.workbndu[i]) )
+                            {
+                                state.xc[i] = state.workbndu[i];
+                            }
+                        }
+                    }
+                    
+                    //
+                    // Calculate F, compare it with FPrev.
+                    //
+                    // Break if F>=FPrev
+                    // (sometimes possible at extremum due to numerical noise).
+                    //
+                    f = 0.0;
+                    for(i_=0; i_<=n-1;i_++)
+                    {
+                        f += state.b[i_]*state.xc[i_];
+                    }
+                    f = f+minqpxtax(state, state.xc);
+                    if( (double)(f)>=(double)(fprev) )
+                    {
+                        break;
+                    }
+                    fprev = f;
+                    
+                    //
+                    // Update PrevActiveConstraints
+                    //
+                    for(i=0; i<=n-1; i++)
+                    {
+                        state.prevactiveconstraints[i] = state.activeconstraints[i];
+                    }
+                    
+                    //
+                    // Update report-related fields
+                    //
+                    state.repouteriterationscount = state.repouteriterationscount+1;
+                }
+                state.repterminationtype = 4;
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.xc[i_] = state.xc[i_] + state.xorigin[i_];
+                }
+                return;
+            }
+        }
+
+
+        /*************************************************************************
+        QP solver results
+
+        INPUT PARAMETERS:
+            State   -   algorithm state
+
+        OUTPUT PARAMETERS:
+            X       -   array[0..N-1], solution
+            Rep     -   optimization report. You should check Rep.TerminationType,
+                        which contains completion code, and you may check  another
+                        fields which contain another information  about  algorithm
+                        functioning.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpresults(minqpstate state,
+            ref double[] x,
+            minqpreport rep)
+        {
+            x = new double[0];
+
+            minqpresultsbuf(state, ref x, rep);
+        }
+
+
+        /*************************************************************************
+        QP results
+
+        Buffered implementation of MinQPResults() which uses pre-allocated  buffer
+        to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
+        intended to be used in the inner cycles of performance critical algorithms
+        where array reallocation penalty is too large to be ignored.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpresultsbuf(minqpstate state,
+            ref double[] x,
+            minqpreport rep)
+        {
+            int i_ = 0;
+
+            if( ap.len(x)<state.n )
+            {
+                x = new double[state.n];
+            }
+            for(i_=0; i_<=state.n-1;i_++)
+            {
+                x[i_] = state.xc[i_];
+            }
+            rep.inneriterationscount = state.repinneriterationscount;
+            rep.outeriterationscount = state.repouteriterationscount;
+            rep.nmv = state.repnmv;
+            rep.terminationtype = state.repterminationtype;
+        }
+
+
+        /*************************************************************************
+        Fast version of MinQPSetLinearTerm(), which doesn't check its arguments.
+        For internal use only.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetlineartermfast(minqpstate state,
+            double[] b)
+        {
+            int n = 0;
+            int i_ = 0;
+
+            n = state.n;
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.b[i_] = b[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        Fast version of MinQPSetQuadraticTerm(), which doesn't check its arguments.
+
+        It accepts additional parameter - shift S, which allows to "shift"  matrix
+        A by adding s*I to A. S must be positive (although it is not checked).
+
+        For internal use only.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetquadratictermfast(minqpstate state,
+            double[,] a,
+            bool isupper,
+            double s)
+        {
+            int k = 0;
+            int n = 0;
+            int i_ = 0;
+
+            
+            //
+            // We store off-diagonal part of A in the lower triangle of DenseA.
+            // Diagonal elements of A are stored in the DiagA.
+            // Diagonal of DenseA and uppper triangle are used as temporaries.
+            //
+            // Why such complex storage? Because it:
+            // 1. allows us to easily recover from exceptions (lower triangle
+            //    is unmodified during execution as well as DiagA, and on entry
+            //    we will always find unmodified matrix)
+            // 2. allows us to make Cholesky decomposition in the upper triangle
+            //    of DenseA or to do other SPD-related operations.
+            //
+            n = state.n;
+            state.akind = 0;
+            apserv.rmatrixsetlengthatleast(ref state.densea, n, n);
+            apserv.rvectorsetlengthatleast(ref state.diaga, n);
+            if( isupper )
+            {
+                for(k=0; k<=n-2; k++)
+                {
+                    state.diaga[k] = a[k,k]+s;
+                    for(i_=k+1; i_<=n-1;i_++)
+                    {
+                        state.densea[i_,k] = a[k,i_];
+                    }
+                }
+                state.diaga[n-1] = a[n-1,n-1]+s;
+            }
+            else
+            {
+                state.diaga[0] = a[0,0]+s;
+                for(k=1; k<=n-1; k++)
+                {
+                    for(i_=0; i_<=k-1;i_++)
+                    {
+                        state.densea[k,i_] = a[k,i_];
+                    }
+                    state.diaga[k] = a[k,k]+s;
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Interna lfunction which allows to rewrite diagonal of quadratic term.
+        For internal use only.
+
+        This function can be used only when you have dense A and already made
+        MinQPSetQuadraticTerm(Fast) call.
+
+          -- ALGLIB --
+             Copyright 16.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqprewritediagonal(minqpstate state,
+            double[] s)
+        {
+            int k = 0;
+            int n = 0;
+
+            ap.assert(state.akind==0, "MinQPRewriteDiagonal: internal error (AKind<>0)");
+            n = state.n;
+            for(k=0; k<=n-1; k++)
+            {
+                state.diaga[k] = s[k];
+            }
+        }
+
+
+        /*************************************************************************
+        Fast version of MinQPSetStartingPoint(), which doesn't check its arguments.
+        For internal use only.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetstartingpointfast(minqpstate state,
+            double[] x)
+        {
+            int n = 0;
+            int i_ = 0;
+
+            n = state.n;
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.startx[i_] = x[i_];
+            }
+            state.havex = true;
+        }
+
+
+        /*************************************************************************
+        Fast version of MinQPSetOrigin(), which doesn't check its arguments.
+        For internal use only.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minqpsetoriginfast(minqpstate state,
+            double[] xorigin)
+        {
+            int n = 0;
+            int i_ = 0;
+
+            n = state.n;
+            for(i_=0; i_<=n-1;i_++)
+            {
+                state.xorigin[i_] = xorigin[i_];
+            }
+        }
+
+
+        /*************************************************************************
+        This  function  calculates gradient of quadratic function at XC and stores
+        it in the GC.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        private static void minqpgrad(minqpstate state)
+        {
+            int n = 0;
+            int i = 0;
+            double v = 0;
+            int i_ = 0;
+
+            n = state.n;
+            ap.assert(state.akind==-1 | state.akind==0, "MinQPGrad: internal error");
+            
+            //
+            // zero A
+            //
+            if( state.akind==-1 )
+            {
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.gc[i_] = state.b[i_];
+                }
+                return;
+            }
+            
+            //
+            // dense A
+            //
+            if( state.akind==0 )
+            {
+                for(i_=0; i_<=n-1;i_++)
+                {
+                    state.gc[i_] = state.b[i_];
+                }
+                state.gc[0] = state.gc[0]+state.diaga[0]*state.xc[0];
+                for(i=1; i<=n-1; i++)
+                {
+                    v = 0.0;
+                    for(i_=0; i_<=i-1;i_++)
+                    {
+                        v += state.densea[i,i_]*state.xc[i_];
+                    }
+                    state.gc[i] = state.gc[i]+v+state.diaga[i]*state.xc[i];
+                    v = state.xc[i];
+                    for(i_=0; i_<=i-1;i_++)
+                    {
+                        state.gc[i_] = state.gc[i_] + v*state.densea[i,i_];
+                    }
+                }
+                return;
+            }
+        }
+
+
+        /*************************************************************************
+        This  function  calculates x'*A*x for given X.
+
+          -- ALGLIB --
+             Copyright 11.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        private static double minqpxtax(minqpstate state,
+            double[] x)
+        {
+            double result = 0;
+            int n = 0;
+            int i = 0;
+            int j = 0;
+
+            n = state.n;
+            ap.assert(state.akind==-1 | state.akind==0, "MinQPXTAX: internal error");
+            result = 0;
+            
+            //
+            // zero A
+            //
+            if( state.akind==-1 )
+            {
+                result = 0.0;
+                return result;
+            }
+            
+            //
+            // dense A
+            //
+            if( state.akind==0 )
+            {
+                result = 0;
+                for(i=0; i<=n-1; i++)
+                {
+                    for(j=0; j<=i-1; j++)
+                    {
+                        result = result+state.densea[i,j]*x[i]*x[j];
+                    }
+                    result = result+0.5*state.diaga[i]*math.sqr(x[i]);
+                }
+                return result;
+            }
+            return result;
         }
 
@@ -2995,7 +10200,11 @@
             public double[] gbase;
             public double[,] quadraticmodel;
+            public double[] bndl;
+            public double[] bndu;
+            public bool[] havebndl;
+            public bool[] havebndu;
+            public double[] s;
             public double lambdav;
             public double nu;
-            public double[,] dampedmodel;
             public int modelage;
             public double[] xdir;
@@ -3013,12 +10222,15 @@
             public rcommstate rstate;
             public double[] choleskybuf;
+            public double[] tmp0;
             public double actualdecrease;
             public double predicteddecrease;
-            public double[] fm2;
+            public double xm1;
+            public double xp1;
             public double[] fm1;
-            public double[] fp2;
             public double[] fp1;
             public minlbfgs.minlbfgsstate internalstate;
             public minlbfgs.minlbfgsreport internalrep;
+            public minqp.minqpstate qpstate;
+            public minqp.minqpreport qprep;
             public minlmstate()
             {
@@ -3032,5 +10244,9 @@
                 gbase = new double[0];
                 quadraticmodel = new double[0,0];
-                dampedmodel = new double[0,0];
+                bndl = new double[0];
+                bndu = new double[0];
+                havebndl = new bool[0];
+                havebndu = new bool[0];
+                s = new double[0];
                 xdir = new double[0];
                 deltax = new double[0];
@@ -3038,10 +10254,11 @@
                 rstate = new rcommstate();
                 choleskybuf = new double[0];
-                fm2 = new double[0];
+                tmp0 = new double[0];
                 fm1 = new double[0];
-                fp2 = new double[0];
                 fp1 = new double[0];
                 internalstate = new minlbfgs.minlbfgsstate();
                 internalrep = new minlbfgs.minlbfgsreport();
+                qpstate = new minqp.minqpstate();
+                qprep = new minqp.minqpreport();
             }
         };
@@ -3382,206 +10599,4 @@
 
         /*************************************************************************
-                        IMPROVED LEVENBERG-MARQUARDT METHOD FOR
-                         NON-LINEAR LEAST SQUARES OPTIMIZATION
-
-        DESCRIPTION:
-        This function is used to find minimum of function which is represented  as
-        sum of squares:
-            F(x) = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
-        using:
-        * value of function vector f[]
-        * value of Jacobian of f[]
-        * gradient of merit function F(x)
-
-        This function creates optimizer which uses acceleration strategy 2.  Cheap
-        gradient of merit function (which is twice the product of function  vector
-        and Jacobian) is used for accelerated iterations (see User Guide for  more
-        info on this subject).
-
-        REQUIREMENTS:
-        This algorithm will request following information during its operation:
-
-        * function vector f[] at given point X
-        * function vector f[] and Jacobian of f[] (simultaneously) at given point
-        * gradient of
-
-        There are several overloaded versions of  MinLMOptimize()  function  which
-        correspond  to  different LM-like optimization algorithms provided by this
-        unit. You should choose version which accepts  fvec(),  jac()  and  grad()
-        callbacks. First one is used to calculate f[] at given point,  second  one
-        calculates f[] and Jacobian df[i]/dx[j], last one calculates  gradient  of
-        merit function F(x).
-
-        You can try to initialize MinLMState structure with VJ  function and  then
-        use incorrect version  of  MinLMOptimize()  (for  example,  version  which
-        works  with  general  form function and does not provide Jacobian), but it
-        will  lead  to  exception  being  thrown  after first attempt to calculate
-        Jacobian.
-
-
-        USAGE:
-        1. User initializes algorithm state with MinLMCreateVGJ() call
-        2. User tunes solver parameters with MinLMSetCond(),  MinLMSetStpMax() and
-           other functions
-        3. User calls MinLMOptimize() function which  takes algorithm  state   and
-           callback functions.
-        4. User calls MinLMResults() to get solution
-        5. Optionally, user may call MinLMRestartFrom() to solve  another  problem
-           with same N/M but another starting point and/or another function.
-           MinLMRestartFrom() allows to reuse already initialized structure.
-
-
-        INPUT PARAMETERS:
-            N       -   dimension, N>1
-                        * if given, only leading N elements of X are used
-                        * if not given, automatically determined from size of X
-            M       -   number of functions f[i]
-            X       -   initial solution, array[0..N-1]
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-
-        NOTES:
-        1. you may tune stopping conditions with MinLMSetCond() function
-        2. if target function contains exp() or other fast growing functions,  and
-           optimization algorithm makes too large steps which leads  to  overflow,
-           use MinLMSetStpMax() function to bound algorithm's steps.
-
-          -- ALGLIB --
-             Copyright 30.03.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void minlmcreatevgj(int n,
-            int m,
-            double[] x,
-            minlmstate state)
-        {
-            ap.assert(n>=1, "MinLMCreateVGJ: N<1!");
-            ap.assert(m>=1, "MinLMCreateVGJ: M<1!");
-            ap.assert(ap.len(x)>=n, "MinLMCreateVGJ: Length(X)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "MinLMCreateVGJ: X contains infinite or NaN values!");
-            
-            //
-            // initialize, check parameters
-            //
-            state.n = n;
-            state.m = m;
-            state.algomode = 1;
-            state.hasf = false;
-            state.hasfi = true;
-            state.hasg = false;
-            
-            //
-            // second stage of initialization
-            //
-            lmprepare(n, m, false, state);
-            minlmsetacctype(state, 2);
-            minlmsetcond(state, 0, 0, 0, 0);
-            minlmsetxrep(state, false);
-            minlmsetstpmax(state, 0);
-            minlmrestartfrom(state, x);
-        }
-
-
-        /*************************************************************************
-                           LEVENBERG-MARQUARDT-LIKE METHOD FOR
-                          NON-LINEAR LEAST SQUARES OPTIMIZATION
-
-        DESCRIPTION:
-
-        This function is used to find minimum of function which is represented  as
-        sum of squares:
-            F(x) = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
-        using value of F(), gradient of F(), function vector f[]  and  Jacobian of
-        f[].
-
-        This function is considered obsolete since ALGLIB 3.1.0 and is present for
-        backward  compatibility  only.  We  recommend to use MinLMCreateVGJ, which
-        provides similar, but more consistent interface.
-
-          -- ALGLIB --
-             Copyright 30.03.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void minlmcreatefgj(int n,
-            int m,
-            double[] x,
-            minlmstate state)
-        {
-            ap.assert(n>=1, "MinLMCreateFGJ: N<1!");
-            ap.assert(m>=1, "MinLMCreateFGJ: M<1!");
-            ap.assert(ap.len(x)>=n, "MinLMCreateFGJ: Length(X)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "MinLMCreateFGJ: X contains infinite or NaN values!");
-            
-            //
-            // initialize
-            //
-            state.n = n;
-            state.m = m;
-            state.algomode = 1;
-            state.hasf = true;
-            state.hasfi = false;
-            state.hasg = true;
-            
-            //
-            // init2
-            //
-            lmprepare(n, m, true, state);
-            minlmsetacctype(state, 2);
-            minlmsetcond(state, 0, 0, 0, 0);
-            minlmsetxrep(state, false);
-            minlmsetstpmax(state, 0);
-            minlmrestartfrom(state, x);
-        }
-
-
-        /*************************************************************************
-            CLASSIC LEVENBERG-MARQUARDT METHOD FOR NON-LINEAR OPTIMIZATION
-
-        DESCRIPTION:
-        This function is used to find minimum of function which is represented  as
-        sum of squares:
-            F(x) = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
-        using  value  of  F(),  function  vector  f[] and Jacobian of f[]. Classic
-        Levenberg-Marquardt method is used.
-
-        This function is considered obsolete since ALGLIB 3.1.0 and is present for
-        backward  compatibility  only.  We  recommend  to use MinLMCreateVJ, which
-        provides similar, but more consistent and feature-rich interface.
-
-          -- ALGLIB --
-             Copyright 30.03.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void minlmcreatefj(int n,
-            int m,
-            double[] x,
-            minlmstate state)
-        {
-            ap.assert(n>=1, "MinLMCreateFJ: N<1!");
-            ap.assert(m>=1, "MinLMCreateFJ: M<1!");
-            ap.assert(ap.len(x)>=n, "MinLMCreateFJ: Length(X)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "MinLMCreateFJ: X contains infinite or NaN values!");
-            
-            //
-            // initialize
-            //
-            state.n = n;
-            state.m = m;
-            state.algomode = 1;
-            state.hasf = true;
-            state.hasfi = false;
-            state.hasg = false;
-            
-            //
-            // init 2
-            //
-            lmprepare(n, m, true, state);
-            minlmsetacctype(state, 0);
-            minlmsetcond(state, 0, 0, 0, 0);
-            minlmsetxrep(state, false);
-            minlmsetstpmax(state, 0);
-            minlmrestartfrom(state, x);
-        }
-
-
-        /*************************************************************************
         This function sets stopping conditions for Levenberg-Marquardt optimization
         algorithm.
@@ -3591,6 +10606,9 @@
             EpsG    -   >=0
                         The  subroutine  finishes  its  work   if   the  condition
-                        ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                        G - gradient.
+                        |v|<EpsG is satisfied, where:
+                        * |.| means Euclidian norm
+                        * v - scaled gradient vector, v[i]=g[i]*s[i]
+                        * g - gradient
+                        * s - scaling coefficients set by MinLMSetScale()
             EpsF    -   >=0
                         The  subroutine  finishes  its work if on k+1-th iteration
@@ -3599,5 +10617,9 @@
             EpsX    -   >=0
                         The subroutine finishes its work if  on  k+1-th  iteration
-                        the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
+                        the condition |v|<=EpsX is fulfilled, where:
+                        * |.| means Euclidian norm
+                        * v - scaled step vector, v[i]=dx[i]/s[i]
+                        * dx - ste pvector, dx=X(k+1)-X(k)
+                        * s - scaling coefficients set by MinLMSetScale()
             MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
                         iterations   is    unlimited.   Only   Levenberg-Marquardt
@@ -3688,4 +10710,96 @@
 
         /*************************************************************************
+        This function sets scaling coefficients for LM optimizer.
+
+        ALGLIB optimizers use scaling matrices to test stopping  conditions  (step
+        size and gradient are scaled before comparison with tolerances).  Scale of
+        the I-th variable is a translation invariant measure of:
+        a) "how large" the variable is
+        b) how large the step should be to make significant changes in the function
+
+        Generally, scale is NOT considered to be a form of preconditioner.  But LM
+        optimizer is unique in that it uses scaling matrix both  in  the  stopping
+        condition tests and as Marquardt damping factor.
+
+        Proper scaling is very important for the algorithm performance. It is less
+        important for the quality of results, but still has some influence (it  is
+        easier  to  converge  when  variables  are  properly  scaled, so premature
+        stopping is possible when very badly scalled variables are  combined  with
+        relaxed stopping conditions).
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            S       -   array[N], non-zero scaling coefficients
+                        S[i] may be negative, sign doesn't matter.
+
+          -- ALGLIB --
+             Copyright 14.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlmsetscale(minlmstate state,
+            double[] s)
+        {
+            int i = 0;
+
+            ap.assert(ap.len(s)>=state.n, "MinLMSetScale: Length(S)<N");
+            for(i=0; i<=state.n-1; i++)
+            {
+                ap.assert(math.isfinite(s[i]), "MinLMSetScale: S contains infinite or NAN elements");
+                ap.assert((double)(s[i])!=(double)(0), "MinLMSetScale: S contains zero elements");
+                state.s[i] = Math.Abs(s[i]);
+            }
+        }
+
+
+        /*************************************************************************
+        This function sets boundary constraints for LM optimizer
+
+        Boundary constraints are inactive by default (after initial creation).
+        They are preserved until explicitly turned off with another SetBC() call.
+
+        INPUT PARAMETERS:
+            State   -   structure stores algorithm state
+            BndL    -   lower bounds, array[N].
+                        If some (all) variables are unbounded, you may specify
+                        very small number or -INF (latter is recommended because
+                        it will allow solver to use better algorithm).
+            BndU    -   upper bounds, array[N].
+                        If some (all) variables are unbounded, you may specify
+                        very large number or +INF (latter is recommended because
+                        it will allow solver to use better algorithm).
+
+        NOTE 1: it is possible to specify BndL[i]=BndU[i]. In this case I-th
+        variable will be "frozen" at X[i]=BndL[i]=BndU[i].
+
+        NOTE 2: this solver has following useful properties:
+        * bound constraints are always satisfied exactly
+        * function is evaluated only INSIDE area specified by bound constraints
+          or at its boundary
+
+          -- ALGLIB --
+             Copyright 14.01.2011 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlmsetbc(minlmstate state,
+            double[] bndl,
+            double[] bndu)
+        {
+            int i = 0;
+            int n = 0;
+
+            n = state.n;
+            ap.assert(ap.len(bndl)>=n, "MinLMSetBC: Length(BndL)<N");
+            ap.assert(ap.len(bndu)>=n, "MinLMSetBC: Length(BndU)<N");
+            for(i=0; i<=n-1; i++)
+            {
+                ap.assert(math.isfinite(bndl[i]) | Double.IsNegativeInfinity(bndl[i]), "MinLMSetBC: BndL contains NAN or +INF");
+                ap.assert(math.isfinite(bndu[i]) | Double.IsPositiveInfinity(bndu[i]), "MinLMSetBC: BndU contains NAN or -INF");
+                state.bndl[i] = bndl[i];
+                state.havebndl[i] = math.isfinite(bndl[i]);
+                state.bndu[i] = bndu[i];
+                state.havebndu[i] = math.isfinite(bndu[i]);
+            }
+        }
+
+
+        /*************************************************************************
         This function is used to change acceleration settings
 
@@ -3698,6 +10812,4 @@
           settings: about 3 iterations - when we have analytic Jacobian, up to 2*N
           iterations - when we use finite differences to calculate Jacobian.
-        * AccType=2, after quadratic model is built and LM step is made, we use it
-          as preconditioner for several (5-10) iterations of L-BFGS algorithm.
 
         AccType=1 is recommended when Jacobian  calculation  cost  is  prohibitive
@@ -3707,9 +10819,4 @@
         approximation takes too much time to compute.
 
-        AccType=2 is recommended when Jacobian is cheap - much more  cheaper  than
-        one  Cholesky  factorization.   We   can   reduce   number   of   Cholesky
-        factorizations at the cost of increased number of  Jacobian  calculations.
-        Sometimes it helps.
-
         Table below list  optimization  protocols  (XYZ  protocol  corresponds  to
         MinLMCreateXYZ) and acceleration types they support (and use by  default).
@@ -3717,21 +10824,15 @@
         ACCELERATION TYPES SUPPORTED BY OPTIMIZATION PROTOCOLS:
 
-        protocol    0   1   2   comment
+        protocol    0   1   comment
         V           +   +
-        VJ          +   +   +
-        FGH         +       +
-        VGJ         +   +   +   special protocol, not for widespread use
-        FJ          +       +   obsolete protocol, not recommended
-        FGJ         +       +   obsolete protocol, not recommended
+        VJ          +   +
+        FGH         +
 
         DAFAULT VALUES:
 
-        protocol    0   1   2   comment
-        V               x       without acceleration it is so slooooooooow
+        protocol    0   1   comment
+        V               x   without acceleration it is so slooooooooow
         VJ          x
         FGH         x
-        VGJ                 x   we've implicitly turned (2) by passing gradient
-        FJ          x           obsolete protocol, not recommended
-        FGJ                 x   obsolete protocol, not recommended
 
         NOTE: this  function should be called before optimization. Attempt to call
@@ -3748,4 +10849,8 @@
         {
             ap.assert((acctype==0 | acctype==1) | acctype==2, "MinLMSetAccType: incorrect AccType!");
+            if( acctype==2 )
+            {
+                acctype = 0;
+            }
             if( acctype==0 )
             {
@@ -3766,11 +10871,4 @@
                 }
                 state.makeadditers = false;
-                return;
-            }
-            if( acctype==2 )
-            {
-                ap.assert(state.algomode==1 | state.algomode==2, "MinLMSetAccType: AccType=2 is incompatible with current protocol!");
-                state.maxmodelage = 0;
-                state.makeadditers = true;
                 return;
             }
@@ -3910,12 +11008,4 @@
                 goto lbl_15;
             }
-            if( state.rstate.stage==16 )
-            {
-                goto lbl_16;
-            }
-            if( state.rstate.stage==17 )
-            {
-                goto lbl_17;
-            }
             
             //
@@ -3937,4 +11027,22 @@
             
             //
+            // check consistency of constraints
+            // set constraints
+            //
+            for(i=0; i<=n-1; i++)
+            {
+                if( state.havebndl[i] & state.havebndu[i] )
+                {
+                    if( (double)(state.bndl[i])>(double)(state.bndu[i]) )
+                    {
+                        state.repterminationtype = -3;
+                        result = false;
+                        return result;
+                    }
+                }
+            }
+            minqp.minqpsetbc(state.qpstate, state.bndl, state.bndu);
+            
+            //
             // Initial report of current point
             //
@@ -3947,5 +11055,5 @@
             if( !state.xrep )
             {
-                goto lbl_18;
+                goto lbl_16;
             }
             for(i_=0; i_<=n-1;i_++)
@@ -3956,5 +11064,5 @@
             if( !state.hasf )
             {
-                goto lbl_20;
+                goto lbl_18;
             }
             state.needf = true;
@@ -3963,6 +11071,6 @@
         lbl_0:
             state.needf = false;
-            goto lbl_21;
-        lbl_20:
+            goto lbl_19;
+        lbl_18:
             ap.assert(state.hasfi, "MinLM: internal error 2!");
             state.needfi = true;
@@ -3977,5 +11085,5 @@
             }
             state.f = v;
-        lbl_21:
+        lbl_19:
             state.repnfunc = state.repnfunc+1;
             for(i_=0; i_<=n-1;i_++)
@@ -3989,5 +11097,5 @@
         lbl_2:
             state.xupdated = false;
-        lbl_18:
+        lbl_16:
             
             //
@@ -4009,8 +11117,8 @@
             //
             //
-        lbl_22:
+        lbl_20:
             if( false )
             {
-                goto lbl_23;
+                goto lbl_21;
             }
             
@@ -4040,21 +11148,21 @@
             if( !(state.algomode==0 | state.algomode==1) )
             {
+                goto lbl_22;
+            }
+            
+            //
+            // Calculate f[] and Jacobian
+            //
+            if( !(state.modelage>state.maxmodelage | !(state.deltaxready & state.deltafready)) )
+            {
                 goto lbl_24;
             }
             
             //
-            // Calculate f[] and Jacobian
-            //
-            if( !(state.modelage>state.maxmodelage | !(state.deltaxready & state.deltafready)) )
+            // Refresh model (using either finite differences or analytic Jacobian)
+            //
+            if( state.algomode!=0 )
             {
                 goto lbl_26;
-            }
-            
-            //
-            // Refresh model (using either finite differences or analytic Jacobian)
-            //
-            if( state.algomode!=0 )
-            {
-                goto lbl_28;
             }
             
@@ -4065,14 +11173,28 @@
             ap.assert(state.hasfi, "MinLMIteration: internal error when estimating Jacobian (no f[])");
             k = 0;
-        lbl_30:
+        lbl_28:
             if( k>n-1 )
             {
-                goto lbl_32;
-            }
+                goto lbl_30;
+            }
+            
+            //
+            // We guard X[k] from leaving [BndL,BndU].
+            // In case BndL=BndU, we assume that derivative in this direction is zero.
+            //
             for(i_=0; i_<=n-1;i_++)
             {
                 state.x[i_] = state.xbase[i_];
             }
-            state.x[k] = state.x[k]-state.diffstep;
+            state.x[k] = state.x[k]-state.s[k]*state.diffstep;
+            if( state.havebndl[k] )
+            {
+                state.x[k] = Math.Max(state.x[k], state.bndl[k]);
+            }
+            if( state.havebndu[k] )
+            {
+                state.x[k] = Math.Min(state.x[k], state.bndu[k]);
+            }
+            state.xm1 = state.x[k];
             clearrequestfields(state);
             state.needfi = true;
@@ -4089,5 +11211,14 @@
                 state.x[i_] = state.xbase[i_];
             }
-            state.x[k] = state.x[k]+state.diffstep;
+            state.x[k] = state.x[k]+state.s[k]*state.diffstep;
+            if( state.havebndl[k] )
+            {
+                state.x[k] = Math.Max(state.x[k], state.bndl[k]);
+            }
+            if( state.havebndu[k] )
+            {
+                state.x[k] = Math.Min(state.x[k], state.bndu[k]);
+            }
+            state.xp1 = state.x[k];
             clearrequestfields(state);
             state.needfi = true;
@@ -4100,16 +11231,27 @@
                 state.fp1[i_] = state.fi[i_];
             }
-            v = 1/(2*state.diffstep);
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.j[i_,k] = v*state.fp1[i_];
-            }
-            for(i_=0; i_<=m-1;i_++)
-            {
-                state.j[i_,k] = state.j[i_,k] - v*state.fm1[i_];
+            v = state.xp1-state.xm1;
+            if( (double)(v)!=(double)(0) )
+            {
+                v = 1/v;
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.j[i_,k] = v*state.fp1[i_];
+                }
+                for(i_=0; i_<=m-1;i_++)
+                {
+                    state.j[i_,k] = state.j[i_,k] - v*state.fm1[i_];
+                }
+            }
+            else
+            {
+                for(i=0; i<=m-1; i++)
+                {
+                    state.j[i,k] = 0;
+                }
             }
             k = k+1;
-            goto lbl_30;
-        lbl_32:
+            goto lbl_28;
+        lbl_30:
             
             //
@@ -4133,6 +11275,6 @@
             //
             state.modelage = 0;
-            goto lbl_29;
-        lbl_28:
+            goto lbl_27;
+        lbl_26:
             
             //
@@ -4156,7 +11298,7 @@
             //
             state.modelage = 0;
-        lbl_29:
-            goto lbl_27;
-        lbl_26:
+        lbl_27:
+            goto lbl_25;
+        lbl_24:
             
             //
@@ -4211,5 +11353,5 @@
             //
             state.modelage = state.modelage+1;
-        lbl_27:
+        lbl_25:
             
             //
@@ -4245,8 +11387,8 @@
             //
             bflag = true;
-        lbl_24:
+        lbl_22:
             if( state.algomode!=2 )
             {
-                goto lbl_33;
+                goto lbl_31;
             }
             ap.assert(!state.hasfi, "MinLMIteration: internal error (HasFI is True in Hessian-based mode)");
@@ -4280,5 +11422,5 @@
             bflag = true;
             state.modelage = 0;
-        lbl_33:
+        lbl_31:
             ap.assert(bflag, "MinLM: internal integrity check failed!");
             state.deltaxready = false;
@@ -4293,5 +11435,5 @@
                 for(i=0; i<=n-1; i++)
                 {
-                    state.lambdav = Math.Max(state.lambdav, Math.Abs(state.quadraticmodel[i,i]));
+                    state.lambdav = Math.Max(state.lambdav, Math.Abs(state.quadraticmodel[i,i])*math.sqr(state.s[i]));
                 }
                 state.lambdav = 0.001*state.lambdav;
@@ -4305,17 +11447,11 @@
             // Test stopping conditions for function gradient
             //
-            v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.gbase[i_]*state.gbase[i_];
-            }
-            v = Math.Sqrt(v);
-            if( (double)(v)>(double)(state.epsg) )
+            if( (double)(boundedscaledantigradnorm(state, state.xbase, state.gbase))>(double)(state.epsg) )
+            {
+                goto lbl_33;
+            }
+            if( state.modelage!=0 )
             {
                 goto lbl_35;
-            }
-            if( state.modelage!=0 )
-            {
-                goto lbl_37;
             }
             
@@ -4326,5 +11462,5 @@
             if( !state.xrep )
             {
-                goto lbl_39;
+                goto lbl_37;
             }
             for(i_=0; i_<=n-1;i_++)
@@ -4339,9 +11475,9 @@
         lbl_8:
             state.xupdated = false;
-        lbl_39:
+        lbl_37:
             result = false;
             return result;
-            goto lbl_38;
-        lbl_37:
+            goto lbl_36;
+        lbl_35:
             
             //
@@ -4350,7 +11486,7 @@
             //
             state.modelage = state.maxmodelage+1;
-            goto lbl_22;
-        lbl_38:
-        lbl_35:
+            goto lbl_20;
+        lbl_36:
+        lbl_33:
             
             //
@@ -4361,4 +11497,5 @@
             //
             // After this block IFlag is set to:
+            // * -3, if constraints are infeasible
             // * -2, if model update is needed (either Lambda growth is too large
             //       or step is too short, but we can't rely on model and stop iterations)
@@ -4369,8 +11506,8 @@
             //
             iflag = -99;
-        lbl_41:
+        lbl_39:
             if( false )
             {
-                goto lbl_42;
+                goto lbl_40;
             }
             
@@ -4381,21 +11518,11 @@
             {
                 iflag = -2;
-                goto lbl_42;
-            }
-            
-            //
-            // DampedModel = QuadraticModel+lambda*I
-            //
-            ablas.rmatrixcopy(n, n, state.quadraticmodel, 0, 0, ref state.dampedmodel, 0, 0);
-            for(i=0; i<=n-1; i++)
-            {
-                state.dampedmodel[i,i] = state.dampedmodel[i,i]+state.lambdav;
-            }
-            
-            //
-            // 1. try to solve (RawModel+Lambda*I)*dx = -g.
-            //    increase lambda if left part is not positive definite.
-            // 2. bound step by StpMax
-            //    increase lambda if step is larger than StpMax
+                goto lbl_40;
+            }
+            
+            //
+            // Setup quadratic solver and solve quadratic programming problem.
+            // After problem is solved we'll try to bound step by StpMax
+            // (Lambda will be increased if step size is too large).
             //
             // We use BFlag variable to indicate that we have to increase Lambda.
@@ -4403,12 +11530,25 @@
             //
             bflag = true;
-            state.repncholesky = state.repncholesky+1;
-            if( trfac.spdmatrixcholeskyrec(ref state.dampedmodel, 0, n, true, ref state.choleskybuf) )
-            {
+            minqp.minqpsetstartingpointfast(state.qpstate, state.xbase);
+            minqp.minqpsetoriginfast(state.qpstate, state.xbase);
+            minqp.minqpsetlineartermfast(state.qpstate, state.gbase);
+            minqp.minqpsetquadratictermfast(state.qpstate, state.quadraticmodel, true, 0.0);
+            for(i=0; i<=n-1; i++)
+            {
+                state.tmp0[i] = state.quadraticmodel[i,i]+state.lambdav/math.sqr(state.s[i]);
+            }
+            minqp.minqprewritediagonal(state.qpstate, state.tmp0);
+            minqp.minqpoptimize(state.qpstate);
+            minqp.minqpresultsbuf(state.qpstate, ref state.xdir, state.qprep);
+            if( state.qprep.terminationtype>0 )
+            {
+                
+                //
+                // successful solution of QP problem
+                //
                 for(i_=0; i_<=n-1;i_++)
                 {
-                    state.xdir[i_] = -state.gbase[i_];
-                }
-                fbls.fblscholeskysolve(state.dampedmodel, 1.0, n, true, ref state.xdir, ref state.choleskybuf);
+                    state.xdir[i_] = state.xdir[i_] - state.xbase[i_];
+                }
                 v = 0.0;
                 for(i_=0; i_<=n-1;i_++)
@@ -4431,4 +11571,14 @@
             else
             {
+                
+                //
+                // Either problem is non-convex (increase LambdaV) or constraints are inconsistent
+                //
+                ap.assert(state.qprep.terminationtype==-3 | state.qprep.terminationtype==-5, "MinLM: unexpected completion code from QP solver");
+                if( state.qprep.terminationtype==-3 )
+                {
+                    iflag = -3;
+                    goto lbl_40;
+                }
                 bflag = false;
             }
@@ -4443,7 +11593,7 @@
                 {
                     iflag = -1;
-                    goto lbl_42;
-                }
-                goto lbl_41;
+                    goto lbl_40;
+                }
+                goto lbl_39;
             }
             
@@ -4479,16 +11629,16 @@
             state.deltaxready = true;
             v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.deltax[i_]*state.deltax[i_];
+            for(i=0; i<=n-1; i++)
+            {
+                v = v+math.sqr(state.deltax[i]/state.s[i]);
             }
             v = Math.Sqrt(v);
             if( (double)(v)>(double)(state.epsx) )
             {
+                goto lbl_41;
+            }
+            if( state.modelage!=0 )
+            {
                 goto lbl_43;
-            }
-            if( state.modelage!=0 )
-            {
-                goto lbl_45;
             }
             
@@ -4500,5 +11650,5 @@
             if( !state.xrep )
             {
-                goto lbl_47;
+                goto lbl_45;
             }
             for(i_=0; i_<=n-1;i_++)
@@ -4513,9 +11663,9 @@
         lbl_9:
             state.xupdated = false;
-        lbl_47:
+        lbl_45:
             result = false;
             return result;
-            goto lbl_46;
-        lbl_45:
+            goto lbl_44;
+        lbl_43:
             
             //
@@ -4524,7 +11674,7 @@
             //
             iflag = -2;
-            goto lbl_42;
-        lbl_46:
-        lbl_43:
+            goto lbl_40;
+        lbl_44:
+        lbl_41:
             
             //
@@ -4549,5 +11699,5 @@
             if( !state.hasfi )
             {
-                goto lbl_49;
+                goto lbl_47;
             }
             state.needfi = true;
@@ -4571,6 +11721,6 @@
             }
             state.deltafready = true;
-            goto lbl_50;
-        lbl_49:
+            goto lbl_48;
+        lbl_47:
             state.needf = true;
             state.rstate.stage = 11;
@@ -4578,5 +11728,5 @@
         lbl_11:
             state.needf = false;
-        lbl_50:
+        lbl_48:
             state.repnfunc = state.repnfunc+1;
             if( (double)(state.f)>=(double)(state.fbase) )
@@ -4589,7 +11739,7 @@
                 {
                     iflag = -1;
-                    goto lbl_42;
-                }
-                goto lbl_41;
+                    goto lbl_40;
+                }
+                goto lbl_39;
             }
             
@@ -4598,17 +11748,23 @@
             //
             iflag = 0;
-            goto lbl_42;
-            goto lbl_41;
-        lbl_42:
+            goto lbl_40;
+            goto lbl_39;
+        lbl_40:
             state.nu = 1;
-            ap.assert(iflag>=-2 & iflag<=0, "MinLM: internal integrity check failed!");
+            ap.assert(iflag>=-3 & iflag<=0, "MinLM: internal integrity check failed!");
+            if( iflag==-3 )
+            {
+                state.repterminationtype = -3;
+                result = false;
+                return result;
+            }
             if( iflag==-2 )
             {
                 state.modelage = state.maxmodelage+1;
-                goto lbl_22;
+                goto lbl_20;
             }
             if( iflag==-1 )
             {
-                goto lbl_23;
+                goto lbl_21;
             }
             
@@ -4635,14 +11791,14 @@
             if( (double)(state.predicteddecrease)<=(double)(0) )
             {
-                goto lbl_23;
+                goto lbl_21;
             }
             v = state.actualdecrease/state.predicteddecrease;
             if( (double)(v)>=(double)(0.1) )
             {
+                goto lbl_49;
+            }
+            if( increaselambda(ref state.lambdav, ref state.nu) )
+            {
                 goto lbl_51;
-            }
-            if( increaselambda(ref state.lambdav, ref state.nu) )
-            {
-                goto lbl_53;
             }
             
@@ -4653,5 +11809,5 @@
             if( !state.xrep )
             {
-                goto lbl_55;
+                goto lbl_53;
             }
             for(i_=0; i_<=n-1;i_++)
@@ -4666,9 +11822,9 @@
         lbl_12:
             state.xupdated = false;
-        lbl_55:
+        lbl_53:
             result = false;
             return result;
-        lbl_53:
         lbl_51:
+        lbl_49:
             if( (double)(v)>(double)(0.5) )
             {
@@ -4693,5 +11849,5 @@
             if( !state.xrep )
             {
-                goto lbl_57;
+                goto lbl_55;
             }
             for(i_=0; i_<=n-1;i_++)
@@ -4705,5 +11861,5 @@
         lbl_13:
             state.xupdated = false;
-        lbl_57:
+        lbl_55:
             state.repiterationscount = state.repiterationscount+1;
             if( state.repiterationscount>=state.maxits & state.maxits>0 )
@@ -4720,9 +11876,9 @@
             if( state.repterminationtype<=0 )
             {
+                goto lbl_57;
+            }
+            if( !state.xrep )
+            {
                 goto lbl_59;
-            }
-            if( !state.xrep )
-            {
-                goto lbl_61;
             }
             
@@ -4740,114 +11896,32 @@
         lbl_14:
             state.xupdated = false;
-        lbl_61:
+        lbl_59:
             result = false;
             return result;
-        lbl_59:
+        lbl_57:
             state.modelage = state.modelage+1;
-            
-            //
-            // Additional iterations for unconstrained problems:
-            // preconditioned L-BFGS is used.
-            //
-            // NOTE: additional iterations are incompatible with secant updates
-            // because they invalidate
-            //
-            if( !((double)(state.stpmax)==(double)(0) & state.makeadditers) )
-            {
-                goto lbl_63;
-            }
-            ap.assert(state.hasg | state.m!=0, "MinLM: no grad or Jacobian for additional iterations");
-            
-            //
-            // Make preconditioned iterations
-            //
-            minlbfgs.minlbfgssetcholeskypreconditioner(state.internalstate, state.dampedmodel, true);
-            minlbfgs.minlbfgsrestartfrom(state.internalstate, state.xbase);
-        lbl_65:
-            if( !minlbfgs.minlbfgsiteration(state.internalstate) )
-            {
-                goto lbl_66;
-            }
-            if( !state.internalstate.needfg )
-            {
-                goto lbl_67;
+            goto lbl_20;
+        lbl_21:
+            
+            //
+            // Lambda is too large, we have to break iterations.
+            //
+            state.repterminationtype = 7;
+            if( !state.xrep )
+            {
+                goto lbl_61;
             }
             for(i_=0; i_<=n-1;i_++)
             {
-                state.x[i_] = state.internalstate.x[i_];
-            }
+                state.x[i_] = state.xbase[i_];
+            }
+            state.f = state.fbase;
             clearrequestfields(state);
-            if( !state.hasg )
-            {
-                goto lbl_69;
-            }
-            state.needfg = true;
+            state.xupdated = true;
             state.rstate.stage = 15;
             goto lbl_rcomm;
         lbl_15:
-            state.needfg = false;
-            state.repngrad = state.repngrad+1;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.internalstate.g[i_] = state.g[i_];
-            }
-            state.internalstate.f = state.f;
-            goto lbl_70;
-        lbl_69:
-            state.needfij = true;
-            state.rstate.stage = 16;
-            goto lbl_rcomm;
-        lbl_16:
-            state.needfij = false;
-            state.repnfunc = state.repnfunc+1;
-            state.repnjac = state.repnjac+1;
-            for(i=0; i<=n-1; i++)
-            {
-                state.internalstate.g[i] = 0;
-            }
-            for(i=0; i<=m-1; i++)
-            {
-                v = 2*state.fi[i];
-                for(i_=0; i_<=n-1;i_++)
-                {
-                    state.internalstate.g[i_] = state.internalstate.g[i_] + v*state.j[i,i_];
-                }
-                state.internalstate.f = state.internalstate.f+math.sqr(state.fi[i]);
-            }
-        lbl_70:
-        lbl_67:
-            goto lbl_65;
-        lbl_66:
-            minlbfgs.minlbfgsresultsbuf(state.internalstate, ref state.xbase, state.internalrep);
-            
-            //
-            // Invalidate DeltaX/DeltaF (control variables used for integrity checks)
-            //
-            state.deltaxready = false;
-            state.deltafready = false;
-        lbl_63:
-            goto lbl_22;
-        lbl_23:
-            
-            //
-            // Lambda is too large, we have to break iterations.
-            //
-            state.repterminationtype = 7;
-            if( !state.xrep )
-            {
-                goto lbl_71;
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.x[i_] = state.xbase[i_];
-            }
-            state.f = state.fbase;
-            clearrequestfields(state);
-            state.xupdated = true;
-            state.rstate.stage = 17;
-            goto lbl_rcomm;
-        lbl_17:
             state.xupdated = false;
-        lbl_71:
+        lbl_61:
             result = false;
             return result;
@@ -4965,4 +12039,78 @@
 
         /*************************************************************************
+        This is obsolete function.
+
+        Since ALGLIB 3.3 it is equivalent to MinLMCreateVJ().
+
+          -- ALGLIB --
+             Copyright 30.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlmcreatevgj(int n,
+            int m,
+            double[] x,
+            minlmstate state)
+        {
+            minlmcreatevj(n, m, x, state);
+        }
+
+
+        /*************************************************************************
+        This is obsolete function.
+
+        Since ALGLIB 3.3 it is equivalent to MinLMCreateFJ().
+
+          -- ALGLIB --
+             Copyright 30.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlmcreatefgj(int n,
+            int m,
+            double[] x,
+            minlmstate state)
+        {
+            minlmcreatefj(n, m, x, state);
+        }
+
+
+        /*************************************************************************
+        This function is considered obsolete since ALGLIB 3.1.0 and is present for
+        backward  compatibility  only.  We  recommend  to use MinLMCreateVJ, which
+        provides similar, but more consistent and feature-rich interface.
+
+          -- ALGLIB --
+             Copyright 30.03.2009 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlmcreatefj(int n,
+            int m,
+            double[] x,
+            minlmstate state)
+        {
+            ap.assert(n>=1, "MinLMCreateFJ: N<1!");
+            ap.assert(m>=1, "MinLMCreateFJ: M<1!");
+            ap.assert(ap.len(x)>=n, "MinLMCreateFJ: Length(X)<N!");
+            ap.assert(apserv.isfinitevector(x, n), "MinLMCreateFJ: X contains infinite or NaN values!");
+            
+            //
+            // initialize
+            //
+            state.n = n;
+            state.m = m;
+            state.algomode = 1;
+            state.hasf = true;
+            state.hasfi = false;
+            state.hasg = false;
+            
+            //
+            // init 2
+            //
+            lmprepare(n, m, true, state);
+            minlmsetacctype(state, 0);
+            minlmsetcond(state, 0, 0, 0, 0);
+            minlmsetxrep(state, false);
+            minlmsetstpmax(state, 0);
+            minlmrestartfrom(state, x);
+        }
+
+
+        /*************************************************************************
         Prepare internal structures (except for RComm).
 
@@ -4990,7 +12138,5 @@
                 state.fibase = new double[m];
                 state.deltaf = new double[m];
-                state.fm2 = new double[m];
                 state.fm1 = new double[m];
-                state.fp2 = new double[m];
                 state.fp1 = new double[m];
             }
@@ -5002,8 +12148,8 @@
             state.deltax = new double[n];
             state.quadraticmodel = new double[n, n];
-            state.dampedmodel = new double[n, n];
             state.xbase = new double[n];
             state.gbase = new double[n];
             state.xdir = new double[n];
+            state.tmp0 = new double[n];
             
             //
@@ -5016,4 +12162,34 @@
             minlbfgs.minlbfgscreate(n, Math.Min(additers, n), state.x, state.internalstate);
             minlbfgs.minlbfgssetcond(state.internalstate, 0.0, 0.0, 0.0, Math.Min(additers, n));
+            
+            //
+            // Prepare internal QP solver
+            //
+            minqp.minqpcreate(n, state.qpstate);
+            minqp.minqpsetalgocholesky(state.qpstate);
+            
+            //
+            // Prepare boundary constraints
+            //
+            state.bndl = new double[n];
+            state.bndu = new double[n];
+            state.havebndl = new bool[n];
+            state.havebndu = new bool[n];
+            for(i=0; i<=n-1; i++)
+            {
+                state.bndl[i] = Double.NegativeInfinity;
+                state.havebndl[i] = false;
+                state.bndu[i] = Double.PositiveInfinity;
+                state.havebndu[i] = false;
+            }
+            
+            //
+            // Prepare scaling matrix
+            //
+            state.s = new double[n];
+            for(i=0; i<=n-1; i++)
+            {
+                state.s[i] = 1.0;
+            }
         }
 
@@ -5083,713 +12259,56 @@
 
 
-    }
-    public class mincg
-    {
-        public class mincgstate
-        {
-            public int n;
-            public double epsg;
-            public double epsf;
-            public double epsx;
-            public int maxits;
-            public double stpmax;
-            public bool xrep;
-            public int cgtype;
-            public int nfev;
-            public int mcstage;
-            public int k;
-            public double[] xk;
-            public double[] dk;
-            public double[] xn;
-            public double[] dn;
-            public double[] d;
-            public double fold;
-            public double stp;
-            public double[] work;
-            public double[] yk;
-            public double laststep;
-            public double[] x;
-            public double f;
-            public double[] g;
-            public bool needfg;
-            public bool xupdated;
-            public rcommstate rstate;
-            public int repiterationscount;
-            public int repnfev;
-            public int repterminationtype;
-            public int debugrestartscount;
-            public linmin.linminstate lstate;
-            public double betahs;
-            public double betady;
-            public mincgstate()
-            {
-                xk = new double[0];
-                dk = new double[0];
-                xn = new double[0];
-                dn = new double[0];
-                d = new double[0];
-                work = new double[0];
-                yk = new double[0];
-                x = new double[0];
-                g = new double[0];
-                rstate = new rcommstate();
-                lstate = new linmin.linminstate();
-            }
-        };
-
-
-        public class mincgreport
-        {
-            public int iterationscount;
-            public int nfev;
-            public int terminationtype;
-        };
-
-
-
-
         /*************************************************************************
-                NONLINEAR CONJUGATE GRADIENT METHOD
-
-        DESCRIPTION:
-        The subroutine minimizes function F(x) of N arguments by using one of  the
-        nonlinear conjugate gradient methods.
-
-        These CG methods are globally convergent (even on non-convex functions) as
-        long as grad(f) is Lipschitz continuous in  a  some  neighborhood  of  the
-        L = { x : f(x)<=f(x0) }.
-
-
-        REQUIREMENTS:
-        Algorithm will request following information during its operation:
-        * function value F and its gradient G (simultaneously) at given point X
-
-
-        USAGE:
-        1. User initializes algorithm state with MinCGCreate() call
-        2. User tunes solver parameters with MinCGSetCond(), MinCGSetStpMax() and
-           other functions
-        3. User calls MinCGOptimize() function which takes algorithm  state   and
-           pointer (delegate, etc.) to callback function which calculates F/G.
-        4. User calls MinCGResults() to get solution
-        5. Optionally, user may call MinCGRestartFrom() to solve another  problem
-           with same N but another starting point and/or another function.
-           MinCGRestartFrom() allows to reuse already initialized structure.
-
-
-        INPUT PARAMETERS:
-            N       -   problem dimension, N>0:
-                        * if given, only leading N elements of X are used
-                        * if not given, automatically determined from size of X
-            X       -   starting point, array[0..N-1].
-
-        OUTPUT PARAMETERS:
-            State   -   structure which stores algorithm state
+        Returns norm of bounded scaled anti-gradient.
+
+        Bounded antigradient is a vector obtained from  anti-gradient  by  zeroing
+        components which point outwards:
+            result = norm(v)
+            v[i]=0     if ((-g[i]<0)and(x[i]=bndl[i])) or
+                          ((-g[i]>0)and(x[i]=bndu[i]))
+            v[i]=-g[i]*s[i] otherwise, where s[i] is a scale for I-th variable
+
+        This function may be used to check a stopping criterion.
 
           -- ALGLIB --
-             Copyright 25.03.2010 by Bochkanov Sergey
+             Copyright 14.01.2011 by Bochkanov Sergey
         *************************************************************************/
-        public static void mincgcreate(int n,
+        private static double boundedscaledantigradnorm(minlmstate state,
             double[] x,
-            mincgstate state)
-        {
-            ap.assert(n>=1, "MinCGCreate: N too small!");
-            ap.assert(ap.len(x)>=n, "MinCGCreate: Length(X)<N!");
-            ap.assert(apserv.isfinitevector(x, n), "MinCGCreate: X contains infinite or NaN values!");
-            
-            //
-            // Initialize
-            //
-            state.n = n;
-            mincgsetcond(state, 0, 0, 0, 0);
-            mincgsetxrep(state, false);
-            mincgsetstpmax(state, 0);
-            mincgsetcgtype(state, -1);
-            state.xk = new double[n];
-            state.dk = new double[n];
-            state.xn = new double[n];
-            state.dn = new double[n];
-            state.x = new double[n];
-            state.d = new double[n];
-            state.g = new double[n];
-            state.work = new double[n];
-            state.yk = new double[n];
-            mincgrestartfrom(state, x);
-        }
-
-
-        /*************************************************************************
-        This function sets stopping conditions for CG optimization algorithm.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            EpsG    -   >=0
-                        The  subroutine  finishes  its  work   if   the  condition
-                        ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                        G - gradient.
-            EpsF    -   >=0
-                        The  subroutine  finishes  its work if on k+1-th iteration
-                        the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
-                        is satisfied.
-            EpsX    -   >=0
-                        The subroutine finishes its work if  on  k+1-th  iteration
-                        the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
-            MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
-                        iterations is unlimited.
-
-        Passing EpsG=0, EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to
-        automatic stopping criterion selection (small EpsX).
-
-          -- ALGLIB --
-             Copyright 02.04.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mincgsetcond(mincgstate state,
-            double epsg,
-            double epsf,
-            double epsx,
-            int maxits)
-        {
-            ap.assert(math.isfinite(epsg), "MinCGSetCond: EpsG is not finite number!");
-            ap.assert((double)(epsg)>=(double)(0), "MinCGSetCond: negative EpsG!");
-            ap.assert(math.isfinite(epsf), "MinCGSetCond: EpsF is not finite number!");
-            ap.assert((double)(epsf)>=(double)(0), "MinCGSetCond: negative EpsF!");
-            ap.assert(math.isfinite(epsx), "MinCGSetCond: EpsX is not finite number!");
-            ap.assert((double)(epsx)>=(double)(0), "MinCGSetCond: negative EpsX!");
-            ap.assert(maxits>=0, "MinCGSetCond: negative MaxIts!");
-            if( (((double)(epsg)==(double)(0) & (double)(epsf)==(double)(0)) & (double)(epsx)==(double)(0)) & maxits==0 )
-            {
-                epsx = 1.0E-6;
-            }
-            state.epsg = epsg;
-            state.epsf = epsf;
-            state.epsx = epsx;
-            state.maxits = maxits;
-        }
-
-
-        /*************************************************************************
-        This function turns on/off reporting.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            NeedXRep-   whether iteration reports are needed or not
-
-        If NeedXRep is True, algorithm will call rep() callback function if  it is
-        provided to MinCGOptimize().
-
-          -- ALGLIB --
-             Copyright 02.04.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mincgsetxrep(mincgstate state,
-            bool needxrep)
-        {
-            state.xrep = needxrep;
-        }
-
-
-        /*************************************************************************
-        This function sets CG algorithm.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            CGType  -   algorithm type:
-                        * -1    automatic selection of the best algorithm
-                        * 0     DY (Dai and Yuan) algorithm
-                        * 1     Hybrid DY-HS algorithm
-
-          -- ALGLIB --
-             Copyright 02.04.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mincgsetcgtype(mincgstate state,
-            int cgtype)
-        {
-            ap.assert(cgtype>=-1 & cgtype<=1, "MinCGSetCGType: incorrect CGType!");
-            if( cgtype==-1 )
-            {
-                cgtype = 1;
-            }
-            state.cgtype = cgtype;
-        }
-
-
-        /*************************************************************************
-        This function sets maximum step length
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
-                        want to limit step length.
-
-        Use this subroutine when you optimize target function which contains exp()
-        or  other  fast  growing  functions,  and optimization algorithm makes too
-        large  steps  which  leads  to overflow. This function allows us to reject
-        steps  that  are  too  large  (and  therefore  expose  us  to the possible
-        overflow) without actually calculating function value at the x+stp*d.
-
-          -- ALGLIB --
-             Copyright 02.04.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mincgsetstpmax(mincgstate state,
-            double stpmax)
-        {
-            ap.assert(math.isfinite(stpmax), "MinCGSetStpMax: StpMax is not finite!");
-            ap.assert((double)(stpmax)>=(double)(0), "MinCGSetStpMax: StpMax<0!");
-            state.stpmax = stpmax;
-        }
-
-
-        /*************************************************************************
-
-          -- ALGLIB --
-             Copyright 20.04.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static bool mincgiteration(mincgstate state)
-        {
-            bool result = new bool();
+            double[] g)
+        {
+            double result = 0;
             int n = 0;
             int i = 0;
-            double betak = 0;
             double v = 0;
-            double vv = 0;
-            int mcinfo = 0;
-            int i_ = 0;
-
-            
-            //
-            // Reverse communication preparations
-            // I know it looks ugly, but it works the same way
-            // anywhere from C++ to Python.
-            //
-            // This code initializes locals by:
-            // * random values determined during code
-            //   generation - on first subroutine call
-            // * values from previous call - on subsequent calls
-            //
-            if( state.rstate.stage>=0 )
-            {
-                n = state.rstate.ia[0];
-                i = state.rstate.ia[1];
-                mcinfo = state.rstate.ia[2];
-                betak = state.rstate.ra[0];
-                v = state.rstate.ra[1];
-                vv = state.rstate.ra[2];
-            }
-            else
-            {
-                n = -983;
-                i = -989;
-                mcinfo = -834;
-                betak = 900;
-                v = -287;
-                vv = 364;
-            }
-            if( state.rstate.stage==0 )
-            {
-                goto lbl_0;
-            }
-            if( state.rstate.stage==1 )
-            {
-                goto lbl_1;
-            }
-            if( state.rstate.stage==2 )
-            {
-                goto lbl_2;
-            }
-            if( state.rstate.stage==3 )
-            {
-                goto lbl_3;
-            }
-            
-            //
-            // Routine body
-            //
-            
-            //
-            // Prepare
-            //
+
+            result = 0;
             n = state.n;
-            state.repterminationtype = 0;
-            state.repiterationscount = 0;
-            state.repnfev = 0;
-            state.debugrestartscount = 0;
-            
-            //
-            // Calculate F/G, initialize algorithm
-            //
-            clearrequestfields(state);
-            state.needfg = true;
-            state.rstate.stage = 0;
-            goto lbl_rcomm;
-        lbl_0:
-            state.needfg = false;
-            if( !state.xrep )
-            {
-                goto lbl_4;
-            }
-            clearrequestfields(state);
-            state.xupdated = true;
-            state.rstate.stage = 1;
-            goto lbl_rcomm;
-        lbl_1:
-            state.xupdated = false;
-        lbl_4:
-            v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.g[i_]*state.g[i_];
-            }
-            v = Math.Sqrt(v);
-            if( (double)(v)==(double)(0) )
-            {
-                state.repterminationtype = 4;
-                result = false;
-                return result;
-            }
-            state.repnfev = 1;
-            state.k = 0;
-            state.fold = state.f;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.xk[i_] = state.x[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.dk[i_] = -state.g[i_];
-            }
-            
-            //
-            // Main cycle
-            //
-        lbl_6:
-            if( false )
-            {
-                goto lbl_7;
-            }
-            
-            //
-            // Store G[k] for later calculation of Y[k]
-            //
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.yk[i_] = -state.g[i_];
-            }
-            
-            //
-            // Calculate X(k+1): minimize F(x+alpha*d)
-            //
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.d[i_] = state.dk[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.x[i_] = state.xk[i_];
-            }
-            state.mcstage = 0;
-            state.stp = 1.0;
-            linmin.linminnormalized(ref state.d, ref state.stp, n);
-            if( (double)(state.laststep)!=(double)(0) )
-            {
-                state.stp = state.laststep;
-            }
-            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.stpmax, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
-        lbl_8:
-            if( state.mcstage==0 )
-            {
-                goto lbl_9;
-            }
-            clearrequestfields(state);
-            state.needfg = true;
-            state.rstate.stage = 2;
-            goto lbl_rcomm;
-        lbl_2:
-            state.needfg = false;
-            linmin.mcsrch(n, ref state.x, ref state.f, ref state.g, state.d, ref state.stp, state.stpmax, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
-            goto lbl_8;
-        lbl_9:
-            if( !state.xrep )
-            {
-                goto lbl_10;
-            }
-            clearrequestfields(state);
-            state.xupdated = true;
-            state.rstate.stage = 3;
-            goto lbl_rcomm;
-        lbl_3:
-            state.xupdated = false;
-        lbl_10:
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.xn[i_] = state.x[i_];
-            }
-            if( mcinfo==1 )
-            {
-                
-                //
-                // Standard Wolfe conditions hold
-                // Calculate Y[K] and BetaK
-                //
-                for(i_=0; i_<=n-1;i_++)
-                {
-                    state.yk[i_] = state.yk[i_] + state.g[i_];
-                }
-                vv = 0.0;
-                for(i_=0; i_<=n-1;i_++)
-                {
-                    vv += state.yk[i_]*state.dk[i_];
-                }
-                v = 0.0;
-                for(i_=0; i_<=n-1;i_++)
-                {
-                    v += state.g[i_]*state.g[i_];
-                }
-                state.betady = v/vv;
-                v = 0.0;
-                for(i_=0; i_<=n-1;i_++)
-                {
-                    v += state.g[i_]*state.yk[i_];
-                }
-                state.betahs = v/vv;
-                if( state.cgtype==0 )
-                {
-                    betak = state.betady;
-                }
-                if( state.cgtype==1 )
-                {
-                    betak = Math.Max(0, Math.Min(state.betady, state.betahs));
-                }
-            }
-            else
-            {
-                
-                //
-                // Something is wrong (may be function is too wild or too flat).
-                //
-                // We'll set BetaK=0, which will restart CG algorithm.
-                // We can stop later (during normal checks) if stopping conditions are met.
-                //
-                betak = 0;
-                state.debugrestartscount = state.debugrestartscount+1;
-            }
-            
-            //
-            // Calculate D(k+1)
-            //
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.dn[i_] = -state.g[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.dn[i_] = state.dn[i_] + betak*state.dk[i_];
-            }
-            
-            //
-            // Update info about step length
-            //
-            v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.d[i_]*state.d[i_];
-            }
-            state.laststep = Math.Sqrt(v)*state.stp;
-            
-            //
-            // Update information and Hessian.
-            // Check stopping conditions.
-            //
-            state.repnfev = state.repnfev+state.nfev;
-            state.repiterationscount = state.repiterationscount+1;
-            if( state.repiterationscount>=state.maxits & state.maxits>0 )
-            {
-                
-                //
-                // Too many iterations
-                //
-                state.repterminationtype = 5;
-                result = false;
-                return result;
-            }
-            v = 0.0;
-            for(i_=0; i_<=n-1;i_++)
-            {
-                v += state.g[i_]*state.g[i_];
-            }
-            if( (double)(Math.Sqrt(v))<=(double)(state.epsg) )
-            {
-                
-                //
-                // Gradient is small enough
-                //
-                state.repterminationtype = 4;
-                result = false;
-                return result;
-            }
-            if( (double)(state.fold-state.f)<=(double)(state.epsf*Math.Max(Math.Abs(state.fold), Math.Max(Math.Abs(state.f), 1.0))) )
-            {
-                
-                //
-                // F(k+1)-F(k) is small enough
-                //
-                state.repterminationtype = 1;
-                result = false;
-                return result;
-            }
-            if( (double)(state.laststep)<=(double)(state.epsx) )
-            {
-                
-                //
-                // X(k+1)-X(k) is small enough
-                //
-                state.repterminationtype = 2;
-                result = false;
-                return result;
-            }
-            
-            //
-            // Shift Xk/Dk, update other information
-            //
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.xk[i_] = state.xn[i_];
-            }
-            for(i_=0; i_<=n-1;i_++)
-            {
-                state.dk[i_] = state.dn[i_];
-            }
-            state.fold = state.f;
-            state.k = state.k+1;
-            goto lbl_6;
-        lbl_7:
-            result = false;
+            for(i=0; i<=n-1; i++)
+            {
+                v = -(g[i]*state.s[i]);
+                if( state.havebndl[i] )
+                {
+                    if( (double)(x[i])<=(double)(state.bndl[i]) & (double)(-g[i])<(double)(0) )
+                    {
+                        v = 0;
+                    }
+                }
+                if( state.havebndu[i] )
+                {
+                    if( (double)(x[i])>=(double)(state.bndu[i]) & (double)(-g[i])>(double)(0) )
+                    {
+                        v = 0;
+                    }
+                }
+                result = result+math.sqr(v);
+            }
+            result = Math.Sqrt(result);
             return result;
-            
-            //
-            // Saving state
-            //
-        lbl_rcomm:
-            result = true;
-            state.rstate.ia[0] = n;
-            state.rstate.ia[1] = i;
-            state.rstate.ia[2] = mcinfo;
-            state.rstate.ra[0] = betak;
-            state.rstate.ra[1] = v;
-            state.rstate.ra[2] = vv;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Conjugate gradient results
-
-        INPUT PARAMETERS:
-            State   -   algorithm state
-
-        OUTPUT PARAMETERS:
-            X       -   array[0..N-1], solution
-            Rep     -   optimization report:
-                        * Rep.TerminationType completetion code:
-                            * -2    rounding errors prevent further improvement.
-                                    X contains best point found.
-                            * -1    incorrect parameters were specified
-                            *  1    relative function improvement is no more than
-                                    EpsF.
-                            *  2    relative step is no more than EpsX.
-                            *  4    gradient norm is no more than EpsG
-                            *  5    MaxIts steps was taken
-                            *  7    stopping conditions are too stringent,
-                                    further improvement is impossible
-                        * Rep.IterationsCount contains iterations count
-                        * NFEV countains number of function calculations
-
-          -- ALGLIB --
-             Copyright 20.04.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mincgresults(mincgstate state,
-            ref double[] x,
-            mincgreport rep)
-        {
-            x = new double[0];
-
-            mincgresultsbuf(state, ref x, rep);
-        }
-
-
-        /*************************************************************************
-        Conjugate gradient results
-
-        Buffered implementation of MinCGResults(), which uses pre-allocated buffer
-        to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
-        intended to be used in the inner cycles of performance critical algorithms
-        where array reallocation penalty is too large to be ignored.
-
-          -- ALGLIB --
-             Copyright 20.04.2009 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mincgresultsbuf(mincgstate state,
-            ref double[] x,
-            mincgreport rep)
-        {
-            int i_ = 0;
-
-            if( ap.len(x)<state.n )
-            {
-                x = new double[state.n];
-            }
-            for(i_=0; i_<=state.n-1;i_++)
-            {
-                x[i_] = state.xn[i_];
-            }
-            rep.iterationscount = state.repiterationscount;
-            rep.nfev = state.repnfev;
-            rep.terminationtype = state.repterminationtype;
-        }
-
-
-        /*************************************************************************
-        This  subroutine  restarts  CG  algorithm from new point. All optimization
-        parameters are left unchanged.
-
-        This  function  allows  to  solve multiple  optimization  problems  (which
-        must have same number of dimensions) without object reallocation penalty.
-
-        INPUT PARAMETERS:
-            State   -   structure used to store algorithm state.
-            X       -   new starting point.
-
-          -- ALGLIB --
-             Copyright 30.07.2010 by Bochkanov Sergey
-        *************************************************************************/
-        public static void mincgrestartfrom(mincgstate state,
-            double[] x)
-        {
-            int i_ = 0;
-
-            ap.assert(ap.len(x)>=state.n, "MinCGRestartFrom: Length(X)<N!");
-            ap.assert(apserv.isfinitevector(x, state.n), "MinCGCreate: X contains infinite or NaN values!");
-            for(i_=0; i_<=state.n-1;i_++)
-            {
-                state.x[i_] = x[i_];
-            }
-            state.laststep = 0;
-            state.rstate.ia = new int[2+1];
-            state.rstate.ra = new double[2+1];
-            state.rstate.stage = -1;
-            clearrequestfields(state);
-        }
-
-
-        /*************************************************************************
-        Clears request fileds (to be sure that we don't forgot to clear something)
-        *************************************************************************/
-        private static void clearrequestfields(mincgstate state)
-        {
-            state.needfg = false;
-            state.xupdated = false;
         }
 
 
     }
-    public class minasa
+    public class mincomp
     {
         public class minasastate
@@ -5875,4 +12394,5 @@
         public const int n2 = 2;
         public const double stpmin = 1.0E-300;
+        public const double gtol = 0.3;
         public const double gpaftol = 0.0001;
         public const double gpadecay = 0.5;
@@ -5881,60 +12401,58 @@
 
         /*************************************************************************
-                      NONLINEAR BOUND CONSTRAINED OPTIMIZATION USING
-                              MODIFIED ACTIVE SET ALGORITHM
-                           WILLIAM W. HAGER AND HONGCHAO ZHANG
-
-        DESCRIPTION:
-        The  subroutine  minimizes  function  F(x)  of  N  arguments  with   bound
-        constraints: BndL[i] <= x[i] <= BndU[i]
-
-        This method is  globally  convergent  as  long  as  grad(f)  is  Lipschitz
-        continuous on a level set: L = { x : f(x)<=f(x0) }.
-
-
-        REQUIREMENTS:
-        Algorithm will request following information during its operation:
-        * function value F and its gradient G (simultaneously) at given point X
-
-
-        USAGE:
-        1. User initializes algorithm state with MinASACreate() call
-        2. User tunes solver parameters with MinASASetCond() MinASASetStpMax() and
-           other functions
-        3. User calls MinASAOptimize() function which takes algorithm  state   and
-           pointer (delegate, etc.) to callback function which calculates F/G.
-        4. User calls MinASAResults() to get solution
-        5. Optionally, user may call MinASARestartFrom() to solve another  problem
-           with same N but another starting point and/or another function.
-           MinASARestartFrom() allows to reuse already initialized structure.
-
-
-        INPUT PARAMETERS:
-            N       -   problem dimension, N>0:
-                        * if given, only leading N elements of X are used
-                        * if not given, automatically determined from sizes of
-                          X/BndL/BndU.
-            X       -   starting point, array[0..N-1].
-            BndL    -   lower bounds, array[0..N-1].
-                        all elements MUST be specified,  i.e.  all  variables  are
-                        bounded. However, if some (all) variables  are  unbounded,
-                        you may specify very small number as bound: -1000,  -1.0E6
-                        or -1.0E300, or something like that.
-            BndU    -   upper bounds, array[0..N-1].
-                        all elements MUST be specified,  i.e.  all  variables  are
-                        bounded. However, if some (all) variables  are  unbounded,
-                        you may specify very large number as bound: +1000,  +1.0E6
-                        or +1.0E300, or something like that.
-
-        OUTPUT PARAMETERS:
-            State   -   structure stores algorithm state
-
-        NOTES:
-
-        1. you may tune stopping conditions with MinASASetCond() function
-        2. if target function contains exp() or other fast growing functions,  and
-           optimization algorithm makes too large steps which leads  to  overflow,
-           use MinASASetStpMax() function to bound algorithm's steps.
-        3. this function does NOT support infinite/NaN values in X, BndL, BndU.
+        Obsolete function, use MinLBFGSSetPrecDefault() instead.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlbfgssetdefaultpreconditioner(minlbfgs.minlbfgsstate state)
+        {
+            minlbfgs.minlbfgssetprecdefault(state);
+        }
+
+
+        /*************************************************************************
+        Obsolete function, use MinLBFGSSetCholeskyPreconditioner() instead.
+
+          -- ALGLIB --
+             Copyright 13.10.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minlbfgssetcholeskypreconditioner(minlbfgs.minlbfgsstate state,
+            double[,] p,
+            bool isupper)
+        {
+            minlbfgs.minlbfgssetpreccholesky(state, p, isupper);
+        }
+
+
+        /*************************************************************************
+        This is obsolete function which was used by previous version of the  BLEIC
+        optimizer. It does nothing in the current version of BLEIC.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetbarrierwidth(minbleic.minbleicstate state,
+            double mu)
+        {
+        }
+
+
+        /*************************************************************************
+        This is obsolete function which was used by previous version of the  BLEIC
+        optimizer. It does nothing in the current version of BLEIC.
+
+          -- ALGLIB --
+             Copyright 28.11.2010 by Bochkanov Sergey
+        *************************************************************************/
+        public static void minbleicsetbarrierdecay(minbleic.minbleicstate state,
+            double mudecay)
+        {
+        }
+
+
+        /*************************************************************************
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
@@ -5990,24 +12508,6 @@
 
         /*************************************************************************
-        This function sets stopping conditions for the ASA optimization algorithm.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            EpsG    -   >=0
-                        The  subroutine  finishes  its  work   if   the  condition
-                        ||G||<EpsG is satisfied, where ||.|| means Euclidian norm,
-                        G - gradient.
-            EpsF    -   >=0
-                        The  subroutine  finishes  its work if on k+1-th iteration
-                        the  condition  |F(k+1)-F(k)|<=EpsF*max{|F(k)|,|F(k+1)|,1}
-                        is satisfied.
-            EpsX    -   >=0
-                        The subroutine finishes its work if  on  k+1-th  iteration
-                        the condition |X(k+1)-X(k)| <= EpsX is fulfilled.
-            MaxIts  -   maximum number of iterations. If MaxIts=0, the  number  of
-                        iterations is unlimited.
-
-        Passing EpsG=0, EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to
-        automatic stopping criterion selection (small EpsX).
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
@@ -6039,12 +12539,6 @@
 
         /*************************************************************************
-        This function turns on/off reporting.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            NeedXRep-   whether iteration reports are needed or not
-
-        If NeedXRep is True, algorithm will call rep() callback function if  it is
-        provided to MinASAOptimize().
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
@@ -6059,12 +12553,6 @@
 
         /*************************************************************************
-        This function sets optimization algorithm.
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm stat
-            UAType  -   algorithm type:
-                        * -1    automatic selection of the best algorithm
-                        * 0     DY (Dai and Yuan) algorithm
-                        * 1     Hybrid DY-HS algorithm
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
@@ -6084,16 +12572,6 @@
 
         /*************************************************************************
-        This function sets maximum step length
-
-        INPUT PARAMETERS:
-            State   -   structure which stores algorithm state
-            StpMax  -   maximum step length, >=0. Set StpMax to 0.0,  if you don't
-                        want to limit step length (zero by default).
-
-        Use this subroutine when you optimize target function which contains exp()
-        or  other  fast  growing  functions,  and optimization algorithm makes too
-        large  steps  which  leads  to overflow. This function allows us to reject
-        steps  that  are  too  large  (and  therefore  expose  us  to the possible
-        overflow) without actually calculating function value at the x+stp*d.
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
@@ -6703,5 +13181,5 @@
                 state.stp = state.laststep;
             }
-            linmin.mcsrch(n, ref state.xn, ref state.f, ref state.gc, state.d, ref state.stp, state.stpmax, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
+            linmin.mcsrch(n, ref state.xn, ref state.f, ref state.gc, state.d, ref state.stp, state.stpmax, gtol, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
         lbl_51:
             if( state.mcstage==0 )
@@ -6744,5 +13222,5 @@
                 }
             }
-            linmin.mcsrch(n, ref state.xn, ref state.f, ref state.gc, state.d, ref state.stp, state.stpmax, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
+            linmin.mcsrch(n, ref state.xn, ref state.f, ref state.gc, state.d, ref state.stp, state.stpmax, gtol, ref mcinfo, ref state.nfev, ref state.work, state.lstate, ref state.mcstage);
             goto lbl_51;
         lbl_52:
@@ -7047,26 +13525,6 @@
 
         /*************************************************************************
-        ASA results
-
-        INPUT PARAMETERS:
-            State   -   algorithm state
-
-        OUTPUT PARAMETERS:
-            X       -   array[0..N-1], solution
-            Rep     -   optimization report:
-                        * Rep.TerminationType completetion code:
-                            * -2    rounding errors prevent further improvement.
-                                    X contains best point found.
-                            * -1    incorrect parameters were specified
-                            *  1    relative function improvement is no more than
-                                    EpsF.
-                            *  2    relative step is no more than EpsX.
-                            *  4    gradient norm is no more than EpsG
-                            *  5    MaxIts steps was taken
-                            *  7    stopping conditions are too stringent,
-                                    further improvement is impossible
-                        * Rep.IterationsCount contains iterations count
-                        * NFEV countains number of function calculations
-                        * ActiveConstraints contains number of active constraints
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
@@ -7084,10 +13542,6 @@
 
         /*************************************************************************
-        ASA results
-
-        Buffered implementation of MinASAResults() which uses pre-allocated buffer
-        to store X[]. If buffer size is  too  small,  it  resizes  buffer.  It  is
-        intended to be used in the inner cycles of performance critical algorithms
-        where array reallocation penalty is too large to be ignored.
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
@@ -7124,15 +13578,6 @@
 
         /*************************************************************************
-        This  subroutine  restarts  CG  algorithm from new point. All optimization
-        parameters are left unchanged.
-
-        This  function  allows  to  solve multiple  optimization  problems  (which
-        must have same number of dimensions) without object reallocation penalty.
-
-        INPUT PARAMETERS:
-            State   -   structure previously allocated with MinCGCreate call.
-            X       -   new starting point.
-            BndL    -   new lower bounds
-            BndU    -   new upper bounds
+        Obsolete optimization algorithm.
+        Was replaced by MinBLEIC subpackage.
 
           -- ALGLIB --
Index: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/specialfunctions.cs
===================================================================
--- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.1.0/ALGLIB-3.1.0/specialfunctions.cs	(revision 6866)
+++ trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/specialfunctions.cs	(revision 7294)
@@ -790,508 +790,4 @@
 
     /*************************************************************************
-    Calculation of the value of the Chebyshev polynomials of the
-    first and second kinds.
-
-    Parameters:
-        r   -   polynomial kind, either 1 or 2.
-        n   -   degree, n>=0
-        x   -   argument, -1 <= x <= 1
-
-    Result:
-        the value of the Chebyshev polynomial at x
-    *************************************************************************/
-    public static double chebyshevcalculate(int r, int n, double x)
-    {
-
-        double result = chebyshev.chebyshevcalculate(r, n, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Summation of Chebyshev polynomials using Clenshaws recurrence formula.
-
-    This routine calculates
-        c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
-    or
-        c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
-    depending on the R.
-
-    Parameters:
-        r   -   polynomial kind, either 1 or 2.
-        n   -   degree, n>=0
-        x   -   argument
-
-    Result:
-        the value of the Chebyshev polynomial at x
-    *************************************************************************/
-    public static double chebyshevsum(double[] c, int r, int n, double x)
-    {
-
-        double result = chebyshev.chebyshevsum(c, r, n, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
-
-    Input parameters:
-        N   -   polynomial degree, n>=0
-
-    Output parameters:
-        C   -   coefficients
-    *************************************************************************/
-    public static void chebyshevcoefficients(int n, out double[] c)
-    {
-        c = new double[0];
-        chebyshev.chebyshevcoefficients(n, ref c);
-        return;
-    }
-
-    /*************************************************************************
-    Conversion of a series of Chebyshev polynomials to a power series.
-
-    Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
-    B[0] + B[1]*X + ... + B[N]*X^N.
-
-    Input parameters:
-        A   -   Chebyshev series coefficients
-        N   -   degree, N>=0
-
-    Output parameters
-        B   -   power series coefficients
-    *************************************************************************/
-    public static void fromchebyshev(double[] a, int n, out double[] b)
-    {
-        b = new double[0];
-        chebyshev.fromchebyshev(a, n, ref b);
-        return;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Dawson's Integral
-
-    Approximates the integral
-
-                                x
-                                -
-                         2     | |        2
-     dawsn(x)  =  exp( -x  )   |    exp( t  ) dt
-                             | |
-                              -
-                              0
-
-    Three different rational approximations are employed, for
-    the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
-
-    ACCURACY:
-
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE      0,10        10000       6.9e-16     1.0e-16
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double dawsonintegral(double x)
-    {
-
-        double result = dawson.dawsonintegral(x);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Complete elliptic integral of the first kind
-
-    Approximates the integral
-
-
-
-               pi/2
-                -
-               | |
-               |           dt
-    K(m)  =    |    ------------------
-               |                   2
-             | |    sqrt( 1 - m sin t )
-              -
-               0
-
-    using the approximation
-
-        P(x)  -  log x Q(x).
-
-    ACCURACY:
-
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE       0,1        30000       2.5e-16     6.8e-17
-
-    Cephes Math Library, Release 2.8:  June, 2000
-    Copyright 1984, 1987, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double ellipticintegralk(double m)
-    {
-
-        double result = elliptic.ellipticintegralk(m);
-        return result;
-    }
-
-    /*************************************************************************
-    Complete elliptic integral of the first kind
-
-    Approximates the integral
-
-
-
-               pi/2
-                -
-               | |
-               |           dt
-    K(m)  =    |    ------------------
-               |                   2
-             | |    sqrt( 1 - m sin t )
-              -
-               0
-
-    where m = 1 - m1, using the approximation
-
-        P(x)  -  log x Q(x).
-
-    The argument m1 is used rather than m so that the logarithmic
-    singularity at m = 1 will be shifted to the origin; this
-    preserves maximum accuracy.
-
-    K(0) = pi/2.
-
-    ACCURACY:
-
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE       0,1        30000       2.5e-16     6.8e-17
-
-    Àëãîðèòì âçÿò èç áèáëèîòåêè Cephes
-    *************************************************************************/
-    public static double ellipticintegralkhighprecision(double m1)
-    {
-
-        double result = elliptic.ellipticintegralkhighprecision(m1);
-        return result;
-    }
-
-    /*************************************************************************
-    Incomplete elliptic integral of the first kind F(phi|m)
-
-    Approximates the integral
-
-
-
-                   phi
-                    -
-                   | |
-                   |           dt
-    F(phi_\m)  =    |    ------------------
-                   |                   2
-                 | |    sqrt( 1 - m sin t )
-                  -
-                   0
-
-    of amplitude phi and modulus m, using the arithmetic -
-    geometric mean algorithm.
-
-
-
-
-    ACCURACY:
-
-    Tested at random points with m in [0, 1] and phi as indicated.
-
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE     -10,10       200000      7.4e-16     1.0e-16
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double incompleteellipticintegralk(double phi, double m)
-    {
-
-        double result = elliptic.incompleteellipticintegralk(phi, m);
-        return result;
-    }
-
-    /*************************************************************************
-    Complete elliptic integral of the second kind
-
-    Approximates the integral
-
-
-               pi/2
-                -
-               | |                 2
-    E(m)  =    |    sqrt( 1 - m sin t ) dt
-             | |
-              -
-               0
-
-    using the approximation
-
-         P(x)  -  x log x Q(x).
-
-    ACCURACY:
-
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE       0, 1       10000       2.1e-16     7.3e-17
-
-    Cephes Math Library, Release 2.8: June, 2000
-    Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double ellipticintegrale(double m)
-    {
-
-        double result = elliptic.ellipticintegrale(m);
-        return result;
-    }
-
-    /*************************************************************************
-    Incomplete elliptic integral of the second kind
-
-    Approximates the integral
-
-
-                   phi
-                    -
-                   | |
-                   |                   2
-    E(phi_\m)  =    |    sqrt( 1 - m sin t ) dt
-                   |
-                 | |
-                  -
-                   0
-
-    of amplitude phi and modulus m, using the arithmetic -
-    geometric mean algorithm.
-
-    ACCURACY:
-
-    Tested at random arguments with phi in [-10, 10] and m in
-    [0, 1].
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE     -10,10      150000       3.3e-15     1.4e-16
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double incompleteellipticintegrale(double phi, double m)
-    {
-
-        double result = elliptic.incompleteellipticintegrale(phi, m);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Exponential integral Ei(x)
-
-                  x
-                   -     t
-                  | |   e
-       Ei(x) =   -|-   ---  dt .
-                | |     t
-                 -
-                -inf
-
-    Not defined for x <= 0.
-    See also expn.c.
-
-
-
-    ACCURACY:
-
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE       0,100       50000      8.6e-16     1.3e-16
-
-    Cephes Math Library Release 2.8:  May, 1999
-    Copyright 1999 by Stephen L. Moshier
-    *************************************************************************/
-    public static double exponentialintegralei(double x)
-    {
-
-        double result = expintegrals.exponentialintegralei(x);
-        return result;
-    }
-
-    /*************************************************************************
-    Exponential integral En(x)
-
-    Evaluates the exponential integral
-
-                    inf.
-                      -
-                     | |   -xt
-                     |    e
-         E (x)  =    |    ----  dt.
-          n          |      n
-                   | |     t
-                    -
-                     1
-
-
-    Both n and x must be nonnegative.
-
-    The routine employs either a power series, a continued
-    fraction, or an asymptotic formula depending on the
-    relative values of n and x.
-
-    ACCURACY:
-
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE      0, 30       10000       1.7e-15     3.6e-16
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1985, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double exponentialintegralen(double x, int n)
-    {
-
-        double result = expintegrals.exponentialintegralen(x, n);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Fresnel integral
-
-    Evaluates the Fresnel integrals
-
-              x
-              -
-             | |
-    C(x) =   |   cos(pi/2 t**2) dt,
-           | |
-            -
-             0
-
-              x
-              -
-             | |
-    S(x) =   |   sin(pi/2 t**2) dt.
-           | |
-            -
-             0
-
-
-    The integrals are evaluated by a power series for x < 1.
-    For x >= 1 auxiliary functions f(x) and g(x) are employed
-    such that
-
-    C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
-    S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
-
-
-
-    ACCURACY:
-
-     Relative error.
-
-    Arithmetic  function   domain     # trials      peak         rms
-      IEEE       S(x)      0, 10       10000       2.0e-15     3.2e-16
-      IEEE       C(x)      0, 10       10000       1.8e-15     3.3e-16
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static void fresnelintegral(double x, ref double c, ref double s)
-    {
-
-        fresnel.fresnelintegral(x, ref c, ref s);
-        return;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Calculation of the value of the Hermite polynomial.
-
-    Parameters:
-        n   -   degree, n>=0
-        x   -   argument
-
-    Result:
-        the value of the Hermite polynomial Hn at x
-    *************************************************************************/
-    public static double hermitecalculate(int n, double x)
-    {
-
-        double result = hermite.hermitecalculate(n, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Summation of Hermite polynomials using Clenshaws recurrence formula.
-
-    This routine calculates
-        c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
-
-    Parameters:
-        n   -   degree, n>=0
-        x   -   argument
-
-    Result:
-        the value of the Hermite polynomial at x
-    *************************************************************************/
-    public static double hermitesum(double[] c, int n, double x)
-    {
-
-        double result = hermite.hermitesum(c, n, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
-
-    Input parameters:
-        N   -   polynomial degree, n>=0
-
-    Output parameters:
-        C   -   coefficients
-    *************************************************************************/
-    public static void hermitecoefficients(int n, out double[] c)
-    {
-        c = new double[0];
-        hermite.hermitecoefficients(n, ref c);
-        return;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
     Incomplete beta integral
 
@@ -1374,4 +870,865 @@
         double result = ibetaf.invincompletebeta(a, b, y);
         return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Binomial distribution
+
+    Returns the sum of the terms 0 through k of the Binomial
+    probability density:
+
+      k
+      --  ( n )   j      n-j
+      >   (   )  p  (1-p)
+      --  ( j )
+     j=0
+
+    The terms are not summed directly; instead the incomplete
+    beta integral is employed, according to the formula
+
+    y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
+
+    The arguments must be positive, with p ranging from 0 to 1.
+
+    ACCURACY:
+
+    Tested at random points (a,b,p), with p between 0 and 1.
+
+                  a,b                     Relative error:
+    arithmetic  domain     # trials      peak         rms
+     For p between 0.001 and 1:
+       IEEE     0,100       100000      4.3e-15     2.6e-16
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double binomialdistribution(int k, int n, double p)
+    {
+
+        double result = binomialdistr.binomialdistribution(k, n, p);
+        return result;
+    }
+
+    /*************************************************************************
+    Complemented binomial distribution
+
+    Returns the sum of the terms k+1 through n of the Binomial
+    probability density:
+
+      n
+      --  ( n )   j      n-j
+      >   (   )  p  (1-p)
+      --  ( j )
+     j=k+1
+
+    The terms are not summed directly; instead the incomplete
+    beta integral is employed, according to the formula
+
+    y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
+
+    The arguments must be positive, with p ranging from 0 to 1.
+
+    ACCURACY:
+
+    Tested at random points (a,b,p).
+
+                  a,b                     Relative error:
+    arithmetic  domain     # trials      peak         rms
+     For p between 0.001 and 1:
+       IEEE     0,100       100000      6.7e-15     8.2e-16
+     For p between 0 and .001:
+       IEEE     0,100       100000      1.5e-13     2.7e-15
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double binomialcdistribution(int k, int n, double p)
+    {
+
+        double result = binomialdistr.binomialcdistribution(k, n, p);
+        return result;
+    }
+
+    /*************************************************************************
+    Inverse binomial distribution
+
+    Finds the event probability p such that the sum of the
+    terms 0 through k of the Binomial probability density
+    is equal to the given cumulative probability y.
+
+    This is accomplished using the inverse beta integral
+    function and the relation
+
+    1 - p = incbi( n-k, k+1, y ).
+
+    ACCURACY:
+
+    Tested at random points (a,b,p).
+
+                  a,b                     Relative error:
+    arithmetic  domain     # trials      peak         rms
+     For p between 0.001 and 1:
+       IEEE     0,100       100000      2.3e-14     6.4e-16
+       IEEE     0,10000     100000      6.6e-12     1.2e-13
+     For p between 10^-6 and 0.001:
+       IEEE     0,100       100000      2.0e-12     1.3e-14
+       IEEE     0,10000     100000      1.5e-12     3.2e-14
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double invbinomialdistribution(int k, int n, double y)
+    {
+
+        double result = binomialdistr.invbinomialdistribution(k, n, y);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Calculation of the value of the Chebyshev polynomials of the
+    first and second kinds.
+
+    Parameters:
+        r   -   polynomial kind, either 1 or 2.
+        n   -   degree, n>=0
+        x   -   argument, -1 <= x <= 1
+
+    Result:
+        the value of the Chebyshev polynomial at x
+    *************************************************************************/
+    public static double chebyshevcalculate(int r, int n, double x)
+    {
+
+        double result = chebyshev.chebyshevcalculate(r, n, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Summation of Chebyshev polynomials using Clenshaws recurrence formula.
+
+    This routine calculates
+        c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
+    or
+        c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
+    depending on the R.
+
+    Parameters:
+        r   -   polynomial kind, either 1 or 2.
+        n   -   degree, n>=0
+        x   -   argument
+
+    Result:
+        the value of the Chebyshev polynomial at x
+    *************************************************************************/
+    public static double chebyshevsum(double[] c, int r, int n, double x)
+    {
+
+        double result = chebyshev.chebyshevsum(c, r, n, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
+
+    Input parameters:
+        N   -   polynomial degree, n>=0
+
+    Output parameters:
+        C   -   coefficients
+    *************************************************************************/
+    public static void chebyshevcoefficients(int n, out double[] c)
+    {
+        c = new double[0];
+        chebyshev.chebyshevcoefficients(n, ref c);
+        return;
+    }
+
+    /*************************************************************************
+    Conversion of a series of Chebyshev polynomials to a power series.
+
+    Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
+    B[0] + B[1]*X + ... + B[N]*X^N.
+
+    Input parameters:
+        A   -   Chebyshev series coefficients
+        N   -   degree, N>=0
+
+    Output parameters
+        B   -   power series coefficients
+    *************************************************************************/
+    public static void fromchebyshev(double[] a, int n, out double[] b)
+    {
+        b = new double[0];
+        chebyshev.fromchebyshev(a, n, ref b);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Chi-square distribution
+
+    Returns the area under the left hand tail (from 0 to x)
+    of the Chi square probability density function with
+    v degrees of freedom.
+
+
+                                      x
+                                       -
+                           1          | |  v/2-1  -t/2
+     P( x | v )   =   -----------     |   t      e     dt
+                       v/2  -       | |
+                      2    | (v/2)   -
+                                      0
+
+    where x is the Chi-square variable.
+
+    The incomplete gamma integral is used, according to the
+    formula
+
+    y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
+
+    The arguments must both be positive.
+
+    ACCURACY:
+
+    See incomplete gamma function
+
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double chisquaredistribution(double v, double x)
+    {
+
+        double result = chisquaredistr.chisquaredistribution(v, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Complemented Chi-square distribution
+
+    Returns the area under the right hand tail (from x to
+    infinity) of the Chi square probability density function
+    with v degrees of freedom:
+
+                                     inf.
+                                       -
+                           1          | |  v/2-1  -t/2
+     P( x | v )   =   -----------     |   t      e     dt
+                       v/2  -       | |
+                      2    | (v/2)   -
+                                      x
+
+    where x is the Chi-square variable.
+
+    The incomplete gamma integral is used, according to the
+    formula
+
+    y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
+
+    The arguments must both be positive.
+
+    ACCURACY:
+
+    See incomplete gamma function
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double chisquarecdistribution(double v, double x)
+    {
+
+        double result = chisquaredistr.chisquarecdistribution(v, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Inverse of complemented Chi-square distribution
+
+    Finds the Chi-square argument x such that the integral
+    from x to infinity of the Chi-square density is equal
+    to the given cumulative probability y.
+
+    This is accomplished using the inverse gamma integral
+    function and the relation
+
+       x/2 = igami( df/2, y );
+
+    ACCURACY:
+
+    See inverse incomplete gamma function
+
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double invchisquaredistribution(double v, double y)
+    {
+
+        double result = chisquaredistr.invchisquaredistribution(v, y);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Dawson's Integral
+
+    Approximates the integral
+
+                                x
+                                -
+                         2     | |        2
+     dawsn(x)  =  exp( -x  )   |    exp( t  ) dt
+                             | |
+                              -
+                              0
+
+    Three different rational approximations are employed, for
+    the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
+
+    ACCURACY:
+
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE      0,10        10000       6.9e-16     1.0e-16
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double dawsonintegral(double x)
+    {
+
+        double result = dawson.dawsonintegral(x);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Complete elliptic integral of the first kind
+
+    Approximates the integral
+
+
+
+               pi/2
+                -
+               | |
+               |           dt
+    K(m)  =    |    ------------------
+               |                   2
+             | |    sqrt( 1 - m sin t )
+              -
+               0
+
+    using the approximation
+
+        P(x)  -  log x Q(x).
+
+    ACCURACY:
+
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE       0,1        30000       2.5e-16     6.8e-17
+
+    Cephes Math Library, Release 2.8:  June, 2000
+    Copyright 1984, 1987, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double ellipticintegralk(double m)
+    {
+
+        double result = elliptic.ellipticintegralk(m);
+        return result;
+    }
+
+    /*************************************************************************
+    Complete elliptic integral of the first kind
+
+    Approximates the integral
+
+
+
+               pi/2
+                -
+               | |
+               |           dt
+    K(m)  =    |    ------------------
+               |                   2
+             | |    sqrt( 1 - m sin t )
+              -
+               0
+
+    where m = 1 - m1, using the approximation
+
+        P(x)  -  log x Q(x).
+
+    The argument m1 is used rather than m so that the logarithmic
+    singularity at m = 1 will be shifted to the origin; this
+    preserves maximum accuracy.
+
+    K(0) = pi/2.
+
+    ACCURACY:
+
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE       0,1        30000       2.5e-16     6.8e-17
+
+    Àëãîðèòì âçÿò èç áèáëèîòåêè Cephes
+    *************************************************************************/
+    public static double ellipticintegralkhighprecision(double m1)
+    {
+
+        double result = elliptic.ellipticintegralkhighprecision(m1);
+        return result;
+    }
+
+    /*************************************************************************
+    Incomplete elliptic integral of the first kind F(phi|m)
+
+    Approximates the integral
+
+
+
+                   phi
+                    -
+                   | |
+                   |           dt
+    F(phi_\m)  =    |    ------------------
+                   |                   2
+                 | |    sqrt( 1 - m sin t )
+                  -
+                   0
+
+    of amplitude phi and modulus m, using the arithmetic -
+    geometric mean algorithm.
+
+
+
+
+    ACCURACY:
+
+    Tested at random points with m in [0, 1] and phi as indicated.
+
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE     -10,10       200000      7.4e-16     1.0e-16
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double incompleteellipticintegralk(double phi, double m)
+    {
+
+        double result = elliptic.incompleteellipticintegralk(phi, m);
+        return result;
+    }
+
+    /*************************************************************************
+    Complete elliptic integral of the second kind
+
+    Approximates the integral
+
+
+               pi/2
+                -
+               | |                 2
+    E(m)  =    |    sqrt( 1 - m sin t ) dt
+             | |
+              -
+               0
+
+    using the approximation
+
+         P(x)  -  x log x Q(x).
+
+    ACCURACY:
+
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE       0, 1       10000       2.1e-16     7.3e-17
+
+    Cephes Math Library, Release 2.8: June, 2000
+    Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double ellipticintegrale(double m)
+    {
+
+        double result = elliptic.ellipticintegrale(m);
+        return result;
+    }
+
+    /*************************************************************************
+    Incomplete elliptic integral of the second kind
+
+    Approximates the integral
+
+
+                   phi
+                    -
+                   | |
+                   |                   2
+    E(phi_\m)  =    |    sqrt( 1 - m sin t ) dt
+                   |
+                 | |
+                  -
+                   0
+
+    of amplitude phi and modulus m, using the arithmetic -
+    geometric mean algorithm.
+
+    ACCURACY:
+
+    Tested at random arguments with phi in [-10, 10] and m in
+    [0, 1].
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE     -10,10      150000       3.3e-15     1.4e-16
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double incompleteellipticintegrale(double phi, double m)
+    {
+
+        double result = elliptic.incompleteellipticintegrale(phi, m);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Exponential integral Ei(x)
+
+                  x
+                   -     t
+                  | |   e
+       Ei(x) =   -|-   ---  dt .
+                | |     t
+                 -
+                -inf
+
+    Not defined for x <= 0.
+    See also expn.c.
+
+
+
+    ACCURACY:
+
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE       0,100       50000      8.6e-16     1.3e-16
+
+    Cephes Math Library Release 2.8:  May, 1999
+    Copyright 1999 by Stephen L. Moshier
+    *************************************************************************/
+    public static double exponentialintegralei(double x)
+    {
+
+        double result = expintegrals.exponentialintegralei(x);
+        return result;
+    }
+
+    /*************************************************************************
+    Exponential integral En(x)
+
+    Evaluates the exponential integral
+
+                    inf.
+                      -
+                     | |   -xt
+                     |    e
+         E (x)  =    |    ----  dt.
+          n          |      n
+                   | |     t
+                    -
+                     1
+
+
+    Both n and x must be nonnegative.
+
+    The routine employs either a power series, a continued
+    fraction, or an asymptotic formula depending on the
+    relative values of n and x.
+
+    ACCURACY:
+
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE      0, 30       10000       1.7e-15     3.6e-16
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1985, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double exponentialintegralen(double x, int n)
+    {
+
+        double result = expintegrals.exponentialintegralen(x, n);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    F distribution
+
+    Returns the area from zero to x under the F density
+    function (also known as Snedcor's density or the
+    variance ratio density).  This is the density
+    of x = (u1/df1)/(u2/df2), where u1 and u2 are random
+    variables having Chi square distributions with df1
+    and df2 degrees of freedom, respectively.
+    The incomplete beta integral is used, according to the
+    formula
+
+    P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
+
+
+    The arguments a and b are greater than zero, and x is
+    nonnegative.
+
+    ACCURACY:
+
+    Tested at random points (a,b,x).
+
+                   x     a,b                     Relative error:
+    arithmetic  domain  domain     # trials      peak         rms
+       IEEE      0,1    0,100       100000      9.8e-15     1.7e-15
+       IEEE      1,5    0,100       100000      6.5e-15     3.5e-16
+       IEEE      0,1    1,10000     100000      2.2e-11     3.3e-12
+       IEEE      1,5    1,10000     100000      1.1e-11     1.7e-13
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double fdistribution(int a, int b, double x)
+    {
+
+        double result = fdistr.fdistribution(a, b, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Complemented F distribution
+
+    Returns the area from x to infinity under the F density
+    function (also known as Snedcor's density or the
+    variance ratio density).
+
+
+                         inf.
+                          -
+                 1       | |  a-1      b-1
+    1-P(x)  =  ------    |   t    (1-t)    dt
+               B(a,b)  | |
+                        -
+                         x
+
+
+    The incomplete beta integral is used, according to the
+    formula
+
+    P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
+
+
+    ACCURACY:
+
+    Tested at random points (a,b,x) in the indicated intervals.
+                   x     a,b                     Relative error:
+    arithmetic  domain  domain     # trials      peak         rms
+       IEEE      0,1    1,100       100000      3.7e-14     5.9e-16
+       IEEE      1,5    1,100       100000      8.0e-15     1.6e-15
+       IEEE      0,1    1,10000     100000      1.8e-11     3.5e-13
+       IEEE      1,5    1,10000     100000      2.0e-11     3.0e-12
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double fcdistribution(int a, int b, double x)
+    {
+
+        double result = fdistr.fcdistribution(a, b, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Inverse of complemented F distribution
+
+    Finds the F density argument x such that the integral
+    from x to infinity of the F density is equal to the
+    given probability p.
+
+    This is accomplished using the inverse beta integral
+    function and the relations
+
+         z = incbi( df2/2, df1/2, p )
+         x = df2 (1-z) / (df1 z).
+
+    Note: the following relations hold for the inverse of
+    the uncomplemented F distribution:
+
+         z = incbi( df1/2, df2/2, p )
+         x = df2 z / (df1 (1-z)).
+
+    ACCURACY:
+
+    Tested at random points (a,b,p).
+
+                 a,b                     Relative error:
+    arithmetic  domain     # trials      peak         rms
+     For p between .001 and 1:
+       IEEE     1,100       100000      8.3e-15     4.7e-16
+       IEEE     1,10000     100000      2.1e-11     1.4e-13
+     For p between 10^-6 and 10^-3:
+       IEEE     1,100        50000      1.3e-12     8.4e-15
+       IEEE     1,10000      50000      3.0e-12     4.8e-14
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double invfdistribution(int a, int b, double y)
+    {
+
+        double result = fdistr.invfdistribution(a, b, y);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Fresnel integral
+
+    Evaluates the Fresnel integrals
+
+              x
+              -
+             | |
+    C(x) =   |   cos(pi/2 t**2) dt,
+           | |
+            -
+             0
+
+              x
+              -
+             | |
+    S(x) =   |   sin(pi/2 t**2) dt.
+           | |
+            -
+             0
+
+
+    The integrals are evaluated by a power series for x < 1.
+    For x >= 1 auxiliary functions f(x) and g(x) are employed
+    such that
+
+    C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
+    S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
+
+
+
+    ACCURACY:
+
+     Relative error.
+
+    Arithmetic  function   domain     # trials      peak         rms
+      IEEE       S(x)      0, 10       10000       2.0e-15     3.2e-16
+      IEEE       C(x)      0, 10       10000       1.8e-15     3.3e-16
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static void fresnelintegral(double x, ref double c, ref double s)
+    {
+
+        fresnel.fresnelintegral(x, ref c, ref s);
+        return;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Calculation of the value of the Hermite polynomial.
+
+    Parameters:
+        n   -   degree, n>=0
+        x   -   argument
+
+    Result:
+        the value of the Hermite polynomial Hn at x
+    *************************************************************************/
+    public static double hermitecalculate(int n, double x)
+    {
+
+        double result = hermite.hermitecalculate(n, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Summation of Hermite polynomials using Clenshaws recurrence formula.
+
+    This routine calculates
+        c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
+
+    Parameters:
+        n   -   degree, n>=0
+        x   -   argument
+
+    Result:
+        the value of the Hermite polynomial at x
+    *************************************************************************/
+    public static double hermitesum(double[] c, int n, double x)
+    {
+
+        double result = hermite.hermitesum(c, n, x);
+        return result;
+    }
+
+    /*************************************************************************
+    Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
+
+    Input parameters:
+        N   -   polynomial degree, n>=0
+
+    Output parameters:
+        C   -   coefficients
+    *************************************************************************/
+    public static void hermitecoefficients(int n, out double[] c)
+    {
+        c = new double[0];
+        hermite.hermitecoefficients(n, ref c);
+        return;
     }
 
@@ -1552,4 +1909,100 @@
 
     /*************************************************************************
+    Poisson distribution
+
+    Returns the sum of the first k+1 terms of the Poisson
+    distribution:
+
+      k         j
+      --   -m  m
+      >   e    --
+      --       j!
+     j=0
+
+    The terms are not summed directly; instead the incomplete
+    gamma integral is employed, according to the relation
+
+    y = pdtr( k, m ) = igamc( k+1, m ).
+
+    The arguments must both be positive.
+    ACCURACY:
+
+    See incomplete gamma function
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double poissondistribution(int k, double m)
+    {
+
+        double result = poissondistr.poissondistribution(k, m);
+        return result;
+    }
+
+    /*************************************************************************
+    Complemented Poisson distribution
+
+    Returns the sum of the terms k+1 to infinity of the Poisson
+    distribution:
+
+     inf.       j
+      --   -m  m
+      >   e    --
+      --       j!
+     j=k+1
+
+    The terms are not summed directly; instead the incomplete
+    gamma integral is employed, according to the formula
+
+    y = pdtrc( k, m ) = igam( k+1, m ).
+
+    The arguments must both be positive.
+
+    ACCURACY:
+
+    See incomplete gamma function
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double poissoncdistribution(int k, double m)
+    {
+
+        double result = poissondistr.poissoncdistribution(k, m);
+        return result;
+    }
+
+    /*************************************************************************
+    Inverse Poisson distribution
+
+    Finds the Poisson variable x such that the integral
+    from 0 to x of the Poisson density is equal to the
+    given probability y.
+
+    This is accomplished using the inverse gamma integral
+    function and the relation
+
+       m = igami( k+1, y ).
+
+    ACCURACY:
+
+    See inverse incomplete gamma function
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double invpoissondistribution(int k, double y)
+    {
+
+        double result = poissondistr.invpoissondistribution(k, y);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
     Psi (digamma) function
 
@@ -1594,4 +2047,81 @@
 
         double result = psif.psi(x);
+        return result;
+    }
+
+}
+public partial class alglib
+{
+
+
+    /*************************************************************************
+    Student's t distribution
+
+    Computes the integral from minus infinity to t of the Student
+    t distribution with integer k > 0 degrees of freedom:
+
+                                         t
+                                         -
+                                        | |
+                 -                      |         2   -(k+1)/2
+                | ( (k+1)/2 )           |  (     x   )
+          ----------------------        |  ( 1 + --- )        dx
+                        -               |  (      k  )
+          sqrt( k pi ) | ( k/2 )        |
+                                      | |
+                                       -
+                                      -inf.
+
+    Relation to incomplete beta integral:
+
+           1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
+    where
+           z = k/(k + t**2).
+
+    For t < -2, this is the method of computation.  For higher t,
+    a direct method is derived from integration by parts.
+    Since the function is symmetric about t=0, the area under the
+    right tail of the density is found by calling the function
+    with -t instead of t.
+
+    ACCURACY:
+
+    Tested at random 1 <= k <= 25.  The "domain" refers to t.
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE     -100,-2      50000       5.9e-15     1.4e-15
+       IEEE     -2,100      500000       2.7e-15     4.9e-17
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double studenttdistribution(int k, double t)
+    {
+
+        double result = studenttdistr.studenttdistribution(k, t);
+        return result;
+    }
+
+    /*************************************************************************
+    Functional inverse of Student's t distribution
+
+    Given probability p, finds the argument t such that stdtr(k,t)
+    is equal to p.
+
+    ACCURACY:
+
+    Tested at random 1 <= k <= 100.  The "domain" refers to p:
+                         Relative error:
+    arithmetic   domain     # trials      peak         rms
+       IEEE    .001,.999     25000       5.7e-15     8.0e-16
+       IEEE    10^-6,.001    25000       2.0e-12     2.9e-14
+
+    Cephes Math Library Release 2.8:  June, 2000
+    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+    *************************************************************************/
+    public static double invstudenttdistribution(int k, double p)
+    {
+
+        double result = studenttdistr.invstudenttdistribution(k, p);
         return result;
     }
@@ -1695,534 +2225,4 @@
         trigintegrals.hyperbolicsinecosineintegrals(x, ref shi, ref chi);
         return;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Binomial distribution
-
-    Returns the sum of the terms 0 through k of the Binomial
-    probability density:
-
-      k
-      --  ( n )   j      n-j
-      >   (   )  p  (1-p)
-      --  ( j )
-     j=0
-
-    The terms are not summed directly; instead the incomplete
-    beta integral is employed, according to the formula
-
-    y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
-
-    The arguments must be positive, with p ranging from 0 to 1.
-
-    ACCURACY:
-
-    Tested at random points (a,b,p), with p between 0 and 1.
-
-                  a,b                     Relative error:
-    arithmetic  domain     # trials      peak         rms
-     For p between 0.001 and 1:
-       IEEE     0,100       100000      4.3e-15     2.6e-16
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double binomialdistribution(int k, int n, double p)
-    {
-
-        double result = binomialdistr.binomialdistribution(k, n, p);
-        return result;
-    }
-
-    /*************************************************************************
-    Complemented binomial distribution
-
-    Returns the sum of the terms k+1 through n of the Binomial
-    probability density:
-
-      n
-      --  ( n )   j      n-j
-      >   (   )  p  (1-p)
-      --  ( j )
-     j=k+1
-
-    The terms are not summed directly; instead the incomplete
-    beta integral is employed, according to the formula
-
-    y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
-
-    The arguments must be positive, with p ranging from 0 to 1.
-
-    ACCURACY:
-
-    Tested at random points (a,b,p).
-
-                  a,b                     Relative error:
-    arithmetic  domain     # trials      peak         rms
-     For p between 0.001 and 1:
-       IEEE     0,100       100000      6.7e-15     8.2e-16
-     For p between 0 and .001:
-       IEEE     0,100       100000      1.5e-13     2.7e-15
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double binomialcdistribution(int k, int n, double p)
-    {
-
-        double result = binomialdistr.binomialcdistribution(k, n, p);
-        return result;
-    }
-
-    /*************************************************************************
-    Inverse binomial distribution
-
-    Finds the event probability p such that the sum of the
-    terms 0 through k of the Binomial probability density
-    is equal to the given cumulative probability y.
-
-    This is accomplished using the inverse beta integral
-    function and the relation
-
-    1 - p = incbi( n-k, k+1, y ).
-
-    ACCURACY:
-
-    Tested at random points (a,b,p).
-
-                  a,b                     Relative error:
-    arithmetic  domain     # trials      peak         rms
-     For p between 0.001 and 1:
-       IEEE     0,100       100000      2.3e-14     6.4e-16
-       IEEE     0,10000     100000      6.6e-12     1.2e-13
-     For p between 10^-6 and 0.001:
-       IEEE     0,100       100000      2.0e-12     1.3e-14
-       IEEE     0,10000     100000      1.5e-12     3.2e-14
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double invbinomialdistribution(int k, int n, double y)
-    {
-
-        double result = binomialdistr.invbinomialdistribution(k, n, y);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Chi-square distribution
-
-    Returns the area under the left hand tail (from 0 to x)
-    of the Chi square probability density function with
-    v degrees of freedom.
-
-
-                                      x
-                                       -
-                           1          | |  v/2-1  -t/2
-     P( x | v )   =   -----------     |   t      e     dt
-                       v/2  -       | |
-                      2    | (v/2)   -
-                                      0
-
-    where x is the Chi-square variable.
-
-    The incomplete gamma integral is used, according to the
-    formula
-
-    y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
-
-    The arguments must both be positive.
-
-    ACCURACY:
-
-    See incomplete gamma function
-
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double chisquaredistribution(double v, double x)
-    {
-
-        double result = chisquaredistr.chisquaredistribution(v, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Complemented Chi-square distribution
-
-    Returns the area under the right hand tail (from x to
-    infinity) of the Chi square probability density function
-    with v degrees of freedom:
-
-                                     inf.
-                                       -
-                           1          | |  v/2-1  -t/2
-     P( x | v )   =   -----------     |   t      e     dt
-                       v/2  -       | |
-                      2    | (v/2)   -
-                                      x
-
-    where x is the Chi-square variable.
-
-    The incomplete gamma integral is used, according to the
-    formula
-
-    y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
-
-    The arguments must both be positive.
-
-    ACCURACY:
-
-    See incomplete gamma function
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double chisquarecdistribution(double v, double x)
-    {
-
-        double result = chisquaredistr.chisquarecdistribution(v, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Inverse of complemented Chi-square distribution
-
-    Finds the Chi-square argument x such that the integral
-    from x to infinity of the Chi-square density is equal
-    to the given cumulative probability y.
-
-    This is accomplished using the inverse gamma integral
-    function and the relation
-
-       x/2 = igami( df/2, y );
-
-    ACCURACY:
-
-    See inverse incomplete gamma function
-
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double invchisquaredistribution(double v, double y)
-    {
-
-        double result = chisquaredistr.invchisquaredistribution(v, y);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    F distribution
-
-    Returns the area from zero to x under the F density
-    function (also known as Snedcor's density or the
-    variance ratio density).  This is the density
-    of x = (u1/df1)/(u2/df2), where u1 and u2 are random
-    variables having Chi square distributions with df1
-    and df2 degrees of freedom, respectively.
-    The incomplete beta integral is used, according to the
-    formula
-
-    P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
-
-
-    The arguments a and b are greater than zero, and x is
-    nonnegative.
-
-    ACCURACY:
-
-    Tested at random points (a,b,x).
-
-                   x     a,b                     Relative error:
-    arithmetic  domain  domain     # trials      peak         rms
-       IEEE      0,1    0,100       100000      9.8e-15     1.7e-15
-       IEEE      1,5    0,100       100000      6.5e-15     3.5e-16
-       IEEE      0,1    1,10000     100000      2.2e-11     3.3e-12
-       IEEE      1,5    1,10000     100000      1.1e-11     1.7e-13
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double fdistribution(int a, int b, double x)
-    {
-
-        double result = fdistr.fdistribution(a, b, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Complemented F distribution
-
-    Returns the area from x to infinity under the F density
-    function (also known as Snedcor's density or the
-    variance ratio density).
-
-
-                         inf.
-                          -
-                 1       | |  a-1      b-1
-    1-P(x)  =  ------    |   t    (1-t)    dt
-               B(a,b)  | |
-                        -
-                         x
-
-
-    The incomplete beta integral is used, according to the
-    formula
-
-    P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
-
-
-    ACCURACY:
-
-    Tested at random points (a,b,x) in the indicated intervals.
-                   x     a,b                     Relative error:
-    arithmetic  domain  domain     # trials      peak         rms
-       IEEE      0,1    1,100       100000      3.7e-14     5.9e-16
-       IEEE      1,5    1,100       100000      8.0e-15     1.6e-15
-       IEEE      0,1    1,10000     100000      1.8e-11     3.5e-13
-       IEEE      1,5    1,10000     100000      2.0e-11     3.0e-12
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double fcdistribution(int a, int b, double x)
-    {
-
-        double result = fdistr.fcdistribution(a, b, x);
-        return result;
-    }
-
-    /*************************************************************************
-    Inverse of complemented F distribution
-
-    Finds the F density argument x such that the integral
-    from x to infinity of the F density is equal to the
-    given probability p.
-
-    This is accomplished using the inverse beta integral
-    function and the relations
-
-         z = incbi( df2/2, df1/2, p )
-         x = df2 (1-z) / (df1 z).
-
-    Note: the following relations hold for the inverse of
-    the uncomplemented F distribution:
-
-         z = incbi( df1/2, df2/2, p )
-         x = df2 z / (df1 (1-z)).
-
-    ACCURACY:
-
-    Tested at random points (a,b,p).
-
-                 a,b                     Relative error:
-    arithmetic  domain     # trials      peak         rms
-     For p between .001 and 1:
-       IEEE     1,100       100000      8.3e-15     4.7e-16
-       IEEE     1,10000     100000      2.1e-11     1.4e-13
-     For p between 10^-6 and 10^-3:
-       IEEE     1,100        50000      1.3e-12     8.4e-15
-       IEEE     1,10000      50000      3.0e-12     4.8e-14
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double invfdistribution(int a, int b, double y)
-    {
-
-        double result = fdistr.invfdistribution(a, b, y);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Poisson distribution
-
-    Returns the sum of the first k+1 terms of the Poisson
-    distribution:
-
-      k         j
-      --   -m  m
-      >   e    --
-      --       j!
-     j=0
-
-    The terms are not summed directly; instead the incomplete
-    gamma integral is employed, according to the relation
-
-    y = pdtr( k, m ) = igamc( k+1, m ).
-
-    The arguments must both be positive.
-    ACCURACY:
-
-    See incomplete gamma function
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double poissondistribution(int k, double m)
-    {
-
-        double result = poissondistr.poissondistribution(k, m);
-        return result;
-    }
-
-    /*************************************************************************
-    Complemented Poisson distribution
-
-    Returns the sum of the terms k+1 to infinity of the Poisson
-    distribution:
-
-     inf.       j
-      --   -m  m
-      >   e    --
-      --       j!
-     j=k+1
-
-    The terms are not summed directly; instead the incomplete
-    gamma integral is employed, according to the formula
-
-    y = pdtrc( k, m ) = igam( k+1, m ).
-
-    The arguments must both be positive.
-
-    ACCURACY:
-
-    See incomplete gamma function
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double poissoncdistribution(int k, double m)
-    {
-
-        double result = poissondistr.poissoncdistribution(k, m);
-        return result;
-    }
-
-    /*************************************************************************
-    Inverse Poisson distribution
-
-    Finds the Poisson variable x such that the integral
-    from 0 to x of the Poisson density is equal to the
-    given probability y.
-
-    This is accomplished using the inverse gamma integral
-    function and the relation
-
-       m = igami( k+1, y ).
-
-    ACCURACY:
-
-    See inverse incomplete gamma function
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double invpoissondistribution(int k, double y)
-    {
-
-        double result = poissondistr.invpoissondistribution(k, y);
-        return result;
-    }
-
-}
-public partial class alglib
-{
-
-
-    /*************************************************************************
-    Student's t distribution
-
-    Computes the integral from minus infinity to t of the Student
-    t distribution with integer k > 0 degrees of freedom:
-
-                                         t
-                                         -
-                                        | |
-                 -                      |         2   -(k+1)/2
-                | ( (k+1)/2 )           |  (     x   )
-          ----------------------        |  ( 1 + --- )        dx
-                        -               |  (      k  )
-          sqrt( k pi ) | ( k/2 )        |
-                                      | |
-                                       -
-                                      -inf.
-
-    Relation to incomplete beta integral:
-
-           1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
-    where
-           z = k/(k + t**2).
-
-    For t < -2, this is the method of computation.  For higher t,
-    a direct method is derived from integration by parts.
-    Since the function is symmetric about t=0, the area under the
-    right tail of the density is found by calling the function
-    with -t instead of t.
-
-    ACCURACY:
-
-    Tested at random 1 <= k <= 25.  The "domain" refers to t.
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE     -100,-2      50000       5.9e-15     1.4e-15
-       IEEE     -2,100      500000       2.7e-15     4.9e-17
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double studenttdistribution(int k, double t)
-    {
-
-        double result = studenttdistr.studenttdistribution(k, t);
-        return result;
-    }
-
-    /*************************************************************************
-    Functional inverse of Student's t distribution
-
-    Given probability p, finds the argument t such that stdtr(k,t)
-    is equal to p.
-
-    ACCURACY:
-
-    Tested at random 1 <= k <= 100.  The "domain" refers to p:
-                         Relative error:
-    arithmetic   domain     # trials      peak         rms
-       IEEE    .001,.999     25000       5.7e-15     8.0e-16
-       IEEE    10^-6,.001    25000       2.0e-12     2.9e-14
-
-    Cephes Math Library Release 2.8:  June, 2000
-    Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-    *************************************************************************/
-    public static double invstudenttdistribution(int k, double p)
-    {
-
-        double result = studenttdistr.invstudenttdistribution(k, p);
-        return result;
     }
 
@@ -4847,1420 +4847,4 @@
             result = y;
             return result;
-        }
-
-
-    }
-    public class chebyshev
-    {
-        /*************************************************************************
-        Calculation of the value of the Chebyshev polynomials of the
-        first and second kinds.
-
-        Parameters:
-            r   -   polynomial kind, either 1 or 2.
-            n   -   degree, n>=0
-            x   -   argument, -1 <= x <= 1
-
-        Result:
-            the value of the Chebyshev polynomial at x
-        *************************************************************************/
-        public static double chebyshevcalculate(int r,
-            int n,
-            double x)
-        {
-            double result = 0;
-            int i = 0;
-            double a = 0;
-            double b = 0;
-
-            result = 0;
-            
-            //
-            // Prepare A and B
-            //
-            if( r==1 )
-            {
-                a = 1;
-                b = x;
-            }
-            else
-            {
-                a = 1;
-                b = 2*x;
-            }
-            
-            //
-            // Special cases: N=0 or N=1
-            //
-            if( n==0 )
-            {
-                result = a;
-                return result;
-            }
-            if( n==1 )
-            {
-                result = b;
-                return result;
-            }
-            
-            //
-            // General case: N>=2
-            //
-            for(i=2; i<=n; i++)
-            {
-                result = 2*x*b-a;
-                a = b;
-                b = result;
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Summation of Chebyshev polynomials using Clenshaws recurrence formula.
-
-        This routine calculates
-            c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
-        or
-            c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
-        depending on the R.
-
-        Parameters:
-            r   -   polynomial kind, either 1 or 2.
-            n   -   degree, n>=0
-            x   -   argument
-
-        Result:
-            the value of the Chebyshev polynomial at x
-        *************************************************************************/
-        public static double chebyshevsum(double[] c,
-            int r,
-            int n,
-            double x)
-        {
-            double result = 0;
-            double b1 = 0;
-            double b2 = 0;
-            int i = 0;
-
-            b1 = 0;
-            b2 = 0;
-            for(i=n; i>=1; i--)
-            {
-                result = 2*x*b1-b2+c[i];
-                b2 = b1;
-                b1 = result;
-            }
-            if( r==1 )
-            {
-                result = -b2+x*b1+c[0];
-            }
-            else
-            {
-                result = -b2+2*x*b1+c[0];
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
-
-        Input parameters:
-            N   -   polynomial degree, n>=0
-
-        Output parameters:
-            C   -   coefficients
-        *************************************************************************/
-        public static void chebyshevcoefficients(int n,
-            ref double[] c)
-        {
-            int i = 0;
-
-            c = new double[0];
-
-            c = new double[n+1];
-            for(i=0; i<=n; i++)
-            {
-                c[i] = 0;
-            }
-            if( n==0 | n==1 )
-            {
-                c[n] = 1;
-            }
-            else
-            {
-                c[n] = Math.Exp((n-1)*Math.Log(2));
-                for(i=0; i<=n/2-1; i++)
-                {
-                    c[n-2*(i+1)] = -(c[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1)/(n-i-1));
-                }
-            }
-        }
-
-
-        /*************************************************************************
-        Conversion of a series of Chebyshev polynomials to a power series.
-
-        Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
-        B[0] + B[1]*X + ... + B[N]*X^N.
-
-        Input parameters:
-            A   -   Chebyshev series coefficients
-            N   -   degree, N>=0
-            
-        Output parameters
-            B   -   power series coefficients
-        *************************************************************************/
-        public static void fromchebyshev(double[] a,
-            int n,
-            ref double[] b)
-        {
-            int i = 0;
-            int k = 0;
-            double e = 0;
-            double d = 0;
-
-            b = new double[0];
-
-            b = new double[n+1];
-            for(i=0; i<=n; i++)
-            {
-                b[i] = 0;
-            }
-            d = 0;
-            i = 0;
-            do
-            {
-                k = i;
-                do
-                {
-                    e = b[k];
-                    b[k] = 0;
-                    if( i<=1 & k==i )
-                    {
-                        b[k] = 1;
-                    }
-                    else
-                    {
-                        if( i!=0 )
-                        {
-                            b[k] = 2*d;
-                        }
-                        if( k>i+1 )
-                        {
-                            b[k] = b[k]-b[k-2];
-                        }
-                    }
-                    d = e;
-                    k = k+1;
-                }
-                while( k<=n );
-                d = b[i];
-                e = 0;
-                k = i;
-                while( k<=n )
-                {
-                    e = e+b[k]*a[k];
-                    k = k+2;
-                }
-                b[i] = e;
-                i = i+1;
-            }
-            while( i<=n );
-        }
-
-
-    }
-    public class dawson
-    {
-        /*************************************************************************
-        Dawson's Integral
-
-        Approximates the integral
-
-                                    x
-                                    -
-                             2     | |        2
-         dawsn(x)  =  exp( -x  )   |    exp( t  ) dt
-                                 | |
-                                  -
-                                  0
-
-        Three different rational approximations are employed, for
-        the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
-
-        ACCURACY:
-
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE      0,10        10000       6.9e-16     1.0e-16
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double dawsonintegral(double x)
-        {
-            double result = 0;
-            double x2 = 0;
-            double y = 0;
-            int sg = 0;
-            double an = 0;
-            double ad = 0;
-            double bn = 0;
-            double bd = 0;
-            double cn = 0;
-            double cd = 0;
-
-            sg = 1;
-            if( (double)(x)<(double)(0) )
-            {
-                sg = -1;
-                x = -x;
-            }
-            if( (double)(x)<(double)(3.25) )
-            {
-                x2 = x*x;
-                an = 1.13681498971755972054E-11;
-                an = an*x2+8.49262267667473811108E-10;
-                an = an*x2+1.94434204175553054283E-8;
-                an = an*x2+9.53151741254484363489E-7;
-                an = an*x2+3.07828309874913200438E-6;
-                an = an*x2+3.52513368520288738649E-4;
-                an = an*x2+-8.50149846724410912031E-4;
-                an = an*x2+4.22618223005546594270E-2;
-                an = an*x2+-9.17480371773452345351E-2;
-                an = an*x2+9.99999999999999994612E-1;
-                ad = 2.40372073066762605484E-11;
-                ad = ad*x2+1.48864681368493396752E-9;
-                ad = ad*x2+5.21265281010541664570E-8;
-                ad = ad*x2+1.27258478273186970203E-6;
-                ad = ad*x2+2.32490249820789513991E-5;
-                ad = ad*x2+3.25524741826057911661E-4;
-                ad = ad*x2+3.48805814657162590916E-3;
-                ad = ad*x2+2.79448531198828973716E-2;
-                ad = ad*x2+1.58874241960120565368E-1;
-                ad = ad*x2+5.74918629489320327824E-1;
-                ad = ad*x2+1.00000000000000000539E0;
-                y = x*an/ad;
-                result = sg*y;
-                return result;
-            }
-            x2 = 1.0/(x*x);
-            if( (double)(x)<(double)(6.25) )
-            {
-                bn = 5.08955156417900903354E-1;
-                bn = bn*x2-2.44754418142697847934E-1;
-                bn = bn*x2+9.41512335303534411857E-2;
-                bn = bn*x2-2.18711255142039025206E-2;
-                bn = bn*x2+3.66207612329569181322E-3;
-                bn = bn*x2-4.23209114460388756528E-4;
-                bn = bn*x2+3.59641304793896631888E-5;
-                bn = bn*x2-2.14640351719968974225E-6;
-                bn = bn*x2+9.10010780076391431042E-8;
-                bn = bn*x2-2.40274520828250956942E-9;
-                bn = bn*x2+3.59233385440928410398E-11;
-                bd = 1.00000000000000000000E0;
-                bd = bd*x2-6.31839869873368190192E-1;
-                bd = bd*x2+2.36706788228248691528E-1;
-                bd = bd*x2-5.31806367003223277662E-2;
-                bd = bd*x2+8.48041718586295374409E-3;
-                bd = bd*x2-9.47996768486665330168E-4;
-                bd = bd*x2+7.81025592944552338085E-5;
-                bd = bd*x2-4.55875153252442634831E-6;
-                bd = bd*x2+1.89100358111421846170E-7;
-                bd = bd*x2-4.91324691331920606875E-9;
-                bd = bd*x2+7.18466403235734541950E-11;
-                y = 1.0/x+x2*bn/(bd*x);
-                result = sg*0.5*y;
-                return result;
-            }
-            if( (double)(x)>(double)(1.0E9) )
-            {
-                result = sg*0.5/x;
-                return result;
-            }
-            cn = -5.90592860534773254987E-1;
-            cn = cn*x2+6.29235242724368800674E-1;
-            cn = cn*x2-1.72858975380388136411E-1;
-            cn = cn*x2+1.64837047825189632310E-2;
-            cn = cn*x2-4.86827613020462700845E-4;
-            cd = 1.00000000000000000000E0;
-            cd = cd*x2-2.69820057197544900361E0;
-            cd = cd*x2+1.73270799045947845857E0;
-            cd = cd*x2-3.93708582281939493482E-1;
-            cd = cd*x2+3.44278924041233391079E-2;
-            cd = cd*x2-9.73655226040941223894E-4;
-            y = 1.0/x+x2*cn/(cd*x);
-            result = sg*0.5*y;
-            return result;
-        }
-
-
-    }
-    public class elliptic
-    {
-        /*************************************************************************
-        Complete elliptic integral of the first kind
-
-        Approximates the integral
-
-
-
-                   pi/2
-                    -
-                   | |
-                   |           dt
-        K(m)  =    |    ------------------
-                   |                   2
-                 | |    sqrt( 1 - m sin t )
-                  -
-                   0
-
-        using the approximation
-
-            P(x)  -  log x Q(x).
-
-        ACCURACY:
-
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE       0,1        30000       2.5e-16     6.8e-17
-
-        Cephes Math Library, Release 2.8:  June, 2000
-        Copyright 1984, 1987, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double ellipticintegralk(double m)
-        {
-            double result = 0;
-
-            result = ellipticintegralkhighprecision(1.0-m);
-            return result;
-        }
-
-
-        /*************************************************************************
-        Complete elliptic integral of the first kind
-
-        Approximates the integral
-
-
-
-                   pi/2
-                    -
-                   | |
-                   |           dt
-        K(m)  =    |    ------------------
-                   |                   2
-                 | |    sqrt( 1 - m sin t )
-                  -
-                   0
-
-        where m = 1 - m1, using the approximation
-
-            P(x)  -  log x Q(x).
-
-        The argument m1 is used rather than m so that the logarithmic
-        singularity at m = 1 will be shifted to the origin; this
-        preserves maximum accuracy.
-
-        K(0) = pi/2.
-
-        ACCURACY:
-
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE       0,1        30000       2.5e-16     6.8e-17
-
-        Àëãîðèòì âçÿò èç áèáëèîòåêè Cephes
-        *************************************************************************/
-        public static double ellipticintegralkhighprecision(double m1)
-        {
-            double result = 0;
-            double p = 0;
-            double q = 0;
-
-            if( (double)(m1)<=(double)(math.machineepsilon) )
-            {
-                result = 1.3862943611198906188E0-0.5*Math.Log(m1);
-            }
-            else
-            {
-                p = 1.37982864606273237150E-4;
-                p = p*m1+2.28025724005875567385E-3;
-                p = p*m1+7.97404013220415179367E-3;
-                p = p*m1+9.85821379021226008714E-3;
-                p = p*m1+6.87489687449949877925E-3;
-                p = p*m1+6.18901033637687613229E-3;
-                p = p*m1+8.79078273952743772254E-3;
-                p = p*m1+1.49380448916805252718E-2;
-                p = p*m1+3.08851465246711995998E-2;
-                p = p*m1+9.65735902811690126535E-2;
-                p = p*m1+1.38629436111989062502E0;
-                q = 2.94078955048598507511E-5;
-                q = q*m1+9.14184723865917226571E-4;
-                q = q*m1+5.94058303753167793257E-3;
-                q = q*m1+1.54850516649762399335E-2;
-                q = q*m1+2.39089602715924892727E-2;
-                q = q*m1+3.01204715227604046988E-2;
-                q = q*m1+3.73774314173823228969E-2;
-                q = q*m1+4.88280347570998239232E-2;
-                q = q*m1+7.03124996963957469739E-2;
-                q = q*m1+1.24999999999870820058E-1;
-                q = q*m1+4.99999999999999999821E-1;
-                result = p-q*Math.Log(m1);
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Incomplete elliptic integral of the first kind F(phi|m)
-
-        Approximates the integral
-
-
-
-                       phi
-                        -
-                       | |
-                       |           dt
-        F(phi_\m)  =    |    ------------------
-                       |                   2
-                     | |    sqrt( 1 - m sin t )
-                      -
-                       0
-
-        of amplitude phi and modulus m, using the arithmetic -
-        geometric mean algorithm.
-
-
-
-
-        ACCURACY:
-
-        Tested at random points with m in [0, 1] and phi as indicated.
-
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE     -10,10       200000      7.4e-16     1.0e-16
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double incompleteellipticintegralk(double phi,
-            double m)
-        {
-            double result = 0;
-            double a = 0;
-            double b = 0;
-            double c = 0;
-            double e = 0;
-            double temp = 0;
-            double pio2 = 0;
-            double t = 0;
-            double k = 0;
-            int d = 0;
-            int md = 0;
-            int s = 0;
-            int npio2 = 0;
-
-            pio2 = 1.57079632679489661923;
-            if( (double)(m)==(double)(0) )
-            {
-                result = phi;
-                return result;
-            }
-            a = 1-m;
-            if( (double)(a)==(double)(0) )
-            {
-                result = Math.Log(Math.Tan(0.5*(pio2+phi)));
-                return result;
-            }
-            npio2 = (int)Math.Floor(phi/pio2);
-            if( npio2%2!=0 )
-            {
-                npio2 = npio2+1;
-            }
-            if( npio2!=0 )
-            {
-                k = ellipticintegralk(1-a);
-                phi = phi-npio2*pio2;
-            }
-            else
-            {
-                k = 0;
-            }
-            if( (double)(phi)<(double)(0) )
-            {
-                phi = -phi;
-                s = -1;
-            }
-            else
-            {
-                s = 0;
-            }
-            b = Math.Sqrt(a);
-            t = Math.Tan(phi);
-            if( (double)(Math.Abs(t))>(double)(10) )
-            {
-                e = 1.0/(b*t);
-                if( (double)(Math.Abs(e))<(double)(10) )
-                {
-                    e = Math.Atan(e);
-                    if( npio2==0 )
-                    {
-                        k = ellipticintegralk(1-a);
-                    }
-                    temp = k-incompleteellipticintegralk(e, m);
-                    if( s<0 )
-                    {
-                        temp = -temp;
-                    }
-                    result = temp+npio2*k;
-                    return result;
-                }
-            }
-            a = 1.0;
-            c = Math.Sqrt(m);
-            d = 1;
-            md = 0;
-            while( (double)(Math.Abs(c/a))>(double)(math.machineepsilon) )
-            {
-                temp = b/a;
-                phi = phi+Math.Atan(t*temp)+md*Math.PI;
-                md = (int)((phi+pio2)/Math.PI);
-                t = t*(1.0+temp)/(1.0-temp*t*t);
-                c = 0.5*(a-b);
-                temp = Math.Sqrt(a*b);
-                a = 0.5*(a+b);
-                b = temp;
-                d = d+d;
-            }
-            temp = (Math.Atan(t)+md*Math.PI)/(d*a);
-            if( s<0 )
-            {
-                temp = -temp;
-            }
-            result = temp+npio2*k;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Complete elliptic integral of the second kind
-
-        Approximates the integral
-
-
-                   pi/2
-                    -
-                   | |                 2
-        E(m)  =    |    sqrt( 1 - m sin t ) dt
-                 | |
-                  -
-                   0
-
-        using the approximation
-
-             P(x)  -  x log x Q(x).
-
-        ACCURACY:
-
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE       0, 1       10000       2.1e-16     7.3e-17
-
-        Cephes Math Library, Release 2.8: June, 2000
-        Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double ellipticintegrale(double m)
-        {
-            double result = 0;
-            double p = 0;
-            double q = 0;
-
-            ap.assert((double)(m)>=(double)(0) & (double)(m)<=(double)(1), "Domain error in EllipticIntegralE: m<0 or m>1");
-            m = 1-m;
-            if( (double)(m)==(double)(0) )
-            {
-                result = 1;
-                return result;
-            }
-            p = 1.53552577301013293365E-4;
-            p = p*m+2.50888492163602060990E-3;
-            p = p*m+8.68786816565889628429E-3;
-            p = p*m+1.07350949056076193403E-2;
-            p = p*m+7.77395492516787092951E-3;
-            p = p*m+7.58395289413514708519E-3;
-            p = p*m+1.15688436810574127319E-2;
-            p = p*m+2.18317996015557253103E-2;
-            p = p*m+5.68051945617860553470E-2;
-            p = p*m+4.43147180560990850618E-1;
-            p = p*m+1.00000000000000000299E0;
-            q = 3.27954898576485872656E-5;
-            q = q*m+1.00962792679356715133E-3;
-            q = q*m+6.50609489976927491433E-3;
-            q = q*m+1.68862163993311317300E-2;
-            q = q*m+2.61769742454493659583E-2;
-            q = q*m+3.34833904888224918614E-2;
-            q = q*m+4.27180926518931511717E-2;
-            q = q*m+5.85936634471101055642E-2;
-            q = q*m+9.37499997197644278445E-2;
-            q = q*m+2.49999999999888314361E-1;
-            result = p-q*m*Math.Log(m);
-            return result;
-        }
-
-
-        /*************************************************************************
-        Incomplete elliptic integral of the second kind
-
-        Approximates the integral
-
-
-                       phi
-                        -
-                       | |
-                       |                   2
-        E(phi_\m)  =    |    sqrt( 1 - m sin t ) dt
-                       |
-                     | |
-                      -
-                       0
-
-        of amplitude phi and modulus m, using the arithmetic -
-        geometric mean algorithm.
-
-        ACCURACY:
-
-        Tested at random arguments with phi in [-10, 10] and m in
-        [0, 1].
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE     -10,10      150000       3.3e-15     1.4e-16
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double incompleteellipticintegrale(double phi,
-            double m)
-        {
-            double result = 0;
-            double pio2 = 0;
-            double a = 0;
-            double b = 0;
-            double c = 0;
-            double e = 0;
-            double temp = 0;
-            double lphi = 0;
-            double t = 0;
-            double ebig = 0;
-            int d = 0;
-            int md = 0;
-            int npio2 = 0;
-            int s = 0;
-
-            pio2 = 1.57079632679489661923;
-            if( (double)(m)==(double)(0) )
-            {
-                result = phi;
-                return result;
-            }
-            lphi = phi;
-            npio2 = (int)Math.Floor(lphi/pio2);
-            if( npio2%2!=0 )
-            {
-                npio2 = npio2+1;
-            }
-            lphi = lphi-npio2*pio2;
-            if( (double)(lphi)<(double)(0) )
-            {
-                lphi = -lphi;
-                s = -1;
-            }
-            else
-            {
-                s = 1;
-            }
-            a = 1.0-m;
-            ebig = ellipticintegrale(m);
-            if( (double)(a)==(double)(0) )
-            {
-                temp = Math.Sin(lphi);
-                if( s<0 )
-                {
-                    temp = -temp;
-                }
-                result = temp+npio2*ebig;
-                return result;
-            }
-            t = Math.Tan(lphi);
-            b = Math.Sqrt(a);
-            
-            //
-            // Thanks to Brian Fitzgerald <fitzgb@mml0.meche.rpi.edu>
-            // for pointing out an instability near odd multiples of pi/2
-            //
-            if( (double)(Math.Abs(t))>(double)(10) )
-            {
-                
-                //
-                // Transform the amplitude
-                //
-                e = 1.0/(b*t);
-                
-                //
-                // ... but avoid multiple recursions.
-                //
-                if( (double)(Math.Abs(e))<(double)(10) )
-                {
-                    e = Math.Atan(e);
-                    temp = ebig+m*Math.Sin(lphi)*Math.Sin(e)-incompleteellipticintegrale(e, m);
-                    if( s<0 )
-                    {
-                        temp = -temp;
-                    }
-                    result = temp+npio2*ebig;
-                    return result;
-                }
-            }
-            c = Math.Sqrt(m);
-            a = 1.0;
-            d = 1;
-            e = 0.0;
-            md = 0;
-            while( (double)(Math.Abs(c/a))>(double)(math.machineepsilon) )
-            {
-                temp = b/a;
-                lphi = lphi+Math.Atan(t*temp)+md*Math.PI;
-                md = (int)((lphi+pio2)/Math.PI);
-                t = t*(1.0+temp)/(1.0-temp*t*t);
-                c = 0.5*(a-b);
-                temp = Math.Sqrt(a*b);
-                a = 0.5*(a+b);
-                b = temp;
-                d = d+d;
-                e = e+c*Math.Sin(lphi);
-            }
-            temp = ebig/ellipticintegralk(m);
-            temp = temp*((Math.Atan(t)+md*Math.PI)/(d*a));
-            temp = temp+e;
-            if( s<0 )
-            {
-                temp = -temp;
-            }
-            result = temp+npio2*ebig;
-            return result;
-        }
-
-
-    }
-    public class expintegrals
-    {
-        public static double exponentialintegralei(double x)
-        {
-            double result = 0;
-            double eul = 0;
-            double f = 0;
-            double f1 = 0;
-            double f2 = 0;
-            double w = 0;
-
-            eul = 0.5772156649015328606065;
-            if( (double)(x)<=(double)(0) )
-            {
-                result = 0;
-                return result;
-            }
-            if( (double)(x)<(double)(2) )
-            {
-                f1 = -5.350447357812542947283;
-                f1 = f1*x+218.5049168816613393830;
-                f1 = f1*x-4176.572384826693777058;
-                f1 = f1*x+55411.76756393557601232;
-                f1 = f1*x-331338.1331178144034309;
-                f1 = f1*x+1592627.163384945414220;
-                f2 = 1.000000000000000000000;
-                f2 = f2*x-52.50547959112862969197;
-                f2 = f2*x+1259.616186786790571525;
-                f2 = f2*x-17565.49581973534652631;
-                f2 = f2*x+149306.2117002725991967;
-                f2 = f2*x-729494.9239640527645655;
-                f2 = f2*x+1592627.163384945429726;
-                f = f1/f2;
-                result = eul+Math.Log(x)+x*f;
-                return result;
-            }
-            if( (double)(x)<(double)(4) )
-            {
-                w = 1/x;
-                f1 = 1.981808503259689673238E-2;
-                f1 = f1*w-1.271645625984917501326;
-                f1 = f1*w-2.088160335681228318920;
-                f1 = f1*w+2.755544509187936721172;
-                f1 = f1*w-4.409507048701600257171E-1;
-                f1 = f1*w+4.665623805935891391017E-2;
-                f1 = f1*w-1.545042679673485262580E-3;
-                f1 = f1*w+7.059980605299617478514E-5;
-                f2 = 1.000000000000000000000;
-                f2 = f2*w+1.476498670914921440652;
-                f2 = f2*w+5.629177174822436244827E-1;
-                f2 = f2*w+1.699017897879307263248E-1;
-                f2 = f2*w+2.291647179034212017463E-2;
-                f2 = f2*w+4.450150439728752875043E-3;
-                f2 = f2*w+1.727439612206521482874E-4;
-                f2 = f2*w+3.953167195549672482304E-5;
-                f = f1/f2;
-                result = Math.Exp(x)*w*(1+w*f);
-                return result;
-            }
-            if( (double)(x)<(double)(8) )
-            {
-                w = 1/x;
-                f1 = -1.373215375871208729803;
-                f1 = f1*w-7.084559133740838761406E-1;
-                f1 = f1*w+1.580806855547941010501;
-                f1 = f1*w-2.601500427425622944234E-1;
-                f1 = f1*w+2.994674694113713763365E-2;
-                f1 = f1*w-1.038086040188744005513E-3;
-                f1 = f1*w+4.371064420753005429514E-5;
-                f1 = f1*w+2.141783679522602903795E-6;
-                f2 = 1.000000000000000000000;
-                f2 = f2*w+8.585231423622028380768E-1;
-                f2 = f2*w+4.483285822873995129957E-1;
-                f2 = f2*w+7.687932158124475434091E-2;
-                f2 = f2*w+2.449868241021887685904E-2;
-                f2 = f2*w+8.832165941927796567926E-4;
-                f2 = f2*w+4.590952299511353531215E-4;
-                f2 = f2*w+-4.729848351866523044863E-6;
-                f2 = f2*w+2.665195537390710170105E-6;
-                f = f1/f2;
-                result = Math.Exp(x)*w*(1+w*f);
-                return result;
-            }
-            if( (double)(x)<(double)(16) )
-            {
-                w = 1/x;
-                f1 = -2.106934601691916512584;
-                f1 = f1*w+1.732733869664688041885;
-                f1 = f1*w-2.423619178935841904839E-1;
-                f1 = f1*w+2.322724180937565842585E-2;
-                f1 = f1*w+2.372880440493179832059E-4;
-                f1 = f1*w-8.343219561192552752335E-5;
-                f1 = f1*w+1.363408795605250394881E-5;
-                f1 = f1*w-3.655412321999253963714E-7;
-                f1 = f1*w+1.464941733975961318456E-8;
-                f1 = f1*w+6.176407863710360207074E-10;
-                f2 = 1.000000000000000000000;
-                f2 = f2*w-2.298062239901678075778E-1;
-                f2 = f2*w+1.105077041474037862347E-1;
-                f2 = f2*w-1.566542966630792353556E-2;
-                f2 = f2*w+2.761106850817352773874E-3;
-                f2 = f2*w-2.089148012284048449115E-4;
-                f2 = f2*w+1.708528938807675304186E-5;
-                f2 = f2*w-4.459311796356686423199E-7;
-                f2 = f2*w+1.394634930353847498145E-8;
-                f2 = f2*w+6.150865933977338354138E-10;
-                f = f1/f2;
-                result = Math.Exp(x)*w*(1+w*f);
-                return result;
-            }
-            if( (double)(x)<(double)(32) )
-            {
-                w = 1/x;
-                f1 = -2.458119367674020323359E-1;
-                f1 = f1*w-1.483382253322077687183E-1;
-                f1 = f1*w+7.248291795735551591813E-2;
-                f1 = f1*w-1.348315687380940523823E-2;
-                f1 = f1*w+1.342775069788636972294E-3;
-                f1 = f1*w-7.942465637159712264564E-5;
-                f1 = f1*w+2.644179518984235952241E-6;
-                f1 = f1*w-4.239473659313765177195E-8;
-                f2 = 1.000000000000000000000;
-                f2 = f2*w-1.044225908443871106315E-1;
-                f2 = f2*w-2.676453128101402655055E-1;
-                f2 = f2*w+9.695000254621984627876E-2;
-                f2 = f2*w-1.601745692712991078208E-2;
-                f2 = f2*w+1.496414899205908021882E-3;
-                f2 = f2*w-8.462452563778485013756E-5;
-                f2 = f2*w+2.728938403476726394024E-6;
-                f2 = f2*w-4.239462431819542051337E-8;
-                f = f1/f2;
-                result = Math.Exp(x)*w*(1+w*f);
-                return result;
-            }
-            if( (double)(x)<(double)(64) )
-            {
-                w = 1/x;
-                f1 = 1.212561118105456670844E-1;
-                f1 = f1*w-5.823133179043894485122E-1;
-                f1 = f1*w+2.348887314557016779211E-1;
-                f1 = f1*w-3.040034318113248237280E-2;
-                f1 = f1*w+1.510082146865190661777E-3;
-                f1 = f1*w-2.523137095499571377122E-5;
-                f2 = 1.000000000000000000000;
-                f2 = f2*w-1.002252150365854016662;
-                f2 = f2*w+2.928709694872224144953E-1;
-                f2 = f2*w-3.337004338674007801307E-2;
-                f2 = f2*w+1.560544881127388842819E-3;
-                f2 = f2*w-2.523137093603234562648E-5;
-                f = f1/f2;
-                result = Math.Exp(x)*w*(1+w*f);
-                return result;
-            }
-            w = 1/x;
-            f1 = -7.657847078286127362028E-1;
-            f1 = f1*w+6.886192415566705051750E-1;
-            f1 = f1*w-2.132598113545206124553E-1;
-            f1 = f1*w+3.346107552384193813594E-2;
-            f1 = f1*w-3.076541477344756050249E-3;
-            f1 = f1*w+1.747119316454907477380E-4;
-            f1 = f1*w-6.103711682274170530369E-6;
-            f1 = f1*w+1.218032765428652199087E-7;
-            f1 = f1*w-1.086076102793290233007E-9;
-            f2 = 1.000000000000000000000;
-            f2 = f2*w-1.888802868662308731041;
-            f2 = f2*w+1.066691687211408896850;
-            f2 = f2*w-2.751915982306380647738E-1;
-            f2 = f2*w+3.930852688233823569726E-2;
-            f2 = f2*w-3.414684558602365085394E-3;
-            f2 = f2*w+1.866844370703555398195E-4;
-            f2 = f2*w-6.345146083130515357861E-6;
-            f2 = f2*w+1.239754287483206878024E-7;
-            f2 = f2*w-1.086076102793126632978E-9;
-            f = f1/f2;
-            result = Math.Exp(x)*w*(1+w*f);
-            return result;
-        }
-
-
-        public static double exponentialintegralen(double x,
-            int n)
-        {
-            double result = 0;
-            double r = 0;
-            double t = 0;
-            double yk = 0;
-            double xk = 0;
-            double pk = 0;
-            double pkm1 = 0;
-            double pkm2 = 0;
-            double qk = 0;
-            double qkm1 = 0;
-            double qkm2 = 0;
-            double psi = 0;
-            double z = 0;
-            int i = 0;
-            int k = 0;
-            double big = 0;
-            double eul = 0;
-
-            eul = 0.57721566490153286060;
-            big = 1.44115188075855872*Math.Pow(10, 17);
-            if( ((n<0 | (double)(x)<(double)(0)) | (double)(x)>(double)(170)) | ((double)(x)==(double)(0) & n<2) )
-            {
-                result = -1;
-                return result;
-            }
-            if( (double)(x)==(double)(0) )
-            {
-                result = (double)1/(double)(n-1);
-                return result;
-            }
-            if( n==0 )
-            {
-                result = Math.Exp(-x)/x;
-                return result;
-            }
-            if( n>5000 )
-            {
-                xk = x+n;
-                yk = 1/(xk*xk);
-                t = n;
-                result = yk*t*(6*x*x-8*t*x+t*t);
-                result = yk*(result+t*(t-2.0*x));
-                result = yk*(result+t);
-                result = (result+1)*Math.Exp(-x)/xk;
-                return result;
-            }
-            if( (double)(x)<=(double)(1) )
-            {
-                psi = -eul-Math.Log(x);
-                for(i=1; i<=n-1; i++)
-                {
-                    psi = psi+(double)1/(double)i;
-                }
-                z = -x;
-                xk = 0;
-                yk = 1;
-                pk = 1-n;
-                if( n==1 )
-                {
-                    result = 0.0;
-                }
-                else
-                {
-                    result = 1.0/pk;
-                }
-                do
-                {
-                    xk = xk+1;
-                    yk = yk*z/xk;
-                    pk = pk+1;
-                    if( (double)(pk)!=(double)(0) )
-                    {
-                        result = result+yk/pk;
-                    }
-                    if( (double)(result)!=(double)(0) )
-                    {
-                        t = Math.Abs(yk/result);
-                    }
-                    else
-                    {
-                        t = 1;
-                    }
-                }
-                while( (double)(t)>=(double)(math.machineepsilon) );
-                t = 1;
-                for(i=1; i<=n-1; i++)
-                {
-                    t = t*z/i;
-                }
-                result = psi*t-result;
-                return result;
-            }
-            else
-            {
-                k = 1;
-                pkm2 = 1;
-                qkm2 = x;
-                pkm1 = 1.0;
-                qkm1 = x+n;
-                result = pkm1/qkm1;
-                do
-                {
-                    k = k+1;
-                    if( k%2==1 )
-                    {
-                        yk = 1;
-                        xk = n+(double)(k-1)/(double)2;
-                    }
-                    else
-                    {
-                        yk = x;
-                        xk = (double)k/(double)2;
-                    }
-                    pk = pkm1*yk+pkm2*xk;
-                    qk = qkm1*yk+qkm2*xk;
-                    if( (double)(qk)!=(double)(0) )
-                    {
-                        r = pk/qk;
-                        t = Math.Abs((result-r)/r);
-                        result = r;
-                    }
-                    else
-                    {
-                        t = 1;
-                    }
-                    pkm2 = pkm1;
-                    pkm1 = pk;
-                    qkm2 = qkm1;
-                    qkm1 = qk;
-                    if( (double)(Math.Abs(pk))>(double)(big) )
-                    {
-                        pkm2 = pkm2/big;
-                        pkm1 = pkm1/big;
-                        qkm2 = qkm2/big;
-                        qkm1 = qkm1/big;
-                    }
-                }
-                while( (double)(t)>=(double)(math.machineepsilon) );
-                result = result*Math.Exp(-x);
-            }
-            return result;
-        }
-
-
-    }
-    public class fresnel
-    {
-        /*************************************************************************
-        Fresnel integral
-
-        Evaluates the Fresnel integrals
-
-                  x
-                  -
-                 | |
-        C(x) =   |   cos(pi/2 t**2) dt,
-               | |
-                -
-                 0
-
-                  x
-                  -
-                 | |
-        S(x) =   |   sin(pi/2 t**2) dt.
-               | |
-                -
-                 0
-
-
-        The integrals are evaluated by a power series for x < 1.
-        For x >= 1 auxiliary functions f(x) and g(x) are employed
-        such that
-
-        C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
-        S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
-
-
-
-        ACCURACY:
-
-         Relative error.
-
-        Arithmetic  function   domain     # trials      peak         rms
-          IEEE       S(x)      0, 10       10000       2.0e-15     3.2e-16
-          IEEE       C(x)      0, 10       10000       1.8e-15     3.3e-16
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static void fresnelintegral(double x,
-            ref double c,
-            ref double s)
-        {
-            double xxa = 0;
-            double f = 0;
-            double g = 0;
-            double cc = 0;
-            double ss = 0;
-            double t = 0;
-            double u = 0;
-            double x2 = 0;
-            double sn = 0;
-            double sd = 0;
-            double cn = 0;
-            double cd = 0;
-            double fn = 0;
-            double fd = 0;
-            double gn = 0;
-            double gd = 0;
-            double mpi = 0;
-            double mpio2 = 0;
-
-            mpi = 3.14159265358979323846;
-            mpio2 = 1.57079632679489661923;
-            xxa = x;
-            x = Math.Abs(xxa);
-            x2 = x*x;
-            if( (double)(x2)<(double)(2.5625) )
-            {
-                t = x2*x2;
-                sn = -2.99181919401019853726E3;
-                sn = sn*t+7.08840045257738576863E5;
-                sn = sn*t-6.29741486205862506537E7;
-                sn = sn*t+2.54890880573376359104E9;
-                sn = sn*t-4.42979518059697779103E10;
-                sn = sn*t+3.18016297876567817986E11;
-                sd = 1.00000000000000000000E0;
-                sd = sd*t+2.81376268889994315696E2;
-                sd = sd*t+4.55847810806532581675E4;
-                sd = sd*t+5.17343888770096400730E6;
-                sd = sd*t+4.19320245898111231129E8;
-                sd = sd*t+2.24411795645340920940E10;
-                sd = sd*t+6.07366389490084639049E11;
-                cn = -4.98843114573573548651E-8;
-                cn = cn*t+9.50428062829859605134E-6;
-                cn = cn*t-6.45191435683965050962E-4;
-                cn = cn*t+1.88843319396703850064E-2;
-                cn = cn*t-2.05525900955013891793E-1;
-                cn = cn*t+9.99999999999999998822E-1;
-                cd = 3.99982968972495980367E-12;
-                cd = cd*t+9.15439215774657478799E-10;
-                cd = cd*t+1.25001862479598821474E-7;
-                cd = cd*t+1.22262789024179030997E-5;
-                cd = cd*t+8.68029542941784300606E-4;
-                cd = cd*t+4.12142090722199792936E-2;
-                cd = cd*t+1.00000000000000000118E0;
-                s = Math.Sign(xxa)*x*x2*sn/sd;
-                c = Math.Sign(xxa)*x*cn/cd;
-                return;
-            }
-            if( (double)(x)>(double)(36974.0) )
-            {
-                c = Math.Sign(xxa)*0.5;
-                s = Math.Sign(xxa)*0.5;
-                return;
-            }
-            x2 = x*x;
-            t = mpi*x2;
-            u = 1/(t*t);
-            t = 1/t;
-            fn = 4.21543555043677546506E-1;
-            fn = fn*u+1.43407919780758885261E-1;
-            fn = fn*u+1.15220955073585758835E-2;
-            fn = fn*u+3.45017939782574027900E-4;
-            fn = fn*u+4.63613749287867322088E-6;
-            fn = fn*u+3.05568983790257605827E-8;
-            fn = fn*u+1.02304514164907233465E-10;
-            fn = fn*u+1.72010743268161828879E-13;
-            fn = fn*u+1.34283276233062758925E-16;
-            fn = fn*u+3.76329711269987889006E-20;
-            fd = 1.00000000000000000000E0;
-            fd = fd*u+7.51586398353378947175E-1;
-            fd = fd*u+1.16888925859191382142E-1;
-            fd = fd*u+6.44051526508858611005E-3;
-            fd = fd*u+1.55934409164153020873E-4;
-            fd = fd*u+1.84627567348930545870E-6;
-            fd = fd*u+1.12699224763999035261E-8;
-            fd = fd*u+3.60140029589371370404E-11;
-            fd = fd*u+5.88754533621578410010E-14;
-            fd = fd*u+4.52001434074129701496E-17;
-            fd = fd*u+1.25443237090011264384E-20;
-            gn = 5.04442073643383265887E-1;
-            gn = gn*u+1.97102833525523411709E-1;
-            gn = gn*u+1.87648584092575249293E-2;
-            gn = gn*u+6.84079380915393090172E-4;
-            gn = gn*u+1.15138826111884280931E-5;
-            gn = gn*u+9.82852443688422223854E-8;
-            gn = gn*u+4.45344415861750144738E-10;
-            gn = gn*u+1.08268041139020870318E-12;
-            gn = gn*u+1.37555460633261799868E-15;
-            gn = gn*u+8.36354435630677421531E-19;
-            gn = gn*u+1.86958710162783235106E-22;
-            gd = 1.00000000000000000000E0;
-            gd = gd*u+1.47495759925128324529E0;
-            gd = gd*u+3.37748989120019970451E-1;
-            gd = gd*u+2.53603741420338795122E-2;
-            gd = gd*u+8.14679107184306179049E-4;
-            gd = gd*u+1.27545075667729118702E-5;
-            gd = gd*u+1.04314589657571990585E-7;
-            gd = gd*u+4.60680728146520428211E-10;
-            gd = gd*u+1.10273215066240270757E-12;
-            gd = gd*u+1.38796531259578871258E-15;
-            gd = gd*u+8.39158816283118707363E-19;
-            gd = gd*u+1.86958710162783236342E-22;
-            f = 1-u*fn/fd;
-            g = t*gn/gd;
-            t = mpio2*x2;
-            cc = Math.Cos(t);
-            ss = Math.Sin(t);
-            t = mpi*x;
-            c = 0.5+(f*ss-g*cc)/t;
-            s = 0.5-(f*cc+g*ss)/t;
-            c = c*Math.Sign(xxa);
-            s = s*Math.Sign(xxa);
-        }
-
-
-    }
-    public class hermite
-    {
-        public static double hermitecalculate(int n,
-            double x)
-        {
-            double result = 0;
-            int i = 0;
-            double a = 0;
-            double b = 0;
-
-            result = 0;
-            
-            //
-            // Prepare A and B
-            //
-            a = 1;
-            b = 2*x;
-            
-            //
-            // Special cases: N=0 or N=1
-            //
-            if( n==0 )
-            {
-                result = a;
-                return result;
-            }
-            if( n==1 )
-            {
-                result = b;
-                return result;
-            }
-            
-            //
-            // General case: N>=2
-            //
-            for(i=2; i<=n; i++)
-            {
-                result = 2*x*b-2*(i-1)*a;
-                a = b;
-                b = result;
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Summation of Hermite polynomials using Clenshaws recurrence formula.
-
-        This routine calculates
-            c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
-
-        Parameters:
-            n   -   degree, n>=0
-            x   -   argument
-
-        Result:
-            the value of the Hermite polynomial at x
-        *************************************************************************/
-        public static double hermitesum(double[] c,
-            int n,
-            double x)
-        {
-            double result = 0;
-            double b1 = 0;
-            double b2 = 0;
-            int i = 0;
-
-            b1 = 0;
-            b2 = 0;
-            result = 0;
-            for(i=n; i>=0; i--)
-            {
-                result = 2*(x*b1-(i+1)*b2)+c[i];
-                b2 = b1;
-                b1 = result;
-            }
-            return result;
-        }
-
-
-        /*************************************************************************
-        Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
-
-        Input parameters:
-            N   -   polynomial degree, n>=0
-
-        Output parameters:
-            C   -   coefficients
-        *************************************************************************/
-        public static void hermitecoefficients(int n,
-            ref double[] c)
-        {
-            int i = 0;
-
-            c = new double[0];
-
-            c = new double[n+1];
-            for(i=0; i<=n; i++)
-            {
-                c[i] = 0;
-            }
-            c[n] = Math.Exp(n*Math.Log(2));
-            for(i=0; i<=n/2-1; i++)
-            {
-                c[n-2*(i+1)] = -(c[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1));
-            }
         }
 
@@ -7209,4 +5793,1985 @@
 
     }
+    public class binomialdistr
+    {
+        /*************************************************************************
+        Binomial distribution
+
+        Returns the sum of the terms 0 through k of the Binomial
+        probability density:
+
+          k
+          --  ( n )   j      n-j
+          >   (   )  p  (1-p)
+          --  ( j )
+         j=0
+
+        The terms are not summed directly; instead the incomplete
+        beta integral is employed, according to the formula
+
+        y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
+
+        The arguments must be positive, with p ranging from 0 to 1.
+
+        ACCURACY:
+
+        Tested at random points (a,b,p), with p between 0 and 1.
+
+                      a,b                     Relative error:
+        arithmetic  domain     # trials      peak         rms
+         For p between 0.001 and 1:
+           IEEE     0,100       100000      4.3e-15     2.6e-16
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double binomialdistribution(int k,
+            int n,
+            double p)
+        {
+            double result = 0;
+            double dk = 0;
+            double dn = 0;
+
+            ap.assert((double)(p)>=(double)(0) & (double)(p)<=(double)(1), "Domain error in BinomialDistribution");
+            ap.assert(k>=-1 & k<=n, "Domain error in BinomialDistribution");
+            if( k==-1 )
+            {
+                result = 0;
+                return result;
+            }
+            if( k==n )
+            {
+                result = 1;
+                return result;
+            }
+            dn = n-k;
+            if( k==0 )
+            {
+                dk = Math.Pow(1.0-p, dn);
+            }
+            else
+            {
+                dk = k+1;
+                dk = ibetaf.incompletebeta(dn, dk, 1.0-p);
+            }
+            result = dk;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Complemented binomial distribution
+
+        Returns the sum of the terms k+1 through n of the Binomial
+        probability density:
+
+          n
+          --  ( n )   j      n-j
+          >   (   )  p  (1-p)
+          --  ( j )
+         j=k+1
+
+        The terms are not summed directly; instead the incomplete
+        beta integral is employed, according to the formula
+
+        y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
+
+        The arguments must be positive, with p ranging from 0 to 1.
+
+        ACCURACY:
+
+        Tested at random points (a,b,p).
+
+                      a,b                     Relative error:
+        arithmetic  domain     # trials      peak         rms
+         For p between 0.001 and 1:
+           IEEE     0,100       100000      6.7e-15     8.2e-16
+         For p between 0 and .001:
+           IEEE     0,100       100000      1.5e-13     2.7e-15
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double binomialcdistribution(int k,
+            int n,
+            double p)
+        {
+            double result = 0;
+            double dk = 0;
+            double dn = 0;
+
+            ap.assert((double)(p)>=(double)(0) & (double)(p)<=(double)(1), "Domain error in BinomialDistributionC");
+            ap.assert(k>=-1 & k<=n, "Domain error in BinomialDistributionC");
+            if( k==-1 )
+            {
+                result = 1;
+                return result;
+            }
+            if( k==n )
+            {
+                result = 0;
+                return result;
+            }
+            dn = n-k;
+            if( k==0 )
+            {
+                if( (double)(p)<(double)(0.01) )
+                {
+                    dk = -nearunityunit.nuexpm1(dn*nearunityunit.nulog1p(-p));
+                }
+                else
+                {
+                    dk = 1.0-Math.Pow(1.0-p, dn);
+                }
+            }
+            else
+            {
+                dk = k+1;
+                dk = ibetaf.incompletebeta(dk, dn, p);
+            }
+            result = dk;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Inverse binomial distribution
+
+        Finds the event probability p such that the sum of the
+        terms 0 through k of the Binomial probability density
+        is equal to the given cumulative probability y.
+
+        This is accomplished using the inverse beta integral
+        function and the relation
+
+        1 - p = incbi( n-k, k+1, y ).
+
+        ACCURACY:
+
+        Tested at random points (a,b,p).
+
+                      a,b                     Relative error:
+        arithmetic  domain     # trials      peak         rms
+         For p between 0.001 and 1:
+           IEEE     0,100       100000      2.3e-14     6.4e-16
+           IEEE     0,10000     100000      6.6e-12     1.2e-13
+         For p between 10^-6 and 0.001:
+           IEEE     0,100       100000      2.0e-12     1.3e-14
+           IEEE     0,10000     100000      1.5e-12     3.2e-14
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double invbinomialdistribution(int k,
+            int n,
+            double y)
+        {
+            double result = 0;
+            double dk = 0;
+            double dn = 0;
+            double p = 0;
+
+            ap.assert(k>=0 & k<n, "Domain error in InvBinomialDistribution");
+            dn = n-k;
+            if( k==0 )
+            {
+                if( (double)(y)>(double)(0.8) )
+                {
+                    p = -nearunityunit.nuexpm1(nearunityunit.nulog1p(y-1.0)/dn);
+                }
+                else
+                {
+                    p = 1.0-Math.Pow(y, 1.0/dn);
+                }
+            }
+            else
+            {
+                dk = k+1;
+                p = ibetaf.incompletebeta(dn, dk, 0.5);
+                if( (double)(p)>(double)(0.5) )
+                {
+                    p = ibetaf.invincompletebeta(dk, dn, 1.0-y);
+                }
+                else
+                {
+                    p = 1.0-ibetaf.invincompletebeta(dn, dk, y);
+                }
+            }
+            result = p;
+            return result;
+        }
+
+
+    }
+    public class chebyshev
+    {
+        /*************************************************************************
+        Calculation of the value of the Chebyshev polynomials of the
+        first and second kinds.
+
+        Parameters:
+            r   -   polynomial kind, either 1 or 2.
+            n   -   degree, n>=0
+            x   -   argument, -1 <= x <= 1
+
+        Result:
+            the value of the Chebyshev polynomial at x
+        *************************************************************************/
+        public static double chebyshevcalculate(int r,
+            int n,
+            double x)
+        {
+            double result = 0;
+            int i = 0;
+            double a = 0;
+            double b = 0;
+
+            result = 0;
+            
+            //
+            // Prepare A and B
+            //
+            if( r==1 )
+            {
+                a = 1;
+                b = x;
+            }
+            else
+            {
+                a = 1;
+                b = 2*x;
+            }
+            
+            //
+            // Special cases: N=0 or N=1
+            //
+            if( n==0 )
+            {
+                result = a;
+                return result;
+            }
+            if( n==1 )
+            {
+                result = b;
+                return result;
+            }
+            
+            //
+            // General case: N>=2
+            //
+            for(i=2; i<=n; i++)
+            {
+                result = 2*x*b-a;
+                a = b;
+                b = result;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Summation of Chebyshev polynomials using Clenshaws recurrence formula.
+
+        This routine calculates
+            c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
+        or
+            c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
+        depending on the R.
+
+        Parameters:
+            r   -   polynomial kind, either 1 or 2.
+            n   -   degree, n>=0
+            x   -   argument
+
+        Result:
+            the value of the Chebyshev polynomial at x
+        *************************************************************************/
+        public static double chebyshevsum(double[] c,
+            int r,
+            int n,
+            double x)
+        {
+            double result = 0;
+            double b1 = 0;
+            double b2 = 0;
+            int i = 0;
+
+            b1 = 0;
+            b2 = 0;
+            for(i=n; i>=1; i--)
+            {
+                result = 2*x*b1-b2+c[i];
+                b2 = b1;
+                b1 = result;
+            }
+            if( r==1 )
+            {
+                result = -b2+x*b1+c[0];
+            }
+            else
+            {
+                result = -b2+2*x*b1+c[0];
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
+
+        Input parameters:
+            N   -   polynomial degree, n>=0
+
+        Output parameters:
+            C   -   coefficients
+        *************************************************************************/
+        public static void chebyshevcoefficients(int n,
+            ref double[] c)
+        {
+            int i = 0;
+
+            c = new double[0];
+
+            c = new double[n+1];
+            for(i=0; i<=n; i++)
+            {
+                c[i] = 0;
+            }
+            if( n==0 | n==1 )
+            {
+                c[n] = 1;
+            }
+            else
+            {
+                c[n] = Math.Exp((n-1)*Math.Log(2));
+                for(i=0; i<=n/2-1; i++)
+                {
+                    c[n-2*(i+1)] = -(c[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1)/(n-i-1));
+                }
+            }
+        }
+
+
+        /*************************************************************************
+        Conversion of a series of Chebyshev polynomials to a power series.
+
+        Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
+        B[0] + B[1]*X + ... + B[N]*X^N.
+
+        Input parameters:
+            A   -   Chebyshev series coefficients
+            N   -   degree, N>=0
+            
+        Output parameters
+            B   -   power series coefficients
+        *************************************************************************/
+        public static void fromchebyshev(double[] a,
+            int n,
+            ref double[] b)
+        {
+            int i = 0;
+            int k = 0;
+            double e = 0;
+            double d = 0;
+
+            b = new double[0];
+
+            b = new double[n+1];
+            for(i=0; i<=n; i++)
+            {
+                b[i] = 0;
+            }
+            d = 0;
+            i = 0;
+            do
+            {
+                k = i;
+                do
+                {
+                    e = b[k];
+                    b[k] = 0;
+                    if( i<=1 & k==i )
+                    {
+                        b[k] = 1;
+                    }
+                    else
+                    {
+                        if( i!=0 )
+                        {
+                            b[k] = 2*d;
+                        }
+                        if( k>i+1 )
+                        {
+                            b[k] = b[k]-b[k-2];
+                        }
+                    }
+                    d = e;
+                    k = k+1;
+                }
+                while( k<=n );
+                d = b[i];
+                e = 0;
+                k = i;
+                while( k<=n )
+                {
+                    e = e+b[k]*a[k];
+                    k = k+2;
+                }
+                b[i] = e;
+                i = i+1;
+            }
+            while( i<=n );
+        }
+
+
+    }
+    public class chisquaredistr
+    {
+        /*************************************************************************
+        Chi-square distribution
+
+        Returns the area under the left hand tail (from 0 to x)
+        of the Chi square probability density function with
+        v degrees of freedom.
+
+
+                                          x
+                                           -
+                               1          | |  v/2-1  -t/2
+         P( x | v )   =   -----------     |   t      e     dt
+                           v/2  -       | |
+                          2    | (v/2)   -
+                                          0
+
+        where x is the Chi-square variable.
+
+        The incomplete gamma integral is used, according to the
+        formula
+
+        y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
+
+        The arguments must both be positive.
+
+        ACCURACY:
+
+        See incomplete gamma function
+
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double chisquaredistribution(double v,
+            double x)
+        {
+            double result = 0;
+
+            ap.assert((double)(x)>=(double)(0) & (double)(v)>=(double)(1), "Domain error in ChiSquareDistribution");
+            result = igammaf.incompletegamma(v/2.0, x/2.0);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Complemented Chi-square distribution
+
+        Returns the area under the right hand tail (from x to
+        infinity) of the Chi square probability density function
+        with v degrees of freedom:
+
+                                         inf.
+                                           -
+                               1          | |  v/2-1  -t/2
+         P( x | v )   =   -----------     |   t      e     dt
+                           v/2  -       | |
+                          2    | (v/2)   -
+                                          x
+
+        where x is the Chi-square variable.
+
+        The incomplete gamma integral is used, according to the
+        formula
+
+        y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
+
+        The arguments must both be positive.
+
+        ACCURACY:
+
+        See incomplete gamma function
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double chisquarecdistribution(double v,
+            double x)
+        {
+            double result = 0;
+
+            ap.assert((double)(x)>=(double)(0) & (double)(v)>=(double)(1), "Domain error in ChiSquareDistributionC");
+            result = igammaf.incompletegammac(v/2.0, x/2.0);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Inverse of complemented Chi-square distribution
+
+        Finds the Chi-square argument x such that the integral
+        from x to infinity of the Chi-square density is equal
+        to the given cumulative probability y.
+
+        This is accomplished using the inverse gamma integral
+        function and the relation
+
+           x/2 = igami( df/2, y );
+
+        ACCURACY:
+
+        See inverse incomplete gamma function
+
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double invchisquaredistribution(double v,
+            double y)
+        {
+            double result = 0;
+
+            ap.assert(((double)(y)>=(double)(0) & (double)(y)<=(double)(1)) & (double)(v)>=(double)(1), "Domain error in InvChiSquareDistribution");
+            result = 2*igammaf.invincompletegammac(0.5*v, y);
+            return result;
+        }
+
+
+    }
+    public class dawson
+    {
+        /*************************************************************************
+        Dawson's Integral
+
+        Approximates the integral
+
+                                    x
+                                    -
+                             2     | |        2
+         dawsn(x)  =  exp( -x  )   |    exp( t  ) dt
+                                 | |
+                                  -
+                                  0
+
+        Three different rational approximations are employed, for
+        the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
+
+        ACCURACY:
+
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE      0,10        10000       6.9e-16     1.0e-16
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double dawsonintegral(double x)
+        {
+            double result = 0;
+            double x2 = 0;
+            double y = 0;
+            int sg = 0;
+            double an = 0;
+            double ad = 0;
+            double bn = 0;
+            double bd = 0;
+            double cn = 0;
+            double cd = 0;
+
+            sg = 1;
+            if( (double)(x)<(double)(0) )
+            {
+                sg = -1;
+                x = -x;
+            }
+            if( (double)(x)<(double)(3.25) )
+            {
+                x2 = x*x;
+                an = 1.13681498971755972054E-11;
+                an = an*x2+8.49262267667473811108E-10;
+                an = an*x2+1.94434204175553054283E-8;
+                an = an*x2+9.53151741254484363489E-7;
+                an = an*x2+3.07828309874913200438E-6;
+                an = an*x2+3.52513368520288738649E-4;
+                an = an*x2+-8.50149846724410912031E-4;
+                an = an*x2+4.22618223005546594270E-2;
+                an = an*x2+-9.17480371773452345351E-2;
+                an = an*x2+9.99999999999999994612E-1;
+                ad = 2.40372073066762605484E-11;
+                ad = ad*x2+1.48864681368493396752E-9;
+                ad = ad*x2+5.21265281010541664570E-8;
+                ad = ad*x2+1.27258478273186970203E-6;
+                ad = ad*x2+2.32490249820789513991E-5;
+                ad = ad*x2+3.25524741826057911661E-4;
+                ad = ad*x2+3.48805814657162590916E-3;
+                ad = ad*x2+2.79448531198828973716E-2;
+                ad = ad*x2+1.58874241960120565368E-1;
+                ad = ad*x2+5.74918629489320327824E-1;
+                ad = ad*x2+1.00000000000000000539E0;
+                y = x*an/ad;
+                result = sg*y;
+                return result;
+            }
+            x2 = 1.0/(x*x);
+            if( (double)(x)<(double)(6.25) )
+            {
+                bn = 5.08955156417900903354E-1;
+                bn = bn*x2-2.44754418142697847934E-1;
+                bn = bn*x2+9.41512335303534411857E-2;
+                bn = bn*x2-2.18711255142039025206E-2;
+                bn = bn*x2+3.66207612329569181322E-3;
+                bn = bn*x2-4.23209114460388756528E-4;
+                bn = bn*x2+3.59641304793896631888E-5;
+                bn = bn*x2-2.14640351719968974225E-6;
+                bn = bn*x2+9.10010780076391431042E-8;
+                bn = bn*x2-2.40274520828250956942E-9;
+                bn = bn*x2+3.59233385440928410398E-11;
+                bd = 1.00000000000000000000E0;
+                bd = bd*x2-6.31839869873368190192E-1;
+                bd = bd*x2+2.36706788228248691528E-1;
+                bd = bd*x2-5.31806367003223277662E-2;
+                bd = bd*x2+8.48041718586295374409E-3;
+                bd = bd*x2-9.47996768486665330168E-4;
+                bd = bd*x2+7.81025592944552338085E-5;
+                bd = bd*x2-4.55875153252442634831E-6;
+                bd = bd*x2+1.89100358111421846170E-7;
+                bd = bd*x2-4.91324691331920606875E-9;
+                bd = bd*x2+7.18466403235734541950E-11;
+                y = 1.0/x+x2*bn/(bd*x);
+                result = sg*0.5*y;
+                return result;
+            }
+            if( (double)(x)>(double)(1.0E9) )
+            {
+                result = sg*0.5/x;
+                return result;
+            }
+            cn = -5.90592860534773254987E-1;
+            cn = cn*x2+6.29235242724368800674E-1;
+            cn = cn*x2-1.72858975380388136411E-1;
+            cn = cn*x2+1.64837047825189632310E-2;
+            cn = cn*x2-4.86827613020462700845E-4;
+            cd = 1.00000000000000000000E0;
+            cd = cd*x2-2.69820057197544900361E0;
+            cd = cd*x2+1.73270799045947845857E0;
+            cd = cd*x2-3.93708582281939493482E-1;
+            cd = cd*x2+3.44278924041233391079E-2;
+            cd = cd*x2-9.73655226040941223894E-4;
+            y = 1.0/x+x2*cn/(cd*x);
+            result = sg*0.5*y;
+            return result;
+        }
+
+
+    }
+    public class elliptic
+    {
+        /*************************************************************************
+        Complete elliptic integral of the first kind
+
+        Approximates the integral
+
+
+
+                   pi/2
+                    -
+                   | |
+                   |           dt
+        K(m)  =    |    ------------------
+                   |                   2
+                 | |    sqrt( 1 - m sin t )
+                  -
+                   0
+
+        using the approximation
+
+            P(x)  -  log x Q(x).
+
+        ACCURACY:
+
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE       0,1        30000       2.5e-16     6.8e-17
+
+        Cephes Math Library, Release 2.8:  June, 2000
+        Copyright 1984, 1987, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double ellipticintegralk(double m)
+        {
+            double result = 0;
+
+            result = ellipticintegralkhighprecision(1.0-m);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Complete elliptic integral of the first kind
+
+        Approximates the integral
+
+
+
+                   pi/2
+                    -
+                   | |
+                   |           dt
+        K(m)  =    |    ------------------
+                   |                   2
+                 | |    sqrt( 1 - m sin t )
+                  -
+                   0
+
+        where m = 1 - m1, using the approximation
+
+            P(x)  -  log x Q(x).
+
+        The argument m1 is used rather than m so that the logarithmic
+        singularity at m = 1 will be shifted to the origin; this
+        preserves maximum accuracy.
+
+        K(0) = pi/2.
+
+        ACCURACY:
+
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE       0,1        30000       2.5e-16     6.8e-17
+
+        Àëãîðèòì âçÿò èç áèáëèîòåêè Cephes
+        *************************************************************************/
+        public static double ellipticintegralkhighprecision(double m1)
+        {
+            double result = 0;
+            double p = 0;
+            double q = 0;
+
+            if( (double)(m1)<=(double)(math.machineepsilon) )
+            {
+                result = 1.3862943611198906188E0-0.5*Math.Log(m1);
+            }
+            else
+            {
+                p = 1.37982864606273237150E-4;
+                p = p*m1+2.28025724005875567385E-3;
+                p = p*m1+7.97404013220415179367E-3;
+                p = p*m1+9.85821379021226008714E-3;
+                p = p*m1+6.87489687449949877925E-3;
+                p = p*m1+6.18901033637687613229E-3;
+                p = p*m1+8.79078273952743772254E-3;
+                p = p*m1+1.49380448916805252718E-2;
+                p = p*m1+3.08851465246711995998E-2;
+                p = p*m1+9.65735902811690126535E-2;
+                p = p*m1+1.38629436111989062502E0;
+                q = 2.94078955048598507511E-5;
+                q = q*m1+9.14184723865917226571E-4;
+                q = q*m1+5.94058303753167793257E-3;
+                q = q*m1+1.54850516649762399335E-2;
+                q = q*m1+2.39089602715924892727E-2;
+                q = q*m1+3.01204715227604046988E-2;
+                q = q*m1+3.73774314173823228969E-2;
+                q = q*m1+4.88280347570998239232E-2;
+                q = q*m1+7.03124996963957469739E-2;
+                q = q*m1+1.24999999999870820058E-1;
+                q = q*m1+4.99999999999999999821E-1;
+                result = p-q*Math.Log(m1);
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Incomplete elliptic integral of the first kind F(phi|m)
+
+        Approximates the integral
+
+
+
+                       phi
+                        -
+                       | |
+                       |           dt
+        F(phi_\m)  =    |    ------------------
+                       |                   2
+                     | |    sqrt( 1 - m sin t )
+                      -
+                       0
+
+        of amplitude phi and modulus m, using the arithmetic -
+        geometric mean algorithm.
+
+
+
+
+        ACCURACY:
+
+        Tested at random points with m in [0, 1] and phi as indicated.
+
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE     -10,10       200000      7.4e-16     1.0e-16
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double incompleteellipticintegralk(double phi,
+            double m)
+        {
+            double result = 0;
+            double a = 0;
+            double b = 0;
+            double c = 0;
+            double e = 0;
+            double temp = 0;
+            double pio2 = 0;
+            double t = 0;
+            double k = 0;
+            int d = 0;
+            int md = 0;
+            int s = 0;
+            int npio2 = 0;
+
+            pio2 = 1.57079632679489661923;
+            if( (double)(m)==(double)(0) )
+            {
+                result = phi;
+                return result;
+            }
+            a = 1-m;
+            if( (double)(a)==(double)(0) )
+            {
+                result = Math.Log(Math.Tan(0.5*(pio2+phi)));
+                return result;
+            }
+            npio2 = (int)Math.Floor(phi/pio2);
+            if( npio2%2!=0 )
+            {
+                npio2 = npio2+1;
+            }
+            if( npio2!=0 )
+            {
+                k = ellipticintegralk(1-a);
+                phi = phi-npio2*pio2;
+            }
+            else
+            {
+                k = 0;
+            }
+            if( (double)(phi)<(double)(0) )
+            {
+                phi = -phi;
+                s = -1;
+            }
+            else
+            {
+                s = 0;
+            }
+            b = Math.Sqrt(a);
+            t = Math.Tan(phi);
+            if( (double)(Math.Abs(t))>(double)(10) )
+            {
+                e = 1.0/(b*t);
+                if( (double)(Math.Abs(e))<(double)(10) )
+                {
+                    e = Math.Atan(e);
+                    if( npio2==0 )
+                    {
+                        k = ellipticintegralk(1-a);
+                    }
+                    temp = k-incompleteellipticintegralk(e, m);
+                    if( s<0 )
+                    {
+                        temp = -temp;
+                    }
+                    result = temp+npio2*k;
+                    return result;
+                }
+            }
+            a = 1.0;
+            c = Math.Sqrt(m);
+            d = 1;
+            md = 0;
+            while( (double)(Math.Abs(c/a))>(double)(math.machineepsilon) )
+            {
+                temp = b/a;
+                phi = phi+Math.Atan(t*temp)+md*Math.PI;
+                md = (int)((phi+pio2)/Math.PI);
+                t = t*(1.0+temp)/(1.0-temp*t*t);
+                c = 0.5*(a-b);
+                temp = Math.Sqrt(a*b);
+                a = 0.5*(a+b);
+                b = temp;
+                d = d+d;
+            }
+            temp = (Math.Atan(t)+md*Math.PI)/(d*a);
+            if( s<0 )
+            {
+                temp = -temp;
+            }
+            result = temp+npio2*k;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Complete elliptic integral of the second kind
+
+        Approximates the integral
+
+
+                   pi/2
+                    -
+                   | |                 2
+        E(m)  =    |    sqrt( 1 - m sin t ) dt
+                 | |
+                  -
+                   0
+
+        using the approximation
+
+             P(x)  -  x log x Q(x).
+
+        ACCURACY:
+
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE       0, 1       10000       2.1e-16     7.3e-17
+
+        Cephes Math Library, Release 2.8: June, 2000
+        Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double ellipticintegrale(double m)
+        {
+            double result = 0;
+            double p = 0;
+            double q = 0;
+
+            ap.assert((double)(m)>=(double)(0) & (double)(m)<=(double)(1), "Domain error in EllipticIntegralE: m<0 or m>1");
+            m = 1-m;
+            if( (double)(m)==(double)(0) )
+            {
+                result = 1;
+                return result;
+            }
+            p = 1.53552577301013293365E-4;
+            p = p*m+2.50888492163602060990E-3;
+            p = p*m+8.68786816565889628429E-3;
+            p = p*m+1.07350949056076193403E-2;
+            p = p*m+7.77395492516787092951E-3;
+            p = p*m+7.58395289413514708519E-3;
+            p = p*m+1.15688436810574127319E-2;
+            p = p*m+2.18317996015557253103E-2;
+            p = p*m+5.68051945617860553470E-2;
+            p = p*m+4.43147180560990850618E-1;
+            p = p*m+1.00000000000000000299E0;
+            q = 3.27954898576485872656E-5;
+            q = q*m+1.00962792679356715133E-3;
+            q = q*m+6.50609489976927491433E-3;
+            q = q*m+1.68862163993311317300E-2;
+            q = q*m+2.61769742454493659583E-2;
+            q = q*m+3.34833904888224918614E-2;
+            q = q*m+4.27180926518931511717E-2;
+            q = q*m+5.85936634471101055642E-2;
+            q = q*m+9.37499997197644278445E-2;
+            q = q*m+2.49999999999888314361E-1;
+            result = p-q*m*Math.Log(m);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Incomplete elliptic integral of the second kind
+
+        Approximates the integral
+
+
+                       phi
+                        -
+                       | |
+                       |                   2
+        E(phi_\m)  =    |    sqrt( 1 - m sin t ) dt
+                       |
+                     | |
+                      -
+                       0
+
+        of amplitude phi and modulus m, using the arithmetic -
+        geometric mean algorithm.
+
+        ACCURACY:
+
+        Tested at random arguments with phi in [-10, 10] and m in
+        [0, 1].
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE     -10,10      150000       3.3e-15     1.4e-16
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double incompleteellipticintegrale(double phi,
+            double m)
+        {
+            double result = 0;
+            double pio2 = 0;
+            double a = 0;
+            double b = 0;
+            double c = 0;
+            double e = 0;
+            double temp = 0;
+            double lphi = 0;
+            double t = 0;
+            double ebig = 0;
+            int d = 0;
+            int md = 0;
+            int npio2 = 0;
+            int s = 0;
+
+            pio2 = 1.57079632679489661923;
+            if( (double)(m)==(double)(0) )
+            {
+                result = phi;
+                return result;
+            }
+            lphi = phi;
+            npio2 = (int)Math.Floor(lphi/pio2);
+            if( npio2%2!=0 )
+            {
+                npio2 = npio2+1;
+            }
+            lphi = lphi-npio2*pio2;
+            if( (double)(lphi)<(double)(0) )
+            {
+                lphi = -lphi;
+                s = -1;
+            }
+            else
+            {
+                s = 1;
+            }
+            a = 1.0-m;
+            ebig = ellipticintegrale(m);
+            if( (double)(a)==(double)(0) )
+            {
+                temp = Math.Sin(lphi);
+                if( s<0 )
+                {
+                    temp = -temp;
+                }
+                result = temp+npio2*ebig;
+                return result;
+            }
+            t = Math.Tan(lphi);
+            b = Math.Sqrt(a);
+            
+            //
+            // Thanks to Brian Fitzgerald <fitzgb@mml0.meche.rpi.edu>
+            // for pointing out an instability near odd multiples of pi/2
+            //
+            if( (double)(Math.Abs(t))>(double)(10) )
+            {
+                
+                //
+                // Transform the amplitude
+                //
+                e = 1.0/(b*t);
+                
+                //
+                // ... but avoid multiple recursions.
+                //
+                if( (double)(Math.Abs(e))<(double)(10) )
+                {
+                    e = Math.Atan(e);
+                    temp = ebig+m*Math.Sin(lphi)*Math.Sin(e)-incompleteellipticintegrale(e, m);
+                    if( s<0 )
+                    {
+                        temp = -temp;
+                    }
+                    result = temp+npio2*ebig;
+                    return result;
+                }
+            }
+            c = Math.Sqrt(m);
+            a = 1.0;
+            d = 1;
+            e = 0.0;
+            md = 0;
+            while( (double)(Math.Abs(c/a))>(double)(math.machineepsilon) )
+            {
+                temp = b/a;
+                lphi = lphi+Math.Atan(t*temp)+md*Math.PI;
+                md = (int)((lphi+pio2)/Math.PI);
+                t = t*(1.0+temp)/(1.0-temp*t*t);
+                c = 0.5*(a-b);
+                temp = Math.Sqrt(a*b);
+                a = 0.5*(a+b);
+                b = temp;
+                d = d+d;
+                e = e+c*Math.Sin(lphi);
+            }
+            temp = ebig/ellipticintegralk(m);
+            temp = temp*((Math.Atan(t)+md*Math.PI)/(d*a));
+            temp = temp+e;
+            if( s<0 )
+            {
+                temp = -temp;
+            }
+            result = temp+npio2*ebig;
+            return result;
+        }
+
+
+    }
+    public class expintegrals
+    {
+        /*************************************************************************
+        Exponential integral Ei(x)
+
+                      x
+                       -     t
+                      | |   e
+           Ei(x) =   -|-   ---  dt .
+                    | |     t
+                     -
+                    -inf
+
+        Not defined for x <= 0.
+        See also expn.c.
+
+
+
+        ACCURACY:
+
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE       0,100       50000      8.6e-16     1.3e-16
+
+        Cephes Math Library Release 2.8:  May, 1999
+        Copyright 1999 by Stephen L. Moshier
+        *************************************************************************/
+        public static double exponentialintegralei(double x)
+        {
+            double result = 0;
+            double eul = 0;
+            double f = 0;
+            double f1 = 0;
+            double f2 = 0;
+            double w = 0;
+
+            eul = 0.5772156649015328606065;
+            if( (double)(x)<=(double)(0) )
+            {
+                result = 0;
+                return result;
+            }
+            if( (double)(x)<(double)(2) )
+            {
+                f1 = -5.350447357812542947283;
+                f1 = f1*x+218.5049168816613393830;
+                f1 = f1*x-4176.572384826693777058;
+                f1 = f1*x+55411.76756393557601232;
+                f1 = f1*x-331338.1331178144034309;
+                f1 = f1*x+1592627.163384945414220;
+                f2 = 1.000000000000000000000;
+                f2 = f2*x-52.50547959112862969197;
+                f2 = f2*x+1259.616186786790571525;
+                f2 = f2*x-17565.49581973534652631;
+                f2 = f2*x+149306.2117002725991967;
+                f2 = f2*x-729494.9239640527645655;
+                f2 = f2*x+1592627.163384945429726;
+                f = f1/f2;
+                result = eul+Math.Log(x)+x*f;
+                return result;
+            }
+            if( (double)(x)<(double)(4) )
+            {
+                w = 1/x;
+                f1 = 1.981808503259689673238E-2;
+                f1 = f1*w-1.271645625984917501326;
+                f1 = f1*w-2.088160335681228318920;
+                f1 = f1*w+2.755544509187936721172;
+                f1 = f1*w-4.409507048701600257171E-1;
+                f1 = f1*w+4.665623805935891391017E-2;
+                f1 = f1*w-1.545042679673485262580E-3;
+                f1 = f1*w+7.059980605299617478514E-5;
+                f2 = 1.000000000000000000000;
+                f2 = f2*w+1.476498670914921440652;
+                f2 = f2*w+5.629177174822436244827E-1;
+                f2 = f2*w+1.699017897879307263248E-1;
+                f2 = f2*w+2.291647179034212017463E-2;
+                f2 = f2*w+4.450150439728752875043E-3;
+                f2 = f2*w+1.727439612206521482874E-4;
+                f2 = f2*w+3.953167195549672482304E-5;
+                f = f1/f2;
+                result = Math.Exp(x)*w*(1+w*f);
+                return result;
+            }
+            if( (double)(x)<(double)(8) )
+            {
+                w = 1/x;
+                f1 = -1.373215375871208729803;
+                f1 = f1*w-7.084559133740838761406E-1;
+                f1 = f1*w+1.580806855547941010501;
+                f1 = f1*w-2.601500427425622944234E-1;
+                f1 = f1*w+2.994674694113713763365E-2;
+                f1 = f1*w-1.038086040188744005513E-3;
+                f1 = f1*w+4.371064420753005429514E-5;
+                f1 = f1*w+2.141783679522602903795E-6;
+                f2 = 1.000000000000000000000;
+                f2 = f2*w+8.585231423622028380768E-1;
+                f2 = f2*w+4.483285822873995129957E-1;
+                f2 = f2*w+7.687932158124475434091E-2;
+                f2 = f2*w+2.449868241021887685904E-2;
+                f2 = f2*w+8.832165941927796567926E-4;
+                f2 = f2*w+4.590952299511353531215E-4;
+                f2 = f2*w+-4.729848351866523044863E-6;
+                f2 = f2*w+2.665195537390710170105E-6;
+                f = f1/f2;
+                result = Math.Exp(x)*w*(1+w*f);
+                return result;
+            }
+            if( (double)(x)<(double)(16) )
+            {
+                w = 1/x;
+                f1 = -2.106934601691916512584;
+                f1 = f1*w+1.732733869664688041885;
+                f1 = f1*w-2.423619178935841904839E-1;
+                f1 = f1*w+2.322724180937565842585E-2;
+                f1 = f1*w+2.372880440493179832059E-4;
+                f1 = f1*w-8.343219561192552752335E-5;
+                f1 = f1*w+1.363408795605250394881E-5;
+                f1 = f1*w-3.655412321999253963714E-7;
+                f1 = f1*w+1.464941733975961318456E-8;
+                f1 = f1*w+6.176407863710360207074E-10;
+                f2 = 1.000000000000000000000;
+                f2 = f2*w-2.298062239901678075778E-1;
+                f2 = f2*w+1.105077041474037862347E-1;
+                f2 = f2*w-1.566542966630792353556E-2;
+                f2 = f2*w+2.761106850817352773874E-3;
+                f2 = f2*w-2.089148012284048449115E-4;
+                f2 = f2*w+1.708528938807675304186E-5;
+                f2 = f2*w-4.459311796356686423199E-7;
+                f2 = f2*w+1.394634930353847498145E-8;
+                f2 = f2*w+6.150865933977338354138E-10;
+                f = f1/f2;
+                result = Math.Exp(x)*w*(1+w*f);
+                return result;
+            }
+            if( (double)(x)<(double)(32) )
+            {
+                w = 1/x;
+                f1 = -2.458119367674020323359E-1;
+                f1 = f1*w-1.483382253322077687183E-1;
+                f1 = f1*w+7.248291795735551591813E-2;
+                f1 = f1*w-1.348315687380940523823E-2;
+                f1 = f1*w+1.342775069788636972294E-3;
+                f1 = f1*w-7.942465637159712264564E-5;
+                f1 = f1*w+2.644179518984235952241E-6;
+                f1 = f1*w-4.239473659313765177195E-8;
+                f2 = 1.000000000000000000000;
+                f2 = f2*w-1.044225908443871106315E-1;
+                f2 = f2*w-2.676453128101402655055E-1;
+                f2 = f2*w+9.695000254621984627876E-2;
+                f2 = f2*w-1.601745692712991078208E-2;
+                f2 = f2*w+1.496414899205908021882E-3;
+                f2 = f2*w-8.462452563778485013756E-5;
+                f2 = f2*w+2.728938403476726394024E-6;
+                f2 = f2*w-4.239462431819542051337E-8;
+                f = f1/f2;
+                result = Math.Exp(x)*w*(1+w*f);
+                return result;
+            }
+            if( (double)(x)<(double)(64) )
+            {
+                w = 1/x;
+                f1 = 1.212561118105456670844E-1;
+                f1 = f1*w-5.823133179043894485122E-1;
+                f1 = f1*w+2.348887314557016779211E-1;
+                f1 = f1*w-3.040034318113248237280E-2;
+                f1 = f1*w+1.510082146865190661777E-3;
+                f1 = f1*w-2.523137095499571377122E-5;
+                f2 = 1.000000000000000000000;
+                f2 = f2*w-1.002252150365854016662;
+                f2 = f2*w+2.928709694872224144953E-1;
+                f2 = f2*w-3.337004338674007801307E-2;
+                f2 = f2*w+1.560544881127388842819E-3;
+                f2 = f2*w-2.523137093603234562648E-5;
+                f = f1/f2;
+                result = Math.Exp(x)*w*(1+w*f);
+                return result;
+            }
+            w = 1/x;
+            f1 = -7.657847078286127362028E-1;
+            f1 = f1*w+6.886192415566705051750E-1;
+            f1 = f1*w-2.132598113545206124553E-1;
+            f1 = f1*w+3.346107552384193813594E-2;
+            f1 = f1*w-3.076541477344756050249E-3;
+            f1 = f1*w+1.747119316454907477380E-4;
+            f1 = f1*w-6.103711682274170530369E-6;
+            f1 = f1*w+1.218032765428652199087E-7;
+            f1 = f1*w-1.086076102793290233007E-9;
+            f2 = 1.000000000000000000000;
+            f2 = f2*w-1.888802868662308731041;
+            f2 = f2*w+1.066691687211408896850;
+            f2 = f2*w-2.751915982306380647738E-1;
+            f2 = f2*w+3.930852688233823569726E-2;
+            f2 = f2*w-3.414684558602365085394E-3;
+            f2 = f2*w+1.866844370703555398195E-4;
+            f2 = f2*w-6.345146083130515357861E-6;
+            f2 = f2*w+1.239754287483206878024E-7;
+            f2 = f2*w-1.086076102793126632978E-9;
+            f = f1/f2;
+            result = Math.Exp(x)*w*(1+w*f);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Exponential integral En(x)
+
+        Evaluates the exponential integral
+
+                        inf.
+                          -
+                         | |   -xt
+                         |    e
+             E (x)  =    |    ----  dt.
+              n          |      n
+                       | |     t
+                        -
+                         1
+
+
+        Both n and x must be nonnegative.
+
+        The routine employs either a power series, a continued
+        fraction, or an asymptotic formula depending on the
+        relative values of n and x.
+
+        ACCURACY:
+
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE      0, 30       10000       1.7e-15     3.6e-16
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1985, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double exponentialintegralen(double x,
+            int n)
+        {
+            double result = 0;
+            double r = 0;
+            double t = 0;
+            double yk = 0;
+            double xk = 0;
+            double pk = 0;
+            double pkm1 = 0;
+            double pkm2 = 0;
+            double qk = 0;
+            double qkm1 = 0;
+            double qkm2 = 0;
+            double psi = 0;
+            double z = 0;
+            int i = 0;
+            int k = 0;
+            double big = 0;
+            double eul = 0;
+
+            eul = 0.57721566490153286060;
+            big = 1.44115188075855872*Math.Pow(10, 17);
+            if( ((n<0 | (double)(x)<(double)(0)) | (double)(x)>(double)(170)) | ((double)(x)==(double)(0) & n<2) )
+            {
+                result = -1;
+                return result;
+            }
+            if( (double)(x)==(double)(0) )
+            {
+                result = (double)1/(double)(n-1);
+                return result;
+            }
+            if( n==0 )
+            {
+                result = Math.Exp(-x)/x;
+                return result;
+            }
+            if( n>5000 )
+            {
+                xk = x+n;
+                yk = 1/(xk*xk);
+                t = n;
+                result = yk*t*(6*x*x-8*t*x+t*t);
+                result = yk*(result+t*(t-2.0*x));
+                result = yk*(result+t);
+                result = (result+1)*Math.Exp(-x)/xk;
+                return result;
+            }
+            if( (double)(x)<=(double)(1) )
+            {
+                psi = -eul-Math.Log(x);
+                for(i=1; i<=n-1; i++)
+                {
+                    psi = psi+(double)1/(double)i;
+                }
+                z = -x;
+                xk = 0;
+                yk = 1;
+                pk = 1-n;
+                if( n==1 )
+                {
+                    result = 0.0;
+                }
+                else
+                {
+                    result = 1.0/pk;
+                }
+                do
+                {
+                    xk = xk+1;
+                    yk = yk*z/xk;
+                    pk = pk+1;
+                    if( (double)(pk)!=(double)(0) )
+                    {
+                        result = result+yk/pk;
+                    }
+                    if( (double)(result)!=(double)(0) )
+                    {
+                        t = Math.Abs(yk/result);
+                    }
+                    else
+                    {
+                        t = 1;
+                    }
+                }
+                while( (double)(t)>=(double)(math.machineepsilon) );
+                t = 1;
+                for(i=1; i<=n-1; i++)
+                {
+                    t = t*z/i;
+                }
+                result = psi*t-result;
+                return result;
+            }
+            else
+            {
+                k = 1;
+                pkm2 = 1;
+                qkm2 = x;
+                pkm1 = 1.0;
+                qkm1 = x+n;
+                result = pkm1/qkm1;
+                do
+                {
+                    k = k+1;
+                    if( k%2==1 )
+                    {
+                        yk = 1;
+                        xk = n+(double)(k-1)/(double)2;
+                    }
+                    else
+                    {
+                        yk = x;
+                        xk = (double)k/(double)2;
+                    }
+                    pk = pkm1*yk+pkm2*xk;
+                    qk = qkm1*yk+qkm2*xk;
+                    if( (double)(qk)!=(double)(0) )
+                    {
+                        r = pk/qk;
+                        t = Math.Abs((result-r)/r);
+                        result = r;
+                    }
+                    else
+                    {
+                        t = 1;
+                    }
+                    pkm2 = pkm1;
+                    pkm1 = pk;
+                    qkm2 = qkm1;
+                    qkm1 = qk;
+                    if( (double)(Math.Abs(pk))>(double)(big) )
+                    {
+                        pkm2 = pkm2/big;
+                        pkm1 = pkm1/big;
+                        qkm2 = qkm2/big;
+                        qkm1 = qkm1/big;
+                    }
+                }
+                while( (double)(t)>=(double)(math.machineepsilon) );
+                result = result*Math.Exp(-x);
+            }
+            return result;
+        }
+
+
+    }
+    public class fdistr
+    {
+        /*************************************************************************
+        F distribution
+
+        Returns the area from zero to x under the F density
+        function (also known as Snedcor's density or the
+        variance ratio density).  This is the density
+        of x = (u1/df1)/(u2/df2), where u1 and u2 are random
+        variables having Chi square distributions with df1
+        and df2 degrees of freedom, respectively.
+        The incomplete beta integral is used, according to the
+        formula
+
+        P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
+
+
+        The arguments a and b are greater than zero, and x is
+        nonnegative.
+
+        ACCURACY:
+
+        Tested at random points (a,b,x).
+
+                       x     a,b                     Relative error:
+        arithmetic  domain  domain     # trials      peak         rms
+           IEEE      0,1    0,100       100000      9.8e-15     1.7e-15
+           IEEE      1,5    0,100       100000      6.5e-15     3.5e-16
+           IEEE      0,1    1,10000     100000      2.2e-11     3.3e-12
+           IEEE      1,5    1,10000     100000      1.1e-11     1.7e-13
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double fdistribution(int a,
+            int b,
+            double x)
+        {
+            double result = 0;
+            double w = 0;
+
+            ap.assert((a>=1 & b>=1) & (double)(x)>=(double)(0), "Domain error in FDistribution");
+            w = a*x;
+            w = w/(b+w);
+            result = ibetaf.incompletebeta(0.5*a, 0.5*b, w);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Complemented F distribution
+
+        Returns the area from x to infinity under the F density
+        function (also known as Snedcor's density or the
+        variance ratio density).
+
+
+                             inf.
+                              -
+                     1       | |  a-1      b-1
+        1-P(x)  =  ------    |   t    (1-t)    dt
+                   B(a,b)  | |
+                            -
+                             x
+
+
+        The incomplete beta integral is used, according to the
+        formula
+
+        P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
+
+
+        ACCURACY:
+
+        Tested at random points (a,b,x) in the indicated intervals.
+                       x     a,b                     Relative error:
+        arithmetic  domain  domain     # trials      peak         rms
+           IEEE      0,1    1,100       100000      3.7e-14     5.9e-16
+           IEEE      1,5    1,100       100000      8.0e-15     1.6e-15
+           IEEE      0,1    1,10000     100000      1.8e-11     3.5e-13
+           IEEE      1,5    1,10000     100000      2.0e-11     3.0e-12
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double fcdistribution(int a,
+            int b,
+            double x)
+        {
+            double result = 0;
+            double w = 0;
+
+            ap.assert((a>=1 & b>=1) & (double)(x)>=(double)(0), "Domain error in FCDistribution");
+            w = b/(b+a*x);
+            result = ibetaf.incompletebeta(0.5*b, 0.5*a, w);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Inverse of complemented F distribution
+
+        Finds the F density argument x such that the integral
+        from x to infinity of the F density is equal to the
+        given probability p.
+
+        This is accomplished using the inverse beta integral
+        function and the relations
+
+             z = incbi( df2/2, df1/2, p )
+             x = df2 (1-z) / (df1 z).
+
+        Note: the following relations hold for the inverse of
+        the uncomplemented F distribution:
+
+             z = incbi( df1/2, df2/2, p )
+             x = df2 z / (df1 (1-z)).
+
+        ACCURACY:
+
+        Tested at random points (a,b,p).
+
+                     a,b                     Relative error:
+        arithmetic  domain     # trials      peak         rms
+         For p between .001 and 1:
+           IEEE     1,100       100000      8.3e-15     4.7e-16
+           IEEE     1,10000     100000      2.1e-11     1.4e-13
+         For p between 10^-6 and 10^-3:
+           IEEE     1,100        50000      1.3e-12     8.4e-15
+           IEEE     1,10000      50000      3.0e-12     4.8e-14
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double invfdistribution(int a,
+            int b,
+            double y)
+        {
+            double result = 0;
+            double w = 0;
+
+            ap.assert(((a>=1 & b>=1) & (double)(y)>(double)(0)) & (double)(y)<=(double)(1), "Domain error in InvFDistribution");
+            
+            //
+            // Compute probability for x = 0.5
+            //
+            w = ibetaf.incompletebeta(0.5*b, 0.5*a, 0.5);
+            
+            //
+            // If that is greater than y, then the solution w < .5
+            // Otherwise, solve at 1-y to remove cancellation in (b - b*w)
+            //
+            if( (double)(w)>(double)(y) | (double)(y)<(double)(0.001) )
+            {
+                w = ibetaf.invincompletebeta(0.5*b, 0.5*a, y);
+                result = (b-b*w)/(a*w);
+            }
+            else
+            {
+                w = ibetaf.invincompletebeta(0.5*a, 0.5*b, 1.0-y);
+                result = b*w/(a*(1.0-w));
+            }
+            return result;
+        }
+
+
+    }
+    public class fresnel
+    {
+        /*************************************************************************
+        Fresnel integral
+
+        Evaluates the Fresnel integrals
+
+                  x
+                  -
+                 | |
+        C(x) =   |   cos(pi/2 t**2) dt,
+               | |
+                -
+                 0
+
+                  x
+                  -
+                 | |
+        S(x) =   |   sin(pi/2 t**2) dt.
+               | |
+                -
+                 0
+
+
+        The integrals are evaluated by a power series for x < 1.
+        For x >= 1 auxiliary functions f(x) and g(x) are employed
+        such that
+
+        C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
+        S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
+
+
+
+        ACCURACY:
+
+         Relative error.
+
+        Arithmetic  function   domain     # trials      peak         rms
+          IEEE       S(x)      0, 10       10000       2.0e-15     3.2e-16
+          IEEE       C(x)      0, 10       10000       1.8e-15     3.3e-16
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static void fresnelintegral(double x,
+            ref double c,
+            ref double s)
+        {
+            double xxa = 0;
+            double f = 0;
+            double g = 0;
+            double cc = 0;
+            double ss = 0;
+            double t = 0;
+            double u = 0;
+            double x2 = 0;
+            double sn = 0;
+            double sd = 0;
+            double cn = 0;
+            double cd = 0;
+            double fn = 0;
+            double fd = 0;
+            double gn = 0;
+            double gd = 0;
+            double mpi = 0;
+            double mpio2 = 0;
+
+            mpi = 3.14159265358979323846;
+            mpio2 = 1.57079632679489661923;
+            xxa = x;
+            x = Math.Abs(xxa);
+            x2 = x*x;
+            if( (double)(x2)<(double)(2.5625) )
+            {
+                t = x2*x2;
+                sn = -2.99181919401019853726E3;
+                sn = sn*t+7.08840045257738576863E5;
+                sn = sn*t-6.29741486205862506537E7;
+                sn = sn*t+2.54890880573376359104E9;
+                sn = sn*t-4.42979518059697779103E10;
+                sn = sn*t+3.18016297876567817986E11;
+                sd = 1.00000000000000000000E0;
+                sd = sd*t+2.81376268889994315696E2;
+                sd = sd*t+4.55847810806532581675E4;
+                sd = sd*t+5.17343888770096400730E6;
+                sd = sd*t+4.19320245898111231129E8;
+                sd = sd*t+2.24411795645340920940E10;
+                sd = sd*t+6.07366389490084639049E11;
+                cn = -4.98843114573573548651E-8;
+                cn = cn*t+9.50428062829859605134E-6;
+                cn = cn*t-6.45191435683965050962E-4;
+                cn = cn*t+1.88843319396703850064E-2;
+                cn = cn*t-2.05525900955013891793E-1;
+                cn = cn*t+9.99999999999999998822E-1;
+                cd = 3.99982968972495980367E-12;
+                cd = cd*t+9.15439215774657478799E-10;
+                cd = cd*t+1.25001862479598821474E-7;
+                cd = cd*t+1.22262789024179030997E-5;
+                cd = cd*t+8.68029542941784300606E-4;
+                cd = cd*t+4.12142090722199792936E-2;
+                cd = cd*t+1.00000000000000000118E0;
+                s = Math.Sign(xxa)*x*x2*sn/sd;
+                c = Math.Sign(xxa)*x*cn/cd;
+                return;
+            }
+            if( (double)(x)>(double)(36974.0) )
+            {
+                c = Math.Sign(xxa)*0.5;
+                s = Math.Sign(xxa)*0.5;
+                return;
+            }
+            x2 = x*x;
+            t = mpi*x2;
+            u = 1/(t*t);
+            t = 1/t;
+            fn = 4.21543555043677546506E-1;
+            fn = fn*u+1.43407919780758885261E-1;
+            fn = fn*u+1.15220955073585758835E-2;
+            fn = fn*u+3.45017939782574027900E-4;
+            fn = fn*u+4.63613749287867322088E-6;
+            fn = fn*u+3.05568983790257605827E-8;
+            fn = fn*u+1.02304514164907233465E-10;
+            fn = fn*u+1.72010743268161828879E-13;
+            fn = fn*u+1.34283276233062758925E-16;
+            fn = fn*u+3.76329711269987889006E-20;
+            fd = 1.00000000000000000000E0;
+            fd = fd*u+7.51586398353378947175E-1;
+            fd = fd*u+1.16888925859191382142E-1;
+            fd = fd*u+6.44051526508858611005E-3;
+            fd = fd*u+1.55934409164153020873E-4;
+            fd = fd*u+1.84627567348930545870E-6;
+            fd = fd*u+1.12699224763999035261E-8;
+            fd = fd*u+3.60140029589371370404E-11;
+            fd = fd*u+5.88754533621578410010E-14;
+            fd = fd*u+4.52001434074129701496E-17;
+            fd = fd*u+1.25443237090011264384E-20;
+            gn = 5.04442073643383265887E-1;
+            gn = gn*u+1.97102833525523411709E-1;
+            gn = gn*u+1.87648584092575249293E-2;
+            gn = gn*u+6.84079380915393090172E-4;
+            gn = gn*u+1.15138826111884280931E-5;
+            gn = gn*u+9.82852443688422223854E-8;
+            gn = gn*u+4.45344415861750144738E-10;
+            gn = gn*u+1.08268041139020870318E-12;
+            gn = gn*u+1.37555460633261799868E-15;
+            gn = gn*u+8.36354435630677421531E-19;
+            gn = gn*u+1.86958710162783235106E-22;
+            gd = 1.00000000000000000000E0;
+            gd = gd*u+1.47495759925128324529E0;
+            gd = gd*u+3.37748989120019970451E-1;
+            gd = gd*u+2.53603741420338795122E-2;
+            gd = gd*u+8.14679107184306179049E-4;
+            gd = gd*u+1.27545075667729118702E-5;
+            gd = gd*u+1.04314589657571990585E-7;
+            gd = gd*u+4.60680728146520428211E-10;
+            gd = gd*u+1.10273215066240270757E-12;
+            gd = gd*u+1.38796531259578871258E-15;
+            gd = gd*u+8.39158816283118707363E-19;
+            gd = gd*u+1.86958710162783236342E-22;
+            f = 1-u*fn/fd;
+            g = t*gn/gd;
+            t = mpio2*x2;
+            cc = Math.Cos(t);
+            ss = Math.Sin(t);
+            t = mpi*x;
+            c = 0.5+(f*ss-g*cc)/t;
+            s = 0.5-(f*cc+g*ss)/t;
+            c = c*Math.Sign(xxa);
+            s = s*Math.Sign(xxa);
+        }
+
+
+    }
+    public class hermite
+    {
+        /*************************************************************************
+        Calculation of the value of the Hermite polynomial.
+
+        Parameters:
+            n   -   degree, n>=0
+            x   -   argument
+
+        Result:
+            the value of the Hermite polynomial Hn at x
+        *************************************************************************/
+        public static double hermitecalculate(int n,
+            double x)
+        {
+            double result = 0;
+            int i = 0;
+            double a = 0;
+            double b = 0;
+
+            result = 0;
+            
+            //
+            // Prepare A and B
+            //
+            a = 1;
+            b = 2*x;
+            
+            //
+            // Special cases: N=0 or N=1
+            //
+            if( n==0 )
+            {
+                result = a;
+                return result;
+            }
+            if( n==1 )
+            {
+                result = b;
+                return result;
+            }
+            
+            //
+            // General case: N>=2
+            //
+            for(i=2; i<=n; i++)
+            {
+                result = 2*x*b-2*(i-1)*a;
+                a = b;
+                b = result;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Summation of Hermite polynomials using Clenshaws recurrence formula.
+
+        This routine calculates
+            c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
+
+        Parameters:
+            n   -   degree, n>=0
+            x   -   argument
+
+        Result:
+            the value of the Hermite polynomial at x
+        *************************************************************************/
+        public static double hermitesum(double[] c,
+            int n,
+            double x)
+        {
+            double result = 0;
+            double b1 = 0;
+            double b2 = 0;
+            int i = 0;
+
+            b1 = 0;
+            b2 = 0;
+            result = 0;
+            for(i=n; i>=0; i--)
+            {
+                result = 2*(x*b1-(i+1)*b2)+c[i];
+                b2 = b1;
+                b1 = result;
+            }
+            return result;
+        }
+
+
+        /*************************************************************************
+        Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
+
+        Input parameters:
+            N   -   polynomial degree, n>=0
+
+        Output parameters:
+            C   -   coefficients
+        *************************************************************************/
+        public static void hermitecoefficients(int n,
+            ref double[] c)
+        {
+            int i = 0;
+
+            c = new double[0];
+
+            c = new double[n+1];
+            for(i=0; i<=n; i++)
+            {
+                c[i] = 0;
+            }
+            c[n] = Math.Exp(n*Math.Log(2));
+            for(i=0; i<=n/2-1; i++)
+            {
+                c[n-2*(i+1)] = -(c[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1));
+            }
+        }
+
+
+    }
     public class jacobianelliptic
     {
@@ -7439,4 +8004,14 @@
     public class legendre
     {
+        /*************************************************************************
+        Calculation of the value of the Legendre polynomial Pn.
+
+        Parameters:
+            n   -   degree, n>=0
+            x   -   argument
+
+        Result:
+            the value of the Legendre polynomial Pn at x
+        *************************************************************************/
         public static double legendrecalculate(int n,
             double x)
@@ -7470,4 +8045,17 @@
 
 
+        /*************************************************************************
+        Summation of Legendre polynomials using Clenshaws recurrence formula.
+
+        This routine calculates
+            c[0]*P0(x) + c[1]*P1(x) + ... + c[N]*PN(x)
+
+        Parameters:
+            n   -   degree, n>=0
+            x   -   argument
+
+        Result:
+            the value of the Legendre polynomial at x
+        *************************************************************************/
         public static double legendresum(double[] c,
             int n,
@@ -7492,4 +8080,13 @@
 
 
+        /*************************************************************************
+        Representation of Pn as C[0] + C[1]*X + ... + C[N]*X^N
+
+        Input parameters:
+            N   -   polynomial degree, n>=0
+
+        Output parameters:
+            C   -   coefficients
+        *************************************************************************/
         public static void legendrecoefficients(int n,
             ref double[] c)
@@ -7513,4 +8110,110 @@
                 c[n-2*(i+1)] = -(c[n-2*i]*(n-2*i)*(n-2*i-1)/2/(i+1)/(2*(n-i)-1));
             }
+        }
+
+
+    }
+    public class poissondistr
+    {
+        /*************************************************************************
+        Poisson distribution
+
+        Returns the sum of the first k+1 terms of the Poisson
+        distribution:
+
+          k         j
+          --   -m  m
+          >   e    --
+          --       j!
+         j=0
+
+        The terms are not summed directly; instead the incomplete
+        gamma integral is employed, according to the relation
+
+        y = pdtr( k, m ) = igamc( k+1, m ).
+
+        The arguments must both be positive.
+        ACCURACY:
+
+        See incomplete gamma function
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double poissondistribution(int k,
+            double m)
+        {
+            double result = 0;
+
+            ap.assert(k>=0 & (double)(m)>(double)(0), "Domain error in PoissonDistribution");
+            result = igammaf.incompletegammac(k+1, m);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Complemented Poisson distribution
+
+        Returns the sum of the terms k+1 to infinity of the Poisson
+        distribution:
+
+         inf.       j
+          --   -m  m
+          >   e    --
+          --       j!
+         j=k+1
+
+        The terms are not summed directly; instead the incomplete
+        gamma integral is employed, according to the formula
+
+        y = pdtrc( k, m ) = igam( k+1, m ).
+
+        The arguments must both be positive.
+
+        ACCURACY:
+
+        See incomplete gamma function
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double poissoncdistribution(int k,
+            double m)
+        {
+            double result = 0;
+
+            ap.assert(k>=0 & (double)(m)>(double)(0), "Domain error in PoissonDistributionC");
+            result = igammaf.incompletegamma(k+1, m);
+            return result;
+        }
+
+
+        /*************************************************************************
+        Inverse Poisson distribution
+
+        Finds the Poisson variable x such that the integral
+        from 0 to x of the Poisson density is equal to the
+        given probability y.
+
+        This is accomplished using the inverse gamma integral
+        function and the relation
+
+           m = igami( k+1, y ).
+
+        ACCURACY:
+
+        See inverse incomplete gamma function
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double invpoissondistribution(int k,
+            double y)
+        {
+            double result = 0;
+
+            ap.assert((k>=0 & (double)(y)>=(double)(0)) & (double)(y)<(double)(1), "Domain error in InvPoissonDistribution");
+            result = igammaf.invincompletegammac(k+1, y);
+            return result;
         }
 
@@ -7645,4 +8348,188 @@
             }
             result = y;
+            return result;
+        }
+
+
+    }
+    public class studenttdistr
+    {
+        /*************************************************************************
+        Student's t distribution
+
+        Computes the integral from minus infinity to t of the Student
+        t distribution with integer k > 0 degrees of freedom:
+
+                                             t
+                                             -
+                                            | |
+                     -                      |         2   -(k+1)/2
+                    | ( (k+1)/2 )           |  (     x   )
+              ----------------------        |  ( 1 + --- )        dx
+                            -               |  (      k  )
+              sqrt( k pi ) | ( k/2 )        |
+                                          | |
+                                           -
+                                          -inf.
+
+        Relation to incomplete beta integral:
+
+               1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
+        where
+               z = k/(k + t**2).
+
+        For t < -2, this is the method of computation.  For higher t,
+        a direct method is derived from integration by parts.
+        Since the function is symmetric about t=0, the area under the
+        right tail of the density is found by calling the function
+        with -t instead of t.
+
+        ACCURACY:
+
+        Tested at random 1 <= k <= 25.  The "domain" refers to t.
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE     -100,-2      50000       5.9e-15     1.4e-15
+           IEEE     -2,100      500000       2.7e-15     4.9e-17
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double studenttdistribution(int k,
+            double t)
+        {
+            double result = 0;
+            double x = 0;
+            double rk = 0;
+            double z = 0;
+            double f = 0;
+            double tz = 0;
+            double p = 0;
+            double xsqk = 0;
+            int j = 0;
+
+            ap.assert(k>0, "Domain error in StudentTDistribution");
+            if( (double)(t)==(double)(0) )
+            {
+                result = 0.5;
+                return result;
+            }
+            if( (double)(t)<(double)(-2.0) )
+            {
+                rk = k;
+                z = rk/(rk+t*t);
+                result = 0.5*ibetaf.incompletebeta(0.5*rk, 0.5, z);
+                return result;
+            }
+            if( (double)(t)<(double)(0) )
+            {
+                x = -t;
+            }
+            else
+            {
+                x = t;
+            }
+            rk = k;
+            z = 1.0+x*x/rk;
+            if( k%2!=0 )
+            {
+                xsqk = x/Math.Sqrt(rk);
+                p = Math.Atan(xsqk);
+                if( k>1 )
+                {
+                    f = 1.0;
+                    tz = 1.0;
+                    j = 3;
+                    while( j<=k-2 & (double)(tz/f)>(double)(math.machineepsilon) )
+                    {
+                        tz = tz*((j-1)/(z*j));
+                        f = f+tz;
+                        j = j+2;
+                    }
+                    p = p+f*xsqk/z;
+                }
+                p = p*2.0/Math.PI;
+            }
+            else
+            {
+                f = 1.0;
+                tz = 1.0;
+                j = 2;
+                while( j<=k-2 & (double)(tz/f)>(double)(math.machineepsilon) )
+                {
+                    tz = tz*((j-1)/(z*j));
+                    f = f+tz;
+                    j = j+2;
+                }
+                p = f*x/Math.Sqrt(z*rk);
+            }
+            if( (double)(t)<(double)(0) )
+            {
+                p = -p;
+            }
+            result = 0.5+0.5*p;
+            return result;
+        }
+
+
+        /*************************************************************************
+        Functional inverse of Student's t distribution
+
+        Given probability p, finds the argument t such that stdtr(k,t)
+        is equal to p.
+
+        ACCURACY:
+
+        Tested at random 1 <= k <= 100.  The "domain" refers to p:
+                             Relative error:
+        arithmetic   domain     # trials      peak         rms
+           IEEE    .001,.999     25000       5.7e-15     8.0e-16
+           IEEE    10^-6,.001    25000       2.0e-12     2.9e-14
+
+        Cephes Math Library Release 2.8:  June, 2000
+        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
+        *************************************************************************/
+        public static double invstudenttdistribution(int k,
+            double p)
+        {
+            double result = 0;
+            double t = 0;
+            double rk = 0;
+            double z = 0;
+            int rflg = 0;
+
+            ap.assert((k>0 & (double)(p)>(double)(0)) & (double)(p)<(double)(1), "Domain error in InvStudentTDistribution");
+            rk = k;
+            if( (double)(p)>(double)(0.25) & (double)(p)<(double)(0.75) )
+            {
+                if( (double)(p)==(double)(0.5) )
+                {
+                    result = 0;
+                    return result;
+                }
+                z = 1.0-2.0*p;
+                z = ibetaf.invincompletebeta(0.5, 0.5*rk, Math.Abs(z));
+                t = Math.Sqrt(rk*z/(1.0-z));
+                if( (double)(p)<(double)(0.5) )
+                {
+                    t = -t;
+                }
+                result = t;
+                return result;
+            }
+            rflg = -1;
+            if( (double)(p)>=(double)(0.5) )
+            {
+                p = 1.0-p;
+                rflg = 1;
+            }
+            z = ibetaf.invincompletebeta(0.5*rk, 0.5, 2.0*p);
+            if( (double)(math.maxrealnumber*z)<(double)(rk) )
+            {
+                result = rflg*math.maxrealnumber;
+                return result;
+            }
+            t = Math.Sqrt(rk/z-rk);
+            result = rflg*t;
             return result;
         }
@@ -8103,762 +8990,4 @@
 
     }
-    public class binomialdistr
-    {
-        /*************************************************************************
-        Binomial distribution
-
-        Returns the sum of the terms 0 through k of the Binomial
-        probability density:
-
-          k
-          --  ( n )   j      n-j
-          >   (   )  p  (1-p)
-          --  ( j )
-         j=0
-
-        The terms are not summed directly; instead the incomplete
-        beta integral is employed, according to the formula
-
-        y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
-
-        The arguments must be positive, with p ranging from 0 to 1.
-
-        ACCURACY:
-
-        Tested at random points (a,b,p), with p between 0 and 1.
-
-                      a,b                     Relative error:
-        arithmetic  domain     # trials      peak         rms
-         For p between 0.001 and 1:
-           IEEE     0,100       100000      4.3e-15     2.6e-16
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double binomialdistribution(int k,
-            int n,
-            double p)
-        {
-            double result = 0;
-            double dk = 0;
-            double dn = 0;
-
-            ap.assert((double)(p)>=(double)(0) & (double)(p)<=(double)(1), "Domain error in BinomialDistribution");
-            ap.assert(k>=-1 & k<=n, "Domain error in BinomialDistribution");
-            if( k==-1 )
-            {
-                result = 0;
-                return result;
-            }
-            if( k==n )
-            {
-                result = 1;
-                return result;
-            }
-            dn = n-k;
-            if( k==0 )
-            {
-                dk = Math.Pow(1.0-p, dn);
-            }
-            else
-            {
-                dk = k+1;
-                dk = ibetaf.incompletebeta(dn, dk, 1.0-p);
-            }
-            result = dk;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Complemented binomial distribution
-
-        Returns the sum of the terms k+1 through n of the Binomial
-        probability density:
-
-          n
-          --  ( n )   j      n-j
-          >   (   )  p  (1-p)
-          --  ( j )
-         j=k+1
-
-        The terms are not summed directly; instead the incomplete
-        beta integral is employed, according to the formula
-
-        y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
-
-        The arguments must be positive, with p ranging from 0 to 1.
-
-        ACCURACY:
-
-        Tested at random points (a,b,p).
-
-                      a,b                     Relative error:
-        arithmetic  domain     # trials      peak         rms
-         For p between 0.001 and 1:
-           IEEE     0,100       100000      6.7e-15     8.2e-16
-         For p between 0 and .001:
-           IEEE     0,100       100000      1.5e-13     2.7e-15
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double binomialcdistribution(int k,
-            int n,
-            double p)
-        {
-            double result = 0;
-            double dk = 0;
-            double dn = 0;
-
-            ap.assert((double)(p)>=(double)(0) & (double)(p)<=(double)(1), "Domain error in BinomialDistributionC");
-            ap.assert(k>=-1 & k<=n, "Domain error in BinomialDistributionC");
-            if( k==-1 )
-            {
-                result = 1;
-                return result;
-            }
-            if( k==n )
-            {
-                result = 0;
-                return result;
-            }
-            dn = n-k;
-            if( k==0 )
-            {
-                if( (double)(p)<(double)(0.01) )
-                {
-                    dk = -nearunityunit.nuexpm1(dn*nearunityunit.nulog1p(-p));
-                }
-                else
-                {
-                    dk = 1.0-Math.Pow(1.0-p, dn);
-                }
-            }
-            else
-            {
-                dk = k+1;
-                dk = ibetaf.incompletebeta(dk, dn, p);
-            }
-            result = dk;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Inverse binomial distribution
-
-        Finds the event probability p such that the sum of the
-        terms 0 through k of the Binomial probability density
-        is equal to the given cumulative probability y.
-
-        This is accomplished using the inverse beta integral
-        function and the relation
-
-        1 - p = incbi( n-k, k+1, y ).
-
-        ACCURACY:
-
-        Tested at random points (a,b,p).
-
-                      a,b                     Relative error:
-        arithmetic  domain     # trials      peak         rms
-         For p between 0.001 and 1:
-           IEEE     0,100       100000      2.3e-14     6.4e-16
-           IEEE     0,10000     100000      6.6e-12     1.2e-13
-         For p between 10^-6 and 0.001:
-           IEEE     0,100       100000      2.0e-12     1.3e-14
-           IEEE     0,10000     100000      1.5e-12     3.2e-14
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double invbinomialdistribution(int k,
-            int n,
-            double y)
-        {
-            double result = 0;
-            double dk = 0;
-            double dn = 0;
-            double p = 0;
-
-            ap.assert(k>=0 & k<n, "Domain error in InvBinomialDistribution");
-            dn = n-k;
-            if( k==0 )
-            {
-                if( (double)(y)>(double)(0.8) )
-                {
-                    p = -nearunityunit.nuexpm1(nearunityunit.nulog1p(y-1.0)/dn);
-                }
-                else
-                {
-                    p = 1.0-Math.Pow(y, 1.0/dn);
-                }
-            }
-            else
-            {
-                dk = k+1;
-                p = ibetaf.incompletebeta(dn, dk, 0.5);
-                if( (double)(p)>(double)(0.5) )
-                {
-                    p = ibetaf.invincompletebeta(dk, dn, 1.0-y);
-                }
-                else
-                {
-                    p = 1.0-ibetaf.invincompletebeta(dn, dk, y);
-                }
-            }
-            result = p;
-            return result;
-        }
-
-
-    }
-    public class chisquaredistr
-    {
-        /*************************************************************************
-        Chi-square distribution
-
-        Returns the area under the left hand tail (from 0 to x)
-        of the Chi square probability density function with
-        v degrees of freedom.
-
-
-                                          x
-                                           -
-                               1          | |  v/2-1  -t/2
-         P( x | v )   =   -----------     |   t      e     dt
-                           v/2  -       | |
-                          2    | (v/2)   -
-                                          0
-
-        where x is the Chi-square variable.
-
-        The incomplete gamma integral is used, according to the
-        formula
-
-        y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
-
-        The arguments must both be positive.
-
-        ACCURACY:
-
-        See incomplete gamma function
-
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double chisquaredistribution(double v,
-            double x)
-        {
-            double result = 0;
-
-            ap.assert((double)(x)>=(double)(0) & (double)(v)>=(double)(1), "Domain error in ChiSquareDistribution");
-            result = igammaf.incompletegamma(v/2.0, x/2.0);
-            return result;
-        }
-
-
-        public static double chisquarecdistribution(double v,
-            double x)
-        {
-            double result = 0;
-
-            ap.assert((double)(x)>=(double)(0) & (double)(v)>=(double)(1), "Domain error in ChiSquareDistributionC");
-            result = igammaf.incompletegammac(v/2.0, x/2.0);
-            return result;
-        }
-
-
-        /*************************************************************************
-        Inverse of complemented Chi-square distribution
-
-        Finds the Chi-square argument x such that the integral
-        from x to infinity of the Chi-square density is equal
-        to the given cumulative probability y.
-
-        This is accomplished using the inverse gamma integral
-        function and the relation
-
-           x/2 = igami( df/2, y );
-
-        ACCURACY:
-
-        See inverse incomplete gamma function
-
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double invchisquaredistribution(double v,
-            double y)
-        {
-            double result = 0;
-
-            ap.assert(((double)(y)>=(double)(0) & (double)(y)<=(double)(1)) & (double)(v)>=(double)(1), "Domain error in InvChiSquareDistribution");
-            result = 2*igammaf.invincompletegammac(0.5*v, y);
-            return result;
-        }
-
-
-    }
-    public class fdistr
-    {
-        /*************************************************************************
-        F distribution
-
-        Returns the area from zero to x under the F density
-        function (also known as Snedcor's density or the
-        variance ratio density).  This is the density
-        of x = (u1/df1)/(u2/df2), where u1 and u2 are random
-        variables having Chi square distributions with df1
-        and df2 degrees of freedom, respectively.
-        The incomplete beta integral is used, according to the
-        formula
-
-        P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
-
-
-        The arguments a and b are greater than zero, and x is
-        nonnegative.
-
-        ACCURACY:
-
-        Tested at random points (a,b,x).
-
-                       x     a,b                     Relative error:
-        arithmetic  domain  domain     # trials      peak         rms
-           IEEE      0,1    0,100       100000      9.8e-15     1.7e-15
-           IEEE      1,5    0,100       100000      6.5e-15     3.5e-16
-           IEEE      0,1    1,10000     100000      2.2e-11     3.3e-12
-           IEEE      1,5    1,10000     100000      1.1e-11     1.7e-13
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double fdistribution(int a,
-            int b,
-            double x)
-        {
-            double result = 0;
-            double w = 0;
-
-            ap.assert((a>=1 & b>=1) & (double)(x)>=(double)(0), "Domain error in FDistribution");
-            w = a*x;
-            w = w/(b+w);
-            result = ibetaf.incompletebeta(0.5*a, 0.5*b, w);
-            return result;
-        }
-
-
-        /*************************************************************************
-        Complemented F distribution
-
-        Returns the area from x to infinity under the F density
-        function (also known as Snedcor's density or the
-        variance ratio density).
-
-
-                             inf.
-                              -
-                     1       | |  a-1      b-1
-        1-P(x)  =  ------    |   t    (1-t)    dt
-                   B(a,b)  | |
-                            -
-                             x
-
-
-        The incomplete beta integral is used, according to the
-        formula
-
-        P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
-
-
-        ACCURACY:
-
-        Tested at random points (a,b,x) in the indicated intervals.
-                       x     a,b                     Relative error:
-        arithmetic  domain  domain     # trials      peak         rms
-           IEEE      0,1    1,100       100000      3.7e-14     5.9e-16
-           IEEE      1,5    1,100       100000      8.0e-15     1.6e-15
-           IEEE      0,1    1,10000     100000      1.8e-11     3.5e-13
-           IEEE      1,5    1,10000     100000      2.0e-11     3.0e-12
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double fcdistribution(int a,
-            int b,
-            double x)
-        {
-            double result = 0;
-            double w = 0;
-
-            ap.assert((a>=1 & b>=1) & (double)(x)>=(double)(0), "Domain error in FCDistribution");
-            w = b/(b+a*x);
-            result = ibetaf.incompletebeta(0.5*b, 0.5*a, w);
-            return result;
-        }
-
-
-        /*************************************************************************
-        Inverse of complemented F distribution
-
-        Finds the F density argument x such that the integral
-        from x to infinity of the F density is equal to the
-        given probability p.
-
-        This is accomplished using the inverse beta integral
-        function and the relations
-
-             z = incbi( df2/2, df1/2, p )
-             x = df2 (1-z) / (df1 z).
-
-        Note: the following relations hold for the inverse of
-        the uncomplemented F distribution:
-
-             z = incbi( df1/2, df2/2, p )
-             x = df2 z / (df1 (1-z)).
-
-        ACCURACY:
-
-        Tested at random points (a,b,p).
-
-                     a,b                     Relative error:
-        arithmetic  domain     # trials      peak         rms
-         For p between .001 and 1:
-           IEEE     1,100       100000      8.3e-15     4.7e-16
-           IEEE     1,10000     100000      2.1e-11     1.4e-13
-         For p between 10^-6 and 10^-3:
-           IEEE     1,100        50000      1.3e-12     8.4e-15
-           IEEE     1,10000      50000      3.0e-12     4.8e-14
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double invfdistribution(int a,
-            int b,
-            double y)
-        {
-            double result = 0;
-            double w = 0;
-
-            ap.assert(((a>=1 & b>=1) & (double)(y)>(double)(0)) & (double)(y)<=(double)(1), "Domain error in InvFDistribution");
-            
-            //
-            // Compute probability for x = 0.5
-            //
-            w = ibetaf.incompletebeta(0.5*b, 0.5*a, 0.5);
-            
-            //
-            // If that is greater than y, then the solution w < .5
-            // Otherwise, solve at 1-y to remove cancellation in (b - b*w)
-            //
-            if( (double)(w)>(double)(y) | (double)(y)<(double)(0.001) )
-            {
-                w = ibetaf.invincompletebeta(0.5*b, 0.5*a, y);
-                result = (b-b*w)/(a*w);
-            }
-            else
-            {
-                w = ibetaf.invincompletebeta(0.5*a, 0.5*b, 1.0-y);
-                result = b*w/(a*(1.0-w));
-            }
-            return result;
-        }
-
-
-    }
-    public class poissondistr
-    {
-        /*************************************************************************
-        Poisson distribution
-
-        Returns the sum of the first k+1 terms of the Poisson
-        distribution:
-
-          k         j
-          --   -m  m
-          >   e    --
-          --       j!
-         j=0
-
-        The terms are not summed directly; instead the incomplete
-        gamma integral is employed, according to the relation
-
-        y = pdtr( k, m ) = igamc( k+1, m ).
-
-        The arguments must both be positive.
-        ACCURACY:
-
-        See incomplete gamma function
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double poissondistribution(int k,
-            double m)
-        {
-            double result = 0;
-
-            ap.assert(k>=0 & (double)(m)>(double)(0), "Domain error in PoissonDistribution");
-            result = igammaf.incompletegammac(k+1, m);
-            return result;
-        }
-
-
-        /*************************************************************************
-        Complemented Poisson distribution
-
-        Returns the sum of the terms k+1 to infinity of the Poisson
-        distribution:
-
-         inf.       j
-          --   -m  m
-          >   e    --
-          --       j!
-         j=k+1
-
-        The terms are not summed directly; instead the incomplete
-        gamma integral is employed, according to the formula
-
-        y = pdtrc( k, m ) = igam( k+1, m ).
-
-        The arguments must both be positive.
-
-        ACCURACY:
-
-        See incomplete gamma function
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double poissoncdistribution(int k,
-            double m)
-        {
-            double result = 0;
-
-            ap.assert(k>=0 & (double)(m)>(double)(0), "Domain error in PoissonDistributionC");
-            result = igammaf.incompletegamma(k+1, m);
-            return result;
-        }
-
-
-        /*************************************************************************
-        Inverse Poisson distribution
-
-        Finds the Poisson variable x such that the integral
-        from 0 to x of the Poisson density is equal to the
-        given probability y.
-
-        This is accomplished using the inverse gamma integral
-        function and the relation
-
-           m = igami( k+1, y ).
-
-        ACCURACY:
-
-        See inverse incomplete gamma function
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double invpoissondistribution(int k,
-            double y)
-        {
-            double result = 0;
-
-            ap.assert((k>=0 & (double)(y)>=(double)(0)) & (double)(y)<(double)(1), "Domain error in InvPoissonDistribution");
-            result = igammaf.invincompletegammac(k+1, y);
-            return result;
-        }
-
-
-    }
-    public class studenttdistr
-    {
-        /*************************************************************************
-        Student's t distribution
-
-        Computes the integral from minus infinity to t of the Student
-        t distribution with integer k > 0 degrees of freedom:
-
-                                             t
-                                             -
-                                            | |
-                     -                      |         2   -(k+1)/2
-                    | ( (k+1)/2 )           |  (     x   )
-              ----------------------        |  ( 1 + --- )        dx
-                            -               |  (      k  )
-              sqrt( k pi ) | ( k/2 )        |
-                                          | |
-                                           -
-                                          -inf.
-
-        Relation to incomplete beta integral:
-
-               1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
-        where
-               z = k/(k + t**2).
-
-        For t < -2, this is the method of computation.  For higher t,
-        a direct method is derived from integration by parts.
-        Since the function is symmetric about t=0, the area under the
-        right tail of the density is found by calling the function
-        with -t instead of t.
-
-        ACCURACY:
-
-        Tested at random 1 <= k <= 25.  The "domain" refers to t.
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE     -100,-2      50000       5.9e-15     1.4e-15
-           IEEE     -2,100      500000       2.7e-15     4.9e-17
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double studenttdistribution(int k,
-            double t)
-        {
-            double result = 0;
-            double x = 0;
-            double rk = 0;
-            double z = 0;
-            double f = 0;
-            double tz = 0;
-            double p = 0;
-            double xsqk = 0;
-            int j = 0;
-
-            ap.assert(k>0, "Domain error in StudentTDistribution");
-            if( (double)(t)==(double)(0) )
-            {
-                result = 0.5;
-                return result;
-            }
-            if( (double)(t)<(double)(-2.0) )
-            {
-                rk = k;
-                z = rk/(rk+t*t);
-                result = 0.5*ibetaf.incompletebeta(0.5*rk, 0.5, z);
-                return result;
-            }
-            if( (double)(t)<(double)(0) )
-            {
-                x = -t;
-            }
-            else
-            {
-                x = t;
-            }
-            rk = k;
-            z = 1.0+x*x/rk;
-            if( k%2!=0 )
-            {
-                xsqk = x/Math.Sqrt(rk);
-                p = Math.Atan(xsqk);
-                if( k>1 )
-                {
-                    f = 1.0;
-                    tz = 1.0;
-                    j = 3;
-                    while( j<=k-2 & (double)(tz/f)>(double)(math.machineepsilon) )
-                    {
-                        tz = tz*((j-1)/(z*j));
-                        f = f+tz;
-                        j = j+2;
-                    }
-                    p = p+f*xsqk/z;
-                }
-                p = p*2.0/Math.PI;
-            }
-            else
-            {
-                f = 1.0;
-                tz = 1.0;
-                j = 2;
-                while( j<=k-2 & (double)(tz/f)>(double)(math.machineepsilon) )
-                {
-                    tz = tz*((j-1)/(z*j));
-                    f = f+tz;
-                    j = j+2;
-                }
-                p = f*x/Math.Sqrt(z*rk);
-            }
-            if( (double)(t)<(double)(0) )
-            {
-                p = -p;
-            }
-            result = 0.5+0.5*p;
-            return result;
-        }
-
-
-        /*************************************************************************
-        Functional inverse of Student's t distribution
-
-        Given probability p, finds the argument t such that stdtr(k,t)
-        is equal to p.
-
-        ACCURACY:
-
-        Tested at random 1 <= k <= 100.  The "domain" refers to p:
-                             Relative error:
-        arithmetic   domain     # trials      peak         rms
-           IEEE    .001,.999     25000       5.7e-15     8.0e-16
-           IEEE    10^-6,.001    25000       2.0e-12     2.9e-14
-
-        Cephes Math Library Release 2.8:  June, 2000
-        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
-        *************************************************************************/
-        public static double invstudenttdistribution(int k,
-            double p)
-        {
-            double result = 0;
-            double t = 0;
-            double rk = 0;
-            double z = 0;
-            int rflg = 0;
-
-            ap.assert((k>0 & (double)(p)>(double)(0)) & (double)(p)<(double)(1), "Domain error in InvStudentTDistribution");
-            rk = k;
-            if( (double)(p)>(double)(0.25) & (double)(p)<(double)(0.75) )
-            {
-                if( (double)(p)==(double)(0.5) )
-                {
-                    result = 0;
-                    return result;
-                }
-                z = 1.0-2.0*p;
-                z = ibetaf.invincompletebeta(0.5, 0.5*rk, Math.Abs(z));
-                t = Math.Sqrt(rk*z/(1.0-z));
-                if( (double)(p)<(double)(0.5) )
-                {
-                    t = -t;
-                }
-                result = t;
-                return result;
-            }
-            rflg = -1;
-            if( (double)(p)>=(double)(0.5) )
-            {
-                p = 1.0-p;
-                rflg = 1;
-            }
-            z = ibetaf.invincompletebeta(0.5*rk, 0.5, 2.0*p);
-            if( (double)(math.maxrealnumber*z)<(double)(rk) )
-            {
-                result = rflg*math.maxrealnumber;
-                return result;
-            }
-            t = Math.Sqrt(rk/z-rk);
-            result = rflg*t;
-            return result;
-        }
-
-
-    }
 }
 
