Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Inaccuracy in Accord.Math Pseudoinverse #313

@FlorianKraemer2

Description

@FlorianKraemer2

Hello, I worte this little Programm in Context of polynomial approximation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KinesysClassLibrary;
using System.IO;




namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //Defining Variables

            int nPoly = 5;
            double[] t ={1352,1354,1356,1358,1360,1362,1364,1366,1368,1370,1372,1374,1376,1378,1380,1382,1384,1386,1388,1390,1392,1394,1396,1398,1400,1402,1404,
1406,1408,1410,1412,1414,1416,1418,1420,1422,1424,1426,1428,1430,1432,1434,1436,1438,1440,1442,1444,1446,1448,1450,1452,1454,1456,1458,1460,1462,1464,
1466,1468,1470,1472,1474,1476,1478,1480,1482,1484,1486,1488,1490,1492,1494,1496,1498,1500,1502,1504,1506,1508,1510,1512,1514,1516,1518,1520,
1522,1524,1526,1528,1530,1532,1534,1536,1538,1540,1542,1544,1546,1548,1550,1552,1554,1556,1558,1560,1562,1564,1566,1568,1570,1572,1574,1576,
1578,1580,1582,1584,1586,1588,1590,1592,1594,1596,1598,1600,1602,1604,1606,1608,1610,1612,1614,1616,1618,1620,1622,1624,1626,1628,1630,1632,
1634,1636,1638,1640,1642,1644,1646,1648,1650,1652,1654,1656,1658,1660,1662,1664,1666,1668,1670,1672,1674,1676,1678,1680,1682,1684,1686,1688,
1690,1692,1694,1696,1698,1700,1702};

            // Write Console Output to Textfile
            FileStream ostrm;
            StreamWriter writer;
            TextWriter oldOut = Console.Out;
            try
            {
                ostrm = new FileStream("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(ostrm);
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot open Redirect.txt for writing");
                Console.WriteLine(e.Message);
                return;
            }
            Console.SetOut(writer);

            //Generating the Matrix
            double[,] sPoly = new double[t.Length, nPoly + 1];
            for (int ii = 0; ii < t.Length; ii++)
            {
                for (int jj = 0; jj < nPoly + 1; jj++)
                {
                    sPoly[ii, jj] = Math.Pow(t[ii], jj);
                }
            }


            // Writing sPoly to Textfile
            string line = "";
            for (int i = 0; i < sPoly.GetLength(0); i++)
            {
                for (int j = 0; j < sPoly.GetLength(1); j++)
                {
                    line += Convert.ToString(sPoly[i, j]) + "\t";
                }
                Console.WriteLine(line);
                line = "";
            }
            Console.WriteLine("");

            // Writing the PseudoInverse of sPoly to Textfile
            for (int i = 0; i < KSMath.pinv(sPoly).GetLength(0); i++)
            {
                for (int j = 0; j < KSMath.pinv(sPoly).GetLength(1); j++)
                {
                    line += Convert.ToString(KSMath.pinv(sPoly)[i, j]) + "\t";
                }
                Console.WriteLine(line);
                line = "";
            }


            Console.WriteLine("");
            Console.SetOut(oldOut);
            writer.Close();
            ostrm.Close();
            Console.WriteLine("Done");

        }

    }
}

Where KSMath.pinv just calls the Accord.Math Pseudoinverse function:

        public static double[,] pinv(double[,] A)
        {
            return A.PseudoInverse();
        }

The corresponding Outputfile (see attachment) is different to that produced by the same procedure in MATLAB.
FromCSharp.txt
From MATLAB.txt

I think the MATLAB calculation is correct especially because all values in the first line of the PseudoInverse calculated by Accord.Math is exactly zero.

I hope did not merely made a stupid programming error....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions