Implements the AAD Algorithm
@return HashMap where the key is the internal index of the random variable with respect to which the partial derivative was computed. This key then gives access to the actual derivative.
194891
public Map<Integer, RandomVariable> getGradient(){

		int numberOfCalculationSteps = getFunctionList().size();

		RandomVariable[] omegaHat = new RandomVariable[numberOfCalculationSteps];

		omegaHat[numberOfCalculationSteps-1] = new RandomVariableFromDoubleArray(1.0);

		for(int variableIndex = numberOfCalculationSteps-2; variableIndex >= 0; variableIndex--){

			omegaHat[variableIndex] = new RandomVariableFromDoubleArray(0.0);

			ArrayList<Integer> childrenList = getAADRandomVariableFromList(variableIndex).getChildrenIndices();

			for(int functionIndex:childrenList){
				RandomVariable D_i_j = getPartialDerivative(functionIndex, variableIndex);
				omegaHat[variableIndex] = omegaHat[variableIndex].addProduct(D_i_j, omegaHat[functionIndex]);
			}
		}

		ArrayList<Integer> arrayListOfAllIndicesOfDependentRandomVariables = getArrayListOfAllIndicesOfDependentRandomVariables();

		Map<Integer, RandomVariable> gradient = new HashMap<Integer, RandomVariable>();

		for(Integer indexOfDependentRandomVariable: arrayListOfAllIndicesOfDependentRandomVariables){
			gradient.put(indexOfDependentRandomVariable, omegaHat[arrayListOfAllIndicesOfDependentRandomVariables.get(indexOfDependentRandomVariable)]);
		}

		return gradient;
	}
161873
public function key()
    {
        if (empty($this->key) && !empty($this->definition->primary)) {
            $keys = array_values(
                array_filter(
                    preg_split('~[\s,]+~', $this->definition->primary)
                )
            );
            $keys = array_map(
                function ($key) {
                    return Normalise::toVariable($key);
                },
                $keys
            );

            $this->key = count($keys) > 1 ? $keys : array_shift($keys);
        }

        if (empty($this->key) && $this->has('id')) {
            $this->key = 'id';
        }

        return $this->key;
    }
207971
public Map<Integer, String> getInvertedIndex() {
		TreeMap<Integer, String> inverted = new TreeMap<Integer, String>();
		Iterator<String> keyiter = this.tokenForm2index.keySet().iterator();
		while (keyiter.hasNext()) {
			String key = keyiter.next();
			int[] index = tokenForm2index.get(key);
			Integer i = Integer.valueOf(index[0]);
			inverted.put(i, key);
		}
		return inverted;
	}
1214413
public static <K, T, V> MultiDimensionalMap<K, T, V> newHashBackedMap() {
        return new MultiDimensionalMap<>(new HashMap<Pair<K, T>, V>());
    }
64437
public function key()
    {
        if ($this->assocArray) {
            $keys = array_keys($this->val);
            return $keys[$this->position];
        }

        return $this->position;
    }
517864
public Matrix getD () {
		Matrix X = new Matrix(n,n);
		double[][] D = X.getArray();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				D[i][j] = 0.0;
			}
			D[i][i] = d[i];
			if (e[i] > 0) {
				D[i][i+1] = e[i];
			} else if (e[i] < 0) {
				D[i][i-1] = e[i];
			}
		}
		return X;
	}
78325
public Map<String, IndexInformation> getIndexInformation() {
        ImmutableMap.Builder<String, IndexInformation> copy = ImmutableMap.builder();
        copy.putAll(indexes);
        return copy.build();
    }
740103
public SDVariable rarg() {
        val args = args();
        if(args == null || args.length != 2)
            throw new ND4JIllegalStateException("In order to use this function, the number of arguments for this function must be 2.");
        return args[1];
    }
972746
public final String key() {
    key = (key == null) ? UUID.randomUUID().toString() : key;
    return key;
  }
585029
public HashMap<Variable,Object> chooseValues() {
		MultiVariable mv = (MultiVariable)myVariable;
		Variable[] internalVars = mv.getInternalVariables();
		HashMap<Variable,Object> values = new HashMap<Variable,Object>();
		for (Variable v: internalVars) {
			if (v instanceof MultiVariable) {
				MultiVariable nestedMv = (MultiVariable)v;
				MultiDomain nestedMd = nestedMv.getDomain();
				HashMap<Variable,Object> nested = nestedMd.chooseValues();
				for (Entry<Variable,Object> e : nested.entrySet()) {
					values.put(e.getKey(), e.getValue());
				}
			}
			else {
				values.put(v,v.getDomain().chooseValue());
			}
		}
		return values;
	}
906448
public function IdentificationVariableDeclaration()
    {
        $joins                    = [];
        $rangeVariableDeclaration = $this->RangeVariableDeclaration();
        $indexBy                  = $this->lexer->isNextToken(Lexer::T_INDEX)
            ? $this->IndexBy()
            : null;

        $rangeVariableDeclaration->isRoot = true;

        while ($this->lexer->isNextToken(Lexer::T_LEFT) ||
            $this->lexer->isNextToken(Lexer::T_INNER) ||
            $this->lexer->isNextToken(Lexer::T_JOIN)
        ) {
            $joins[] = $this->Join();
        }

        return new AST\IdentificationVariableDeclaration(
            $rangeVariableDeclaration,
            $indexBy,
            $joins
        );
    }
