1
votes

I had just started to learn about Math.NET Numerics and find this useful and powerful for Matrix Multiplication purpose. It's quite challenging for me now as I had just started to learn C#, I hope I could find some solutions here to assist me throughout the journey. Thank you in advance!

Objectives:

  1. Extract data from Excel file which is filled in the first & second column and put them in a nx2 Matrix (where n can be any number of rows, here I set 3000, so it's 3000x2 Matrix)

  2. Extract data from the first column of the Matrix to multiply by 5 and store in a vector 1 (VColumn). Similarly, extract data from the second column of the Matrix to multiply by 2 and store in a vector 2 (VRow). With this method I'm able to multiply each column with different values. (Is there any other more direct methods to multiply each column value individually in Matrix form using .Multiply keywords?) From what I see from the output, it is display in a line instead of one column.

Displayed in line instead of one column

  1. To place vector 1 and vector 2 into Matrix form (3000x2 Matrix format). But the output I'm getting is 0. Not sure this is the correct way though... how do I code for this part or any other alternatives?

Output zero

I'm not sure if this is the right way to code for Matrix Multiplications. If there are alternative ways, please do share as I'm still learning. Many thanks!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.Data.Text;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Matrix<Double> fileMatrix = DelimitedReader.Read<Double>
(@"C:\Users\Student\Downloads\MatricesData.xls", false, "\t", true); //Objective 1

            textBox1.AppendText(fileMatrix.ToString());

            Vector<Double> x = Vector<Double>.Build.Dense(3000, 1); //Objective 2
            Vector<Double> y = Vector<Double>.Build.Dense(3000, 1);

            Vector<Double> VColumn = fileMatrix.Column(0);
            Vector<Double> VRow = fileMatrix.Column(1);
            VColumn.Multiply(5.0, x);
            VRow.Multiply(1.0, y);

            textBox1.AppendText(x.ToString());
            textBox1.AppendText(y.ToString());

            Matrix<Double> z = Matrix<Double>.Build.Dense(3000, 2); //Objective 3: Need help
            z.InsertColumn(0, VColumn);
            z.InsertColumn(1, VRow);

            textBox1.AppendText(z.ToString());
        }
    }
}
2
You can post multiply your n×2 matrix with a 2×2 matrix with 5 and 2 in the diagonals. This way you wont need to extract vectors to manipulate them.John Alexiou

2 Answers

1
votes

Using linear algebra you can achieve the same result using matrix*matrix multiplication

pic

Using MathNet the above is

static void Main(string[] args)
{
    var fileMatrix = DelimitedReader.Read<double>("data.csv", 
        sparse: false,
        delimiter: "\t",
        hasHeaders: true
        );

    // 5.2  1.8
    // 3.2  0.2
    // 1.8  2.8
    // 4.4  3.4
    // 5.2  0.6
    // ...


    var coefMatrix = CreateMatrix.DiagonalOfDiagonalArray(new double[] { 5, 2 });

    var resultMatrix = fileMatrix * coefMatrix;

    // 26  3.6
    // 16  0.4
    //  9  5.6
    // 22  6.8
    // 26  1.2
    // 16  2.8
    // ...

}
0
votes

InsertColumn creates a new matrix and inserts one column:

Matrix InsertColumn(int columnIndex, Vector column) Creates a new matrix and inserts the given column at the given index.

You want some version of SetColumn:

void SetColumn(int columnIndex, Vector column) or void SetColumn(int columnIndex, int rowIndex, int length, Vector column) or void SetColumn(int columnIndex, Double[] column)