Gets a new {@code FactorGraph} identical to this one, except with
an additional factor. {@code factor} must be defined over
variables which are already in {@code this} graph.
511105
public FactorGraph addFactor(String factorName, Factor factor) {
    Preconditions.checkArgument(getVariables().containsAll(factor.getVars()));

    Factor[] newFactors = Arrays.copyOf(factors, factors.length + 1);
    String[] newFactorNames = Arrays.copyOf(factorNames, factorNames.length + 1);
    ;
    newFactors[factors.length] = factor;
    newFactorNames[factors.length] = factorName;
    return new FactorGraph(variables, newFactors, newFactorNames, conditionedVariables,
        conditionedValues, inferenceHint);
  }
9563
public ParametricFactorGraph build() {
    return new ParametricFactorGraph(new DynamicFactorGraph(variables, constantFactors, constantFactorNames),
        parametricFactors, factorPatterns, parametricFactorNames); 
  }
1188993
@Override
	public RandomVariable addProduct(final RandomVariable factor1, final double factor2) {
		return apply(new DoubleBinaryOperator() {
			@Override
			public double applyAsDouble(double x, double y) {
				return x + y * factor2;
			}
		}, factor1);
	}
114129
private Assignment doSamplingRound(FactorGraph factorGraph, Assignment curAssignment) {
	  Assignment assignment = curAssignment;
	  int[] variableNums = factorGraph.getVariables().getVariableNumsArray();
		for (int i = 0; i < variableNums.length; i++) {
			assignment = doSample(factorGraph, assignment, variableNums[i]);
		}
		return assignment;
	}
511087
public Graph<K, VV, EV> getUndirected() {

		DataSet<Edge<K, EV>> undirectedEdges = edges.
			flatMap(new RegularAndReversedEdgesMap<>()).name("To undirected graph");
		return new Graph<>(vertices, undirectedEdges, this.context);
	}
25614
public void addConstantFactor(String factorName, Factor factor) {
    constantFactors.add(ReplicatedFactor.fromFactor(factor));
    constantFactorNames.add(factorName);
  }
308791
public static Factor product(List<Factor> factors) {
    return product(factors.toArray(new Factor[factors.size()]));
  }
64027
private static MaxMarginalSet cliqueTreeToMaxMarginalSet(CliqueTree cliqueTree,
      FactorGraph originalFactorGraph) {
    for (int i = 0; i < cliqueTree.numFactors(); i++) {
      computeMarginal(cliqueTree, i, false);
    }
    return new FactorMaxMarginalSet(cliqueTree, originalFactorGraph.getConditionedValues());
  }
173196
public IfImportState nodesForIf(NodeDef from, GraphDef graph) {
        //Assume we start with a switch statement
        int currNodeIndex = graph.getNodeList().indexOf(from);
        val trueDefName = from.getInput(1);
        val falseDefName = from.getInput(0);
        val scopeId = UUID.randomUUID().toString();
        val scopeName = scopeId + "-" + trueDefName.substring(0,trueDefName.indexOf("/"));
        val trueDefScopeName = scopeName + "-true-scope";
        val falseDefScopeName = scopeName + "-false-scope";


        boolean onFalseDefinition = true;
        //start with the true
        boolean onTrueDefinition = false;

        List<NodeDef> falseBodyNodes = new ArrayList<>();
        List<NodeDef> trueBodyNodes = new ArrayList<>();
        List<NodeDef> conditionNodes = new ArrayList<>();
        Set<String> seenNames = new LinkedHashSet<>();
        /**
         * Accumulate a list backwards to get proper ordering.
         *
         */
        for(int i = currNodeIndex; i >= 0; i--) {
            //switch to false names
            if(graph.getNode(i).getName().equals(trueDefName)) {
                onFalseDefinition = false;
                onTrueDefinition = true;
            }

            //on predicate now
            if(graph.getNode(i).getName().contains("pred_id")) {
                onTrueDefinition = false;
            }
            //don't readd the same node, this causes a stackoverflow
            if(onTrueDefinition  && !graph.getNode(i).equals(from)) {
                trueBodyNodes.add(graph.getNode(i));
            }
            else if(onFalseDefinition && !graph.getNode(i).equals(from)) {
                falseBodyNodes.add(graph.getNode(i));
            }
            //condition scope now
            else {
                val currNode = graph.getNode(i);
                if(currNode.equals(from))
                    continue;

                //break only after bootstrapping the first node (the predicate id node)
                if(!seenNames.contains(graph.getNode(i).getName()) && !graph.getNode(i).getName().contains("pred_id")) {
                    break;
                }

                /**
                 * Continuously add inputs seen for each node in the sub graph that occurs.
                 * Starting from the predicate id, any node that has inputs in the condition scope
                 * are by definition within the scope. Any node not encountered after that is considered out of scope.
                 * This means we break.
                 */
                for(int inputIdx = 0; inputIdx < currNode.getInputCount(); inputIdx++) {
                    seenNames.add(currNode.getInput(inputIdx));
                }



                //ensure the "current node" is added as well
                seenNames.add(graph.getNode(i).getName());
                conditionNodes.add(graph.getNode(i));
            }
        }

        /**
         * Since we are going over the graph backwards,
         * we need to reverse the nodes to ensure proper ordering.
         */
        Collections.reverse(falseBodyNodes);
        Collections.reverse(trueBodyNodes);
        Collections.reverse(conditionNodes);


        return IfImportState.builder()
                .condNodes(conditionNodes)
                .falseNodes(falseBodyNodes)
                .trueNodes(trueBodyNodes)
                .conditionBodyScopeName(falseDefScopeName)
                .falseBodyScopeName(falseDefScopeName)
                .trueBodyScopeName(trueDefScopeName)
                .conditionBodyScopeName(scopeName)
                .build();
    }
