See {@link #sparseSoftmaxCrossEntropy(String, SDVariable, SDVariable)}
775598
public SDVariable sparseSoftmaxCrossEntropy(@NonNull SDVariable logits, @NonNull SDVariable labels) {
        return sparseSoftmaxCrossEntropy(null, logits, labels);
    }
123136
private SparseIntegerVector getSemanticVector(String word) {
        SparseIntegerVector v = wordToSemantics.get(word);
        if (v == null) {
            // lock on the word in case multiple threads attempt to add it at
            // once
            synchronized(this) {
                // recheck in case another thread added it while we were waiting
                // for the lock
                v = wordToSemantics.get(word);
                if (v == null) {
                    v = new CompactSparseIntegerVector(Integer.MAX_VALUE);
                    wordToSemantics.put(word, v);
                }
            }
        }
        return v;
    }
1135742
@Override
    public void axpy(long n, double alpha, INDArray x, INDArray y) {
        BaseSparseNDArray sparseX = (BaseSparseNDArray) x;
        DataBuffer pointers = sparseX.getVectorCoordinates();
        switch (x.data().dataType()) {
            case DOUBLE:
                DefaultOpExecutioner.validateDataType(DataType.DOUBLE, x);
                DefaultOpExecutioner.validateDataType(DataType.DOUBLE, y);
                daxpyi(n, alpha, x, pointers, y);
                break;
            case FLOAT:
                DefaultOpExecutioner.validateDataType(DataType.FLOAT, x);
                DefaultOpExecutioner.validateDataType(DataType.FLOAT, y);
                saxpyi(n, alpha, x, pointers, y);
                break;
            case HALF:
                DefaultOpExecutioner.validateDataType(DataType.HALF, x);
                DefaultOpExecutioner.validateDataType(DataType.HALF, y);
                haxpyi(n, alpha, x, pointers, y);
                break;
            default:
                throw new UnsupportedOperationException();
        }
    }
1241035
public SDVariable mmul(SDVariable x, SDVariable y) {
        return mmul(null, x, y);
    }
581579
private static TensorBuilder getTensorFromVariables(VariableNumMap variables) {
    // Get the dimensions and dimension sizes for the tensor.
    int[] dimensions = variables.getVariableNumsArray();
    int[] sizes = new int[dimensions.length];
    List<DiscreteVariable> varTypes = variables.getDiscreteVariables();
    for (int i = 0; i < varTypes.size(); i++) {
      sizes[i] = varTypes.get(i).numValues();
    }
    return new SparseTensorBuilder(dimensions, sizes);
  }
985450
@Override
    public int predict(double[] x, double[] posteriori) {
        Arrays.fill(posteriori, 0.0);

        for (int i = 0; i < trees.length; i++) {
            posteriori[trees[i].predict(x)] += alpha[i];
        }

        double sum = Math.sum(posteriori);
        for (int i = 0; i < k; i++) {
            posteriori[i] /= sum;
        }

        return Math.whichMax(posteriori);
    }
98084
public SDVariable transpose(String name, SDVariable x) {
        SDVariable result = f().transpose(x);
        return updateVariableNameAndReference(result, name);
    }
975588
@Override
    public void rot(long N, INDArray X, INDArray Y, double c, double s) {


        if (X instanceof BaseSparseNDArray) {
            BaseSparseNDArray sparseX = (BaseSparseNDArray) X;

            switch (X.data().dataType()) {
                case DOUBLE:
                    droti(N, X, sparseX.getVectorCoordinates(), Y, c, s);
                    break;
                case FLOAT:
                    sroti(N, X, sparseX.getVectorCoordinates(), Y, c, s);
                    break;
                case HALF:
                    hroti(N, X, sparseX.getVectorCoordinates(), Y, c, s);
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }
453417
public SDVariable squaredNorm(String name, SDVariable x, boolean keepDims, int... dimensions) {
        validateNumerical("squaredNorm", x);
        SDVariable result = f().squaredNorm(x, keepDims, dimensions);
        return updateVariableNameAndReference(result, name);
    }
201563
def max(x, y, context=None):
    
    return _apply_function_in_current_context(
        BigFloat,
        mpfr.mpfr_max,
        (
            BigFloat._implicit_convert(x),
            BigFloat._implicit_convert(y),
        ),
        context,
    )
1259908
public void plus(LinearSparseVector sv, float w) {
		if (sv == null)
			return;
		for (int i = 0; i < sv.length; i++) {
			float val = (float) sv.data[i] * w;
			add(sv.index[i], val);
		}
	}
536117
@Override
  public MeanVarianceMinMax put(double[] vals) {
    final int l = vals.length;
    if(l < 2) {
      if(l == 1) {
        put(vals[0]);
      }
      return this;
    }
    // First pass:
    double s1 = 0.;
    for(int i = 0; i < l; i++) {
      final double v = vals[i];
      s1 += v;
      min = v < min ? v : min;
      max = v > max ? v : max;
    }
    final double om1 = s1 / l;
    // Second pass:
    double om2 = 0., err = 0.;
    for(int i = 0; i < l; i++) {
      final double v = vals[i] - om1;
      om2 += v * v;
      err += v;
    }
    s1 += err;
    om2 += err / l;
    if(n <= 0) {
      n = l;
      sum = s1;
      m2 = om2;
      return this;
    }
    final double tmp = n * s1 - sum * l;
    final double oldn = n; // tmp copy
    n += l;
    sum += s1 + err;
    m2 += om2 + tmp * tmp / (l * n * oldn);
    return this;
  }
1136433
public void factorize() throws Exception {
		long t0 = System.currentTimeMillis();
		this.LcolumnsValues = new double[dim][];
		
		if(this.rescaler != null){
			double[] cn_00_original = null;
			double[] cn_2_original = null;
			double[] cn_00_scaled = null;
			double[] cn_2_scaled = null;
			if(log.isDebugEnabled()){
				cn_00_original = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), Integer.MAX_VALUE);
				log.debug("cn_00_original Q before scaling: " + ArrayUtils.toString(cn_00_original));
				cn_2_original = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), 2);
				log.debug("cn_2_original Q before scaling : " + ArrayUtils.toString(cn_2_original));
			}
			//scaling the Q matrix, we have:
			//Q1 = U.Q.U[T] = U.L.L[T].U[T] = (U.L).(U.L)[T] 
			//and because U is diagonal it preserves the triangular form of U.L, so
			//Q1 = U.Q.U[T] = L1.L1[T] is the new Cholesky decomposition  
			DoubleMatrix1D Uv = rescaler.getMatrixScalingFactorsSymm(Q);
			if(log.isDebugEnabled()){
				boolean checkOK = rescaler.checkScaling(ColtUtils.fillSubdiagonalSymmetricMatrix(Q), Uv, Uv);
				if(!checkOK){
					log.warn("Scaling failed (checkScaling = false)");
				}
			}
			this.U = Uv;
			this.Q = (SparseDoubleMatrix2D)ColtUtils.diagonalMatrixMult(Uv, Q, Uv);
			if(log.isDebugEnabled()){
				cn_00_scaled = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), Integer.MAX_VALUE);
				log.debug("cn_00_scaled Q after scaling : " + ArrayUtils.toString(cn_00_scaled));
				cn_2_scaled = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), 2);
				log.debug("cn_2_scaled Q after scaling  : " + ArrayUtils.toString(cn_2_scaled));
				
				if(cn_00_original[0] < cn_00_scaled[0] || cn_2_original[0] < cn_2_scaled[0]){
					//log.info("Q: " + ArrayUtils.toString(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()));
					log.warn("Problematic scaling");
					//throw new RuntimeException("Scaling failed");
				}
			}
		}
		
		final int[] currentColumnIndexHolder = new int[] { -1 };
		
		IntIntDoubleFunction myFunct = new IntIntDoubleFunction() {
			@Override
			public double apply(int i, int s, double pis) {
				int step = currentColumnIndexHolder[0];
				//log.debug("i:" + i + ", step:" + step + ": " + pis);
				if (i >= step) {
					// at this point, step is the row index and j is the column index,
					// but we take into account only the lower left subdiagonal part of Q (that is symmetric)
					//log.debug("i:" + i + ", step:" + step + ": " + pis);
					Svalues[i - step] = pis;
				}
				return pis;
			}
		};
		
		//view Q column by column
		for (int step = 0; step < dim; step++) {
			//log.debug("column:" + step);
			
			// reset and fill initial values
			Svalues = new double[dim - step];
			DoubleMatrix2D P = Q.viewPart(0, step, dim, 1);
			currentColumnIndexHolder[0] = step;
			P.forEachNonZero(myFunct);
			doStep(step);
		}
		
		this.factorizeTime += (System.currentTimeMillis() - t0);
	}
