Posted Friday, March 11, 2005 10:30 AM
|
|
|
|
Hi,
Application : ASP.NET
I want to initialze and populate a Matrix with data fetched from some Database.
1. Which constructor of Matrix class should i use?
2. How should i fetch data from Database (Row by Row or Column by Column) to fill the matrix.
|
|
Posted Saturday, March 12, 2005 2:41 AM
|
|
|
|
Hi
Please review the following lines of code, I think it is the simplest way to fetch data into a Matrix object from a database:
'Assuming the data is stored in an SQL server database
Dim SQLConn As SqlClient.SqlConnection
'(Use OleDb.OleDbConnection, OleDb.OleDbDataAdapter for other databases)
Dim adapter As SqlClient.SqlDataAdapter
Dim table As DataTable
'create an connection and connect to database
SQLConn = New SqlClient.SqlConnection("Server=MyServerName;Database=MyDatabase;User ID=MyUserID;Password=MyPassword;")
SQLConn.Open()
'create a DataAdapter specifying the SELECT command
'Column1, Column2, Column3 ... contain numeric data in MYTABLE table
adapter = New SqlClient.SqlDataAdapter("SELECT Column1, Column2, Column3 FROM MYTABLE", SQLConn)
'now use the DataAdapter to fill the DataTable
table = New DataTable
adapter.Fill(table)
'Move data from DataTable to Matrix
Dim A As Matrix = New Matrix(table.Rows.Count, table.Columns.Count)
Dim i, j As Integer
For i = 0 To table.Rows.Count - 1
For j = 0 To table.Columns.Count - 1
A(i, j) = table.Rows(i).Item(j)
Next
Next
As you see data is fetched in a DataTable first and then are entered in a Matrix using a double For-Next loop.
Please let me know if the above answers your questions.
Trifon Triantafillidis | Lead Developer |
|
|
|
Posted Saturday, March 12, 2005 3:27 PM
|
|
|
|
Trifon
Thanks a lot the reply. It was helpful and i am working on it.
One more thing when i calculate eigen vector and eigen value of a matrix, the results are given in complex number format. example
(1,0i), (2, 0i) etc
My matrix is a real number matrix so eigen value and vectors will be real.
So how do i get only real part of the result.
Thanks once again
Raj
|
|
Posted Sunday, March 13, 2005 12:32 AM
|
|
|
|
quote: Originally posted by mahichr
My matrix is a real number matrix so eigen value and vectors will be real.
Raj,
Real matrices can have either real or complex eigenvalues and eigenvectors that's why we have made it so that Eigen class will return results in CMatrix classes. Symmetric real matrices have only real eigenvalues and eigenvectors.
Anyway you can use the Real method of the CMatrix object to get just the real part of it e.g. :
Dim E As Eigen
E = New Eigen(MyMatrix)
Dim realEigenvectors As Matrix = E.Eigenvectors.Real
Dim firstEigenvalue As Double = E.Eigenvalue(0).Real
If your matrix is symmetric then instead of using Eigen class use the SymEigen. SymEigen returns the results in Matrix classes.
Trifon Triantafillidis | Lead Developer |
|
|
|
|
|