358073
public static Variables instance(GraphRewrite event)
    {
        Variables instance = (Variables) event.getRewriteContext().get(Variables.class);
        if (instance == null)
        {
            instance = new Variables();
            event.getRewriteContext().put(Variables.class, instance);
        }
        return instance;
    }
175254
public Assignment outcomeToAssignment(String[] factorVariables, Object[] outcome) {
    return outcomeToAssignment(Arrays.asList(factorVariables), Arrays.asList(outcome));
  }
1140379
private Assignment doSample(FactorGraph factorGraph, Assignment curAssignment, int varNum) {
		// Retain the assignments to all other variables.
		Assignment otherVarAssignment = curAssignment.removeAll(varNum);

		// Multiply together all of the factors which define a probability distribution over 
		// variable varNum, conditioned on all other variables.
		Set<Integer> factorNums = factorGraph.getFactorsWithVariable(varNum);

		Preconditions.checkState(factorNums.size() > 0, "Variable not in factor: " + varNum + " " + factorNums);
		List<Factor> factorsToCombine = new ArrayList<Factor>();
		for (Integer factorNum : factorNums) {
			Factor conditional = factorGraph.getFactor(factorNum)
				.conditional(otherVarAssignment);
			factorsToCombine.add(conditional.marginalize(otherVarAssignment.getVariableNums()));
		}
		Factor toSampleFrom = factorsToCombine.get(0).product(factorsToCombine.subList(1, factorsToCombine.size()));
		// Draw the sample and update the sampler's current assignment. 
		Assignment subsetValues = toSampleFrom.sample();
		
		return otherVarAssignment.union(subsetValues);
	}
683076
public void addUnreplicatedFactor(String factorName, T factor, VariableNumMap vars) {
    parametricFactors.add(factor);
    factorPatterns.add(new WrapperVariablePattern(vars));
    parametricFactorNames.add(factorName);
  }
572699
@Override
	public RandomVariable addProduct(RandomVariable factor1, double factor2) {
		return new RandomVariableDifferentiableAADPathwise(
				getValues().addProduct(factor1, factor2),
				Arrays.asList(this, factor1, new RandomVariableFromDoubleArray(factor2)),
				OperatorType.ADDPRODUCT);
	}
213248
def create_factor_general(self, *args, **kwargs):
        
        try:
            return self.create_factor(*args, **kwargs)
        except TypeError:
            pass
        try:
            return self.create_multi_output_factor(*args, **kwargs)
        except TypeError:
            pass
        try:
            return self.create_node_creation_factor(*args, **kwargs)
        except TypeError:
            pass
        raise FactorDefinitionError("Could not find a matching signature")
1367243
public static <N> ImmutableGraph<N> emptyGraph(Graph<N> graph) {
        return ImmutableGraph.copyOf(GraphBuilder.from(graph).build());
    }
742939
public void setMajorTickMarkLengthFactor(final double FACTOR) {
        if (null == majorTickMarkLengthFactor) {
            _majorTickMarkLengthFactor = Helper.clamp(0.0, 1.0, FACTOR);
            fireUpdateEvent(REDRAW_EVENT);
        } else {
            majorTickMarkLengthFactor.set(FACTOR);
        }
    }