001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.jblas.calculation; 025 026 import org.jblas.DoubleMatrix; 027 import org.ujmp.core.Matrix; 028 import org.ujmp.core.calculation.MtimesCalculation; 029 import org.ujmp.core.doublematrix.DenseDoubleMatrix2D; 030 import org.ujmp.core.interfaces.HasColumnMajorDoubleArray1D; 031 import org.ujmp.jblas.JBlasDenseDoubleMatrix2D; 032 033 public class Mtimes implements MtimesCalculation<Matrix, Matrix, Matrix> { 034 035 public void calc(Matrix source1, Matrix source2, Matrix target) { 036 final DoubleMatrix m1; 037 final DoubleMatrix m2; 038 if (source1 instanceof JBlasDenseDoubleMatrix2D) { 039 m1 = ((JBlasDenseDoubleMatrix2D) source1).getWrappedObject(); 040 } else if (source1 instanceof HasColumnMajorDoubleArray1D) { 041 m1 = new JBlasDenseDoubleMatrix2D(source1.getRowCount(), source1 042 .getColumnCount(), ((HasColumnMajorDoubleArray1D) source1) 043 .getColumnMajorDoubleArray1D()).getWrappedObject(); 044 } else { 045 m1 = new JBlasDenseDoubleMatrix2D(source1).getWrappedObject(); 046 } 047 if (source2 instanceof JBlasDenseDoubleMatrix2D) { 048 m2 = ((JBlasDenseDoubleMatrix2D) source2).getWrappedObject(); 049 } else if (source2 instanceof HasColumnMajorDoubleArray1D) { 050 m2 = new JBlasDenseDoubleMatrix2D(source2.getRowCount(), source2 051 .getColumnCount(), ((HasColumnMajorDoubleArray1D) source2) 052 .getColumnMajorDoubleArray1D()).getWrappedObject(); 053 } else { 054 m2 = new JBlasDenseDoubleMatrix2D(source2).getWrappedObject(); 055 } 056 if (target instanceof JBlasDenseDoubleMatrix2D) { 057 final DoubleMatrix t = ((JBlasDenseDoubleMatrix2D) target) 058 .getWrappedObject(); 059 m1.mmuli(m2, t); 060 } else if (target instanceof HasColumnMajorDoubleArray1D) { 061 final DoubleMatrix t = new DoubleMatrix((int) target.getRowCount(), 062 (int) target.getColumnCount(), 063 ((HasColumnMajorDoubleArray1D) target) 064 .getColumnMajorDoubleArray1D()); 065 m1.mmuli(m2, t); 066 } else if (target instanceof DenseDoubleMatrix2D) { 067 final DenseDoubleMatrix2D t = (DenseDoubleMatrix2D) target; 068 final DoubleMatrix r = new DoubleMatrix( 069 (int) source1.getRowCount(), (int) source2.getColumnCount()); 070 m1.mmuli(m2, r); 071 for (long[] c : target.allCoordinates()) { 072 t.setDouble(r.get((int) c[0], (int) c[1]), c); 073 } 074 } else { 075 final DoubleMatrix r = new DoubleMatrix( 076 (int) source1.getRowCount(), (int) source2.getColumnCount()); 077 m1.mmuli(m2, r); 078 for (long[] c : target.allCoordinates()) { 079 target.setAsDouble(r.get((int) c[0], (int) c[1]), c); 080 } 081 } 082 } 083 }