org.apache.sis.math.Vector类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(14.3k)|赞(0)|评价(0)|浏览(88)

本文整理了Java中org.apache.sis.math.Vector类的一些代码示例,展示了Vector类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vector类的具体详情如下:
包路径:org.apache.sis.math.Vector
类名称:Vector

Vector介绍

[英]A vector of real numbers. An instance of Vector can be a wrapper around an array of Java primitive type (typically float[] or double[]), or it may be a function calculating values on the fly. Often the two above-cited cases are used together, for example in a time series where:

  • x[i] is a linear function of i (e.g. the sampling time of measurements performed at a fixed time interval)
  • y[i] is the measurement of a phenomenon at time x[i].
    Instantiation Instances of Vector are usually created by calls to the #create(Object,boolean) static method. The supplied array is not cloned – changes to the primitive array are reflected in the vector, and vice-versa. Vectors can be a view over a subsection of the given array, or can provide a view of the elements in reverse order, etc. The example below creates a view over a subsection: java

Usage
The methods that are most often used after Vector creation are #size() and #doubleValue(int)or #intValue(int). Those methods make abstraction of the underlying data type. For example if the vector is backed by an array of type int[], then calls to doubleValue(index) will:

  • Convert the int[index] value to a double value.
  • If #isUnsigned() is true, apply the necessary bitmask before conversion.
    Widening conversions (for example from short to long) are always allowed. Narrowing conversions are allowed if the result can be represented at least approximately by the target type. For example conversions from double to float are always allowed (values that are too large for the float type are represented by positive of negative infinity), but conversions from long to short are allowed only if the value is between Short#MIN_VALUE and Short#MAX_VALUE inclusive.

Comparison with other API: the above functionalities look like similar functionalities provided by java.nio.ByteBufferin standard Java, but they actually serve different purposes. The ByteBuffer getter methods (for example getShort(int), getLong(int), etc.) allow to decode a sequence of bytes in a way determined by the type of the value to decode (2 bytes for a short, 8 bytes for a long, etc.) – the type of the stored value must be known before to read it. By contrast, this Vector class is used in situations where the decoding has already been done by the code that create a Vector object, but the data type may not be known by the code that will use the Vector object. For example a method performing a numerical calculation may want to see the data as double values without concern about whether the data were really stored as double or as float values. For the situations where a Buffer is needed, inter-operability is provided by the #buffer()method and by accepting buffer in the #create(Object,boolean) method.
[中]实数向量。Vector的实例可以是Java基元类型(通常为float[]或double[])数组的包装器,也可以是动态计算值的函数。上述两种情况通常一起使用,例如在时间序列中:
x[i]是i的线性函数(例如,在固定时间间隔内进行的测量的采样时间)
y[i]是对时间x[i]的现象的测量。
向量的实例化实例通常通过调用#create(Object,boolean)静态方法来创建。未克隆提供的数组–对基元数组的更改将反映在向量中,反之亦然。向量可以是给定数组的一个子段上的视图,也可以提供元素的相反顺序视图,等等。下面的示例创建了一个子段上的视图:java
用法
向量创建后最常用的方法是#size()和#doubleValue(int)或#intValue(int)。这些方法对底层数据类型进行了抽象。例如,如果向量由int[]类型的数组支持,那么对doubleValue(index)的调用将:
将int[index]值转换为双精度值。
如果#isUnsigned()为真,请在转换前应用必要的位掩码。
始终允许扩大转换(例如从短到长)。如果结果至少可以用目标类型近似表示,则允许缩小转换范围。例如,始终允许从double到float的转换(对于float类型来说太大的值用负无穷大的正表示),但只有当值介于short#MIN_值和short#MAX_值之间时,才允许从long到short的转换。
与其他API的比较:上述功能看起来类似于java提供的功能。尼奥。ByteBufferin是标准Java,但它们实际上有不同的用途。ByteBuffer getter方法(例如getShort(int)、getLong(int)等)允许以由要解码的值的类型确定的方式对字节序列进行解码(短值为2字节,长值为8字节,等等)——读取之前必须知道存储值的类型。相比之下,该向量类在以下情况下使用:解码已经由创建
向量对象的代码
完成,但将
使用
向量对象的代码可能不知道数据类型。例如,执行数值计算的方法可能希望将数据视为双精度值,而不关心数据是真正存储为双精度值还是浮点值。对于需要缓冲区的情况,可以通过#Buffer()方法和#create(Object,boolean)方法接受缓冲区来提供互操作性。

