001 // --- BEGIN LICENSE BLOCK --- 002 /* 003 * Copyright (c) 2009, Mikio L. Braun 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or without 007 * modification, are permitted provided that the following conditions are 008 * met: 009 * 010 * * Redistributions of source code must retain the above copyright 011 * notice, this list of conditions and the following disclaimer. 012 * 013 * * Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials provided 016 * with the distribution. 017 * 018 * * Neither the name of the Technische Universit??t Berlin nor the 019 * names of its contributors may be used to endorse or promote 020 * products derived from this software without specific prior 021 * written permission. 022 * 023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 026 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 027 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 028 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 029 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 030 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 031 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 032 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 034 */ 035 // --- END LICENSE BLOCK --- 036 037 package org.jblas; 038 039 import java.lang.Math; 040 041 /** 042 * This class provides the functions from java.lang.Math for matrices. The 043 * functions are applied to each element of the matrix. 044 * 045 * @author Mikio Braun 046 */ 047 public class MatrixFunctions { 048 049 /*# 050 def mapfct(f); <<-EOS 051 for (int i = 0; i < x.length; i++) 052 x.put(i, (double) #{f}(x.get(i))); 053 return x; 054 EOS 055 end 056 057 def cmapfct(f); <<-EOS 058 for (int i = 0; i < x.length; i++) 059 x.put(i, x.get(i).#{f}()); 060 return x; 061 EOS 062 end 063 #*/ 064 065 /** 066 * Sets all elements in this matrix to their absolute values. Note 067 * that this operation is in-place. 068 * @see MatrixFunctions#abs(DoubleMatrix) 069 * @return this matrix 070 */ 071 public static DoubleMatrix absi(DoubleMatrix x) { 072 /*# mapfct('Math.abs') #*/ 073 //RJPP-BEGIN------------------------------------------------------------ 074 for (int i = 0; i < x.length; i++) 075 x.put(i, (double) Math.abs(x.get(i))); 076 return x; 077 //RJPP-END-------------------------------------------------------------- 078 } 079 080 public static ComplexDoubleMatrix absi(ComplexDoubleMatrix x) { 081 /*# cmapfct('abs') #*/ 082 //RJPP-BEGIN------------------------------------------------------------ 083 for (int i = 0; i < x.length; i++) 084 x.put(i, x.get(i).abs()); 085 return x; 086 //RJPP-END-------------------------------------------------------------- 087 } 088 089 /** 090 * Applies the trigonometric <i>arccosine</i> function element wise on this 091 * matrix. Note that this is an in-place operation. 092 * @see MatrixFunctions#acos(DoubleMatrix) 093 * @return this matrix 094 */ 095 public static DoubleMatrix acosi(DoubleMatrix x) { 096 /*# mapfct('Math.acos') #*/ 097 //RJPP-BEGIN------------------------------------------------------------ 098 for (int i = 0; i < x.length; i++) 099 x.put(i, (double) Math.acos(x.get(i))); 100 return x; 101 //RJPP-END-------------------------------------------------------------- 102 } 103 104 /** 105 * Applies the trigonometric <i>arcsine</i> function element wise on this 106 * matrix. Note that this is an in-place operation. 107 * @see MatrixFunctions#asin(DoubleMatrix) 108 * @return this matrix 109 */ 110 public static DoubleMatrix asini(DoubleMatrix x) { 111 /*# mapfct('Math.asin') #*/ 112 //RJPP-BEGIN------------------------------------------------------------ 113 for (int i = 0; i < x.length; i++) 114 x.put(i, (double) Math.asin(x.get(i))); 115 return x; 116 //RJPP-END-------------------------------------------------------------- 117 } 118 119 /** 120 * Applies the trigonometric <i>arctangend</i> function element wise on this 121 * matrix. Note that this is an in-place operation. 122 * @see MatrixFunctions#atan(DoubleMatrix) 123 * @return this matrix 124 */ 125 public static DoubleMatrix atani(DoubleMatrix x) { 126 /*# mapfct('Math.atan') #*/ 127 //RJPP-BEGIN------------------------------------------------------------ 128 for (int i = 0; i < x.length; i++) 129 x.put(i, (double) Math.atan(x.get(i))); 130 return x; 131 //RJPP-END-------------------------------------------------------------- 132 } 133 134 /** 135 * Applies the <i>cube root</i> function element wise on this 136 * matrix. Note that this is an in-place operation. 137 * @see MatrixFunctions#cbrt(DoubleMatrix) 138 * @return this matrix 139 */ 140 public static DoubleMatrix cbrti(DoubleMatrix x) { 141 /*# mapfct('Math.cbrt') #*/ 142 //RJPP-BEGIN------------------------------------------------------------ 143 for (int i = 0; i < x.length; i++) 144 x.put(i, (double) Math.cbrt(x.get(i))); 145 return x; 146 //RJPP-END-------------------------------------------------------------- 147 } 148 149 /** 150 * Element-wise round up by applying the <i>ceil</i> function on each 151 * element. Note that this is an in-place operation. 152 * @see MatrixFunctions#ceil(DoubleMatrix) 153 * @return this matrix 154 */ 155 public static DoubleMatrix ceili(DoubleMatrix x) { 156 /*# mapfct('Math.ceil') #*/ 157 //RJPP-BEGIN------------------------------------------------------------ 158 for (int i = 0; i < x.length; i++) 159 x.put(i, (double) Math.ceil(x.get(i))); 160 return x; 161 //RJPP-END-------------------------------------------------------------- 162 } 163 164 /** 165 * Applies the <i>cosine</i> function element-wise on this 166 * matrix. Note that this is an in-place operation. 167 * @see MatrixFunctions#cos(DoubleMatrix) 168 * @return this matrix 169 */ 170 public static DoubleMatrix cosi(DoubleMatrix x) { 171 /*# mapfct('Math.cos') #*/ 172 //RJPP-BEGIN------------------------------------------------------------ 173 for (int i = 0; i < x.length; i++) 174 x.put(i, (double) Math.cos(x.get(i))); 175 return x; 176 //RJPP-END-------------------------------------------------------------- 177 } 178 179 /** 180 * Applies the <i>hyperbolic cosine</i> function element-wise on this 181 * matrix. Note that this is an in-place operation. 182 * @see MatrixFunctions#cosh(DoubleMatrix) 183 * @return this matrix 184 */ 185 public static DoubleMatrix coshi(DoubleMatrix x) { 186 /*# mapfct('Math.cosh') #*/ 187 //RJPP-BEGIN------------------------------------------------------------ 188 for (int i = 0; i < x.length; i++) 189 x.put(i, (double) Math.cosh(x.get(i))); 190 return x; 191 //RJPP-END-------------------------------------------------------------- 192 } 193 194 /** 195 * Applies the <i>exponential</i> function element-wise on this 196 * matrix. Note that this is an in-place operation. 197 * @see MatrixFunctions#exp(DoubleMatrix) 198 * @return this matrix 199 */ 200 public static DoubleMatrix expi(DoubleMatrix x) { 201 /*# mapfct('Math.exp') #*/ 202 //RJPP-BEGIN------------------------------------------------------------ 203 for (int i = 0; i < x.length; i++) 204 x.put(i, (double) Math.exp(x.get(i))); 205 return x; 206 //RJPP-END-------------------------------------------------------------- 207 } 208 209 /** 210 * Element-wise round down by applying the <i>floor</i> function on each 211 * element. Note that this is an in-place operation. 212 * @see MatrixFunctions#floor(DoubleMatrix) 213 * @return this matrix 214 */ 215 public static DoubleMatrix floori(DoubleMatrix x) { 216 /*# mapfct('Math.floor') #*/ 217 //RJPP-BEGIN------------------------------------------------------------ 218 for (int i = 0; i < x.length; i++) 219 x.put(i, (double) Math.floor(x.get(i))); 220 return x; 221 //RJPP-END-------------------------------------------------------------- 222 } 223 224 /** 225 * Applies the <i>natural logarithm</i> function element-wise on this 226 * matrix. Note that this is an in-place operation. 227 * @see MatrixFunctions#log(DoubleMatrix) 228 * @return this matrix 229 */ 230 public static DoubleMatrix logi(DoubleMatrix x) { 231 /*# mapfct('Math.log') #*/ 232 //RJPP-BEGIN------------------------------------------------------------ 233 for (int i = 0; i < x.length; i++) 234 x.put(i, (double) Math.log(x.get(i))); 235 return x; 236 //RJPP-END-------------------------------------------------------------- 237 } 238 239 /** 240 * Applies the <i>logarithm with basis to 10</i> element-wise on this 241 * matrix. Note that this is an in-place operation. 242 * @see MatrixFunctions#log10(DoubleMatrix) 243 * @return this matrix 244 */ 245 public static DoubleMatrix log10i(DoubleMatrix x) { 246 /*# mapfct('Math.log10') #*/ 247 //RJPP-BEGIN------------------------------------------------------------ 248 for (int i = 0; i < x.length; i++) 249 x.put(i, (double) Math.log10(x.get(i))); 250 return x; 251 //RJPP-END-------------------------------------------------------------- 252 } 253 254 /** 255 * Element-wise power function. Replaces each element with its 256 * power of <tt>d</tt>.Note that this is an in-place operation. 257 * @param d the exponent 258 * @see MatrixFunctions#pow(DoubleMatrix,double) 259 * @return this matrix 260 */ 261 public static DoubleMatrix powi(DoubleMatrix x, double d) { 262 if (d == 2.0) 263 return x.muli(x); 264 else { 265 for (int i = 0; i < x.length; i++) 266 x.put(i, (double) Math.pow(x.get(i), d)); 267 return x; 268 } 269 } 270 271 public static DoubleMatrix powi(double base, DoubleMatrix x) { 272 for (int i = 0; i < x.length; i++) 273 x.put(i, (double) Math.pow(base, x.get(i))); 274 return x; 275 } 276 277 public static DoubleMatrix powi(DoubleMatrix x, DoubleMatrix e) { 278 x.checkLength(e.length); 279 for (int i = 0; i < x.length; i++) 280 x.put(i, (double) Math.pow(x.get(i), e.get(i))); 281 return x; 282 } 283 284 public static DoubleMatrix signumi(DoubleMatrix x) { 285 /*# mapfct('Math.signum') #*/ 286 //RJPP-BEGIN------------------------------------------------------------ 287 for (int i = 0; i < x.length; i++) 288 x.put(i, (double) Math.signum(x.get(i))); 289 return x; 290 //RJPP-END-------------------------------------------------------------- 291 } 292 293 public static DoubleMatrix sini(DoubleMatrix x) { 294 /*# mapfct('Math.sin') #*/ 295 //RJPP-BEGIN------------------------------------------------------------ 296 for (int i = 0; i < x.length; i++) 297 x.put(i, (double) Math.sin(x.get(i))); 298 return x; 299 //RJPP-END-------------------------------------------------------------- 300 } 301 302 public static DoubleMatrix sinhi(DoubleMatrix x) { 303 /*# mapfct('Math.sinh') #*/ 304 //RJPP-BEGIN------------------------------------------------------------ 305 for (int i = 0; i < x.length; i++) 306 x.put(i, (double) Math.sinh(x.get(i))); 307 return x; 308 //RJPP-END-------------------------------------------------------------- 309 } 310 public static DoubleMatrix sqrti(DoubleMatrix x) { 311 /*# mapfct('Math.sqrt') #*/ 312 //RJPP-BEGIN------------------------------------------------------------ 313 for (int i = 0; i < x.length; i++) 314 x.put(i, (double) Math.sqrt(x.get(i))); 315 return x; 316 //RJPP-END-------------------------------------------------------------- 317 } 318 public static DoubleMatrix tani(DoubleMatrix x) { 319 /*# mapfct('Math.tan') #*/ 320 //RJPP-BEGIN------------------------------------------------------------ 321 for (int i = 0; i < x.length; i++) 322 x.put(i, (double) Math.tan(x.get(i))); 323 return x; 324 //RJPP-END-------------------------------------------------------------- 325 } 326 public static DoubleMatrix tanhi(DoubleMatrix x) { 327 /*# mapfct('Math.tanh') #*/ 328 //RJPP-BEGIN------------------------------------------------------------ 329 for (int i = 0; i < x.length; i++) 330 x.put(i, (double) Math.tanh(x.get(i))); 331 return x; 332 //RJPP-END-------------------------------------------------------------- 333 } 334 335 /** 336 * Returns a copy of this matrix where all elements are set to their 337 * absolute values. 338 * @see MatrixFunctions#absi(DoubleMatrix) 339 * @return copy of this matrix 340 */ 341 public static DoubleMatrix abs(DoubleMatrix x) { return absi(x.dup()); } 342 343 /** 344 * Returns a copy of this matrix where the trigonometric <i>acos</i> function is applied 345 * element wise. 346 * @see MatrixFunctions#acosi(DoubleMatrix) 347 * @return copy of this matrix 348 */ 349 public static DoubleMatrix acos(DoubleMatrix x) { return acosi(x.dup()); } 350 public static DoubleMatrix asin(DoubleMatrix x) { return asini(x.dup()); } 351 public static DoubleMatrix atan(DoubleMatrix x) { return atani(x.dup()); } 352 public static DoubleMatrix cbrt(DoubleMatrix x) { return cbrti(x.dup()); } 353 public static DoubleMatrix ceil(DoubleMatrix x) { return ceili(x.dup()); } 354 public static DoubleMatrix cos(DoubleMatrix x) { return cosi(x.dup()); } 355 public static DoubleMatrix cosh(DoubleMatrix x) { return coshi(x.dup()); } 356 public static DoubleMatrix exp(DoubleMatrix x) { return expi(x.dup()); } 357 public static DoubleMatrix floor(DoubleMatrix x) { return floori(x.dup()); } 358 public static DoubleMatrix log(DoubleMatrix x) { return logi(x.dup()); } 359 public static DoubleMatrix log10(DoubleMatrix x) { return log10i(x.dup()); } 360 public static double pow(double x, double y) { return (double)Math.pow(x, y); } 361 public static DoubleMatrix pow(DoubleMatrix x, double e) { return powi(x.dup(), e); } 362 public static DoubleMatrix pow(double b, DoubleMatrix x) { return powi(b, x.dup()); } 363 public static DoubleMatrix pow(DoubleMatrix x, DoubleMatrix e) { return powi(x.dup(), e); } 364 public static DoubleMatrix signum(DoubleMatrix x) { return signumi(x.dup()); } 365 public static DoubleMatrix sin(DoubleMatrix x) { return sini(x.dup()); } 366 public static DoubleMatrix sinh(DoubleMatrix x) { return sinhi(x.dup()); } 367 public static DoubleMatrix sqrt(DoubleMatrix x) { return sqrti(x.dup()); } 368 public static DoubleMatrix tan(DoubleMatrix x) { return tani(x.dup()); } 369 public static DoubleMatrix tanh(DoubleMatrix x) { return tanhi(x.dup()); } 370 371 /*# %w{abs acos asin atan cbrt ceil cos cosh exp floor log log10 signum sin sinh sqrt tan tanh}.map do |fct| <<-EOS 372 public static double #{fct}(double x) { return (double)Math.#{fct}(x); } 373 EOS 374 end 375 #*/ 376 //RJPP-BEGIN------------------------------------------------------------ 377 public static double abs(double x) { return (double)Math.abs(x); } 378 public static double acos(double x) { return (double)Math.acos(x); } 379 public static double asin(double x) { return (double)Math.asin(x); } 380 public static double atan(double x) { return (double)Math.atan(x); } 381 public static double cbrt(double x) { return (double)Math.cbrt(x); } 382 public static double ceil(double x) { return (double)Math.ceil(x); } 383 public static double cos(double x) { return (double)Math.cos(x); } 384 public static double cosh(double x) { return (double)Math.cosh(x); } 385 public static double exp(double x) { return (double)Math.exp(x); } 386 public static double floor(double x) { return (double)Math.floor(x); } 387 public static double log(double x) { return (double)Math.log(x); } 388 public static double log10(double x) { return (double)Math.log10(x); } 389 public static double signum(double x) { return (double)Math.signum(x); } 390 public static double sin(double x) { return (double)Math.sin(x); } 391 public static double sinh(double x) { return (double)Math.sinh(x); } 392 public static double sqrt(double x) { return (double)Math.sqrt(x); } 393 public static double tan(double x) { return (double)Math.tan(x); } 394 public static double tanh(double x) { return (double)Math.tanh(x); } 395 //RJPP-END-------------------------------------------------------------- 396 397 //STOP 398 public static DoubleMatrix floatToDouble(FloatMatrix fm) { 399 DoubleMatrix dm = new DoubleMatrix(fm.rows, fm.columns); 400 401 for (int i = 0; i < fm.length; i++) 402 dm.put(i, (double) fm.get(i)); 403 404 return dm; 405 } 406 407 public static FloatMatrix doubleToFloat(DoubleMatrix dm) { 408 FloatMatrix fm = new FloatMatrix(dm.rows, dm.columns); 409 410 for (int i = 0; i < dm.length; i++) 411 fm.put(i, (float) dm.get(i)); 412 413 return fm; 414 } 415 //START 416 417 //BEGIN 418 // The code below has been automatically generated. 419 // DO NOT EDIT! 420 421 /*# 422 def mapfct(f); <<-EOS 423 for (int i = 0; i < x.length; i++) 424 x.put(i, (float) #{f}(x.get(i))); 425 return x; 426 EOS 427 end 428 429 def cmapfct(f); <<-EOS 430 for (int i = 0; i < x.length; i++) 431 x.put(i, x.get(i).#{f}()); 432 return x; 433 EOS 434 end 435 #*/ 436 437 /** 438 * Sets all elements in this matrix to their absolute values. Note 439 * that this operation is in-place. 440 * @see MatrixFunctions#abs(FloatMatrix) 441 * @return this matrix 442 */ 443 public static FloatMatrix absi(FloatMatrix x) { 444 /*# mapfct('Math.abs') #*/ 445 //RJPP-BEGIN------------------------------------------------------------ 446 for (int i = 0; i < x.length; i++) 447 x.put(i, (float) Math.abs(x.get(i))); 448 return x; 449 //RJPP-END-------------------------------------------------------------- 450 } 451 452 public static ComplexFloatMatrix absi(ComplexFloatMatrix x) { 453 /*# cmapfct('abs') #*/ 454 //RJPP-BEGIN------------------------------------------------------------ 455 for (int i = 0; i < x.length; i++) 456 x.put(i, x.get(i).abs()); 457 return x; 458 //RJPP-END-------------------------------------------------------------- 459 } 460 461 /** 462 * Applies the trigonometric <i>arccosine</i> function element wise on this 463 * matrix. Note that this is an in-place operation. 464 * @see MatrixFunctions#acos(FloatMatrix) 465 * @return this matrix 466 */ 467 public static FloatMatrix acosi(FloatMatrix x) { 468 /*# mapfct('Math.acos') #*/ 469 //RJPP-BEGIN------------------------------------------------------------ 470 for (int i = 0; i < x.length; i++) 471 x.put(i, (float) Math.acos(x.get(i))); 472 return x; 473 //RJPP-END-------------------------------------------------------------- 474 } 475 476 /** 477 * Applies the trigonometric <i>arcsine</i> function element wise on this 478 * matrix. Note that this is an in-place operation. 479 * @see MatrixFunctions#asin(FloatMatrix) 480 * @return this matrix 481 */ 482 public static FloatMatrix asini(FloatMatrix x) { 483 /*# mapfct('Math.asin') #*/ 484 //RJPP-BEGIN------------------------------------------------------------ 485 for (int i = 0; i < x.length; i++) 486 x.put(i, (float) Math.asin(x.get(i))); 487 return x; 488 //RJPP-END-------------------------------------------------------------- 489 } 490 491 /** 492 * Applies the trigonometric <i>arctangend</i> function element wise on this 493 * matrix. Note that this is an in-place operation. 494 * @see MatrixFunctions#atan(FloatMatrix) 495 * @return this matrix 496 */ 497 public static FloatMatrix atani(FloatMatrix x) { 498 /*# mapfct('Math.atan') #*/ 499 //RJPP-BEGIN------------------------------------------------------------ 500 for (int i = 0; i < x.length; i++) 501 x.put(i, (float) Math.atan(x.get(i))); 502 return x; 503 //RJPP-END-------------------------------------------------------------- 504 } 505 506 /** 507 * Applies the <i>cube root</i> function element wise on this 508 * matrix. Note that this is an in-place operation. 509 * @see MatrixFunctions#cbrt(FloatMatrix) 510 * @return this matrix 511 */ 512 public static FloatMatrix cbrti(FloatMatrix x) { 513 /*# mapfct('Math.cbrt') #*/ 514 //RJPP-BEGIN------------------------------------------------------------ 515 for (int i = 0; i < x.length; i++) 516 x.put(i, (float) Math.cbrt(x.get(i))); 517 return x; 518 //RJPP-END-------------------------------------------------------------- 519 } 520 521 /** 522 * Element-wise round up by applying the <i>ceil</i> function on each 523 * element. Note that this is an in-place operation. 524 * @see MatrixFunctions#ceil(FloatMatrix) 525 * @return this matrix 526 */ 527 public static FloatMatrix ceili(FloatMatrix x) { 528 /*# mapfct('Math.ceil') #*/ 529 //RJPP-BEGIN------------------------------------------------------------ 530 for (int i = 0; i < x.length; i++) 531 x.put(i, (float) Math.ceil(x.get(i))); 532 return x; 533 //RJPP-END-------------------------------------------------------------- 534 } 535 536 /** 537 * Applies the <i>cosine</i> function element-wise on this 538 * matrix. Note that this is an in-place operation. 539 * @see MatrixFunctions#cos(FloatMatrix) 540 * @return this matrix 541 */ 542 public static FloatMatrix cosi(FloatMatrix x) { 543 /*# mapfct('Math.cos') #*/ 544 //RJPP-BEGIN------------------------------------------------------------ 545 for (int i = 0; i < x.length; i++) 546 x.put(i, (float) Math.cos(x.get(i))); 547 return x; 548 //RJPP-END-------------------------------------------------------------- 549 } 550 551 /** 552 * Applies the <i>hyperbolic cosine</i> function element-wise on this 553 * matrix. Note that this is an in-place operation. 554 * @see MatrixFunctions#cosh(FloatMatrix) 555 * @return this matrix 556 */ 557 public static FloatMatrix coshi(FloatMatrix x) { 558 /*# mapfct('Math.cosh') #*/ 559 //RJPP-BEGIN------------------------------------------------------------ 560 for (int i = 0; i < x.length; i++) 561 x.put(i, (float) Math.cosh(x.get(i))); 562 return x; 563 //RJPP-END-------------------------------------------------------------- 564 } 565 566 /** 567 * Applies the <i>exponential</i> function element-wise on this 568 * matrix. Note that this is an in-place operation. 569 * @see MatrixFunctions#exp(FloatMatrix) 570 * @return this matrix 571 */ 572 public static FloatMatrix expi(FloatMatrix x) { 573 /*# mapfct('Math.exp') #*/ 574 //RJPP-BEGIN------------------------------------------------------------ 575 for (int i = 0; i < x.length; i++) 576 x.put(i, (float) Math.exp(x.get(i))); 577 return x; 578 //RJPP-END-------------------------------------------------------------- 579 } 580 581 /** 582 * Element-wise round down by applying the <i>floor</i> function on each 583 * element. Note that this is an in-place operation. 584 * @see MatrixFunctions#floor(FloatMatrix) 585 * @return this matrix 586 */ 587 public static FloatMatrix floori(FloatMatrix x) { 588 /*# mapfct('Math.floor') #*/ 589 //RJPP-BEGIN------------------------------------------------------------ 590 for (int i = 0; i < x.length; i++) 591 x.put(i, (float) Math.floor(x.get(i))); 592 return x; 593 //RJPP-END-------------------------------------------------------------- 594 } 595 596 /** 597 * Applies the <i>natural logarithm</i> function element-wise on this 598 * matrix. Note that this is an in-place operation. 599 * @see MatrixFunctions#log(FloatMatrix) 600 * @return this matrix 601 */ 602 public static FloatMatrix logi(FloatMatrix x) { 603 /*# mapfct('Math.log') #*/ 604 //RJPP-BEGIN------------------------------------------------------------ 605 for (int i = 0; i < x.length; i++) 606 x.put(i, (float) Math.log(x.get(i))); 607 return x; 608 //RJPP-END-------------------------------------------------------------- 609 } 610 611 /** 612 * Applies the <i>logarithm with basis to 10</i> element-wise on this 613 * matrix. Note that this is an in-place operation. 614 * @see MatrixFunctions#log10(FloatMatrix) 615 * @return this matrix 616 */ 617 public static FloatMatrix log10i(FloatMatrix x) { 618 /*# mapfct('Math.log10') #*/ 619 //RJPP-BEGIN------------------------------------------------------------ 620 for (int i = 0; i < x.length; i++) 621 x.put(i, (float) Math.log10(x.get(i))); 622 return x; 623 //RJPP-END-------------------------------------------------------------- 624 } 625 626 /** 627 * Element-wise power function. Replaces each element with its 628 * power of <tt>d</tt>.Note that this is an in-place operation. 629 * @param d the exponent 630 * @see MatrixFunctions#pow(FloatMatrix,float) 631 * @return this matrix 632 */ 633 public static FloatMatrix powi(FloatMatrix x, float d) { 634 if (d == 2.0f) 635 return x.muli(x); 636 else { 637 for (int i = 0; i < x.length; i++) 638 x.put(i, (float) Math.pow(x.get(i), d)); 639 return x; 640 } 641 } 642 643 public static FloatMatrix powi(float base, FloatMatrix x) { 644 for (int i = 0; i < x.length; i++) 645 x.put(i, (float) Math.pow(base, x.get(i))); 646 return x; 647 } 648 649 public static FloatMatrix powi(FloatMatrix x, FloatMatrix e) { 650 x.checkLength(e.length); 651 for (int i = 0; i < x.length; i++) 652 x.put(i, (float) Math.pow(x.get(i), e.get(i))); 653 return x; 654 } 655 656 public static FloatMatrix signumi(FloatMatrix x) { 657 /*# mapfct('Math.signum') #*/ 658 //RJPP-BEGIN------------------------------------------------------------ 659 for (int i = 0; i < x.length; i++) 660 x.put(i, (float) Math.signum(x.get(i))); 661 return x; 662 //RJPP-END-------------------------------------------------------------- 663 } 664 665 public static FloatMatrix sini(FloatMatrix x) { 666 /*# mapfct('Math.sin') #*/ 667 //RJPP-BEGIN------------------------------------------------------------ 668 for (int i = 0; i < x.length; i++) 669 x.put(i, (float) Math.sin(x.get(i))); 670 return x; 671 //RJPP-END-------------------------------------------------------------- 672 } 673 674 public static FloatMatrix sinhi(FloatMatrix x) { 675 /*# mapfct('Math.sinh') #*/ 676 //RJPP-BEGIN------------------------------------------------------------ 677 for (int i = 0; i < x.length; i++) 678 x.put(i, (float) Math.sinh(x.get(i))); 679 return x; 680 //RJPP-END-------------------------------------------------------------- 681 } 682 public static FloatMatrix sqrti(FloatMatrix x) { 683 /*# mapfct('Math.sqrt') #*/ 684 //RJPP-BEGIN------------------------------------------------------------ 685 for (int i = 0; i < x.length; i++) 686 x.put(i, (float) Math.sqrt(x.get(i))); 687 return x; 688 //RJPP-END-------------------------------------------------------------- 689 } 690 public static FloatMatrix tani(FloatMatrix x) { 691 /*# mapfct('Math.tan') #*/ 692 //RJPP-BEGIN------------------------------------------------------------ 693 for (int i = 0; i < x.length; i++) 694 x.put(i, (float) Math.tan(x.get(i))); 695 return x; 696 //RJPP-END-------------------------------------------------------------- 697 } 698 public static FloatMatrix tanhi(FloatMatrix x) { 699 /*# mapfct('Math.tanh') #*/ 700 //RJPP-BEGIN------------------------------------------------------------ 701 for (int i = 0; i < x.length; i++) 702 x.put(i, (float) Math.tanh(x.get(i))); 703 return x; 704 //RJPP-END-------------------------------------------------------------- 705 } 706 707 /** 708 * Returns a copy of this matrix where all elements are set to their 709 * absolute values. 710 * @see MatrixFunctions#absi(FloatMatrix) 711 * @return copy of this matrix 712 */ 713 public static FloatMatrix abs(FloatMatrix x) { return absi(x.dup()); } 714 715 /** 716 * Returns a copy of this matrix where the trigonometric <i>acos</i> function is applied 717 * element wise. 718 * @see MatrixFunctions#acosi(FloatMatrix) 719 * @return copy of this matrix 720 */ 721 public static FloatMatrix acos(FloatMatrix x) { return acosi(x.dup()); } 722 public static FloatMatrix asin(FloatMatrix x) { return asini(x.dup()); } 723 public static FloatMatrix atan(FloatMatrix x) { return atani(x.dup()); } 724 public static FloatMatrix cbrt(FloatMatrix x) { return cbrti(x.dup()); } 725 public static FloatMatrix ceil(FloatMatrix x) { return ceili(x.dup()); } 726 public static FloatMatrix cos(FloatMatrix x) { return cosi(x.dup()); } 727 public static FloatMatrix cosh(FloatMatrix x) { return coshi(x.dup()); } 728 public static FloatMatrix exp(FloatMatrix x) { return expi(x.dup()); } 729 public static FloatMatrix floor(FloatMatrix x) { return floori(x.dup()); } 730 public static FloatMatrix log(FloatMatrix x) { return logi(x.dup()); } 731 public static FloatMatrix log10(FloatMatrix x) { return log10i(x.dup()); } 732 public static float pow(float x, float y) { return (float)Math.pow(x, y); } 733 public static FloatMatrix pow(FloatMatrix x, float e) { return powi(x.dup(), e); } 734 public static FloatMatrix pow(float b, FloatMatrix x) { return powi(b, x.dup()); } 735 public static FloatMatrix pow(FloatMatrix x, FloatMatrix e) { return powi(x.dup(), e); } 736 public static FloatMatrix signum(FloatMatrix x) { return signumi(x.dup()); } 737 public static FloatMatrix sin(FloatMatrix x) { return sini(x.dup()); } 738 public static FloatMatrix sinh(FloatMatrix x) { return sinhi(x.dup()); } 739 public static FloatMatrix sqrt(FloatMatrix x) { return sqrti(x.dup()); } 740 public static FloatMatrix tan(FloatMatrix x) { return tani(x.dup()); } 741 public static FloatMatrix tanh(FloatMatrix x) { return tanhi(x.dup()); } 742 743 /*# %w{abs acos asin atan cbrt ceil cos cosh exp floor log log10 signum sin sinh sqrt tan tanh}.map do |fct| <<-EOS 744 public static float #{fct}(float x) { return (float)Math.#{fct}(x); } 745 EOS 746 end 747 #*/ 748 //RJPP-BEGIN------------------------------------------------------------ 749 public static float abs(float x) { return (float)Math.abs(x); } 750 public static float acos(float x) { return (float)Math.acos(x); } 751 public static float asin(float x) { return (float)Math.asin(x); } 752 public static float atan(float x) { return (float)Math.atan(x); } 753 public static float cbrt(float x) { return (float)Math.cbrt(x); } 754 public static float ceil(float x) { return (float)Math.ceil(x); } 755 public static float cos(float x) { return (float)Math.cos(x); } 756 public static float cosh(float x) { return (float)Math.cosh(x); } 757 public static float exp(float x) { return (float)Math.exp(x); } 758 public static float floor(float x) { return (float)Math.floor(x); } 759 public static float log(float x) { return (float)Math.log(x); } 760 public static float log10(float x) { return (float)Math.log10(x); } 761 public static float signum(float x) { return (float)Math.signum(x); } 762 public static float sin(float x) { return (float)Math.sin(x); } 763 public static float sinh(float x) { return (float)Math.sinh(x); } 764 public static float sqrt(float x) { return (float)Math.sqrt(x); } 765 public static float tan(float x) { return (float)Math.tan(x); } 766 public static float tanh(float x) { return (float)Math.tanh(x); } 767 //RJPP-END-------------------------------------------------------------- 768 769 770 //END 771 }