Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 SSIS Matrix Multiplication in c#

Author  Topic 

Maccaman
Starting Member

16 Posts

Posted - 2009-11-21 : 13:31:49
Hello All,

I am using Microsoft's Business Intelligence Studio, and I am trying to get data from a table (which is a matrix) and multiply this matrix.

Using an ODE DB Source, I run a query on my data to produce the following table:

Average_Transition MatrixRow MatrixColum MatrixSize
1 1 1 3
2 1 2 3
3 1 3 3
2 2 1 3
2 2 2 3
4 2 3 3
3 3 1 3
1 3 2 3
5 3 3 3


which I connect to a script component. The script component has the input rows as above, and the one output column titled "FinalMatrix". What I am trying to do is store the above information into an array, then have the script return the square of the matrix.

The above data would produce the following matrix:

1 2 3
2 2 4
3 1 5

which when squared becomes:

14 9 26
18 12 34
20 13 38

But when I run the script I end up with:

1 0 0
0 4 0
0 0 25


The script component (in c#) is as follows
[code="plain"]using System;
using System.Data;
using System.Collections;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
///////////////////////////////// Define Variables ///////////////////////////////////////////////
// Insert data into matrix



int rowNum = Row.MatrixRow - 1;
int colNum = Row.MatrixColumn - 1;
int FindSize = Row.MatrixSize;
float AverageTransition = Row.AverageTransition;
int[,] FindSize2 = new int [FindSize,FindSize];
FindSize2[rowNum, colNum] = Row.MatrixSize;
FindSize2.SetValue(Row.MatrixSize, rowNum, colNum);
int FindSize3 = FindSize2[rowNum, colNum];

float[,] c = new float[FindSize, FindSize];
float[,] arrMyArray = new float[FindSize, FindSize];

arrMyArray[rowNum, colNum] = AverageTransition;
arrMyArray.SetValue(AverageTransition, rowNum, colNum);

System.Windows.Forms.MessageBox.Show("Row Num = " + rowNum + " " + ", Col Num = " + " " + colNum + ", Value = " + " " + arrMyArray[rowNum, colNum].ToString());

//The above line shows that the matrix is populated properly

/////////////////////////////////////////////////////////////////////////////////////////////////



if (arrMyArray.GetLength(1) == arrMyArray.GetLength(0)) //Standard c# matrix multiplication code
{
c = new float[FindSize3, FindSize3];

for (int i = 0; i < FindSize3; i++)
{
for (int j = 0; j < FindSize3; j++)
{
c[i, j] = 0;
for (int k = 0; k < FindSize3; k++)
c[i, j] += arrMyArray[i, k] * arrMyArray[k, j];

}
}
}
else
{
System.Windows.Forms.MessageBox.Show("Not a Square Matrix");
}




////////////////////////////////////Output Rows /////////////////////////////////////////////////

OutputBuffer.AddRow();
OutputBuffer.FinalMatrix = c[rowNum, colNum];


}

}

[/code]


I can't figure what is going on here. If I hardcode the array into the script then the multiplication part works fine, and
the squared matrix is calculated correctly. Why doesn't my code work? Im guessing it has something to do with the multiple input rows. I can't hardcode this script as I want to be able to work with multiple matrix sizes. Does anyone know how to help with this?

Thanks in advance.

"I know the human being and fish can co-exist peacefully" - George W. Bush.
Obviously Mr Bush has never been fishing with me :)
   

- Advertisement -