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
Thus, for the matrix:
1 2 34 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 matrixfunction numRows(matrixDoc as DOMDocument) as long numRows=matrixDoc.selectNodes("//row").lengthend function' numColumns retrieves the number of columns in a matrixfunction numColumns(matrixDoc as DOMDocument) as long numColumns=matrixDoc.selectNodes("//row[first()]/col").lengthend function' getRow returns a nodeList containing all the cells in a given rowfunction 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 columnfunction 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 resultfunction 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=targetDocend function' MatSum returns a matrix where each cell is the sum of the cells ' of the two matrices being addedfunction 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=targetDocend function' Inner product multiplies multiplies a row vector by a column vector ' then adds the multiplicands together to create a scalarfunction 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 columnsfunction 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=targetDocend 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.