388487
public static void apply(Program program1, Program program2, TreeGP manager)
   {
      int iMaxDepthForCrossover = manager.getMaxDepthForCrossover();
      TGPCrossoverStrategy method = manager.getCrossoverStrategy();

      RandEngine randEngine = manager.getRandEngine();

      if (method == TGPCrossoverStrategy.CROSSOVER_SUBTREE_BIAS || method == TGPCrossoverStrategy.CROSSVOER_SUBTREE_NO_BIAS)
      {
         boolean bias = (method == TGPCrossoverStrategy.CROSSOVER_SUBTREE_BIAS);

         int iMaxDepth1 = program1.calcDepth();
         int iMaxDepth2 = program2.calcDepth();

         TupleTwo<TreeNode, TreeNode> pCutPoint1;
         TupleTwo<TreeNode, TreeNode> pCutPoint2;

         boolean is_crossover_performed = false;
         // Suppose that at the beginning both the current GP and the other GP do not violate max depth constraint
         // then try to see whether a crossover can be performed in such a way that after the crossover, both GP still have depth <= max depth
         if (iMaxDepth1 <= iMaxDepthForCrossover && iMaxDepth2 <= iMaxDepthForCrossover)
         {
            int max_trials = 50;
            int trials = 0;
            do
            {
               pCutPoint1 = program1.anyNode(bias, randEngine);
               pCutPoint2 = program2.anyNode(bias, randEngine);


               if (pCutPoint1 != null && pCutPoint2 != null)
               {
                  TupleTwo<TupleTwo<TreeNode, TreeNode>, TupleTwo<TreeNode, TreeNode>> result = swap(program1, program2, pCutPoint1, pCutPoint2);

                  iMaxDepth1 = program1.calcDepth();
                  iMaxDepth2 = program2.calcDepth();

                  if (iMaxDepth1 <= iMaxDepthForCrossover && iMaxDepth2 <= iMaxDepthForCrossover) //crossover is successful
                  {
                     is_crossover_performed = true;
                     break;
                  }
                  else
                  {
                     TupleTwo<TreeNode, TreeNode> newCutPoint1 = result._1();
                     TupleTwo<TreeNode, TreeNode> newCutPoint2 = result._2();

                     swap(program1, program2, newCutPoint1, newCutPoint2); // swap back so as to restore to the original GP trees if the crossover is not valid due to max depth violation
                  }
               }

               trials++;
            } while (trials < max_trials);
         }

         // force at least one crossover even if the maximum depth is violated above so that this operator won't end up like a reproduction operator
         if (!is_crossover_performed)
         {
            pCutPoint1 = program1.anyNode(bias, randEngine);
            pCutPoint2 = program2.anyNode(bias, randEngine);

            if (pCutPoint1 != null && pCutPoint2 != null)
            {
               swap(program1, program2, pCutPoint1, pCutPoint2);


               program1.calcLength();
               program2.calcLength();

            }
         }
      }
   }
