001package org.clafer.math; 002 003import java.util.ArrayList; 004import java.util.Arrays; 005import java.util.HashMap; 006import java.util.Iterator; 007import java.util.List; 008import java.util.Map; 009import java.util.Map.Entry; 010import java.util.Set; 011import org.clafer.common.Check; 012 013/** 014 * @author jimmy 015 */ 016public class LinearFunction implements LinearFunctionable { 017 018 private final Rational[] coefficients; 019 private final Variable[] variables; 020 private final Rational constant; 021 public final Map<Variable, Rational> coefficientMap; 022 023 public LinearFunction(long coefficient, Variable variable) { 024 this(new Rational(coefficient), variable); 025 } 026 027 public LinearFunction(Rational coefficient, Variable variable) { 028 this(coefficient, variable, Rational.Zero); 029 } 030 031 public LinearFunction(long constant) { 032 this(new Rational(constant)); 033 } 034 035 public LinearFunction(Rational constant) { 036 this.coefficients = new Rational[0]; 037 this.variables = new Variable[0]; 038 this.constant = Check.notNull(constant); 039 this.coefficientMap = new HashMap<>(0); 040 } 041 042 public LinearFunction(long coefficient, Variable variable, long constant) { 043 this(new Rational(coefficient), variable, new Rational(constant)); 044 } 045 046 public LinearFunction(Rational coefficient, Variable variable, Rational constant) { 047 this.coefficients = new Rational[]{Check.notNull(coefficient)}; 048 this.variables = new Variable[]{Check.notNull(variable)}; 049 this.constant = Check.notNull(constant); 050 this.coefficientMap = new HashMap<>(1); 051 this.coefficientMap.put(variable, coefficient); 052 } 053 054 public LinearFunction(long[] coefficients, Variable[] variables, long constant) { 055 this(convert(coefficients), variables, new Rational(constant)); 056 } 057 058 private static Rational[] convert(long[] is) { 059 Rational[] rs = new Rational[is.length]; 060 for (int i = 0; i < rs.length; i++) { 061 rs[i] = new Rational(is[i]); 062 } 063 return rs; 064 } 065 066 public LinearFunction(Rational[] coefficients, Variable[] variables, Rational constant) { 067 Check.notNull(coefficients); 068 Check.noNulls(variables); 069 if (coefficients.length != variables.length) { 070 throw new IllegalArgumentException(); 071 } 072 Map<Variable, Rational> map = new HashMap<>(coefficients.length); 073 for (int i = 0; i < coefficients.length; i++) { 074 Rational old = map.put(variables[i], coefficients[i]); 075 if (old != null) { 076 map.put(variables[i], coefficients[i].add(old)); 077 } 078 } 079 List<Rational> cs = new ArrayList<>(coefficients.length); 080 List<Variable> vs = new ArrayList<>(variables.length); 081 082 Iterator<Entry<Variable, Rational>> iter = map.entrySet().iterator(); 083 while (iter.hasNext()) { 084 Entry<Variable, Rational> entry = iter.next(); 085 if (entry.getValue().isZero()) { 086 iter.remove(); 087 } else { 088 cs.add(entry.getValue()); 089 vs.add(entry.getKey()); 090 } 091 } 092 093 this.coefficients = cs.toArray(new Rational[cs.size()]); 094 this.variables = vs.toArray(new Variable[vs.size()]); 095 this.constant = constant; 096 this.coefficientMap = map; 097 } 098 099 public int arity() { 100 return coefficients.length; 101 } 102 103 public Rational getCoefficient(Variable variable) { 104 return coefficientMap.get(variable); 105 } 106 107 public Rational[] getCoefficients() { 108 return coefficients; 109 } 110 111 public Variable[] getVariables() { 112 return variables; 113 } 114 115 public Set<Variable> getVariableSet() { 116 return coefficientMap.keySet(); 117 } 118 119 public boolean hasConstant() { 120 return !constant.isZero(); 121 } 122 123 public Rational getConstant() { 124 return constant; 125 } 126 127 public Rational getLowBound() { 128 Rational low = constant; 129 for (int i = 0; i < coefficients.length; i++) { 130 low = low.add(coefficients[i].mul(coefficients[i].isPositive() 131 ? variables[i].getLowBound() 132 : variables[i].getHighBound())); 133 } 134 return low; 135 } 136 137 public Rational getHighBound() { 138 Rational high = constant; 139 for (int i = 0; i < coefficients.length; i++) { 140 high = high.add(coefficients[i].mul(coefficients[i].isPositive() 141 ? variables[i].getHighBound() 142 : variables[i].getLowBound())); 143 } 144 return high; 145 } 146 147 public LinearFunction minus() { 148 Rational[] newCoefficients = new Rational[coefficients.length]; 149 for (int i = 0; i < newCoefficients.length; i++) { 150 newCoefficients[i] = coefficients[i].minus(); 151 } 152 return new LinearFunction(newCoefficients, variables, constant.minus()); 153 } 154 155 public LinearFunction add(LinearFunction addend) { 156 Rational[] newCoefficients = Arrays.copyOf(coefficients, coefficients.length + addend.coefficients.length); 157 System.arraycopy(addend.coefficients, 0, newCoefficients, coefficients.length, addend.coefficients.length); 158 Variable[] newVariables = Arrays.copyOf(variables, variables.length + addend.variables.length); 159 System.arraycopy(addend.variables, 0, newVariables, variables.length, addend.variables.length); 160 Rational newConstant = constant.add(addend.constant); 161 return new LinearFunction(newCoefficients, newVariables, newConstant); 162 } 163 164 public LinearFunction add(long addend) { 165 if (addend == 0) { 166 return this; 167 } 168 return add(new Rational(addend)); 169 } 170 171 public LinearFunction add(Rational addend) { 172 if (addend.isZero()) { 173 return this; 174 } 175 return new LinearFunction(coefficients, variables, constant.add(addend)); 176 } 177 178 public LinearFunction sub(LinearFunction subtrahend) { 179 Rational[] newCoefficients = Arrays.copyOf(coefficients, coefficients.length + subtrahend.coefficients.length); 180 for (int i = 0; i < subtrahend.coefficients.length; i++) { 181 newCoefficients[i + coefficients.length] = subtrahend.coefficients[i].minus(); 182 } 183 Variable[] newVariables = Arrays.copyOf(variables, variables.length + subtrahend.variables.length); 184 System.arraycopy(subtrahend.variables, 0, newVariables, variables.length, subtrahend.variables.length); 185 Rational newConstant = constant.sub(subtrahend.constant); 186 return new LinearFunction(newCoefficients, newVariables, newConstant); 187 } 188 189 public LinearFunction sub(long subtrahend) { 190 if (subtrahend == 0) { 191 return this; 192 } 193 return sub(new Rational(subtrahend)); 194 } 195 196 public LinearFunction sub(Rational subtrahend) { 197 if (subtrahend.isZero()) { 198 return this; 199 } 200 return new LinearFunction(coefficients, variables, constant.sub(subtrahend)); 201 } 202 203 public LinearFunction mul(long multiplier) { 204 if (multiplier == 1) { 205 return this; 206 } 207 return mul(new Rational(multiplier)); 208 } 209 210 public LinearFunction mul(Rational multiplier) { 211 if (multiplier.isOne()) { 212 return this; 213 } 214 Rational[] newCoefficients = new Rational[coefficients.length]; 215 for (int i = 0; i < newCoefficients.length; i++) { 216 newCoefficients[i] = coefficients[i].mul(multiplier); 217 } 218 return new LinearFunction(newCoefficients, variables, constant.mul(multiplier)); 219 } 220 221 public LinearFunction div(long divisor) { 222 if (divisor == 0) { 223 throw new ArithmeticException("/ by zero"); 224 } else if (divisor == 1) { 225 return this; 226 } 227 return div(new Rational(divisor)); 228 } 229 230 public LinearFunction div(Rational divisor) { 231 if (divisor.isZero()) { 232 throw new ArithmeticException("/ by zero"); 233 } else if (divisor.isOne()) { 234 return this; 235 } 236 Rational[] newCoefficients = new Rational[coefficients.length]; 237 for (int i = 0; i < newCoefficients.length; i++) { 238 newCoefficients[i] = coefficients[i].div(divisor); 239 } 240 return new LinearFunction(newCoefficients, variables, constant.div(divisor)); 241 } 242 243 public LinearFunction replace(Variable variable, LinearFunction value) { 244 if (!coefficientMap.containsKey(variable)) { 245 return this; 246 } 247 List<Rational> newCoefficients = new ArrayList<>(coefficients.length + value.coefficients.length); 248 List<Variable> newVariables = new ArrayList<>(newCoefficients.size()); 249 Rational newConstant = constant; 250 for (int i = 0; i < coefficients.length; i++) { 251 if (variable.equals(variables[i])) { 252 for (int j = 0; j < value.coefficients.length; j++) { 253 newCoefficients.add(coefficients[i].mul(value.coefficients[j])); 254 newVariables.add(value.variables[j]); 255 } 256 newConstant = newConstant.add(coefficients[i].mul(value.constant)); 257 } else { 258 newCoefficients.add(coefficients[i]); 259 newVariables.add(variables[i]); 260 } 261 } 262 return new LinearFunction( 263 newCoefficients.toArray(new Rational[newCoefficients.size()]), 264 newVariables.toArray(new Variable[newVariables.size()]), 265 newConstant); 266 } 267 268 @Override 269 public LinearFunction toFunction() { 270 return this; 271 } 272 273 @Override 274 public boolean equals(Object obj) { 275 if (obj == this) { 276 return true; 277 } 278 if (obj instanceof LinearFunction) { 279 LinearFunction other = (LinearFunction) obj; 280 return coefficientMap.equals(other.coefficientMap) 281 && constant.equals(other.constant); 282 } 283 return false; 284 } 285 286 @Override 287 public int hashCode() { 288 return coefficientMap.hashCode() ^ constant.hashCode(); 289 } 290 291 @Override 292 public String toString() { 293 if (coefficients.length == 0) { 294 return constant.toString(); 295 } 296 StringBuilder result = new StringBuilder(); 297 for (int i = 0; i < coefficients.length; i++) { 298 if (i > 0) { 299 result.append(" + "); 300 } 301 result.append(coefficients[i].isPositive() ? coefficients[i] : "(" + coefficients[i] + ")"); 302 result.append("*"); 303 result.append(variables[i]); 304 } 305 if (!constant.isZero()) { 306 result.append(" + "); 307 result.append(constant); 308 } 309 return result.toString(); 310 } 311}