Example #1
0
            public static SparseMatrix ReadSparseMatrix(string path)
            {
                var result = new SparseMatrix();
                var reader = new StreamReader(path);
                var line   = reader.ReadLine();

                result.Size = int.Parse(line);
                reader.ReadLine();

                while (null != (line = reader.ReadLine()))
                {
                    var tokens = line.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    var elem = new SparseElement()
                    {
                        Val = double.Parse(tokens[0]),
                        Row = int.Parse(tokens[1]),
                        Col = int.Parse(tokens[2]),
                    };
                    if (result[elem.Row, elem.Col] == null)
                    {
                        result.Push(elem);
                    }
                    else
                    {
                        result[elem.Row, elem.Col] = new SparseElement()
                        {
                            Row = elem.Row,
                            Col = elem.Col,
                            Val = result[elem.Row, elem.Col] !.Val + elem.Val
                        };
                    }
                }

                return(result);
            }
Example #2
0
 private void Push(SparseElement e) => _matrix.Add(e);
Example #3
0
            public static SparseMatrix ReadTriDiagonal(string path)
            {
                int lineIndex = 1;
                var result    = new SparseMatrix();
                var reader    = new StreamReader(path);

                result.Size = int.Parse(reader.ReadLine());
                var p = int.Parse(reader.ReadLine());
                var q = int.Parse(reader.ReadLine());


                reader.ReadLine();
                lineIndex += 4;
                var line = string.Empty;



                int counter = 0;

                while (counter < result.Size)
                {
                    line = reader.ReadLine();
                    lineIndex++;
                    var tokens = line.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (tokens.Length == 0)
                    {
                        Console.WriteLine($"###{line}###");
                    }
                    var elem = new SparseElement()
                    {
                        Val = double.Parse(tokens[0]),
                        Row = counter,
                        Col = counter,
                    };
                    if (result[elem.Row, elem.Col] == null)
                    {
                        result.Push(elem);
                    }
                    else
                    {
                        result[elem.Row, elem.Col] = new SparseElement()
                        {
                            Row = elem.Row,
                            Col = elem.Col,
                            Val = result[elem.Row, elem.Col] !.Val + elem.Val
                        };
                    }

                    counter++;
                }
                reader.ReadLine();

                int counter2 = 0;

                while (counter2 < result.Size - p)
                {
                    line = reader.ReadLine();
                    lineIndex++;
                    var tokens = line.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (tokens.Length == 0)
                    {
                        Console.WriteLine($"%%%{lineIndex}%%%");
                    }
                    var elem = new SparseElement()
                    {
                        Val = double.Parse(tokens[0]),
                        Row = counter2,
                        Col = counter2 + p,
                    };
                    if (result[elem.Row, elem.Col] == null)
                    {
                        result.Push(elem);
                    }
                    else
                    {
                        result[elem.Row, elem.Col] = new SparseElement()
                        {
                            Row = elem.Row,
                            Col = elem.Col,
                            Val = result[elem.Row, elem.Col] !.Val + elem.Val
                        };
                    }

                    counter2++;
                }
                reader.ReadLine();
                var counter3 = 0;

                while (counter3 < result.Size - q)
                {
                    line = reader.ReadLine();
                    var tokens = line.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    var elem = new SparseElement()
                    {
                        Val = double.Parse(tokens[0]),
                        Row = counter3 + q,
                        Col = counter3,
                    };
                    if (result[elem.Row, elem.Col] == null)
                    {
                        result.Push(elem);
                    }
                    else
                    {
                        result[elem.Row, elem.Col] = new SparseElement()
                        {
                            Row = elem.Row,
                            Col = elem.Col,
                            Val = result[elem.Row, elem.Col] !.Val + elem.Val
                        };
                    }
                    counter3++;
                }

                return(result);
            }