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.core.bytematrix.impl; 025 026 import org.ujmp.core.Matrix; 027 import org.ujmp.core.bytematrix.stub.AbstractDenseByteMatrix2D; 028 import org.ujmp.core.doublematrix.impl.DefaultDenseDoubleMatrix2D; 029 import org.ujmp.core.exceptions.MatrixException; 030 import org.ujmp.core.interfaces.HasColumnMajorByteArray1D; 031 032 public class DefaultDenseByteMatrix2D extends AbstractDenseByteMatrix2D implements HasColumnMajorByteArray1D { 033 private static final long serialVersionUID = -7637602510970244322L; 034 035 private byte[] values = null; 036 037 private long[] size = null; 038 039 private int rows = 0; 040 041 private int cols = 0; 042 043 public DefaultDenseByteMatrix2D(Matrix m) throws MatrixException { 044 this.rows = (int) m.getRowCount(); 045 this.cols = (int) m.getColumnCount(); 046 this.size = new long[] { rows, cols }; 047 if (m instanceof DefaultDenseByteMatrix2D) { 048 byte[] v = ((DefaultDenseByteMatrix2D) m).values; 049 this.values = new byte[v.length]; 050 System.arraycopy(v, 0, this.values, 0, v.length); 051 } else { 052 this.values = new byte[rows * cols]; 053 for (long[] c : m.allCoordinates()) { 054 setByte(m.getAsByte(c), c); 055 } 056 } 057 } 058 059 public DefaultDenseByteMatrix2D(long... size) { 060 this.rows = (int) size[ROW]; 061 this.cols = (int) size[COLUMN]; 062 this.size = new long[] { rows, cols }; 063 this.values = new byte[rows * cols]; 064 } 065 066 public DefaultDenseByteMatrix2D(byte[] v, int rows, int cols) { 067 this.rows = rows; 068 this.cols = cols; 069 this.size = new long[] { rows, cols }; 070 this.values = v; 071 } 072 073 public long[] getSize() { 074 return size; 075 } 076 077 078 public long getRowCount() { 079 return rows; 080 } 081 082 083 public long getColumnCount() { 084 return cols; 085 } 086 087 public byte getByte(long row, long column) { 088 return values[(int) (column * rows + row)]; 089 } 090 091 public void setByte(byte value, long row, long column) { 092 values[(int) (column * rows + row)] = value; 093 } 094 095 public byte getByte(int row, int column) { 096 return values[column * rows + row]; 097 } 098 099 public void setByte(byte value, int row, int column) { 100 values[column * rows + row] = value; 101 } 102 103 104 public final Matrix plus(double v) { 105 double[] result = new double[values.length]; 106 for (int i = result.length; --i != -1;) { 107 result[i] = values[i] + v; 108 } 109 return new DefaultDenseDoubleMatrix2D(result, rows, cols); 110 } 111 112 113 public final Matrix minus(double v) { 114 double[] result = new double[values.length]; 115 for (int i = result.length; --i != -1;) { 116 result[i] = values[i] - v; 117 } 118 return new DefaultDenseDoubleMatrix2D(result, rows, cols); 119 } 120 121 122 public final Matrix times(double v) { 123 double[] result = new double[values.length]; 124 for (int i = result.length; --i != -1;) { 125 result[i] = values[i] * v; 126 } 127 return new DefaultDenseDoubleMatrix2D(result, rows, cols); 128 } 129 130 131 public final Matrix divide(double v) { 132 double[] result = new double[values.length]; 133 for (int i = result.length; --i != -1;) { 134 result[i] = values[i] / v; 135 } 136 return new DefaultDenseDoubleMatrix2D(result, rows, cols); 137 } 138 139 140 public final Matrix copy() throws MatrixException { 141 byte[] result = new byte[values.length]; 142 System.arraycopy(values, 0, result, 0, values.length); 143 Matrix m = new DefaultDenseByteMatrix2D(result, rows, cols); 144 if (getAnnotation() != null) { 145 m.setAnnotation(getAnnotation().clone()); 146 } 147 return m; 148 } 149 150 151 public final Matrix transpose() { 152 final byte[] result = new byte[cols * rows]; 153 for (int c = rows; --c != -1;) { 154 for (int r = cols; --r != -1;) { 155 result[c * cols + r] = values[r * rows + c]; 156 } 157 } 158 return new DefaultDenseByteMatrix2D(result, cols, rows); 159 } 160 161 162 public byte[] getColumnMajorByteArray1D() { 163 return values; 164 } 165 166 }