1112220
@Override
  public SparseTensor slice(int[] dimensionNumbers, int[] key) {
    if (dimensionNumbers.length == 0) {
      return this;
    }
    // Check for an efficient case, where the dimensions are the first
    // elements of this.
    if (ArrayUtils.subarrayEquals(getDimensionNumbers(), dimensionNumbers, 0)) {
      long minKeyInt = 0;
      int numDimensions = dimensionNumbers.length;
      for (int i = 0; i < numDimensions; i++) {
        minKeyInt += indexOffsets[i] * key[i];
      }
      long maxKeyInt = minKeyInt + indexOffsets[numDimensions - 1];

      int startIndex = getNearestIndex(minKeyInt);
      int endIndex = getNearestIndex(maxKeyInt);

      long[] newKeyInts = ArrayUtils.copyOfRange(keyNums, startIndex, endIndex);
      int numKeyInts = newKeyInts.length;
      for (int i = 0; i < numKeyInts; i++) {
        newKeyInts[i] -= minKeyInt;
      }

      int[] newDimensionNumbers = ArrayUtils.copyOfRange(getDimensionNumbers(), dimensionNumbers.length, getDimensionNumbers().length);
      int[] newDimensionSizes = ArrayUtils.copyOfRange(getDimensionSizes(), dimensionNumbers.length, getDimensionSizes().length);
      double[] newValues = ArrayUtils.copyOfRange(values, startIndex, endIndex);
      return new SparseTensor(newDimensionNumbers, newDimensionSizes, newKeyInts, newValues);
    }

    // TODO(jayantk): This is an extremely naive implementation of
    // slice.
    // Figure out the appropriate sizes for the subset of dimensions.
    int[] dimensionSizes = new int[dimensionNumbers.length];
    for (int i = 0; i < dimensionNumbers.length; i++) {
      int dimIndex = getDimensionIndex(dimensionNumbers[i]);
      Preconditions.checkArgument(dimIndex >= 0);
      dimensionSizes[i] = getDimensionSizes()[dimIndex];
    }
    SparseTensorBuilder builder = new SparseTensorBuilder(dimensionNumbers, dimensionSizes);
    builder.put(key, 1.0);
    return elementwiseProduct(builder.build()).sumOutDimensions(Ints.asList(dimensionNumbers));
  }
