Mercurial > hg > jslab
view src/samer/maths/SparseMatrix.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line source
/* * Copyright (c) 2002, Samer Abdallah, King's College London. * All rights reserved. * * This software is provided AS iS and WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. */ package samer.maths; public class SparseMatrix { int max, n; // number of elements int[] i, j; // element indices double[] x; // element values public SparseMatrix(String name) {} public SparseMatrix(String name, SparseMatrix mask) { n=mask.n; i=(int[])mask.i.clone(); j=(int[])mask.j.clone(); x=new double[n]; } /** allocate space for E elements and remove all elements */ public void allocate(int E) { this.max=E; n=0; i=new int[E]; j=new int[E]; x=new double[E]; } /** add a new element to list. (Must be room for it) */ public void addElement( int i, int j, double x) { this.i[n]=i; this.j[n]=j; this.x[n]=x; n++; } public void times(double [] out, double [] in) { Mathx.zero(out); for (int k=0; k<n; k++) { out[i[k]]+=x[k]*in[j[k]]; } } public void transposeTimes(double [] out, double [] in) { Mathx.zero(out); for (int k=0; k<n; k++) { out[j[k]]+=x[k]*in[i[k]]; } } public void zero() { Mathx.zero(x); } public void addOuterProduct(double[] a, double[] b) { for (int k=0; k<n; k++) x[k] += a[i[k]]*b[j[k]]; } public void icaUpdate(SparseMatrix GW, double eta, double batch) { double[] dx=GW.x; Mathx.mul(dx,1/batch); for (int k=0; k<n; k++) x[k] += eta*(dx[k]-x[k]); } public void identity() { for (int k=0; k<n; k++) x[k] = (i[k]==j[k]) ? 1 : 0; } public void getMatrix(Matrix A) { A.zero(); for (int k=0; k<n; k++) A.set(i[k],j[k],x[k]); } public void changed() {} public void dispose() {} }