Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
node.

Thus, for the matrix:

1   2   3
4   5   6

The XML representation would be something like:


   
      
1 2 3 4 5 6

You can create a few basic DOM functions for retrieving characteristics about the matrix:

' numRows retrieves the number of rows in a matrix
function numRows(matrixDoc as DOMDocument) as long
   numRows=matrixDoc.selectNodes("//row").length
end function

' numColumns retrieves the number of columns in a matrix
function numColumns(matrixDoc as DOMDocument) as long
   numColumns=matrixDoc.selectNodes("//row[first()]/col").length
end function

' getRow returns a nodeList containing all the cells in a given row
function getRow(matrixDoc as DOMDocument,index as long) as IXMLDOMNodeList
   set getRow=matrixDoc.selectNodes("//row['"+cstr(index)+"']/col")
end function

' getCoumn returns a nodeList containing all the cells in a given column
function getColumn(matrixDoc as DOMDocument,index as long) as IXMLDOMNodeList
   set getColumn=matrixDoc.selectNodes("//row/col['"+cstr(index)+"']")
end function

' scalarMult multiplies a matrix by a scalar value and 
' returns a new matrix with the result
function scalarMult(matrixDoc as DOMDocument,scalar as Double) as DOMDocument
   dim targetDoc as DOMDocument
   dim cellList as IXMLDOMNodeList
   dim cell as IXMLDOMNode

   set targetDoc=new DOMDocument
   set targetDoc.documentElement=matrixDoc.documentElement.cloneNode(true)
   set cellList=targetDoc.selectNodes("//col")
   for each cell in cellList
       cell.text=cstr(cdbl(cell.text)*scalar)
   next
   set scalarMult=targetDoc
end function

' MatSum returns a matrix where each cell is the sum of the cells 
' of the two matrices being added
function MatSum(matADoc as DOMDocument,matBDoc as DOMDocument)
   dim targetDoc as DOMDocument
   dim cellAList as IXMLDOMNodeList
   dim cellAList as IXMLDOMNodeList
   dim cell as IXMLDOMNode
   dim index as Long

   if ((numRows(matA)<>numRows(matB)) or ((numColumns(matA)<>numColumns(matB)) then
     set matSum=nothing
     exit function
   end if
   set targetDoc=new DOMDocument
   set targetDoc.documentElement=matADoc.documentElement.cloneNode(true)
   set cellAList=targetDoc.selectNodes("//col")
   set cellBList=matBDoc.selectNodes("//col")
   for index=0 to cellAList.length-1
      cellAList.text=cstr(cdbl(cellAList(index).text)+cdbl(cellBList(index).text))
   next
   set MatSum=targetDoc
end function

' Inner product multiplies multiplies a row vector by a column vector 
' then adds the multiplicands together to create a scalar

function innerProduct(row as IXMLDOMNodeList,column as IXMLDOMNodeList) as text
   dim sum as double
   if row.length<>column.length then
      innerProduct="NaN"
      exit function
   end if
   sum=0
   for index=0 to row.length-1
      sum=sum+cdbl(row(index))*cdbl(column(index))
   next
   InnerProduct=cstr(sum)
end function


' matMult multiplies two matrices together, creating a new matrix where every 
' cell is the inner product of the first matrix's rows by the second matrix's columns

function matMult(matADoc as DOMDocument,matBDoc as DOMDocument) as DOMDocument
   dim targetDoc as DOMDocument
   dim aIndex as Integer
   dim bIndex as Integer
   dim rowNode as IXMLDOMElement
   dim columnNode as IXMLDOMElement

   targetDoc.loadXML ""
   for aIndex=0 to matADoc.numRows-1
       set rowNode= _
targetDoc.documentElement.appendNode(targetDoc.createElement("row"))
       set row=getRow(matADoc,aIndex)
       for bIndex=0 to matBDoc.numColumns-1
           set column=getColumn(matBDoc,bIndex)
           set columnNode=rowNode.appendNode(targetDoc.createElement("col"))
           columnNode.text=InnerProduct(row,column)
       next
    next
    set MatMult=targetDoc
end function

While I'm not sure I follow the second part of your question, I suspect what you're trying to say is that the resultant matrix after any operation should be unique from the initial matrices. The sample code takes care of that by either cloning one of the matrices, or creating a new matrix from scratch.

DevX Pro
 
Home » Tip Bank » XML » XSLT
Language: XSLT
Expertise: Beginner
Nov 1, 1999

Design a Matrix

Question:
How do I design a matrix using XML? This matrix needs to be a separate tree from the main XML tree. How do I communicate between the two trees?

Answer:

Design a matrix? This sounds like a college math problem. The design is pretty simple though, provided that you consider the characteristics of a matrix.

  • A matrix is a table of n rows x m columns.
  • A matrix, when multiplied by a scalar, multiplies all items of the matrix by the scalar.
  • When as n * m matrix gets multiplied by an m * p matrix, the result is an m * p in which each element is the dot product of the rows of the first multiplied by the columns of the second.

There are a few other characteristics as well, but I'm not about to write a matrix inverse transform using XML. The matrix itself would be pretty simple, and would consist of a node, a node, and a

Submit a Tip Browse "XML" Tips Browse All Tips
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap