/array/tools/DoubleArrayTools.java

https://bitbucket.org/carrillo_f/arraytools · Java · 1119 lines · 691 code · 129 blank · 299 comment · 136 complexity · 7537389a3b465999f2ae57a58d320789 MD5 · raw file

  1. package array.tools;
  2. import ij.ImageJ;
  3. import inputOutput.TextFileAccess;
  4. import java.io.File;
  5. import java.io.PrintWriter;
  6. import java.text.DecimalFormat;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.TreeMap;
  10. import array.visualization.HeatMap;
  11. import Jama.Matrix;
  12. public class DoubleArrayTools
  13. {
  14. /**
  15. * Entrywise sum of two 2d matrices.
  16. * @param in1
  17. * @param in2
  18. * @param replaceNaNsByZeros Treat NaNs as zeros?
  19. * @return
  20. */
  21. public static double[][] addMatrices( final double[][] in1, final double[][] in2, final boolean replaceNaNsByZeros )
  22. {
  23. double[][] a = clone2DArray( in1 );
  24. double[][] b = clone2DArray( in2 );
  25. if( replaceNaNsByZeros )
  26. {
  27. substitute(a, Double.NaN, 0.0);
  28. substitute(b, Double.NaN, 0.0);
  29. }
  30. for( int j = 0; j < a.length; j++ )
  31. {
  32. for( int i = 0; i < a[ j ].length; i++ )
  33. {
  34. a[ j ][ i ] += b[ j ][ i ];
  35. }
  36. }
  37. return a;
  38. }
  39. /**
  40. * This method adds the values in the matrix and a constant value.
  41. * Original values in matrix in1 are overwritten.
  42. */
  43. public static void addMatrixAndConstantValue( double[][] in1, final double constantValue )
  44. {
  45. for( int j = 0; j < in1.length; j++ )
  46. {
  47. for( int i = 0; i < in1[ j ].length; i++ )
  48. {
  49. in1[ j ][ i ] = in1[ j ][ i ] + constantValue;
  50. }
  51. }
  52. }
  53. /**
  54. * Sums up values for each 2D matrix position of an 2D matrix arrayList.
  55. * @param matrixList
  56. * @param replaceNaNsByZeros Treat NaNs as zeros?
  57. * @return
  58. */
  59. public static double[][] addUpMatrixList( final ArrayList<double[][]> matrixList, final boolean replaceNaNsByZeros )
  60. {
  61. double[][] a = matrixList.get( 0 ).clone();
  62. for( int i = 1; i < matrixList.size(); i++ )
  63. {
  64. a = addMatrices(a, matrixList.get( i ), replaceNaNsByZeros );
  65. }
  66. return a;
  67. }
  68. /**
  69. * Convenient method to get a string representation of an array, using standard delimiter.
  70. * @param matrix
  71. * @return
  72. */
  73. public static String arrayToString( final double[] array )
  74. {
  75. return arrayToString( array, "\t");
  76. }
  77. /**
  78. * Get String representation of 1D matrix.
  79. * @param array Input array
  80. * @param delimiter delimiter to use
  81. * @return
  82. */
  83. public static String arrayToString( final double[] array, final String delimiter )
  84. {
  85. String s = "";
  86. for( int i = 0; i < array.length; i++ )
  87. {
  88. if( i != 0 )
  89. s += delimiter;
  90. s += array[ i ];
  91. }
  92. return s;
  93. }
  94. /**
  95. * Convenient method to get a string representation of an array, using standard delimiter.
  96. * @param matrix
  97. * @return
  98. */
  99. public static String arrayToString( final Double[] array )
  100. {
  101. return arrayToString( array, "\t");
  102. }
  103. /**
  104. * Get String representation of 1D matrix.
  105. * @param array Input array
  106. * @param delimiter delimiter to use
  107. * @return
  108. */
  109. public static String arrayToString( final Double[] array, final String delimiter )
  110. {
  111. String s = "";
  112. for( int i = 0; i < array.length; i++ )
  113. {
  114. if( i != 0 )
  115. s += delimiter;
  116. s += array[ i ];
  117. }
  118. return s;
  119. }
  120. /**
  121. * Get String representation of 1D matrix.
  122. * @param array Input array
  123. * @param delimiter delimiter to use
  124. * @return
  125. */
  126. public static String arrayToString( final double[] array, final String delimiter, final int decimalplaces )
  127. {
  128. String decPlaces = "#.";
  129. for( int i = 0; i < decimalplaces; i++ )
  130. decPlaces += "#";
  131. DecimalFormat df = new DecimalFormat( decPlaces );
  132. String s = "";
  133. for( int i = 0; i < array.length; i++ )
  134. {
  135. if( i != 0 )
  136. s += delimiter;
  137. s += df.format( array[ i ] );
  138. }
  139. return s;
  140. }
  141. /**Creates an independent copy(clone) of the boolean array.
  142. * @param array The array to be cloned.
  143. * @return An independent 'deep' structure clone of the array.
  144. */
  145. public static double[][] clone2DArray(double[][] array) {
  146. int rows=array.length ;
  147. //int rowIs=array[0].length ;
  148. //clone the 'shallow' structure of array
  149. double[][] newArray =(double[][]) array.clone();
  150. //clone the 'deep' structure of array
  151. for(int row=0;row<rows;row++){
  152. newArray[row]=(double[]) array[row].clone();
  153. }
  154. return newArray;
  155. }
  156. /**
  157. * Paste in2 after in1
  158. * @param in1
  159. * @param in2
  160. * @return
  161. */
  162. public static double[] paste( final double[] in1, final double[] in2 ) {
  163. final double[] out = new double[ ( in1.length + in2.length ) ];
  164. int i = 0;
  165. for( double d : in1 ){
  166. out[ i ] = d;
  167. i++;
  168. }
  169. for( double d : in2 ) {
  170. out[ i ] = d;
  171. i++;
  172. }
  173. return out;
  174. }
  175. /**
  176. * Returns true if the array contains the values specified.
  177. * @param array
  178. * @param valueOfInterest
  179. * @return
  180. */
  181. public static boolean containsValue( final double[] array, final double valueOfInterest )
  182. {
  183. boolean contains = false;
  184. for( Double d : array )
  185. {
  186. if( d == valueOfInterest )
  187. {
  188. contains = true;
  189. break;
  190. }
  191. }
  192. return contains;
  193. }
  194. /**
  195. * Returns true if the array contains the values specified.
  196. * @param array
  197. * @param valueOfInterest
  198. * @return
  199. */
  200. public static boolean containsValue( final double[][] array, final double valueOfInterest )
  201. {
  202. boolean contains = false;
  203. for( int i = 0; i < array.length; i++ )
  204. {
  205. for( int j = 0; j < array[ 0 ].length; j++ )
  206. {
  207. if( array[ i ][ j ] == valueOfInterest )
  208. {
  209. contains = true;
  210. break;
  211. }
  212. else if( Double.isNaN( valueOfInterest ) && Double.isNaN( array[ i ][ j ] ) )
  213. {
  214. contains = true;
  215. break;
  216. }
  217. }
  218. }
  219. return contains;
  220. }
  221. /**
  222. * Returns true if the array contains only the values specified.
  223. * @param matrix
  224. * @param valueOfInterest
  225. * @return
  226. */
  227. public static boolean containsOnlyValue( final double[][] matrix, final double valueOfInterest )
  228. {
  229. boolean contains = true;
  230. for( int i = 0; i < matrix.length; i++ )
  231. {
  232. for( int j = 0; j < matrix[ 0 ].length; j++ )
  233. {
  234. if( !Double.isNaN( valueOfInterest ) && matrix[ i ][ j ] != valueOfInterest )
  235. {
  236. contains = false;
  237. break;
  238. }
  239. else if( Double.isNaN( valueOfInterest ) && !Double.isNaN( matrix[ i ][ j ] ) )
  240. {
  241. contains = false;
  242. break;
  243. }
  244. }
  245. }
  246. return contains;
  247. }
  248. /**
  249. * Returns true if the array contains only the values specified.
  250. * @param array
  251. * @param valueOfInterest
  252. * @return
  253. */
  254. public static boolean containsOnlyValue( final double[] array, final double valueOfInterest )
  255. {
  256. boolean contains = true;
  257. for( int i = 0; i < array.length; i++ )
  258. {
  259. if( !Double.isNaN( valueOfInterest ) && array[ i ] != valueOfInterest )
  260. {
  261. contains = false;
  262. break;
  263. }
  264. else if( Double.isNaN( valueOfInterest ) && !Double.isNaN( array[ i ] ) )
  265. {
  266. contains = false;
  267. break;
  268. }
  269. }
  270. return contains;
  271. }
  272. /**
  273. * Displays the values in a matrix using Fiji.
  274. * @param in
  275. */
  276. public static void display2dMatrix( final double[][] in, final String title )
  277. {
  278. new ImageJ();
  279. Matrix m = new Matrix( in );
  280. m = m.transpose();
  281. HeatMap hm = new HeatMap( m.getArray() );
  282. hm.showImage( title );
  283. }
  284. /**
  285. * This method divides the values in the first matrix by values in the second matrix.
  286. * Original values in matrix in1 are overwritten.
  287. */
  288. public static void divideMatrices( double[][] in1, final double[][] in2 )
  289. {
  290. for( int j = 0; j < in1.length; j++ )
  291. {
  292. for( int i = 0; i < in1[ j ].length; i++ )
  293. {
  294. in1[ j ][ i ] = in1[ j ][ i ] / in2[ j ][ i ];
  295. }
  296. }
  297. }
  298. /**
  299. * Calculates the dot product between two arrays.
  300. * a*b = Sum(ai*bi);
  301. * @param in1
  302. * @param in2
  303. * @return
  304. */
  305. public static Double dotProduct( final double[] in1, final double[] in2 )
  306. {
  307. Double sum;
  308. if( in1.length != in2.length )
  309. sum = null;
  310. else
  311. {
  312. sum = 0.0;
  313. for( int i = 0; i < in1.length; i++ )
  314. sum += in1[ i ] * in2[ i ];
  315. }
  316. return sum;
  317. }
  318. public static double[] elementwiseAddition( final double[] v1, final double[] v2 )
  319. {
  320. final double[] out = new double[ v1.length ];
  321. for( int i = 0; i < v1.length; i++ )
  322. out[ i ] = v1[ i ] + v2[ i ];
  323. return out;
  324. }
  325. /**
  326. * This method dividedds the values in the first array by values in the second array.
  327. * Original values in matrix in1 are overwritten.
  328. */
  329. public static void elementwiseDivision( final double[] in1, final double[] in2 ) {
  330. for( int i = 0; i < in1.length; i++ ) {
  331. in1[ i ] = in1[ i ] / in2[ i ];
  332. }
  333. }
  334. /**
  335. * This method multiplies the values in the first matrix by values in the second matrix.
  336. * Original values in matrix in1 are overwritten.
  337. */
  338. public static void elementwiseMultiplication( double[][] in1, final double[][] in2 )
  339. {
  340. for( int j = 0; j < in1.length; j++ )
  341. {
  342. for( int i = 0; i < in1[ j ].length; i++ )
  343. {
  344. in1[ j ][ i ] = in1[ j ][ i ] * in2[ j ][ i ];
  345. }
  346. }
  347. }
  348. /**
  349. * This method multiplies the values in the array by a constant value.
  350. * Original values in matrix in1 are overwritten. By default NAs are kept.
  351. */
  352. public static void elementwiseMultiplication( double[] in, final double constantValue )
  353. {
  354. for( int j = 0; j < in.length; j++ )
  355. {
  356. in[ j ] = in[ j ] * constantValue;
  357. }
  358. }
  359. /**
  360. * This method multiplies the values in the array by a constant value.
  361. * Original values in matrix in1 are overwritten. By default NAs are kept.
  362. */
  363. public static void elementwiseMultiplication( Double[] in, final double constantValue )
  364. {
  365. for( int j = 0; j < in.length; j++ )
  366. {
  367. in[ j ] = in[ j ] * constantValue;
  368. }
  369. }
  370. /**
  371. * This method multiplies the values in the matrix by a constant value.
  372. * Original values in matrix in1 are overwritten. By default NAs are kept.
  373. */
  374. public static void elementwiseMultiplication( double[][] in1, final double constantValue )
  375. {
  376. for( int j = 0; j < in1.length; j++ )
  377. {
  378. for( int i = 0; i < in1[ j ].length; i++ )
  379. {
  380. in1[ j ][ i ] = in1[ j ][ i ] * constantValue;
  381. }
  382. }
  383. }
  384. /**
  385. * Returns the specified column of the matrix.
  386. * @param matrix
  387. * @param colIndex
  388. * @return
  389. */
  390. public static double[] getColumn( final double[][] matrix, final int colIndex )
  391. {
  392. final double[] column = new double[ matrix.length ];
  393. for( int i = 0; i < matrix.length; i++ )
  394. column[ i ] = matrix[ i ][ colIndex ];
  395. return column;
  396. }
  397. /**
  398. * Calculates the eucledian distance between a1 and a2.
  399. * d = SQRT( SUM( (a1i-a2i)^2 ) )
  400. * @param a1
  401. * @param a2
  402. * @return
  403. */
  404. public static double getEucledianDistance( final double[] a1, final double[] a2 )
  405. {
  406. double sum = 0;
  407. for( int i = 0; i < a1.length; i++ )
  408. {
  409. sum += Math.pow( ( a1[i]-a2[i] ), 2 );
  410. }
  411. return Math.sqrt( sum );
  412. }
  413. /**
  414. * Returns the index of the maximum value contained in the array.
  415. * @param array
  416. * @return
  417. */
  418. public static int getIndexOfMaximum( final double[] array )
  419. {
  420. double max = -Double.MAX_VALUE;
  421. int index = -1;
  422. for( int i = 0; i < array.length; i++ )
  423. {
  424. if( array[ i ] > max )
  425. {
  426. max = array[ i ];
  427. index = i;
  428. }
  429. }
  430. return index;
  431. }
  432. /**
  433. * Returns the index of the maximum value contained in the array.
  434. * @param array
  435. * @return
  436. */
  437. public static int getIndexOfMaximum( final Double[] array )
  438. {
  439. double max = -Double.MAX_VALUE;
  440. int index = -1;
  441. for( int i = 0; i < array.length; i++ )
  442. {
  443. if( array[ i ] > max )
  444. {
  445. max = array[ i ];
  446. index = i;
  447. }
  448. }
  449. return index;
  450. }
  451. /**
  452. * Returns the index of the minimum value contained in the matrix. NaN are ignored.
  453. * @param array
  454. * @return
  455. */
  456. public static int[] getIndexOfMinimum( final double[][] matrix )
  457. {
  458. double min = Double.MAX_VALUE;
  459. int[] index = new int[] {-1,-1} ;
  460. for( int i = 0; i < matrix.length; i++ )
  461. {
  462. for( int j = 0; j < matrix[ 0 ].length; j++ )
  463. {
  464. if( Double.isNaN( matrix[ i ][ j ] ) )
  465. continue;
  466. if( matrix[ i ][ j ] < min )
  467. {
  468. min = matrix[ i ][ j ];
  469. index[ 0 ] = i;
  470. index[ 1 ] = j;
  471. }
  472. }
  473. }
  474. return index;
  475. }
  476. /**
  477. * Returns the index of the maximum value contained in the matrix. NaN are ignored.
  478. * @param array
  479. * @return
  480. */
  481. public static int[] getIndexOfMaximum( final double[][] matrix )
  482. {
  483. double max = -Double.MAX_VALUE;
  484. int[] index = new int[] {-1,-1} ;
  485. for( int i = 0; i < matrix.length; i++ )
  486. {
  487. for( int j = 0; j < matrix[ 0 ].length; j++ )
  488. {
  489. if( Double.isNaN( matrix[ i ][ j ] ) )
  490. continue;
  491. if( matrix[ i ][ j ] > max )
  492. {
  493. max = matrix[ i ][ j ];
  494. index[ 0 ] = i;
  495. index[ 1 ] = j;
  496. }
  497. }
  498. }
  499. return index;
  500. }
  501. /**
  502. * Returns the indices of the sorted list. Specify if entries should be sorted increasing or decreasing
  503. * @param arrayToSort
  504. * @return
  505. */
  506. public static int[] getIndicesOfSortedList( final double[] arrayToSort, final boolean increasing )
  507. {
  508. //Get sorted index of eigenValues
  509. TreeMap<Double, List<Integer>> map = new TreeMap<Double, List<Integer>>();
  510. for(int i = 0; i < arrayToSort.length; i++)
  511. {
  512. //Get array with index
  513. List<Integer> ind = map.get( arrayToSort[i] );
  514. if(ind == null){
  515. ind = new ArrayList<Integer>();
  516. map.put(arrayToSort[i], ind);
  517. }
  518. ind.add(i);
  519. }
  520. // Now flatten the list
  521. List<Integer> indices = new ArrayList<Integer>();
  522. for(List<Integer> arr : map.values())
  523. {
  524. indices.addAll(arr);
  525. }
  526. final int[] out = new int[ indices.size() ];
  527. for( int i = 0; i < indices.size(); i++ )
  528. {
  529. if( increasing )
  530. out[ i ] = indices.get( i );
  531. else
  532. out[ i ] = indices.get( indices.size() - ( i + 1 ) );
  533. }
  534. return out;
  535. }
  536. /**
  537. * Returns the maximum value contained in the matrix. NaN are ignored.
  538. * @param array
  539. * @return
  540. */
  541. public static double getMaximum( final double[][] matrix )
  542. {
  543. double max = -Double.MAX_VALUE;
  544. for( int i = 0; i < matrix.length; i++ )
  545. {
  546. for( int j = 0; j < matrix[ 0 ].length; j++ )
  547. {
  548. if( Double.isNaN( matrix[ i ][ j ] ) )
  549. continue;
  550. if( matrix[ i ][ j ] > max )
  551. {
  552. max = matrix[ i ][ j ];
  553. }
  554. }
  555. }
  556. return max;
  557. }
  558. /**
  559. * Returns the maximum value of the array
  560. * @param array
  561. * @return
  562. */
  563. public static double getMaximum( final double[] array )
  564. {
  565. double max = -Double.MAX_VALUE;
  566. for( int i = 0; i < array.length; i++ )
  567. {
  568. if( array[ i ] > max )
  569. max = array[ i ];
  570. }
  571. return max;
  572. }
  573. /**
  574. * Returns the maximum value of the array
  575. * @param array
  576. * @return
  577. */
  578. public static double getMaximum( final Double[] array )
  579. {
  580. double max = -Double.MAX_VALUE;
  581. for( int i = 0; i < array.length; i++ )
  582. {
  583. if( array[ i ] > max )
  584. max = array[ i ];
  585. }
  586. return max;
  587. }
  588. /**
  589. * Returns an array holding min, max value of matrix
  590. * @param matrix
  591. * @return
  592. */
  593. public static double[] getMinMax( double[][] matrix )
  594. {
  595. final double[] minMax = new double[]{ Double.MAX_VALUE, -Double.MAX_VALUE };
  596. double v;
  597. for( int row = 0; row < matrix.length; row++ )
  598. {
  599. for( int col = 0; col < matrix[ 0 ].length; col++ )
  600. {
  601. v = matrix[ row ][ col ];
  602. if( v < minMax[ 0 ] )
  603. minMax[ 0 ] = v;
  604. if( v > minMax[ 1 ] )
  605. minMax[ 1 ] = v;
  606. }
  607. }
  608. return minMax;
  609. }
  610. /**
  611. * Returns an array holding min, max value of matrix
  612. * @param matrix
  613. * @return
  614. */
  615. public static double[] getMinMax( double[] array )
  616. {
  617. final double[] minMax = new double[]{ Double.MAX_VALUE, -Double.MAX_VALUE };
  618. double v;
  619. for( int row = 0; row < array.length; row++ )
  620. {
  621. v = array[ row ];
  622. if( v < minMax[ 0 ] )
  623. minMax[ 0 ] = v;
  624. if( v > minMax[ 1 ] )
  625. minMax[ 1 ] = v;
  626. }
  627. return minMax;
  628. }
  629. /**
  630. * Returns a matrix of dimension dimX x dimY filled with value fillValue
  631. * @param dimX
  632. * @param dimY
  633. * @param fillValue
  634. * @return
  635. */
  636. public static double[][] initiateMatrix( final int dimX, final int dimY, final double fillValue )
  637. {
  638. final double[][] out = new double[ dimX ][ dimY ];
  639. if( fillValue != 0.0 )
  640. {
  641. for( int i = 0; i < dimX; i++ )
  642. {
  643. for( int j = 0; j < dimY; j++)
  644. {
  645. out[ i ][ j ] = fillValue;
  646. }
  647. }
  648. }
  649. return out;
  650. }
  651. /**
  652. * Tests if in1 has the same entries as in2
  653. * @param in1
  654. * @param in2
  655. * @return
  656. */
  657. public static boolean isEqual( final double[] in1, final double[] in2 )
  658. {
  659. //Test if the length is the same
  660. if( in1.length != in2.length )
  661. {
  662. return false;
  663. }
  664. else
  665. {
  666. //Test if the path is the same
  667. boolean same = true;
  668. for( int i = 0; i < in1.length; i++ )
  669. {
  670. if( in1[ i ] != in2[ i ] )
  671. {
  672. same = false;
  673. break;
  674. }
  675. }
  676. return same;
  677. }
  678. }
  679. /**
  680. * Test if in1 and in2 are rotated versions of the same int array.
  681. * Returns true if in1 is A-B-C and in2 is A-B-C, C-A-B or B-C-A
  682. * @param path
  683. * @return
  684. */
  685. public static boolean isRotation( final double[] in1, final double[] in2 )
  686. {
  687. //Test if the path length is the same and have the same parent
  688. final int length1 = in1.length ;
  689. if( length1 != in2.length )
  690. {
  691. return false;
  692. }
  693. //Test if rotation
  694. else
  695. {
  696. double[] currentOrder = in1.clone();
  697. boolean isEqual = isEqual( currentOrder, in2 );
  698. if( isEqual )
  699. {
  700. return true;
  701. }
  702. else
  703. {
  704. for( int i = 0; i < length1 -1 ; i++ )
  705. {
  706. currentOrder = rotateArray( currentOrder );
  707. isEqual = isEqual( currentOrder, in2 );
  708. if( isEqual )
  709. break;
  710. }
  711. return isEqual;
  712. }
  713. }
  714. }
  715. /**
  716. * Test if in1 and in2 are reflected versions of the same int array.
  717. * Returns true if in1 is A-B-C and in2 is C-B-A
  718. * @param path
  719. * @return
  720. */
  721. public static boolean isReflection( final double[] in1, final double[] in2 )
  722. {
  723. return isEqual(reflect( in1 ), in2);
  724. }
  725. /**
  726. * Convenient method to get a string representation of a 2D matrix, using standard delimiters.
  727. * @param matrix
  728. * @return
  729. */
  730. public static String matrixToString( final double[][] matrix )
  731. {
  732. return matrixToString(matrix, "\t", "\n" );
  733. }
  734. /**
  735. * Get String representation of a 2D matrix.
  736. * @param matrix Input matrix
  737. * @param del1 First delimiter (e.g. tab: "\t")
  738. * @param del2 First delimiter (e.g. carriage return: "\n")
  739. * @return
  740. */
  741. public static String matrixToString( final double[][] matrix, final String del1, final String del2 )
  742. {
  743. String s = "";
  744. for( int j = 0; j < matrix.length; j++ )
  745. {
  746. if( j != 0 )
  747. s += del2;
  748. for( int i = 0; i < matrix[ j ].length; i++ )
  749. {
  750. if( i != 0 )
  751. s += del1;
  752. s += matrix[ j ][ i ];
  753. }
  754. }
  755. return s;
  756. }
  757. /**
  758. * Returns mean value of matrix. NaN are ignored.
  759. * @param in
  760. * @return
  761. */
  762. public static double mean( final double[][] matrix )
  763. {
  764. double v;
  765. double sum = 0.0;
  766. int count = 0;
  767. for( int j = 0; j < matrix.length; j++ )
  768. {
  769. for( int i = 0; i < matrix[ j ].length; i++ )
  770. {
  771. v = matrix[ j ][ i ];
  772. if( !Double.isNaN( v ) )
  773. {
  774. sum += v;
  775. count++;
  776. }
  777. }
  778. }
  779. return sum/count;
  780. }
  781. /**
  782. * Returns mean value of array. NaN are ignored.
  783. * @param in
  784. * @return
  785. */
  786. public static double mean( final double[] in )
  787. {
  788. double v;
  789. double sum = 0.0;
  790. int count = 0;
  791. for( int j = 0; j < in.length; j++ )
  792. {
  793. v = in[ j ];
  794. if( !Double.isNaN( v ) )
  795. {
  796. sum += v;
  797. count++;
  798. }
  799. }
  800. return sum/count;
  801. }
  802. /**
  803. * Returns mean value of array. NaN are ignored.
  804. * @param in
  805. * @return
  806. */
  807. public static double[] meanArray( final double[] in1, final double[] in2 )
  808. {
  809. double[] out = null;
  810. if( in1.length == in1.length )
  811. {
  812. out = new double[ in1.length ];
  813. for( int i = 0; i < in1.length; i++ )
  814. {
  815. out[ i ] = ( in1[ i ] + in2[ i ] )/2;
  816. }
  817. }
  818. return out;
  819. }
  820. /**
  821. * Normalizes values in array by its mean value.
  822. * Original values are overwritten
  823. * @param in
  824. */
  825. public static void normalizeByMean( double[] in )
  826. {
  827. final double f = ( 1/mean( in ) );
  828. System.out.println( f );
  829. elementwiseMultiplication( in, f );
  830. }
  831. /**
  832. * Reflects input array. A-B-C will be reflected to C-B-A
  833. * @param in
  834. * @return
  835. */
  836. public static double[] reflect( final double[] in )
  837. {
  838. final double[] refl = new double[ in.length ];
  839. for( int i = 0; i < in.length; i++ )
  840. {
  841. refl[ i ] = in[ in.length - (1 + i) ];
  842. }
  843. return refl;
  844. }
  845. /**
  846. * Rotates an array: Adds the last entry to the first position and shifts all other entries by 1.
  847. * A-B-C will become C-A-B
  848. * @param in
  849. * @return
  850. */
  851. public static double[] rotateArray( final double[] in )
  852. {
  853. final double[] out = new double[ in.length ];
  854. //Move last entry to first position
  855. out[ 0 ] = in[ in.length - 1 ];
  856. for( int i = 0; i < in.length - 1; i++ )
  857. out[ i + 1 ] = in[ i ];
  858. return out;
  859. }
  860. /**
  861. * Save 2d matrix as tiff
  862. * @param in
  863. */
  864. public static void save2dMatrixAsTif( final double[][] in, final String fileName )
  865. {
  866. new ImageJ();
  867. Matrix m = new Matrix( in );
  868. m = m.transpose();
  869. HeatMap hm = new HeatMap( m.getArray() );
  870. hm.saveImage( fileName );
  871. }
  872. /**
  873. * This method substitutes values in matrix by a defined double value.
  874. * @param matrix Matrix to modify
  875. * @param valueToSubstitute Value to look for
  876. * @param newValue Target value
  877. */
  878. public static void substitute( final double[][] matrix, final double valueToSubstitute, final double newValue )
  879. {
  880. for( int j = 0; j < matrix.length; j++ )
  881. {
  882. for( int i = 0; i < matrix[ j ].length; i++ )
  883. {
  884. if( Double.isNaN( valueToSubstitute ) )
  885. {
  886. if( Double.isNaN( matrix[ j ][ i ] ) )
  887. matrix[ j ][ i ] = newValue;
  888. }
  889. else
  890. {
  891. if( matrix[ j ][ i ] == valueToSubstitute )
  892. matrix[ j ][ i ] = newValue;
  893. }
  894. }
  895. }
  896. }
  897. /**
  898. * Sums all matrix entries.
  899. * @param matrix
  900. * @return
  901. */
  902. public static double sumOfMatrixEntries( final double[][] matrix )
  903. {
  904. double sum = 0.0;
  905. for( int j = 0; j < matrix.length; j++ )
  906. {
  907. for( int i = 0; i < matrix[ j ].length; i++ )
  908. {
  909. sum += matrix[ j ][ i ];
  910. }
  911. }
  912. return sum;
  913. }
  914. /**
  915. * Sums all array entries.
  916. * @param matrix
  917. * @return
  918. */
  919. public static double sumOfArrayEntries( final double[] array )
  920. {
  921. double sum = 0.0;
  922. for( int j = 0; j < array.length; j++ )
  923. sum += array[ j ];
  924. return sum;
  925. }
  926. /**
  927. * Converts double[][] to ArrayList<double[]>.
  928. * @param in
  929. * @return
  930. */
  931. public static ArrayList<double[]> toArrayList( final double[][] in )
  932. {
  933. ArrayList<double[]> out = new ArrayList<double[]>();
  934. for( int i = 0; i < in.length; i++ )
  935. out.add( in[ i ] );
  936. return out;
  937. }
  938. /**
  939. * Writes array to file specified
  940. * @param array
  941. * @param file
  942. */
  943. public static void writeToFile( final double[] array, final File file )
  944. {
  945. PrintWriter out = TextFileAccess.openFileWrite( file );
  946. for( Double d : array )
  947. out.println( d );
  948. out.close();
  949. }
  950. /**
  951. * Writes array to file specified
  952. * @param array
  953. * @param file
  954. */
  955. public static void writeToFile( final double[][] matrix, final File file )
  956. {
  957. PrintWriter out = TextFileAccess.openFileWrite( file );
  958. for( int i = 0; i < matrix.length; i++ )
  959. out.println( arrayToString( matrix[ i ] ) );
  960. out.close();
  961. }
  962. /**
  963. *
  964. * @param args
  965. */
  966. public static void main(String[] args)
  967. {
  968. final double[] array1 = { 1.098383839, 3.03030303003344, 5.83838383838 };
  969. final double[] array2 = { 4.0, -2.0, -1.0 };
  970. System.out.println( arrayToString( paste(array2, array2) ) );
  971. //System.out.println( arrayToString(array1, "\t", 3 ) );
  972. final double[][] matrix1 = new double[][]{
  973. { 0.0, 0.908597820519329, 0.9348686513077822, 0.932407031890514, 0.9280261140145634, 0.9384882238428602 },
  974. { 0.908597820519329, Double.NaN, 0.8962549951762211, 0.9695756559868266, 0.8531231610175372, 0.9005486194348331 },
  975. { 0.9348686513077822, 0.8962549951762211, 0.0, 0.9651262503390295, 0.899104002646973, 0.858505533294865 },
  976. { 0.932407031890514, 0.9695756559868266, 0.9651262503390295, 0.0, 0.9427823996475913, 0.9473669049367176 },
  977. { 0.9280261140145634, 0.8531231610175372, 0.899104002646973, 0.9427823996475913, 0.0, 0.8783195043804446 },
  978. { 0.9384882238428602, 0.9005486194348331, 0.858505533294865, 0.9473669049367176, 0.8783195043804446, 0.0 },
  979. };
  980. final double[][] matrix2 = new double[][]{
  981. { 1, 1, 1, 1 },
  982. { 1, 1, Double.NaN, 1 },
  983. { 1, 1, 1, 1 },
  984. { 1, 1, 1, 1 },
  985. };
  986. //System.out.println( arrayToString( getColumn( matrix1, 0 ) ) );
  987. //System.out.println( DoubleArrayTools.arrayToString( meanArray(array1, array2) ) );
  988. //System.out.println( a1 + " " + a2 );
  989. }
  990. }