public MatrisBase <dynamic> Fill(int row,
                                         int col,
                                         float fill)
        {
            if (row <= 0 || col <= 0)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            row = Math.Min(row, (int)MatrisLimits.forRows);
            col = Math.Min(col, (int)MatrisLimits.forCols);

            List <List <dynamic> > vals = new List <List <dynamic> >();

            for (int i = 0; i < row; i++)
            {
                vals.Add(new List <dynamic>());
                for (int j = 0; j < col; j++)
                {
                    vals[i].Add(fill);
                }
            }

            MatrisBase <dynamic> res = new MatrisBase <dynamic>()
            {
                Row = row, Col = col
            };

            res.SetValues(vals);
            return(res);
        }
        public MatrisBase <dynamic> RandFloat(int row,
                                              int col,
                                              float min    = (float)0.0,
                                              float max    = (float)1.0,
                                              dynamic seed = null)
        {
            if (row <= 0 || col <= 0)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            row = Math.Min(row, (int)MatrisLimits.forRows);
            col = Math.Min(col, (int)MatrisLimits.forCols);

            Random random;
            int    s;

            if (seed != null)
            {
                random = new Random(seed);
                s      = seed;
            }
            else
            {
                s      = Environment.TickCount & int.MaxValue;
                random = new Random(s);
            }

            if (max < min)
            {
                throw new Exception(CompilerMessage.MAT_MINMAX_ORDER);
            }

            List <List <dynamic> > vals = new List <List <dynamic> >();
            float realmax = max - min;

            for (int i = 0; i < row; i++)
            {
                vals.Add(new List <dynamic>());
                for (int j = 0; j < col; j++)
                {
                    vals[i].Add((dynamic)(min + (((float)random.NextDouble()) * realmax)));
                }
            }

            MatrisBase <dynamic> res = new MatrisBase <dynamic>()
            {
                Row = row, Col = col, Seed = s, CreatedFromSeed = true
            };

            res.SetValues(vals);
            return(res);
        }
Beispiel #3
0
        private void InnerEchelon(MatrisBase <object> A, MatrisBase <object> result)
        {
            int                   nr             = A.Row;
            int                   nc             = A.Col;
            List <int>            zeroCols       = new List <int>();
            List <List <object> > filteredResult = A.Copy().GetValues();

            for (int j = 0; j < nc; j++)
            {
                if (A.IsZeroCol(j, 0, (float)0.0))
                {
                    for (int i = 0; i < nr; i++)
                    {
                        filteredResult[i].RemoveAt(j - zeroCols.Count);
                    }

                    zeroCols.Add(j);
                    nc--;
                }
            }

            result.SetValues(filteredResult);

            for (int r = 0; r < nr; r++)
            {
                if (result.IsZeroRow(r, 0, (float)0.0))
                {
                    result.SwapToEnd(r, 0);
                    nr--;
                }
            }

            int  p = 0;
            bool next;
            int  swapCount = 0;

            while (p < nr && p < nc)
            {
                next = false;
                int r = 1;
                while (float.Parse(result.GetValues()[p][p].ToString()) == (float)0.0)
                {
                    if (p + 1 < nr)
                    {
                        if (result.IsZeroRow(p, 0, (float)0.0))
                        {
                            nr--;
                        }

                        if (!Sub(result, p, nr, p, p + 1, 0).IsZeroCol(0, 0))
                        {
                            for (int ri = p + 1; ri < nr; ri++)
                            {
                                if (Math.Abs(float.Parse(result.GetValues()[ri][p].ToString())) > 1e-6)
                                {
                                    result.Swap(p, ri, based: 0);
                                    swapCount++;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            p++;
                        }
                        next = true;
                        break;
                    }
                    else
                    {
                        // Zeros to bottom
                        for (int i = 0; i < A.Row; i++)
                        {
                            if (result.IsZeroRow(i, 0, (float)0.0))
                            {
                                result.SwapToEnd(i, 0);
                            }
                        }
                        // Restore zero columns
                        if (zeroCols.Count > 0)
                        {
                            foreach (int j in zeroCols)
                            {
                                for (int i = 0; i < result.Row; i++)
                                {
                                    result.GetValues()[i].Insert(j, (dynamic)(float)0.0);
                                }
                            }
                            result.SetCol(A.Col);
                        }
                        result.FixMinusZero();
                        result.SwapCount = swapCount;
                        return;
                    }
                }

                if (next)
                {
                    continue;
                }

                for (; r >= 1 && r < (nr - p); r++)
                {
                    if (float.Parse(result.GetValues()[p + r][p].ToString()) != (float)0.0)
                    {
                        float x = -(float.Parse(result.GetValues()[p + r][p].ToString()) / float.Parse(result.GetValues()[p][p].ToString()));
                        for (int c = p; c < nc; c++)
                        {
                            result.GetValues()[p + r][c] = (dynamic)((float.Parse(result.GetValues()[p][c].ToString()) * x) + float.Parse(result.GetValues()[p + r][c].ToString()));
                        }
                    }
                }
                p++;
            }

            // Zeros to bottom
            for (int i = 0; i < A.Row; i++)
            {
                if (result.IsZeroRow(i, 0, (float)0.0))
                {
                    result.SwapToEnd(i, 0);
                }
            }
            // Restore zero columns
            if (zeroCols.Count > 0)
            {
                foreach (int j in zeroCols)
                {
                    for (int i = 0; i < result.Row; i++)
                    {
                        result.GetValues()[i].Insert(j, (dynamic)(float)0.0);
                    }
                }
                result.SetCol(A.Col);
            }

            result.FixMinusZero();
            result.SwapCount = swapCount;
        }
        public MatrisBase <dynamic> SymFloat(int dimension,
                                             float min    = 0,
                                             float max    = 1,
                                             dynamic seed = null)
        {
            if (dimension <= 0)
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            dimension = Math.Min(dimension, Math.Min((int)MatrisLimits.forRows, (int)MatrisLimits.forCols));

            Random random;
            int    s;

            if (seed != null)
            {
                random = new Random(seed);
                s      = seed;
            }
            else
            {
                s      = Environment.TickCount & int.MaxValue;
                random = new Random(s);
            }

            if (max < min)
            {
                throw new Exception(CompilerMessage.MAT_MINMAX_ORDER);
            }

            List <List <dynamic> > vals = new List <List <dynamic> >();

            for (int i = 0; i < dimension; i++)
            {
                vals.Add(new List <dynamic>());
            }

            float   realmax = max - min;
            dynamic val;

            for (int i = 0; i < dimension; i++)
            {
                for (int j = i; j < dimension; j++)
                {
                    val = (dynamic)(min + (((float)random.NextDouble()) * realmax));
                    vals[i].Add(val);
                    if (i != j)
                    {
                        vals[j].Add(val);
                    }
                }
            }

            MatrisBase <dynamic> res = new MatrisBase <dynamic>()
            {
                Row = dimension, Col = dimension, Seed = s, CreatedFromSeed = true
            };

            res.SetValues(vals);
            return(res);
        }