Guest User

Untitled

a guest
Oct 18th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. package edu.msud.cs.cs1.IPJ5;
  2. public class Matrix {
  3.  
  4. public static double dot (double[]x, double[]y)
  5. {
  6. int n = x.length;
  7. double sum = 0;
  8. if(x.length != y.length) {
  9. System.out.println("undefined dimensions");
  10. }
  11. else {
  12. for (int i = 0; i < n; i++)
  13. {
  14. sum += x[i] * y[i];
  15. }
  16. }
  17. return sum;
  18. }
  19. public static double[][] multiply(double[][]x, double[][]y) { //matrix-matrix
  20. double[][] c = new double[x.length][y[0].length];
  21. /* ^^create new array two hold both arrays, of size R: array 1 rows, C: array 2 columns*/
  22.  
  23. if (y.length == x[0].length) {
  24. for (int i = 0; i < x.length; i++) {
  25. for (int j = 0; j < y[0].length; j++) {
  26.  
  27. for (int k = 0; k < x[0].length; k++) {
  28. c[i][j] += x[i][k] * y[k][j];
  29.  
  30. }
  31.  
  32. }
  33. }
  34. }
  35. else
  36. {
  37. System.out.println("Dimension requirements not satisfied for accurate calculation");
  38. }
  39. return c;
  40. }
  41. public static double[] multiply(double[]x, double[][]y) { //vector-matrix
  42. double[] c = new double[y[0].length];
  43. // System.out.println(y[0].length);
  44. /* ^^create new array two hold both arrays, of size R: array 1 rows, C: array 2 columns*/
  45.  
  46. // c[0] = x[0] * y[0][0];
  47. for (int i = 0; i < x.length; i++) {
  48. for (int j = 0; j < y[0].length; j++) {
  49.  
  50. c[i] += x[i] * y[i][j];
  51.  
  52. }
  53. }
  54.  
  55. return c;
  56. }
  57. public static double[] multiply(double[][]x, double[]y) { //matrix-vector
  58. double[] c = new double[x[0].length];
  59.  
  60.  
  61. for (int i = 0; i < x[0].length; i++) {
  62. for (int j = 0; j < y.length; j++) {
  63.  
  64. c[i] +=x[i][j] * y[j];
  65.  
  66. }
  67. }
  68.  
  69. return c;
  70. }
  71. public static void printArray (double[][]v) //static method to print results of other methods
  72. {
  73. for(int i = 0; i < v.length; i++)
  74. {
  75. for(int j = 0; j < v.length; j++)
  76. {
  77. System.out.print(v[i][j] + " ");
  78. }
  79. System.out.println();
  80. }
  81. }
  82. public static void printArray (double[]v) //static method to print results of other methods
  83. {
  84. for(int i = 0; i < v.length; i++)
  85. {
  86. System.out.print(v[i] + " ");
  87. }
  88. }
  89.  
  90. public static void main (String[] args)
  91. {
  92. // double[] a = { 1,2,3,4 };
  93. // double[] b = { 1,2,3,4 };
  94. // System.out.println(dot(a,b));
  95. double[][]a = {
  96. {1,2,3,4},
  97. {5,6,7,8},
  98. {9,10,11,12},
  99. //print array a
  100. };
  101. for(int i = 0; i < a[0].length-1; i++)
  102. {
  103. for(int j = 0; j < a[0].length; j++)
  104. {
  105. System.out.print(a[i][j] + " ");
  106. }
  107. System.out.println();
  108. }
  109.  
  110.  
  111. double[]b = {
  112. -1,-2,-3
  113.  
  114. };
  115. System.out.println();
  116. //print array b
  117. for(int i = 0; i < b.length; i++)
  118. {
  119.  
  120. System.out.print(b[i] + " ");
  121. }
  122. System.out.println();
  123. printArray(multiply(b,a));
  124.  
  125. }
  126. }
Add Comment
Please, Sign In to add comment