Java Reference

Java Reference

modelbuilder/AffineExpression.java
Go to the documentation of this file.
1 // Copyright 2010-2022 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.modelbuilder;
15 
17 public final class AffineExpression implements LinearExpr {
18  private final int varIndex;
19  private final double coefficient;
20  private final double offset;
21 
22  public AffineExpression(int varIndex, double coefficient, double 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 double 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 double getOffset() {
58  return offset;
59  }
60 }
AffineExpression(int varIndex, double coefficient, double offset)
int numElements()
Returns the number of terms (excluding the constant one) in this expression.
double getCoefficient(int index)
Returns the ith coefficient.
double getOffset()
Returns the constant part of the expression.
int getVariableIndex(int index)
Returns the index of the ith variable.
A linear expression (sum (ai * xi) + b).