1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/*
Copyright (c) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
is hereby granted without fee, provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
CERN makes no representations about the suitability of this software for any purpose.
It is provided "as is" without expressed or implied warranty.
*/
package cern.jet.math;
import cern.colt.function.IntFunction;
import cern.colt.function.IntIntFunction;
import cern.colt.function.IntIntProcedure;
import cern.colt.function.IntProcedure;
/**
Integer Function objects to be passed to generic methods.
Same as {@link Functions} except operating on integers.
<p>
For aliasing see {@link #intFunctions}.
@author wolfgang.hoschek@cern.ch
@version 1.0, 09/24/99
*/
public class IntFunctions extends Object {
/**
Little trick to allow for "aliasing", that is, renaming this class.
Writing code like
<p>
<tt>IntFunctions.chain(IntFunctions.plus,IntFunctions.mult(3),IntFunctions.chain(IntFunctions.square,IntFunctions.div(2)));</tt>
<p>
is a bit awkward, to say the least.
Using the aliasing you can instead write
<p>
<tt>IntFunctions F = IntFunctions.intFunctions; <br>
F.chain(F.plus,F.mult(3),F.chain(F.square,F.div(2)));</tt>
<p>
*/
public static final IntFunctions intFunctions = new IntFunctions();
/*****************************
* <H3>Unary functions</H3>
*****************************/
/**
* Function that returns <tt>Math.abs(a) == (a < 0) ? -a : a</tt>.
*/
public static final IntFunction abs = new IntFunction() {
public final int apply(int a) { return (a < 0) ? -a : a; }
};
/**
* Function that returns <tt>a--</tt>.
*/
public static final IntFunction dec = new IntFunction() {
public final int apply(int a) { return a--; }
};
/**
* Function that returns <tt>(int) Arithmetic.factorial(a)</tt>.
*/
public static final IntFunction factorial = new IntFunction() {
public final int apply(int a) { return (int) Arithmetic.factorial(a); }
};
/**
* Function that returns its argument.
*/
public static final IntFunction identity = new IntFunction() {
public final int apply(int a) { return a; }
};
/**
* Function that returns <tt>a++</tt>.
*/
public static final IntFunction inc = new IntFunction() {
public final int apply(int a) { return a++; }
};
/**
* Function that returns <tt>-a</tt>.
*/
public static final IntFunction neg = new IntFunction() {
public final int apply(int a) { return -a; }
};
/**
* Function that returns <tt>~a</tt>.
*/
public static final IntFunction not = new IntFunction() {
public final int apply(int a) { return ~a; }
};
/**
* Function that returns <tt>a < 0 ? -1 : a > 0 ? 1 : 0</tt>.
*/
public static final IntFunction sign = new IntFunction() {
public final int apply(int a) { return a < 0 ? -1 : a > 0 ? 1 : 0; }
};
/**
* Function that returns <tt>a * a</tt>.
*/
public static final IntFunction square = new IntFunction() {
public final int apply(int a) { return a * a; }
};
/*****************************
* <H3>Binary functions</H3>
*****************************/
/**
* Function that returns <tt>a & b</tt>.
*/
public static final IntIntFunction and = new IntIntFunction() {
public final int apply(int a, int b) { return a & b; }
};
/**
* Function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>.
*/
public static final IntIntFunction compare = new IntIntFunction() {
public final int apply(int a, int b) { return a < b ? -1 : a > b ? 1 : 0; }
};
/**
* Function that returns <tt>a / b</tt>.
*/
public static final IntIntFunction div = new IntIntFunction() {
public final int apply(int a, int b) { return a / b; }
};
/**
* Function that returns <tt>a == b ? 1 : 0</tt>.
*/
public static final IntIntFunction equals = new IntIntFunction() {
public final int apply(int a, int b) { return a == b ? 1 : 0; }
};
/**
* Function that returns <tt>a == b</tt>.
*/
public static final IntIntProcedure isEqual = new IntIntProcedure() {
public final boolean apply(int a, int b) { return a == b; }
};
/**
* Function that returns <tt>a < b</tt>.
*/
public static final IntIntProcedure isLess = new IntIntProcedure() {
public final boolean apply(int a, int b) { return a < b; }
};
/**
* Function that returns <tt>a > b</tt>.
*/
public static final IntIntProcedure isGreater = new IntIntProcedure() {
public final boolean apply(int a, int b) { return a > b; }
};
/**
* Function that returns <tt>Math.max(a,b)</tt>.
*/
public static final IntIntFunction max = new IntIntFunction() {
public final int apply(int a, int b) { return (a >= b) ? a : b; }
};
/**
* Function that returns <tt>Math.min(a,b)</tt>.
*/
public static final IntIntFunction min = new IntIntFunction() {
public final int apply(int a, int b) { return (a <= b) ? a : b; }
};
/**
* Function that returns <tt>a - b</tt>.
*/
public static final IntIntFunction minus = new IntIntFunction() {
public final int apply(int a, int b) { return a - b; }
};
/**
* Function that returns <tt>a % b</tt>.
*/
public static final IntIntFunction mod = new IntIntFunction() {
public final int apply(int a, int b) { return a % b; }
};
/**
* Function that returns <tt>a * b</tt>.
*/
public static final IntIntFunction mult = new IntIntFunction() {
public final int apply(int a, int b) { return a * b; }
};
/**
* Function that returns <tt>a | b</tt>.
*/
public static final IntIntFunction or = new IntIntFunction() {
public final int apply(int a, int b) { return a | b; }
};
/**
* Function that returns <tt>a + b</tt>.
*/
public static final IntIntFunction plus = new IntIntFunction() {
public final int apply(int a, int b) { return a + b; }
};
/**
* Function that returns <tt>(int) Math.pow(a,b)</tt>.
*/
public static final IntIntFunction pow = new IntIntFunction() {
public final int apply(int a, int b) { return (int) Math.pow(a,b); }
};
/**
* Function that returns <tt>a << b</tt>.
*/
public static final IntIntFunction shiftLeft = new IntIntFunction() {
public final int apply(int a, int b) { return a << b; }
};
/**
* Function that returns <tt>a >> b</tt>.
*/
public static final IntIntFunction shiftRightSigned = new IntIntFunction() {
public final int apply(int a, int b) { return a >> b; }
};
/**
* Function that returns <tt>a >>> b</tt>.
*/
public static final IntIntFunction shiftRightUnsigned = new IntIntFunction() {
public final int apply(int a, int b) { return a >>> b; }
};
/**
* Function that returns <tt>a ^ b</tt>.
*/
public static final IntIntFunction xor = new IntIntFunction() {
public final int apply(int a, int b) { return a ^ b; }
};
/**
* Makes this class non instantiable, but still let's others inherit from it.
*/
protected IntFunctions() {}
/**
* Constructs a function that returns <tt>a & b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction and(final int b) {
return new IntFunction() {
public final int apply(int a) { return a & b; }
};
}
/**
* Constructs a function that returns <tt>(from<=a && a<=to) ? 1 : 0</tt>.
* <tt>a</tt> is a variable, <tt>from</tt> and <tt>to</tt> are fixed.
*/
public static IntFunction between(final int from, final int to) {
return new IntFunction() {
public final int apply(int a) { return (from<=a && a<=to) ? 1 : 0; }
};
}
/**
* Constructs a unary function from a binary function with the first operand (argument) fixed to the given constant <tt>c</tt>.
* The second operand is variable (free).
*
* @param function a binary function taking operands in the form <tt>function.apply(c,var)</tt>.
* @return the unary function <tt>function(c,var)</tt>.
*/
public static IntFunction bindArg1(final IntIntFunction function, final int c) {
return new IntFunction() {
public final int apply(int var) { return function.apply(c,var); }
};
}
/**
* Constructs a unary function from a binary function with the second operand (argument) fixed to the given constant <tt>c</tt>.
* The first operand is variable (free).
*
* @param function a binary function taking operands in the form <tt>function.apply(var,c)</tt>.
* @return the unary function <tt>function(var,c)</tt>.
*/
public static IntFunction bindArg2(final IntIntFunction function, final int c) {
return new IntFunction() {
public final int apply(int var) { return function.apply(var,c); }
};
}
/**
* Constructs the function <tt>g( h(a) )</tt>.
*
* @param g a unary function.
* @param h a unary function.
* @return the unary function <tt>g( h(a) )</tt>.
*/
public static IntFunction chain(final IntFunction g, final IntFunction h) {
return new IntFunction() {
public final int apply(int a) { return g.apply(h.apply(a)); }
};
}
/**
* Constructs the function <tt>g( h(a,b) )</tt>.
*
* @param g a unary function.
* @param h a binary function.
* @return the unary function <tt>g( h(a,b) )</tt>.
*/
public static IntIntFunction chain(final IntFunction g, final IntIntFunction h) {
return new IntIntFunction() {
public final int apply(int a, int b) { return g.apply(h.apply(a,b)); }
};
}
/**
* Constructs the function <tt>f( g(a), h(b) )</tt>.
*
* @param f a binary function.
* @param g a unary function.
* @param h a unary function.
* @return the binary function <tt>f( g(a), h(b) )</tt>.
*/
public static IntIntFunction chain(final IntIntFunction f, final IntFunction g, final IntFunction h) {
return new IntIntFunction() {
public final int apply(int a, int b) { return f.apply(g.apply(a), h.apply(b)); }
};
}
/**
* Constructs a function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction compare(final int b) {
return new IntFunction() {
public final int apply(int a) { return a < b ? -1 : a > b ? 1 : 0; }
};
}
/**
* Constructs a function that returns the constant <tt>c</tt>.
*/
public static IntFunction constant(final int c) {
return new IntFunction() {
public final int apply(int a) { return c; }
};
}
/**
* Constructs a function that returns <tt>a / b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction div(final int b) {
return new IntFunction() {
public final int apply(int a) { return a / b; }
};
}
/**
* Constructs a function that returns <tt>a == b ? 1 : 0</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction equals(final int b) {
return new IntFunction() {
public final int apply(int a) { return a == b ? 1 : 0; }
};
}
/**
* Constructs a function that returns <tt>from<=a && a<=to</tt>.
* <tt>a</tt> is a variable, <tt>from</tt> and <tt>to</tt> are fixed.
*/
public static IntProcedure isBetween(final int from, final int to) {
return new IntProcedure() {
public final boolean apply(int a) { return from<=a && a<=to; }
};
}
/**
* Constructs a function that returns <tt>a == b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntProcedure isEqual(final int b) {
return new IntProcedure() {
public final boolean apply(int a) { return a==b; }
};
}
/**
* Constructs a function that returns <tt>a > b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntProcedure isGreater(final int b) {
return new IntProcedure() {
public final boolean apply(int a) { return a > b; }
};
}
/**
* Constructs a function that returns <tt>a < b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntProcedure isLess(final int b) {
return new IntProcedure() {
public final boolean apply(int a) { return a < b; }
};
}
/**
* Constructs a function that returns <tt>Math.max(a,b)</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction max(final int b) {
return new IntFunction() {
public final int apply(int a) { return (a >= b) ? a : b; }
};
}
/**
* Constructs a function that returns <tt>Math.min(a,b)</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction min(final int b) {
return new IntFunction() {
public final int apply(int a) { return (a <= b) ? a : b; }
};
}
/**
* Constructs a function that returns <tt>a - b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction minus(final int b) {
return new IntFunction() {
public final int apply(int a) { return a - b; }
};
}
/**
* Constructs a function that returns <tt>a % b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction mod(final int b) {
return new IntFunction() {
public final int apply(int a) { return a % b; }
};
}
/**
* Constructs a function that returns <tt>a * b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction mult(final int b) {
return new IntFunction() {
public final int apply(int a) { return a * b; }
};
}
/**
* Constructs a function that returns <tt>a | b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction or(final int b) {
return new IntFunction() {
public final int apply(int a) { return a | b; }
};
}
/**
* Constructs a function that returns <tt>a + b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction plus(final int b) {
return new IntFunction() {
public final int apply(int a) { return a + b; }
};
}
/**
* Constructs a function that returns <tt>(int) Math.pow(a,b)</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction pow(final int b) {
return new IntFunction() {
public final int apply(int a) { return (int) Math.pow(a,b); }
};
}
/**
* Constructs a function that returns a 32 bit uniformly distributed random number in the closed interval <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt> (including <tt>Integer.MIN_VALUE</tt> and <tt>Integer.MAX_VALUE</tt>).
* Currently the engine is {@link cern.jet.random.engine.MersenneTwister}
* and is seeded with the current time.
* <p>
* Note that any random engine derived from {@link cern.jet.random.engine.RandomEngine} and any random distribution derived from {@link cern.jet.random.AbstractDistribution} are function objects, because they implement the proper interfaces.
* Thus, if you are not happy with the default, just pass your favourite random generator to function evaluating methods.
*/
public static IntFunction random() {
return new cern.jet.random.engine.MersenneTwister(new java.util.Date());
}
/**
* Constructs a function that returns <tt>a << b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction shiftLeft(final int b) {
return new IntFunction() {
public final int apply(int a) { return a << b; }
};
}
/**
* Constructs a function that returns <tt>a >> b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction shiftRightSigned(final int b) {
return new IntFunction() {
public final int apply(int a) { return a >> b; }
};
}
/**
* Constructs a function that returns <tt>a >>> b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction shiftRightUnsigned(final int b) {
return new IntFunction() {
public final int apply(int a) { return a >>> b; }
};
}
/**
* Constructs a function that returns <tt>function.apply(b,a)</tt>, i.e. applies the function with the first operand as second operand and the second operand as first operand.
*
* @param function a function taking operands in the form <tt>function.apply(a,b)</tt>.
* @return the binary function <tt>function(b,a)</tt>.
*/
public static IntIntFunction swapArgs(final IntIntFunction function) {
return new IntIntFunction() {
public final int apply(int a, int b) { return function.apply(b,a); }
};
}
/**
* Constructs a function that returns <tt>a | b</tt>.
* <tt>a</tt> is a variable, <tt>b</tt> is fixed.
*/
public static IntFunction xor(final int b) {
return new IntFunction() {
public final int apply(int a) { return a ^ b; }
};
}
}
|