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.la; 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 //BEGIN 398 // The code below has been automatically generated. 399 // DO NOT EDIT! 400 401 /*# 402 def mapfct(f); <<-EOS 403 for (int i = 0; i < x.length; i++) 404 x.put(i, (float) #{f}(x.get(i))); 405 return x; 406 EOS 407 end 408 409 def cmapfct(f); <<-EOS 410 for (int i = 0; i < x.length; i++) 411 x.put(i, x.get(i).#{f}()); 412 return x; 413 EOS 414 end 415 #*/ 416 417 /** 418 * Sets all elements in this matrix to their absolute values. Note 419 * that this operation is in-place. 420 * @see MatrixFunctions#abs(FloatMatrix) 421 * @return this matrix 422 */ 423 public static FloatMatrix absi(FloatMatrix x) { 424 /*# mapfct('Math.abs') #*/ 425 //RJPP-BEGIN------------------------------------------------------------ 426 for (int i = 0; i < x.length; i++) 427 x.put(i, (float) Math.abs(x.get(i))); 428 return x; 429 //RJPP-END-------------------------------------------------------------- 430 } 431 432 public static ComplexFloatMatrix absi(ComplexFloatMatrix x) { 433 /*# cmapfct('abs') #*/ 434 //RJPP-BEGIN------------------------------------------------------------ 435 for (int i = 0; i < x.length; i++) 436 x.put(i, x.get(i).abs()); 437 return x; 438 //RJPP-END-------------------------------------------------------------- 439 } 440 441 /** 442 * Applies the trigonometric <i>arccosine</i> function element wise on this 443 * matrix. Note that this is an in-place operation. 444 * @see MatrixFunctions#acos(FloatMatrix) 445 * @return this matrix 446 */ 447 public static FloatMatrix acosi(FloatMatrix x) { 448 /*# mapfct('Math.acos') #*/ 449 //RJPP-BEGIN------------------------------------------------------------ 450 for (int i = 0; i < x.length; i++) 451 x.put(i, (float) Math.acos(x.get(i))); 452 return x; 453 //RJPP-END-------------------------------------------------------------- 454 } 455 456 /** 457 * Applies the trigonometric <i>arcsine</i> function element wise on this 458 * matrix. Note that this is an in-place operation. 459 * @see MatrixFunctions#asin(FloatMatrix) 460 * @return this matrix 461 */ 462 public static FloatMatrix asini(FloatMatrix x) { 463 /*# mapfct('Math.asin') #*/ 464 //RJPP-BEGIN------------------------------------------------------------ 465 for (int i = 0; i < x.length; i++) 466 x.put(i, (float) Math.asin(x.get(i))); 467 return x; 468 //RJPP-END-------------------------------------------------------------- 469 } 470 471 /** 472 * Applies the trigonometric <i>arctangend</i> function element wise on this 473 * matrix. Note that this is an in-place operation. 474 * @see MatrixFunctions#atan(FloatMatrix) 475 * @return this matrix 476 */ 477 public static FloatMatrix atani(FloatMatrix x) { 478 /*# mapfct('Math.atan') #*/ 479 //RJPP-BEGIN------------------------------------------------------------ 480 for (int i = 0; i < x.length; i++) 481 x.put(i, (float) Math.atan(x.get(i))); 482 return x; 483 //RJPP-END-------------------------------------------------------------- 484 } 485 486 /** 487 * Applies the <i>cube root</i> function element wise on this 488 * matrix. Note that this is an in-place operation. 489 * @see MatrixFunctions#cbrt(FloatMatrix) 490 * @return this matrix 491 */ 492 public static FloatMatrix cbrti(FloatMatrix x) { 493 /*# mapfct('Math.cbrt') #*/ 494 //RJPP-BEGIN------------------------------------------------------------ 495 for (int i = 0; i < x.length; i++) 496 x.put(i, (float) Math.cbrt(x.get(i))); 497 return x; 498 //RJPP-END-------------------------------------------------------------- 499 } 500 501 /** 502 * Element-wise round up by applying the <i>ceil</i> function on each 503 * element. Note that this is an in-place operation. 504 * @see MatrixFunctions#ceil(FloatMatrix) 505 * @return this matrix 506 */ 507 public static FloatMatrix ceili(FloatMatrix x) { 508 /*# mapfct('Math.ceil') #*/ 509 //RJPP-BEGIN------------------------------------------------------------ 510 for (int i = 0; i < x.length; i++) 511 x.put(i, (float) Math.ceil(x.get(i))); 512 return x; 513 //RJPP-END-------------------------------------------------------------- 514 } 515 516 /** 517 * Applies the <i>cosine</i> function element-wise on this 518 * matrix. Note that this is an in-place operation. 519 * @see MatrixFunctions#cos(FloatMatrix) 520 * @return this matrix 521 */ 522 public static FloatMatrix cosi(FloatMatrix x) { 523 /*# mapfct('Math.cos') #*/ 524 //RJPP-BEGIN------------------------------------------------------------ 525 for (int i = 0; i < x.length; i++) 526 x.put(i, (float) Math.cos(x.get(i))); 527 return x; 528 //RJPP-END-------------------------------------------------------------- 529 } 530 531 /** 532 * Applies the <i>hyperbolic cosine</i> function element-wise on this 533 * matrix. Note that this is an in-place operation. 534 * @see MatrixFunctions#cosh(FloatMatrix) 535 * @return this matrix 536 */ 537 public static FloatMatrix coshi(FloatMatrix x) { 538 /*# mapfct('Math.cosh') #*/ 539 //RJPP-BEGIN------------------------------------------------------------ 540 for (int i = 0; i < x.length; i++) 541 x.put(i, (float) Math.cosh(x.get(i))); 542 return x; 543 //RJPP-END-------------------------------------------------------------- 544 } 545 546 /** 547 * Applies the <i>exponential</i> function element-wise on this 548 * matrix. Note that this is an in-place operation. 549 * @see MatrixFunctions#exp(FloatMatrix) 550 * @return this matrix 551 */ 552 public static FloatMatrix expi(FloatMatrix x) { 553 /*# mapfct('Math.exp') #*/ 554 //RJPP-BEGIN------------------------------------------------------------ 555 for (int i = 0; i < x.length; i++) 556 x.put(i, (float) Math.exp(x.get(i))); 557 return x; 558 //RJPP-END-------------------------------------------------------------- 559 } 560 561 /** 562 * Element-wise round down by applying the <i>floor</i> function on each 563 * element. Note that this is an in-place operation. 564 * @see MatrixFunctions#floor(FloatMatrix) 565 * @return this matrix 566 */ 567 public static FloatMatrix floori(FloatMatrix x) { 568 /*# mapfct('Math.floor') #*/ 569 //RJPP-BEGIN------------------------------------------------------------ 570 for (int i = 0; i < x.length; i++) 571 x.put(i, (float) Math.floor(x.get(i))); 572 return x; 573 //RJPP-END-------------------------------------------------------------- 574 } 575 576 /** 577 * Applies the <i>natural logarithm</i> function element-wise on this 578 * matrix. Note that this is an in-place operation. 579 * @see MatrixFunctions#log(FloatMatrix) 580 * @return this matrix 581 */ 582 public static FloatMatrix logi(FloatMatrix x) { 583 /*# mapfct('Math.log') #*/ 584 //RJPP-BEGIN------------------------------------------------------------ 585 for (int i = 0; i < x.length; i++) 586 x.put(i, (float) Math.log(x.get(i))); 587 return x; 588 //RJPP-END-------------------------------------------------------------- 589 } 590 591 /** 592 * Applies the <i>logarithm with basis to 10</i> element-wise on this 593 * matrix. Note that this is an in-place operation. 594 * @see MatrixFunctions#log10(FloatMatrix) 595 * @return this matrix 596 */ 597 public static FloatMatrix log10i(FloatMatrix x) { 598 /*# mapfct('Math.log10') #*/ 599 //RJPP-BEGIN------------------------------------------------------------ 600 for (int i = 0; i < x.length; i++) 601 x.put(i, (float) Math.log10(x.get(i))); 602 return x; 603 //RJPP-END-------------------------------------------------------------- 604 } 605 606 /** 607 * Element-wise power function. Replaces each element with its 608 * power of <tt>d</tt>.Note that this is an in-place operation. 609 * @param d the exponent 610 * @see MatrixFunctions#pow(FloatMatrix,float) 611 * @return this matrix 612 */ 613 public static FloatMatrix powi(FloatMatrix x, float d) { 614 if (d == 2.0f) 615 return x.muli(x); 616 else { 617 for (int i = 0; i < x.length; i++) 618 x.put(i, (float) Math.pow(x.get(i), d)); 619 return x; 620 } 621 } 622 623 public static FloatMatrix powi(float base, FloatMatrix x) { 624 for (int i = 0; i < x.length; i++) 625 x.put(i, (float) Math.pow(base, x.get(i))); 626 return x; 627 } 628 629 public static FloatMatrix powi(FloatMatrix x, FloatMatrix e) { 630 x.checkLength(e.length); 631 for (int i = 0; i < x.length; i++) 632 x.put(i, (float) Math.pow(x.get(i), e.get(i))); 633 return x; 634 } 635 636 public static FloatMatrix signumi(FloatMatrix x) { 637 /*# mapfct('Math.signum') #*/ 638 //RJPP-BEGIN------------------------------------------------------------ 639 for (int i = 0; i < x.length; i++) 640 x.put(i, (float) Math.signum(x.get(i))); 641 return x; 642 //RJPP-END-------------------------------------------------------------- 643 } 644 645 public static FloatMatrix sini(FloatMatrix x) { 646 /*# mapfct('Math.sin') #*/ 647 //RJPP-BEGIN------------------------------------------------------------ 648 for (int i = 0; i < x.length; i++) 649 x.put(i, (float) Math.sin(x.get(i))); 650 return x; 651 //RJPP-END-------------------------------------------------------------- 652 } 653 654 public static FloatMatrix sinhi(FloatMatrix x) { 655 /*# mapfct('Math.sinh') #*/ 656 //RJPP-BEGIN------------------------------------------------------------ 657 for (int i = 0; i < x.length; i++) 658 x.put(i, (float) Math.sinh(x.get(i))); 659 return x; 660 //RJPP-END-------------------------------------------------------------- 661 } 662 public static FloatMatrix sqrti(FloatMatrix x) { 663 /*# mapfct('Math.sqrt') #*/ 664 //RJPP-BEGIN------------------------------------------------------------ 665 for (int i = 0; i < x.length; i++) 666 x.put(i, (float) Math.sqrt(x.get(i))); 667 return x; 668 //RJPP-END-------------------------------------------------------------- 669 } 670 public static FloatMatrix tani(FloatMatrix x) { 671 /*# mapfct('Math.tan') #*/ 672 //RJPP-BEGIN------------------------------------------------------------ 673 for (int i = 0; i < x.length; i++) 674 x.put(i, (float) Math.tan(x.get(i))); 675 return x; 676 //RJPP-END-------------------------------------------------------------- 677 } 678 public static FloatMatrix tanhi(FloatMatrix x) { 679 /*# mapfct('Math.tanh') #*/ 680 //RJPP-BEGIN------------------------------------------------------------ 681 for (int i = 0; i < x.length; i++) 682 x.put(i, (float) Math.tanh(x.get(i))); 683 return x; 684 //RJPP-END-------------------------------------------------------------- 685 } 686 687 /** 688 * Returns a copy of this matrix where all elements are set to their 689 * absolute values. 690 * @see MatrixFunctions#absi(FloatMatrix) 691 * @return copy of this matrix 692 */ 693 public static FloatMatrix abs(FloatMatrix x) { return absi(x.dup()); } 694 695 /** 696 * Returns a copy of this matrix where the trigonometric <i>acos</i> function is applied 697 * element wise. 698 * @see MatrixFunctions#acosi(FloatMatrix) 699 * @return copy of this matrix 700 */ 701 public static FloatMatrix acos(FloatMatrix x) { return acosi(x.dup()); } 702 public static FloatMatrix asin(FloatMatrix x) { return asini(x.dup()); } 703 public static FloatMatrix atan(FloatMatrix x) { return atani(x.dup()); } 704 public static FloatMatrix cbrt(FloatMatrix x) { return cbrti(x.dup()); } 705 public static FloatMatrix ceil(FloatMatrix x) { return ceili(x.dup()); } 706 public static FloatMatrix cos(FloatMatrix x) { return cosi(x.dup()); } 707 public static FloatMatrix cosh(FloatMatrix x) { return coshi(x.dup()); } 708 public static FloatMatrix exp(FloatMatrix x) { return expi(x.dup()); } 709 public static FloatMatrix floor(FloatMatrix x) { return floori(x.dup()); } 710 public static FloatMatrix log(FloatMatrix x) { return logi(x.dup()); } 711 public static FloatMatrix log10(FloatMatrix x) { return log10i(x.dup()); } 712 public static float pow(float x, float y) { return (float)Math.pow(x, y); } 713 public static FloatMatrix pow(FloatMatrix x, float e) { return powi(x.dup(), e); } 714 public static FloatMatrix pow(float b, FloatMatrix x) { return powi(b, x.dup()); } 715 public static FloatMatrix pow(FloatMatrix x, FloatMatrix e) { return powi(x.dup(), e); } 716 public static FloatMatrix signum(FloatMatrix x) { return signumi(x.dup()); } 717 public static FloatMatrix sin(FloatMatrix x) { return sini(x.dup()); } 718 public static FloatMatrix sinh(FloatMatrix x) { return sinhi(x.dup()); } 719 public static FloatMatrix sqrt(FloatMatrix x) { return sqrti(x.dup()); } 720 public static FloatMatrix tan(FloatMatrix x) { return tani(x.dup()); } 721 public static FloatMatrix tanh(FloatMatrix x) { return tanhi(x.dup()); } 722 723 /*# %w{abs acos asin atan cbrt ceil cos cosh exp floor log log10 signum sin sinh sqrt tan tanh}.map do |fct| <<-EOS 724 public static float #{fct}(float x) { return (float)Math.#{fct}(x); } 725 EOS 726 end 727 #*/ 728 //RJPP-BEGIN------------------------------------------------------------ 729 public static float abs(float x) { return (float)Math.abs(x); } 730 public static float acos(float x) { return (float)Math.acos(x); } 731 public static float asin(float x) { return (float)Math.asin(x); } 732 public static float atan(float x) { return (float)Math.atan(x); } 733 public static float cbrt(float x) { return (float)Math.cbrt(x); } 734 public static float ceil(float x) { return (float)Math.ceil(x); } 735 public static float cos(float x) { return (float)Math.cos(x); } 736 public static float cosh(float x) { return (float)Math.cosh(x); } 737 public static float exp(float x) { return (float)Math.exp(x); } 738 public static float floor(float x) { return (float)Math.floor(x); } 739 public static float log(float x) { return (float)Math.log(x); } 740 public static float log10(float x) { return (float)Math.log10(x); } 741 public static float signum(float x) { return (float)Math.signum(x); } 742 public static float sin(float x) { return (float)Math.sin(x); } 743 public static float sinh(float x) { return (float)Math.sinh(x); } 744 public static float sqrt(float x) { return (float)Math.sqrt(x); } 745 public static float tan(float x) { return (float)Math.tan(x); } 746 public static float tanh(float x) { return (float)Math.tanh(x); } 747 //RJPP-END-------------------------------------------------------------- 748 749 //END 750 }