587401
@Override
	protected Constraint[] createInternalConstraints(Variable from, Variable to) {
		this.xConstraint.setFrom(((RectangularCuboidRegion)from).getInternalVariables()[0]);
		this.xConstraint.setTo(((RectangularCuboidRegion)to).getInternalVariables()[0]);
		this.yConstraint.setFrom(((RectangularCuboidRegion)from).getInternalVariables()[1]);
		this.yConstraint.setTo(((RectangularCuboidRegion)to).getInternalVariables()[1]);
		this.zConstraint.setFrom(((RectangularCuboidRegion)from).getInternalVariables()[2]);
		this.zConstraint.setTo(((RectangularCuboidRegion)to).getInternalVariables()[2]);
		//xConstraint should not be processed by Y and Z solver		
		xConstraint.skipSolver(((MultiVariable)from).getInternalConstraintSolvers()[1], ((MultiVariable)from).getInternalConstraintSolvers()[2]);
		//yConstraint should not be processed by X and Z solver 
		yConstraint.skipSolver(((MultiVariable)from).getInternalConstraintSolvers()[0], ((MultiVariable)from).getInternalConstraintSolvers()[2]);
		//zConstraint should not be processed by X and Y solver 
		zConstraint.skipSolver(((MultiVariable)from).getInternalConstraintSolvers()[0], ((MultiVariable)from).getInternalConstraintSolvers()[1]);
		return new Constraint[] {this.xConstraint, this.yConstraint, this.zConstraint};
	}
666998
def uncertain_inputs_sparse_regression(max_iters=200, optimize=True, plot=True):
    """"""
    fig, axes = pb.subplots(1, 2, figsize=(12, 5), sharex=True, sharey=True)

    # sample inputs and outputs
    S = np.ones((20, 1))
    X = np.random.uniform(-3., 3., (20, 1))
    Y = np.sin(X) + np.random.randn(20, 1) * 0.05
    # likelihood = GPy.likelihoods.Gaussian(Y)
    Z = np.random.uniform(-3., 3., (7, 1))

    k = GPy.kern.RBF(1)
    # create simple GP Model - no input uncertainty on this one
    m = GPy.models.SparseGPRegression(X, Y, kernel=k, Z=Z)

    if optimize:
        m.optimize('scg', messages=1, max_iters=max_iters)

    if plot:
        m.plot(ax=axes[0])
        axes[0].set_title('no input uncertainty')
    print(m)

    # the same Model with uncertainty
    m = GPy.models.SparseGPRegression(X, Y, kernel=GPy.kern.RBF(1), Z=Z, X_variance=S)
    if optimize:
        m.optimize('scg', messages=1, max_iters=max_iters)
    if plot:
        m.plot(ax=axes[1])
        axes[1].set_title('with input uncertainty')
        fig.canvas.draw()

    print(m)
    return m