Java Reference

Java Reference

LinearExpr.java
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 package com.google.ortools.sat;
15 
17 
19 public interface LinearExpr extends LinearArgument {
21  int numElements();
22 
24  int getVariableIndex(int index);
25 
27  long getCoefficient(int index);
28 
30  long getOffset();
31 
34  return new LinearExprBuilder();
35  }
36 
38  static LinearExpr constant(long value) {
39  return newBuilder().add(value).build();
40  }
41 
43  static LinearExpr term(LinearArgument expr, long coeff) {
44  return newBuilder().addTerm(expr, coeff).build();
45  }
46 
48  static LinearExpr sum(LinearArgument[] exprs) {
49  return newBuilder().addSum(exprs).build();
50  }
51 
53  static LinearExpr weightedSum(LinearArgument[] exprs, long[] coeffs) {
54  return newBuilder().addWeightedSum(exprs, coeffs).build();
55  }
56 
58  int numElements = proto.getVarsCount();
59  if (numElements == 0) {
60  return new ConstantExpression(proto.getOffset());
61  } else if (numElements == 1) {
62  return new AffineExpression(proto.getVars(0), proto.getCoeffs(0), proto.getOffset());
63  } else {
64  int[] varsIndices = new int[numElements];
65  long[] coeffs = new long[numElements];
66  long offset = proto.getOffset();
67  for (int i = 0; i < numElements; ++i) {
68  varsIndices[i] = proto.getVars(i);
69  coeffs[i] = proto.getCoeffs(i);
70  }
71  return new WeightedSumExpression(varsIndices, coeffs, offset);
72  }
73  }
74 }
static LinearExpr term(LinearArgument expr, long coeff)
Shortcut for newBuilder().addTerm(expr, coeff).build()
Definition: LinearExpr.java:43
A specialized linear expression: sum(ai * xi) + b.
A linear expression (sum (ai * xi) + b).
Definition: LinearExpr.java:19
A specialized linear expression: a * x + b.
static LinearExpr weightedSum(LinearArgument[] exprs, long[] coeffs)
Shortcut for newBuilder().addWeightedSum(exprs, coeffs).build()
Definition: LinearExpr.java:53
static LinearExprBuilder newBuilder()
Returns a builder.
Definition: LinearExpr.java:33
static LinearExpr sum(LinearArgument[] exprs)
Shortcut for newBuilder().addSum(exprs).build()
Definition: LinearExpr.java:48
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, long[] coeffs)
LinearExprBuilder addSum(LinearArgument[] exprs)
long getOffset()
Returns the constant part of the expression.
int getVariableIndex(int index)
Returns the index of the ith variable.
A specialized constant linear expression.
A object that can build a LinearExpr object.
.lang.Override long getOffset()
int64 offset = 3;
int getVars(int index)
repeated int32 vars = 1;
int numElements()
Returns the number of terms (excluding the constant one) in this expression.
static LinearExpr rebuildFromLinearExpressionProto(LinearExpressionProto proto)
Definition: LinearExpr.java:57
static LinearExpr constant(long value)
Shortcut for newBuilder().add(value).build()
Definition: LinearExpr.java:38
long getCoeffs(int index)
repeated int64 coeffs = 2;
LinearExprBuilder add(LinearArgument expr)
LinearExpr build()
Builds a linear expression.
Builder class for the LinearExpr container.
LinearExprBuilder addTerm(LinearArgument expr, long coeff)
long getCoefficient(int index)
Returns the ith coefficient.