001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.examples.group.utils.math; 020 021import java.io.Serializable; 022import java.util.Arrays; 023import java.util.Random; 024 025/** 026 * A dense {@link Vector} implementation backed by a double[]. 027 */ 028public class DenseVector extends AbstractVector implements Serializable { 029 030 private static final long serialVersionUID = 1L; 031 private final double[] values; 032 033 /** 034 * Creates a dense vector of the given size. 035 */ 036 public DenseVector(final int size) { 037 this(new double[size]); 038 } 039 040 public DenseVector(final double[] values) { 041 this.values = values; 042 } 043 044 /** 045 * Instantiates a new DenseVector by copying the given other vector. 046 */ 047 public DenseVector(final ImmutableVector other) { 048 final int size = other.size(); 049 this.values = new double[size]; 050 for (int i = 0; i < size; ++i) { 051 this.values[i] = other.get(i); 052 } 053 } 054 055 public DenseVector(final DenseVector other) { 056 this.values = Arrays.copyOf(other.values, other.values.length); 057 } 058 059 @Override 060 public void set(final int i, final double v) { 061 this.values[i] = v; 062 } 063 064 @Override 065 public double get(final int i) { 066 return this.values[i]; 067 } 068 069 @Override 070 public int size() { 071 return this.values.length; 072 } 073 074 /** 075 * Access the underlying storage. This is unsafe. 076 */ 077 public double[] getValues() { 078 return this.values; 079 } 080 081 /** 082 * Creates a random Vector of size 'size' where each element is individually. 083 * drawn from Math.random() 084 * 085 * @return a random Vector of the given size where each element is 086 * individually drawn from Math.random() 087 */ 088 public static DenseVector rand(final int size) { 089 return rand(size, new Random()); 090 } 091 092 /** 093 * Creates a random Vector of size 'size' where each element is individually. 094 * drawn from Math.random() 095 * 096 * @param random the random number generator to use. 097 * @return a random Vector of the given size where each element is 098 * individually drawn from Math.random() 099 */ 100 public static DenseVector rand(final int size, final Random random) { 101 final DenseVector vec = new DenseVector(size); 102 for (int i = 0; i < size; ++i) { 103 vec.values[i] = random.nextDouble(); 104 } 105 return vec; 106 } 107 108 @Override 109 public Vector newInstance() { 110 return new DenseVector(size()); 111 } 112}