Java Reference

Java Reference

AffineExpression.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 public final class AffineExpression implements LinearExpr {
18  private final int varIndex;
19  private final long coefficient;
20  private final long offset;
21 
22  public AffineExpression(int varIndex, long coefficient, long offset) {
23  this.varIndex = varIndex;
24  this.coefficient = coefficient;
25  this.offset = offset;
26  }
27 
28  // LinearArgument interface.
29  @Override
30  public LinearExpr build() {
31  return this;
32  }
33 
34  // LinearExpr interface.
35  @Override
36  public int numElements() {
37  return 1;
38  }
39 
40  @Override
41  public int getVariableIndex(int index) {
42  if (index != 0) {
43  throw new IllegalArgumentException("wrong index in LinearExpr.getIndex(): " + index);
44  }
45  return varIndex;
46  }
47 
48  @Override
49  public long getCoefficient(int index) {
50  if (index != 0) {
51  throw new IllegalArgumentException("wrong index in LinearExpr.getCoefficient(): " + index);
52  }
53  return coefficient;
54  }
55 
56  @Override
57  public long getOffset() {
58  return offset;
59  }
60 }
A linear expression (sum (ai * xi) + b).
Definition: LinearExpr.java:19
long getOffset()
Returns the constant part of the expression.
LinearExpr build()
Builds a linear expression.
A specialized linear expression: a * x + b.
long getCoefficient(int index)
Returns the ith coefficient.
int getVariableIndex(int index)
Returns the index of the ith variable.
int numElements()
Returns the number of terms (excluding the constant one) in this expression.
AffineExpression(int varIndex, long coefficient, long offset)