Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SSIS Matrix Multiplication in c#

Status
Not open for further replies.

Maccaman

Technical User
Nov 21, 2009
1
AU
Hello All,

That was very helpful, but I have become a little stuck with this task so I'm going to be as thorough as possible with my explanation.

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:
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];
        

    }

}


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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top