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.objectmatrix.calculation; 025 026 import java.util.Iterator; 027 028 import org.ujmp.core.Coordinates; 029 import org.ujmp.core.Matrix; 030 import org.ujmp.core.annotation.Annotation; 031 import org.ujmp.core.annotation.DefaultAnnotation; 032 import org.ujmp.core.exceptions.MatrixException; 033 034 public class Transpose extends AbstractObjectCalculation { 035 private static final long serialVersionUID = -2749226948849267413L; 036 037 private int swap1 = ROW; 038 039 private int swap2 = COLUMN; 040 041 public Transpose(Matrix m) { 042 this(m, ROW, COLUMN); 043 } 044 045 public Transpose(Matrix m, int swap1, int swap2) { 046 super(m); 047 this.swap1 = swap1; 048 this.swap2 = swap2; 049 if (getAnnotation() != null) { 050 setAnnotation(transposeAnnotation(m.getAnnotation(), getSize(), swap1, swap2)); 051 } 052 } 053 054 public static Annotation transposeAnnotation(Annotation aorig, long[] newSize, int swap1, 055 int swap2) { 056 if (aorig != null) { 057 Annotation a = new DefaultAnnotation(newSize); 058 a.setMatrixAnnotation(aorig.getMatrixAnnotation()); 059 for (int i = 0; i < newSize.length; i++) { 060 Matrix am = aorig.getDimensionMatrix(i); 061 am = am.transpose(Ret.NEW, swap1, swap2); 062 if (i == swap1) { 063 a.setDimensionMatrix(swap2, am); 064 } else if (i == swap2) { 065 a.setDimensionMatrix(swap1, am); 066 } else { 067 a.setDimensionMatrix(i, am); 068 } 069 } 070 return a; 071 } 072 return null; 073 } 074 075 public Object getObject(long... coordinates) throws MatrixException { 076 return getSource().getAsObject(Coordinates.transpose(coordinates, swap1, swap2)); 077 } 078 079 public long[] getSize() { 080 return Coordinates.transpose(getSource().getSize(), swap1, swap2); 081 } 082 083 public boolean contains(long... coordinates) { 084 return getSource().contains(Coordinates.transpose(coordinates, swap1, swap2)); 085 } 086 087 public boolean isSparse() { 088 return getSource().isSparse(); 089 } 090 091 public long getValueCount() { 092 return getSource().getValueCount(); 093 } 094 095 public Iterable<long[]> availableCoordinates() { 096 return new Iterable<long[]>() { 097 098 public Iterator<long[]> iterator() { 099 return new TransposedIterator(); 100 } 101 }; 102 } 103 104 class TransposedIterator implements Iterator<long[]> { 105 private final Iterator<long[]> iterator = getSource().availableCoordinates().iterator(); 106 107 public boolean hasNext() { 108 return iterator.hasNext(); 109 } 110 111 public long[] next() { 112 return Coordinates.transpose(iterator.next(), swap1, swap2); 113 } 114 115 public void remove() { 116 } 117 } 118 119 public static Annotation transposeAnnotation(Annotation annotation, long[] newSize) { 120 return transposeAnnotation(annotation, newSize, ROW, COLUMN); 121 } 122 123 }