JSci.mathml.MathMLMatrixElementImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.mathml;
import org.w3c.dom.*;
import org.w3c.dom.mathml.*;
import org.apache.xerces.dom.*;
/**
* Implements a MathML matrix
element.
* @version 1.0
* @author Mark Hale
*/
public class MathMLMatrixElementImpl extends MathMLElementImpl implements MathMLMatrixElement {
/**
* Constructs a MathML matrix
element.
*/
public MathMLMatrixElementImpl(MathMLDocumentImpl owner, String qualifiedName) {
super(owner, qualifiedName);
}
public int getNrows() {
return getRowsGetLength();
}
public int getNcols() {
return getRow(1).getNEntries();
}
public MathMLNodeList getRows() {
return new MathMLNodeList() {
public int getLength() {
return getRowsGetLength();
}
public Node item(int index) {
return getRowsItem(index);
}
};
}
public MathMLMatrixrowElement getRow(int index) throws DOMException {
Node row = getRowsItem(index-1);
if (row == null) {
throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
}
return (MathMLMatrixrowElement) row;
}
public MathMLMatrixrowElement setRow(MathMLMatrixrowElement newRow, int index) throws DOMException {
final int rowsLength = getRowsGetLength();
if ((index < 1) || (index > rowsLength+1)) {
throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
}
if (index == rowsLength+1) {
return (MathMLMatrixrowElement) appendChild(newRow);
} else {
return (MathMLMatrixrowElement) replaceChild(newRow, getRowsItem(index-1));
}
}
public MathMLMatrixrowElement insertRow(MathMLMatrixrowElement newRow, int index) throws DOMException {
final int rowsLength = getRowsGetLength();
if ((index < 0) || (index > rowsLength+1)) {
throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
}
if ((index == 0) || (index == rowsLength+1)) {
return (MathMLMatrixrowElement) appendChild(newRow);
} else {
return (MathMLMatrixrowElement) insertBefore(newRow, getRowsItem(index-1));
}
}
public MathMLMatrixrowElement removeRow(int index) throws DOMException {
Node row = getRowsItem(index-1);
if (row == null) {
throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
}
return (MathMLMatrixrowElement) removeChild(row);
}
public void deleteRow(int index) throws DOMException {
removeRow(index);
}
private int getRowsGetLength() {
final int length = getLength();
int numRows = 0;
for (int i = 0; i < length; i++) {
if (item(i) instanceof MathMLMatrixrowElement) {
numRows++;
}
}
return numRows;
}
private Node getRowsItem(int index) {
final int rowsLength = getRowsGetLength();
if ((index < 0) || (index >= rowsLength))
return null;
Node node = null;
int n = -1;
for (int i = 0; n < index; i++) {
node = item(i);
if (node instanceof MathMLMatrixrowElement) {
n++;
}
}
return node;
}
}