Java Reference

Java Reference

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