annotate src/samer/maths/SparseMatrix.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * Copyright (c) 2002, Samer Abdallah, King's College London.
samer@0 3 * All rights reserved.
samer@0 4 *
samer@0 5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 6 * without even the implied warranty of MERCHANTABILITY or
samer@0 7 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 8 */
samer@0 9
samer@0 10 package samer.maths;
samer@0 11
samer@0 12 public class SparseMatrix {
samer@0 13 int max, n; // number of elements
samer@0 14 int[] i, j; // element indices
samer@0 15 double[] x; // element values
samer@0 16
samer@0 17 public SparseMatrix(String name) {}
samer@0 18 public SparseMatrix(String name, SparseMatrix mask) {
samer@0 19 n=mask.n;
samer@0 20 i=(int[])mask.i.clone();
samer@0 21 j=(int[])mask.j.clone();
samer@0 22 x=new double[n];
samer@0 23 }
samer@0 24
samer@0 25 /** allocate space for E elements and remove all elements */
samer@0 26 public void allocate(int E) {
samer@0 27 this.max=E; n=0;
samer@0 28 i=new int[E];
samer@0 29 j=new int[E];
samer@0 30 x=new double[E];
samer@0 31 }
samer@0 32
samer@0 33 /** add a new element to list. (Must be room for it) */
samer@0 34 public void addElement( int i, int j, double x) {
samer@0 35 this.i[n]=i;
samer@0 36 this.j[n]=j;
samer@0 37 this.x[n]=x;
samer@0 38 n++;
samer@0 39 }
samer@0 40
samer@0 41 public void times(double [] out, double [] in) {
samer@0 42 Mathx.zero(out);
samer@0 43 for (int k=0; k<n; k++) {
samer@0 44 out[i[k]]+=x[k]*in[j[k]];
samer@0 45 }
samer@0 46 }
samer@0 47
samer@0 48 public void transposeTimes(double [] out, double [] in) {
samer@0 49 Mathx.zero(out);
samer@0 50 for (int k=0; k<n; k++) {
samer@0 51 out[j[k]]+=x[k]*in[i[k]];
samer@0 52 }
samer@0 53 }
samer@0 54
samer@0 55 public void zero() { Mathx.zero(x); }
samer@0 56
samer@0 57 public void addOuterProduct(double[] a, double[] b) {
samer@0 58 for (int k=0; k<n; k++) x[k] += a[i[k]]*b[j[k]];
samer@0 59 }
samer@0 60
samer@0 61 public void icaUpdate(SparseMatrix GW, double eta, double batch) {
samer@0 62 double[] dx=GW.x;
samer@0 63 Mathx.mul(dx,1/batch);
samer@0 64 for (int k=0; k<n; k++) x[k] += eta*(dx[k]-x[k]);
samer@0 65 }
samer@0 66
samer@0 67 public void identity() { for (int k=0; k<n; k++) x[k] = (i[k]==j[k]) ? 1 : 0; }
samer@0 68 public void getMatrix(Matrix A) {
samer@0 69 A.zero();
samer@0 70 for (int k=0; k<n; k++) A.set(i[k],j[k],x[k]);
samer@0 71 }
samer@0 72
samer@0 73 public void changed() {}
samer@0 74 public void dispose() {}
samer@0 75 }