660525
public RandomVariableInterface[] getInitialValue() {
		RandomVariableInterface[] initialState = getInitialState();

		RandomVariableInterface[] value = new RandomVariableInterface[initialState.length];
		for(int i= 0; i<value.length; i++) {
			value[i] = applyStateSpaceTransform(i,initialState[i]);
		}

		return value;
	}
1143482
public static String generateAccessKeyId() {
		// Based on http://stackoverflow.com/a/41156/535203
		final char[] accessKeyId = new char[ACCESS_KEY_ID_LENGTH];
		for (int i = 0; i < accessKeyId.length; i++) {
			accessKeyId[i] = SYMBOLS[SECURE_RANDOM.nextInt(SYMBOLS.length)];
		}
		return new String(accessKeyId);
	}
302387
@Override
    public INDArray toDense() {
        // TODO support view conversion
        INDArray result = Nd4j.zeros(shape());

        switch (data().dataType()) {
            case DOUBLE:
                for (int i = 0; i < length; i++) {
                    int[] idx = getUnderlyingIndicesOf(i).asInt();
                    double value = values.getDouble(i);
                    result.putScalar(idx, value);
                }
                break;
            case FLOAT:
                for (int i = 0; i < length; i++) {
                    int[] idx = getUnderlyingIndicesOf(i).asInt();
                    float value = values.getFloat(i);
                    result.putScalar(idx, value);
                }
                break;
            default:
                throw new UnsupportedOperationException();
        }
        return result;
    }
1052116
public D getDerivative(boolean... isX) {
		if( derivatives == null ) {
			declareTree(isX.length);
		} else if( isX.length > stale.length ) {
			growTree(isX.length);
		}

		int index = 0;
		int prevIndex = 0;
		for( int level = 0; level < isX.length; level++ ) {
			index |= isX[level] ? 0 : 1 << level;

			if( stale[level][index] ) {
				stale[level][index] = false;
				derivatives[level][index].reshape(inputImage.getWidth(),inputImage.getHeight());

				if( level == 0 ) {
					if( isX[level]) {
						derivX.process(inputImage,derivatives[level][index]);
					} else {
						derivY.process(inputImage,derivatives[level][index]);
					}
				} else {
					D prev = derivatives[level-1][prevIndex];
					if( isX[level]) {
						derivDerivX.process(prev,derivatives[level][index]);
					} else {
						derivDerivY.process(prev,derivatives[level][index]);
					}
				}
			}
			prevIndex = index;
		}

		return derivatives[isX.length-1][index];
	}
591348
public HashMap<Type, Double> getInversePossibilities() {
		HashMap<Type, Double> ret = new HashMap<Type, Double>();
		for (Type t : Type.values()) ret.put(t, 0.0);
		for (Type t : types) {
			//??? IS THIS CORRECT ??? --> Seems correct
			//get inverse of each relation that is 1.0 
			Type inverseRelation = FuzzyAllenIntervalConstraint.getInverseRelation(t);
			
			HashMap<FuzzyAllenIntervalConstraint.Type, Double> possibilities = this.getPossibilities();
			
			//set poss of each inverse relation to 1.0
			ret.put(inverseRelation, possibilities.get(t));
			
			//calculate the Freksa N of each inverse relation
			HashMap<FuzzyAllenIntervalConstraint.Type, Double> fr = new HashMap<FuzzyAllenIntervalConstraint.Type, Double>();
			for(int i = 0; i <  FuzzyAllenIntervalConstraint.freksa_neighbor[inverseRelation.ordinal()].length; i++)
				fr.put(FuzzyAllenIntervalConstraint.lookupTypeByInt(i), FuzzyAllenIntervalConstraint.getPossibilityDegree(FuzzyAllenIntervalConstraint.freksa_neighbor[inverseRelation.ordinal()][i]));
			
			//take the maximum between calculated Freksa N and previously added possibilities
			//(because this is an OR)
			for(FuzzyAllenIntervalConstraint.Type t1: fr.keySet())
				ret.put(t1, Math.max(ret.get(t1), fr.get(t1)));
		}
		
		/*
		System.out.println("=====================================");
		System.out.println("DIRECT " + this + ":\n" + this.possibilities);
		System.out.println("INVERSE:\n" + ret);
		System.out.println("=====================================");
		*/
		
		return ret;
	}
446997
public RandomVariableInterface abs() {
		if(isDeterministic()) {
			double newValueIfNonStochastic = Math.abs(valueIfNonStochastic);
			return new RandomVariableLowMemory(time, newValueIfNonStochastic);
		}
		else {
			double[] newRealizations = new double[realizations.length];
			for(int i=0; i<newRealizations.length; i++) {
				newRealizations[i]		 = Math.abs(realizations[i]);
			}
			return new RandomVariableLowMemory(time, newRealizations);
		}
	}