def gentrait(rows, cols): myclass = classname(rows, cols) fd = startfile("traits", myclass) def line(s=""): print >> fd, s line("public interface %s extends Matrix {" % myclass) line("}") fd.flush() fd.close()
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from common import startfile, SIZES, classname, cells if __name__ == '__main__': fd = startfile("functions", "Identity") def line(s=""): print >> fd, s for rows in SIZES: line("import org.dnikulin.jula.fixed.%s;" % classname(rows, rows)) line() line("public final class Identity {") for rows in SIZES: line(" public static void setIdentity(final %s a) {" % classname(rows, rows)) for (row, col, label) in cells(rows, rows): value = (1 if (row == col) else 0) line(" a.%s = %d;" % (label, value)) line(" }") line() line(" private Identity() {}") line("}") fd.flush()
def genmatrix(rows, cols): myclass = classname(rows, cols) fd = startfile("fixed", myclass) def line(s=""): print >> fd, s traits = [("Matrix_%d_N" % rows), ("Matrix_M_%d" % cols)] if (rows == cols): traits.append("Matrix_M_M") for trait in traits: line("import org.dnikulin.jula.traits.%s;" % trait) line() line("import org.dnikulin.jula.functions.Copy;") line() line("public final class %s implements %s {" % (myclass, ", ".join(traits))) line(" public static final int rows = %d;" % rows) line(" public static final int cols = %d;" % cols) line(" public static final int size = (rows * cols);") line() for row in range(rows): labels = ", ".join([mklabel(row, col) for col in range(cols)]) line(" public double %s;" % labels) line() line(" @Override") line(" public int getRows() {") line(" return rows;") line(" }") line() line(" @Override") line(" public int getCols() {") line(" return cols;") line(" }") line() line(" @Override") line(" public double get(final int row, final int col) {") line(" assert(row >= 0);") line(" assert(col >= 0);") line(" assert(row < rows);") line(" assert(col < cols);") line() line(" switch ((row * cols) + col) {") for (row, col, label) in cells(rows, cols): off = (row * cols) + col line(" case %2d: return %s;" % (off, label)) line(" default: return 0;") line(" }") line(" }") line() line(" @Override") line(" public void set(final int row, final int col, final double val) {") line(" assert(row >= 0);") line(" assert(col >= 0);") line(" assert(row < rows);") line(" assert(col < cols);") line() line(" switch ((row * cols) + col) {") for (row, col, label) in cells(rows, cols): off = (row * cols) + col line(" case %2d: %s = val; return;" % (off, label)) line(" default: return;") line(" }") line(" }") line() line(" @Override") line(" public %s clone() {" % (myclass)) line(" final %s that = new %s();" % (myclass, myclass)) line(" Copy.copy(this, that);") line(" return that;") line(" }") line("}") fd.flush() fd.close()
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from common import startfile, SIZES, classname, cells, mklabel if __name__ == '__main__': fd = startfile("functions", "Multiply") def line(s=""): print >> fd, s for rows in SIZES: for cols in SIZES: line("import org.dnikulin.jula.fixed.%s;" % classname(rows, cols)) line() line("public final class Multiply {") for rows in SIZES: for idxs in SIZES: for cols in SIZES: cnameA = classname(rows, idxs) cnameB = classname(idxs, cols) cnameC = classname(rows, cols) line(" public static void multiply(final %s a, final %s b, final %s c) {" % (cnameA, cnameB, cnameC)) for (_, _, label) in cells(rows, idxs): line(" final double a_%s = a.%s;" % (label, label))
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from common import startfile, SIZES, classname, cells if __name__ == '__main__': fd = startfile("functions", "Copy") def line(s=""): print >> fd, s for rows in SIZES: for cols in SIZES: line("import org.dnikulin.jula.fixed.%s;" % classname(rows, cols)) line() line("public final class Copy {") for rows in SIZES: for cols in SIZES: cname = classname(rows, cols) line(" public static void copy(final %s a, final %s b) {" % (cname, cname)) for (row, col, label) in cells(rows, cols): line(" b.%s = a.%s;" % (label, label)) line(" }") line() line(" private Copy() {}") line("}")
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from common import startfile, SIZES, classname, cells if __name__ == '__main__': fd = startfile("functions", "Zero") def line(s=""): print >> fd, s for rows in SIZES: for cols in SIZES: line("import org.dnikulin.jula.fixed.%s;" % classname(rows, cols)) line() line("public final class Zero {") for rows in SIZES: for cols in SIZES: line(" public static void setZero(final %s a) {" % classname(rows, cols)) for (_, _, label) in cells(rows, cols): line(" a.%s = 0;" % label) line(" }") line() line(" private Zero() {}") line("}") fd.flush()
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from common import startfile, SIZES, classname, cells, mklabel if __name__ == '__main__': fd = startfile("functions", "Transpose") def line(s=""): print >> fd, s for rows in SIZES: for cols in SIZES: line("import org.dnikulin.jula.fixed.%s;" % classname(rows, cols)) line() line("public final class Transpose {") for rows in SIZES: for cols in SIZES: iclass = classname(rows, cols) oclass = classname(cols, rows) line(" public static void transpose(final %s a, final %s b) {" % (iclass, oclass)) for (row, col, ilabel) in cells(rows, cols): olabel = mklabel(col, row) line(" b.%s = a.%s;" % (olabel, ilabel)) line(" }") line()