Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day

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
end function

' numColumns retrieves the number of columns in a matrix
function numColumns(matrixDoc as DOMDocument) as long
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
   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
   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
      exit function
   end if
   for index=0 to row.length-1
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= _
       set row=getRow(matADoc,aIndex)
       for bIndex=0 to matBDoc.numColumns-1
           set column=getColumn(matBDoc,bIndex)
           set columnNode=rowNode.appendNode(targetDoc.createElement("col"))
    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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Design a Matrix

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?


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.



Thanks for your registration, follow us on our social networks to keep up-to-date