Example usage for org.apache.commons.math3.linear ArrayRealVector getDataRef

List of usage examples for org.apache.commons.math3.linear ArrayRealVector getDataRef

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear ArrayRealVector getDataRef.

Prototype

public double[] getDataRef() 

Source Link

Document

Get a reference to the underlying data array.

Usage

From source file:nars.concept.util.BeliefClusterer.java

/**
 * Get the point farthest to its cluster center
 *
 * @param clusters the {@link Cluster}s to search
 * @return point farthest to its cluster center
 * @throws ConvergenceException if clusters are all empty
 */// w w  w  . jav  a 2 s  . c o  m
@NotNull
private ArrayRealVector getFarthestPoint(@NotNull final Collection<Cluster> clusters)
        throws ConvergenceException {

    double maxDistance = Double.NEGATIVE_INFINITY;
    Cluster selectedCluster = null;
    int selectedPoint = -1;
    for (final Cluster cluster : clusters) {

        // get the farthest point
        final ArrayRealVector center = cluster.pos;
        final List<T> points = cluster.getPoints();
        for (int i = 0; i < points.size(); ++i) {
            final double distance = distance(points.get(i), center.getDataRef());
            if (distance > maxDistance) {
                maxDistance = distance;
                selectedCluster = cluster;
                selectedPoint = i;
            }
        }

    }

    // did we find at least one non-empty cluster ?
    if (selectedCluster == null) {
        throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
    }

    return p(selectedCluster.getPoints().remove(selectedPoint));

}

From source file:automenta.vivisect.dimensionalize.HyperassociativeMap.java

/** commit computed coordinates to the objects */
public void apply() {
    Iterator<Entry<V, ArrayRealVector>> e = coordinates.entrySet().iterator();
    while (e.hasNext()) {
        Entry<V, ArrayRealVector> x = e.next();
        ArrayRealVector v = x.getValue();
        if (v != null)
            apply(x.getKey(), v.getDataRef());
        else//from  w w  w . j a v  a 2s  . c om
            e.remove();
    }

}

From source file:automenta.vivisect.dimensionalize.HyperassociativeMap.java

@Deprecated
public ArrayRealVector getPosition(V node) {
    ArrayRealVector location = coordinates.computeIfAbsent(node, (n) -> newVector());
    getPosition(node, location.getDataRef());
    return location;
}

From source file:gamlss.distributions.PE.java

/** Calculate and set initial value of sigma.
 * @param y - vector of values of response variable
 * @return vector of initial values of sigma
 *///  ww w.j  a v a  2 s  .  c o  m
private ArrayRealVector setSigmaInitial(final ArrayRealVector y) {
    //sigma.initial = expression( sigma <- (abs(y-mean(y))+sd(y))/2 )   
    final double mean = new Mean().evaluate(y.getDataRef());
    final double sd = new StandardDeviation().evaluate(y.getDataRef());
    size = y.getDimension();
    double[] out = new double[size];
    for (int i = 0; i < size; i++) {
        out[i] = (FastMath.abs(y.getEntry(i) - mean) + sd) / 2;
    }
    return new ArrayRealVector(out, false);
}

From source file:automenta.vivisect.dimensionalize.HyperassociativeMap.java

