Reputation: 43
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:
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)
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.
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());
}
}
}
Upvotes: 1
Views: 1261
Reputation: 29244
Using linear algebra you can achieve the same result using matrix*matrix multiplication
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
// ...
}
Upvotes: 1
Reputation: 497
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)
Upvotes: 0