代码示例

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Returns {@code true} if this vector contains only integer values.
 * This method may iterate over all values for performing this verification.
 *
 * @return {@code true} if this vector contains only integer values.
 */
public boolean isInteger() {
  if (!Numbers.isInteger(getElementType())) {
    for (int i=size(); --i >= 0;) {
      final double v = doubleValue(i);
      if (v != Math.floor(v)) {
        return false;
      }
    }
  }
  return true;
}

代码示例来源:origin: org.apache.sis.core/sis-feature

/**
 * Creates a new list for the given times.
 */
DateList(final long[] millis) {
  times = Vector.create(millis, false).compress(0);
}

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Returns the value at the given index.
 */
@Override
public int intValue(int index) {
  final Vector v;
  if (index < limit) {
    v = first;
  } else {
    v = second;
    index -= limit;
  }
  return v.intValue(index);
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
 * Wraps the given array in a vector of length {@link #numPoints}. This method should be
 * invoked only when this builder has been created by {@link #LinearTransformBuilder()}.
 * This can be identified by {@code sources != null} or {@code gridSize == null}.
 */
private Vector vector(final double[] data) {
  assert gridSize == null;
  return Vector.create(data, false).subList(0, numPoints);
}

代码示例来源:origin: apache/sis

/**
 * Returns the minimal and maximal values found in this vector.
 *
 * @return minimal and maximal values found in this vector.
 */
public NumberRange<?> range() {
  return range(null, size());
}

代码示例来源:origin: apache/sis

/**
 * Tests the case where values in a grid are repeated horizontally.
 */
@Test
public void testHorizontal() {
  Vector vec = Vector.create(new int[] {
      10, 10, 10, 10,
      12, 12, 12, 12,
      15, 15, 15, 15}, false);
  vec = new RepeatedVector(vec, vec.repetitions(), 0);
  assertArrayEquals(new int[] {4}, vec.repetitions());
  assertEquals(10, vec.intValue  ( 0));
  assertEquals(10, vec.shortValue( 1));
  assertEquals(10, vec.longValue ( 2));
  assertEquals(10, vec.intValue  ( 3));
  assertEquals(12, vec.intValue  ( 4));
  assertEquals(12, vec.shortValue( 7));
  assertEquals(15, vec.longValue ( 8));
  assertEquals(15, vec.intValue  (11));
  Vector sub = vec.subSampling(0, 4, 3);
  assertFalse("Expected the backing array.", sub instanceof RepeatedVector);
  assertArrayEquals(new float[] {10, 12, 15}, sub.floatValues(), (float) STRICT);
}

代码示例来源:origin: org.apache.sis.core/sis-utility

final int length = size();
final Number inc = increment(tolerance);
if (inc != null) {
  return createSequence(getElementType(), get(0), inc, length);
do if (i >= length) {
  final Double NaN = Numerics.valueOf(Double.NaN);
  return new SequenceVector.Doubles(getElementType(), NaN, NaN, length);
} while (isNaN(i++));
final NumberRange<?> range = range();
if (range != null && !range.isEmpty()) {
  final Number min = range.getMinValue();
  final boolean isInteger = (min.doubleValue() >= Long.MIN_VALUE &&
                max.doubleValue() <= Long.MAX_VALUE &&
                isInteger());                                // May scan the vector.
  Vector vec;
  if (isInteger) {

代码示例来源:origin: apache/sis

/**
 * Tests {@link ArrayVector} backed by an array of float type.
 */
@Test
public void testFloatArray() {
  final float[] array = new float[400];
  for (int i=0; i<array.length; i++) {
    array[i] = (i + 100) * 10;
  }
  vector = Vector.create(array, false);
  assertEquals("Floats", vector.getClass().getSimpleName());
  assertSame(vector, Vector.create(vector, false));
  assertEquals(array.length, vector.size());
  assertEquals(Float.class, vector.getElementType());
  /*
   * Tests element values.
   */
  for (int i=0; i<array.length; i++) {
    assertEquals(array[i], vector.floatValue (i), 0f);
    assertEquals(array[i], vector.doubleValue(i), STRICT);
  }
}

代码示例来源:origin: apache/sis

extra[i] = (i + 40) * 10;
Vector v1 = Vector.create(array, false);
Vector v2 = Vector.create(extra, false);
Vector v3 = v1.concatenate(v2);
assertEquals("Length of V3 should be the sum of V1 and V2 length.", 60, v3.size());
assertEquals("Component type should be the common parent of V1 and V2.", Number.class, v3.getElementType());
assertEquals("Sample from V1.", Float  .valueOf(200), v3.get(20));
assertEquals("Sample from V2.", Integer.valueOf(500), v3.get(50));
for (int i=0; i<60; i++) {
  assertEquals(i*10, v3.floatValue(i), 0f);
assertSame("Should be able to restitute the original vector.", v1, v3.subList( 0, 40));
assertSame("Should be able to restitute the original vector.", v2, v3.subList(40, 60));
final Vector expected = v3.pick(10, 25, 30, 0, 35, 39);
v2 = v1.pick( 0, 35, 39);
v1 = v1.pick(10, 25, 30);
v3 = v1.concatenate(v2);
assertEquals(expected, v3);
assertFalse("Expected concatenation of the indices.", v3 instanceof ConcatenatedVector);

代码示例来源:origin: apache/sis

/**
 * Tests a vector backed by an array of strings.
 * This is not recommended, but happen in GDAL extensions of GeoTIFF.
 * See {@code org.apache.sis.storage.geotiff.Type.ASCII}.
 */
@Test
public void testStringArray() {
  vector = Vector.create(new String[] {"100", "80", "-20"}, false);
  assertEquals(  3, vector.size());
  assertEquals(100, vector.intValue(0));
  assertEquals( 80, vector.shortValue(1));
  assertEquals(-20, vector.doubleValue(2), STRICT);
}

代码示例来源:origin: apache/sis

final NumberRange<?> range = source.range();
final double min  = range.getMinDouble(true);
final double span = range.getMaxDouble(true) - min;
final Number increment = source.increment(EPS * span);
double inc;
if (increment != null) {
} else {
  inc = span;
  final int size = source.size();
  for (int i=0; i<size; i++) {
    double v = source.doubleValue(i) - min;
    if (Math.abs(v % inc) > EPS) {
      do {
fromGrid.setElement(dim,   2, min);
final double n = span / inc;
if (n >= 0.5 && n < source.size() - 0.5) {          // Compare as 'double' in case the value is large.
  return ((int) Math.round(n)) + 1;

代码示例来源:origin: apache/sis

/**
 * Tests the case where values in a grid are repeated vertically.
 */
@Test
public void testVertical() {
  Vector vec = Vector.create(new int[] {
      10, 12, 15, 18,
      10, 12, 15, 18,
      10, 12, 15, 18}, false);
  vec = new RepeatedVector(vec, vec.repetitions(), 0);
  assertArrayEquals(new int[] {1,4}, vec.repetitions());
  assertEquals(10, vec.intValue  ( 0));
  assertEquals(12, vec.shortValue( 1));
  assertEquals(15, vec.longValue ( 2));
  assertEquals(18, vec.intValue  ( 3));
  assertEquals(10, vec.intValue  ( 4));
  assertEquals(18, vec.shortValue( 7));
  assertEquals(10, vec.longValue ( 8));
  assertEquals(15, vec.intValue  (10));
  Vector sub = vec.subList(0, 4);
  assertFalse("Expected the backing array.", sub instanceof RepeatedVector);
  assertArrayEquals(new float[] {10, 12, 15, 18}, sub.floatValues(), (float) STRICT);
}

代码示例来源:origin: apache/sis

/**
 * Sets the values in this variable. The values are normally read from the netCDF file by the {@link #read()} method,
 * but this {@code setValues(Object)} method may also be invoked if we want to overwrite those values.
 *
 * @param  array  the values as an array of primitive type (for example {@code float[]}.
 */
final void setValues(final Object array) {
  Vector data = createDecimalVector(array, dataType.isUnsigned);
  /*
   * This method is usually invoked with vector of increasing or decreasing values.  Set a tolerance threshold to the
   * precision of gratest (in magnitude) number, provided that this precision is not larger than increment. If values
   * are not sorted in increasing or decreasing order, the tolerance computed below will be smaller than it could be.
   * This is okay it will cause more conservative compression (i.e. it does not increase the risk of data loss).
   */
  double tolerance = 0;
  if (Numbers.isFloat(data.getElementType())) {
    final int n = data.size() - 1;
    if (n >= 0) {
      double first = data.doubleValue(0);
      double last  = data.doubleValue(n);
      double inc   = Math.abs((last - first) / n);
      if (!Double.isNaN(inc)) {
        double ulp = Math.ulp(Math.max(Math.abs(first), Math.abs(last)));
        tolerance = Math.min(inc, ulp);
      }
    }
  }
  values = data.compress(tolerance);
  values = SHARED_VECTORS.unique(values);
}

代码示例来源:origin: apache/sis

final Vector[] vectors = new Vector[n];
for (Period p = properties[index]; p != null; p = p.previous) {
  vectors[--n] = Vector.create(p.value, false);
  Vector v = vectors[i];
  int length;
  if (v == null || (length = v.size()) == 0) {
    continue;
      v = v.subList(dimension, length);                               // Skip the first coordinate.
      length -= dimension;
      if (length == 0) {
  final Vector v = vectors[--i];
  if (v != null) {
    int c = v.size() / dimension;
    if (c == 1) {
      times[--numPts] = p.endTime;

代码示例来源:origin: apache/sis

Vector vec =  Vector.create(new byte[] {30, 120, -50, -120}, false);
Vector compressed = vec.compress(0);
assertSame(vec, compressed);
vec =  Vector.create(new double[] {30, 120, -50, -120}, false);
assertNotSame(vec, compressed = vec.compress(0));
assertInstanceOf("vector.compress(0)", ArrayVector.class, compressed);
assertEquals("elementType", Byte.class, compressed.getElementType());
assertFalse("isUnsigned()", compressed.isUnsigned());
assertContentEquals(vec, compressed);
vec =  Vector.create(new float[] {30, 120, 250, 1}, false);
assertNotSame(vec, compressed = vec.compress(0));
assertInstanceOf("vector.compress(0)", ArrayVector.class, compressed);
assertEquals("elementType", Byte.class, compressed.getElementType());
assertTrue("isUnsigned()", compressed.isUnsigned());
assertContentEquals(vec, compressed);
vec =  Vector.create(new long[] {32000, 120, -25000, 14}, false);
assertNotSame(vec, compressed = vec.compress(0));
assertInstanceOf("vector.compress(0)", ArrayVector.class, compressed);
assertEquals("elementType", Short.class, compressed.getElementType());
assertFalse("isUnsigned()", compressed.isUnsigned());
assertContentEquals(vec, compressed);
vec =  Vector.create(new float[] {3, 60000, 25, 4}, false);
assertNotSame(vec, compressed = vec.compress(0));
assertInstanceOf("vector.compress(0)", ArrayVector.class, compressed);
assertEquals("elementType", Short.class, compressed.getElementType());

代码示例来源:origin: apache/sis

/**
 * Tests {@link SequenceVector} with float values.
 */
@Test
public void testSequenceOfFloats() {
  vector = Vector.createSequence(100, 0.1, 10);
  assertEquals(Double.class, vector.getElementType());
  assertEquals(10, vector.size());
  for (int i=0; i<vector.size(); i++) {
    assertEquals(100 + 0.1*i, vector.doubleValue(i), 1E-10);
  }
}

代码示例来源:origin: org.apache.sis.storage/sis-netcdf

/**
 * Returns the number of features.
 */
@Override
public long estimateSize() {
  return counts.size();
}

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Return the ordinate value at the given dimension.
 */
@Override
public double getOrdinate(final int dimension) {
  return ordinates[dimension].doubleValue(index);
}

代码示例来源:origin: org.apache.sis.core/sis-utility

if (isSigned || (min >= 0 && max <= 0xFF)) {
  if (source instanceof Bytes) return null;
  final byte[] array = new byte[source.size()];
  for (int i=0; i < array.length; i++) {
    array[i] = (byte) source.intValue(i);
if (isSigned || (min >= 0 && max <= 0xFFFF)) {
  if (source instanceof Shorts) return null;
  final short[] array = new short[source.size()];
  for (int i=0; i < array.length; i++) {
    array[i] = (short) source.intValue(i);
if (isSigned || (min >= 0 && max <= 0xFFFFFFFF)) {
  if (source instanceof Integers) return null;
  final int[] array = new int[source.size()];
  for (int i=0; i < array.length; i++) {
    array[i] = (int) source.longValue(i);
  final long[] array = new long[source.size()];
  for (int i=0; i < array.length; i++) {
    array[i] = source.longValue(i);

代码示例来源:origin: apache/sis

/**
 * Creates a vector of repeated data from the result of a call to {@link Vector#repetitions(int...)}.
 *
 * @param base         the vector on which this vector is derived from.
 * @param repetitions  results of {@link Vector#repetitions(int...)}. Must be non-empty.
 * @param tolerance    tolerance factor for compression of the base vector.
 */
RepeatedVector(final Vector base, final int[] repetitions, final double tolerance) {
  size        = base.size();
  occurrences = repetitions[0];
  cycleLength = (repetitions.length >= 2) ? repetitions[1] : size / occurrences;
  this.base   = base.subSampling(0, occurrences, cycleLength).compress(tolerance);
}

相关文章