/** vertices is passed as a list because the Set iterator from JGraphT is slow */
public ArrayRealVector align(V nodeToAlign, ObjectDoubleHashMap<V> neighbors, V[] vertices) {

    double nodeSpeed = getSpeedFactor(nodeToAlign);

    ArrayRealVector originalPosition = new ArrayRealVector(new double[2], true);
    //getPosition(nodeToAlign);
    //getCurrentPosition(nodeToAlign);
    getPosition(nodeToAlign, originalPosition.getDataRef());

    if (nodeSpeed == 0)
        return originalPosition;

    // calculate equilibrium with neighbors
    ArrayRealVector position = (ArrayRealVector) originalPosition.mapMultiplyToSelf(1.0 / scale);

    getNeighbors(nodeToAlign, neighbors);

    ArrayRealVector delta = newVector();

    double radius = getRadius(nodeToAlign);
    double targetDistance = radius + equilibriumDistance;
    double learningRate = this.learningRate;

    ArrayRealVector tmpAttractVector = newVector();

    // align with neighbours
    neighbors.forEachKeyValue((neighbor, distToNeighbor) -> {

        ArrayRealVector attractVector = getThePosition(neighbor, tmpAttractVector).combineToSelf(1, -1,
                position);/*  w  ww  .  ja v a 2 s .  c o m*/

        double oldDistance = magnitude(attractVector);

        double newDistance;
        double factor = 0;
        double deltaDist = oldDistance - distToNeighbor;
        if (oldDistance > distToNeighbor) {
            newDistance = Math.pow(deltaDist, attractionStrength);

        } else {

            newDistance = -targetDistance * atanh((-deltaDist) / distToNeighbor);

            if (Math.abs(newDistance) > (Math.abs(deltaDist))) {
                newDistance = -targetDistance * (-deltaDist);
            }

        }

        newDistance *= learningRate;
        if (oldDistance != 0) {
            factor = newDistance / oldDistance;
        }

        add(delta, attractVector, factor);
    });

    ArrayRealVector repelVector = newVector();
    double maxEffectiveDistance = targetDistance * maxRepulsionDistance;

    ArrayRealVector nodePos = newVector();

    // calculate repulsion with all non-neighbors

    DistanceMetric distanceFunction = this.distanceFunction;
    double minDistance = this.minDistance;
    double repulsiveWeakness = this.repulsiveWeakness;

    for (V node : vertices) {

        if (node == null)
            continue;

        //vertices.forEach((Consumer<N>)node -> {
        //for (final N node : vertices) {
        if ((node == nodeToAlign) || (neighbors.containsKey(node)))
            continue;

        double oldDistance = distanceFunction.subtractIfLessThan(getThePosition(node, nodePos), position,
                repelVector, maxEffectiveDistance);

        if (oldDistance == Double.POSITIVE_INFINITY)
            continue; //too far to matter

        if (oldDistance < minDistance)
            oldDistance = minDistance; //continue;
        //throw new RuntimeException("invalid oldDistance");

        double newDistance = -targetDistance * Math.pow(oldDistance, -repulsiveWeakness);

        if (Math.abs(newDistance) > targetDistance) {
            newDistance = Math.copySign(targetDistance, newDistance);
        }
        newDistance *= learningRate;

        add(delta, repelVector, newDistance / oldDistance);
    }

    /*if (normalizeRepulsion)
    nodeSpeed/=delta.getNorm(); //TODO check when norm = 0*/

    if (nodeSpeed != 1.0) {
        delta.mapMultiplyToSelf(nodeSpeed);
    }

    double moveDistance = magnitude(delta);
    if (!Double.isFinite(moveDistance))
        throw new RuntimeException("invalid magnitude");

    if (moveDistance > targetDistance * acceptableMaxDistanceFactor) {
        double newLearningRate = ((targetDistance * acceptableMaxDistanceFactor) / moveDistance);
        if (newLearningRate < learningRate) {
            this.learningRate = newLearningRate;
        } else {
            this.learningRate *= LEARNING_RATE_INCREASE_FACTOR / vertices.length;
        }

        moveDistance = DEFAULT_TOTAL_MOVEMENT;
    } else {
        add(position, delta);
    }

    if (moveDistance > maxMovement) {
        maxMovement = moveDistance;
    }
    totalMovement += moveDistance;

    originalPosition.mapMultiplyToSelf(scale);
    move(nodeToAlign, originalPosition.getEntry(0), originalPosition.getEntry(1));
    return originalPosition;
}

From source file:gamlss.distributions.SST.java

/** Calculate and set initial value of sigma.
 * @param y - vector of values of response variable
 * @return vector of initial values of sigma
 *///from w w  w  .  j ava 2s. co  m
private ArrayRealVector setSigmaInitial(final ArrayRealVector y) {
    //sigma.initial = expression(sigma<- rep(sd(y), length(y))),
    tempV = new ArrayRealVector(y.getDimension());
    final double out = new StandardDeviation().evaluate(y.getDataRef());
    tempV.set(out);
    return tempV;
}

From source file:gamlss.distributions.GT.java

/** Calculate and set initial value of sigma.
 * @param y - vector of values of response variable
 * @return vector of initial values of sigma
 *//*from  ww w.  j a va  2 s . co m*/
private ArrayRealVector setSigmaInitial(final ArrayRealVector y) {
    //sigma.initial = expression(sigma<- rep(sd(y)/4, length(y))),
    tempV = new ArrayRealVector(y.getDimension());
    final double out = new StandardDeviation().evaluate(y.getDataRef());
    tempV.set(out / 4.0);
    return tempV;
}

From source file:gamlss.distributions.ST1.java

/** Calculate and set initial value of sigma.
 * @param y - vector of values of response variable
 * @return vector of initial values of sigma
 *///from   w  w w  .  j a v  a  2 s.c o  m
private ArrayRealVector setSigmaInitial(final ArrayRealVector y) {
    //sigma.initial = expression(sigma <- rep(sd(y)/4, length(y))),
    tempV = new ArrayRealVector(y.getDimension());
    final double out = new StandardDeviation().evaluate(y.getDataRef());
    tempV.set(out / 4);
    return tempV;
}

From source file:gamlss.distributions.BCPE.java

/**  Calculates initial value of mu, by assumption these 
 * values lie between observed data and the trend line.
 * @param y - vector of values of response variable
 * @return  a vector of initial values of mu
 *//*from  w ww .j  a  v a 2  s .c  o  m*/
private ArrayRealVector setMuInitial(final ArrayRealVector y) {
    //mu.initial =  expression(mu <- (y+mean(y))/2)
    size = y.getDimension();
    double[] out = new double[size];
    Mean mean = new Mean();
    double yMean = mean.evaluate(y.getDataRef());
    for (int i = 0; i < size; i++) {
        out[i] = (y.getEntry(i) + yMean) / 2;
    }
    return new ArrayRealVector(out, false);
}

From source file:objenome.util.Utils.java

public static ArrayRealVector gradient(DiffableFunction f, Scalar[] parameters, ArrayRealVector result) {
    if (result == null)
        result = new ArrayRealVector(parameters.length);

    double[] d = result.getDataRef();
    for (int i = 0; i < parameters.length; ++i) {
        d[i] = f.partialDerive(parameters[i]);
    }/*  w w w  .  ja va  2s.c om*/
    return result;
}