001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.math.matrix; 031 032import java.util.Random; 033 034import no.uib.cipr.matrix.DenseVector; 035import no.uib.cipr.matrix.Vector; 036import no.uib.cipr.matrix.Vector.Norm; 037 038import org.openimaj.util.function.Function; 039 040/** 041 * Perform the Gram-Schmid process on a vector, returning the orthogonal basis set 042 * whose first vector is the input 043 * 044 * http://zintegra.net/archives/738 045 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 046 */ 047public class GramSchmidtProcess implements Function<double[],Vector[]>{ 048 049 Random r = new Random(); 050 /** 051 * an unseeded random start 052 */ 053 public GramSchmidtProcess() { 054 } 055 /** 056 * Set the random seed of the purtubations 057 * @param seed 058 */ 059 public GramSchmidtProcess(int seed) { 060 this.r = new Random(seed); 061 } 062 063 private Vector project(Vector v, Vector u){ 064 return u.copy().scale((v.dot(u) / u.dot(u))); 065 } 066 067 @Override 068 public Vector[] apply(double[] in) { 069 Vector[] vmat = new Vector[in.length]; 070 071 vmat[0] = new DenseVector(in); 072 double norm = vmat[0].norm(Norm.Two); 073 vmat[0].scale(1/norm); 074 for (int j = 1; j < in.length; j++) { 075 Vector randvec = randvec(vmat[0].size(),norm); 076 vmat[j] = new DenseVector(vmat[0]).add(randvec); 077 for (int i = 0; i < j; i++) { 078 vmat[j].add(-1, project(vmat[j],vmat[i])); 079 } 080 vmat[j].scale(1/vmat[j].norm(Norm.Two)); 081 } 082 return vmat; 083 } 084 085 private Vector randvec(int nvec, double d) { 086 double[] ret = new double[nvec]; 087 for (int i = 0; i < ret.length; i++) { 088 ret[i] = d * r.nextDouble(); 089 } 090 return new DenseVector(ret); 091 } 092 093 /** 094 * @param args 095 */ 096 public static void main(String[] args) { 097 GramSchmidtProcess proc = new GramSchmidtProcess(); 098 099 Vector[] allvec = proc.apply(new double[]{0,0,1}); 100 for (Vector vector : allvec) { 101 System.out.println(vector); 102 } 103 } 104 /** 105 * @param dir 106 * @return construct and perform a {@link GramSchmidtProcess} 107 */ 108 public static Vector[] perform(double[] dir) { 109 110 return new GramSchmidtProcess(0).apply(dir); 111 } 112 113}