001    /**
002     * OneByteMatrix.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 17/12/2007
008     */
009    package jcolibri.extensions.textual.IE.common.crn.matrix;
010    
011    /**
012     * @author Juan A. Recio-Garcia
013     * @version 1.0
014     *
015     */
016    public class OneByteMatrix implements Matrix
017    {
018        byte[][] matrix;
019        boolean trasposed;
020        int rows;
021        int columns;
022        
023        public OneByteMatrix(int rows, int columns)
024        {
025            matrix = new byte[rows][columns];
026            trasposed = false;
027            this.rows = rows;
028            this.columns = columns;
029        }
030        
031        protected OneByteMatrix()
032        {
033            
034        }
035        
036        /* (non-Javadoc)
037         * @see jcolibri.extensions.textual.IE.common.crn.matrix.Matrix#getDeepTraspose()
038         */
039        public Matrix getDeepTraspose()
040        {
041            OneByteMatrix copy = new OneByteMatrix(getColumns(), getRows());
042            for(int c=0; c<columns; c++)
043                for(int r=0; r<rows; r++)
044                {
045                    if(trasposed)
046                        copy.matrix[r][c]=matrix[r][c];
047                    else
048                        copy.matrix[c][r]=matrix[r][c];
049                }
050            return copy;
051        }
052    
053        /* (non-Javadoc)
054         * @see jcolibri.extensions.textual.IE.common.crn.matrix.Matrix#getShallowTraspose()
055         */
056        public Matrix getShallowTraspose()
057        {
058            OneByteMatrix copy = new OneByteMatrix();
059            copy.columns = columns;
060            copy.rows = rows;
061            copy.matrix = matrix;
062            copy.trasposed = !trasposed;
063            return copy;
064        }
065    
066        /* (non-Javadoc)
067         * @see jcolibri.extensions.textual.IE.common.crn.matrix.Matrix#getValue(int, int)
068         */
069        public float getValue(int row, int column)
070        {
071            if(trasposed)
072                return convertByteToFloat(matrix[column][row]);
073            else
074                return convertByteToFloat(matrix[row][column]);
075        }
076    
077        /* (non-Javadoc)
078         * @see jcolibri.extensions.textual.IE.common.crn.matrix.Matrix#multiply(jcolibri.extensions.textual.IE.common.crn.matrix.Matrix)
079         */
080        public Matrix multiply(Matrix other)
081        {
082            // TODO Auto-generated method stub
083            return null;
084        }
085    
086        public void setValue(int row, int column, float value)
087        {
088            if(trasposed)
089                matrix[column][row] = convertFloatToByte(value);
090            else
091                matrix[row][column] = convertFloatToByte(value);
092            
093        }
094        public Matrix copy()
095        {
096            OneByteMatrix copy = new OneByteMatrix(getRows(), getColumns());
097            for(int r=0; r<rows; r++)
098                for(int c=0; c<columns; c++)
099                    if(trasposed)
100                        copy.matrix[r][c]=matrix[c][r];
101                    else
102                        copy.matrix[r][c]=matrix[r][c];
103            return copy;
104        }
105    
106        private float convertByteToFloat(byte value)
107        {
108            float v = value+125;
109            return v / 250;
110        }
111        
112        private byte convertFloatToByte(float value)
113        {
114            float v = value * 250;
115            byte b= ((byte)Math.round(v));
116            b-=125;
117            return b;
118        }
119    
120        public int getColumns()
121        {
122            if(trasposed)
123                return rows;
124            else
125                return columns;
126        }
127    
128        public int getRows()
129        {
130            if(trasposed)
131                return columns;
132            else
133                return rows;
134        }
135        
136        public String toString()
137        {
138            String res = "";
139            for(int r = 0; r<getRows(); r++)
140            {
141                for(int c=0; c<getColumns(); c++)
142                    res += (getValue(r,c)+",");
143                res+="\n";
144            }
145            return res;
146        }
147                
148        public float multiply(Matrix other, int thisRow, int otherColumn)
149        {
150            float sum = 0;
151            for(int i=0; i<other.getRows(); i++)
152            sum = other.getValue(i, otherColumn)*this.getValue(thisRow, i);
153            return sum;
154        }     
155        
156        public static void main(String[] args)
157        {
158            Matrix one = new OneByteMatrix(2,3);
159            one.setValue(0, 0, 0.1f);
160            one.setValue(0, 1, 0.2f);
161            one.setValue(0, 2, 0.3f);
162            one.setValue(1, 0, 0.4f);
163            one.setValue(1, 1, 0.5f);
164            one.setValue(1, 2, 0.9f);
165            System.out.print(one);
166            
167            Matrix oneST = one.getShallowTraspose();
168            System.out.print(oneST);
169            
170            Matrix oneDTT = oneST.getDeepTraspose();
171            System.out.print(oneDTT);
172            
173            Matrix oneDT = one.getDeepTraspose();
174            System.out.print(oneDT);
175        
176        }
177        
178    
179    }