Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

226
LINES

< > BotCompany Repo | #1026089 // IntArray2D [from smile]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2687L/17K).

1  
/*******************************************************************************
2  
 * Copyright (c) 2010-2019 Haifeng Li
3  
 *
4  
 * Smile is free software: you can redistribute it and/or modify
5  
 * it under the terms of the GNU Lesser General Public License as
6  
 * published by the Free Software Foundation, either version 3 of
7  
 * the License, or (at your option) any later version.
8  
 *
9  
 * Smile is distributed in the hope that it will be useful,
10  
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  
 * GNU Lesser General Public License for more details.
13  
 *
14  
 * You should have received a copy of the GNU Lesser General Public License
15  
 * along with Smile.  If not, see <https://www.gnu.org/licenses/>.
16  
 *******************************************************************************/
17  
18  
sclass IntArray2D implements IIntArray2D {
19  
  private int[] A;
20  
  private int nrows;
21  
  private int ncols;
22  
23  
  public IntArray2D(int[][] A) {
24  
      this.nrows = A.length;
25  
      this.ncols = A[0].length;
26  
      this.A = new int[nrows*ncols];
27  
28  
      int pos = 0;
29  
      for (int i = 0; i < nrows; i++) {
30  
          System.arraycopy(A[i], 0, this.A, pos, ncols);
31  
          pos += ncols;
32  
      }
33  
  }
34  
35  
  /**
36  
   * Constructor of all-zero matrix.
37  
   */
38  
  public IntArray2D(int rows, int cols) {
39  
      this.nrows = rows;
40  
      this.ncols = cols;
41  
      A = new int[rows*cols];
42  
  }
43  
44  
  /**
45  
   * Constructor. Fill the matrix with given value.
46  
   */
47  
  public IntArray2D(int rows, int cols, int value) {
48  
      this(rows, cols);
49  
      if (value != 0.0)
50  
          Arrays.fill(A, value);
51  
  }
52  
53  
  /**
54  
   * Constructor.
55  
   * @param value the array of matrix values arranged in row major format
56  
   */
57  
  public IntArray2D(int rows, int cols, int[] value) {
58  
      this.nrows = rows;
59  
      this.ncols = cols;
60  
      this.A = value;
61  
  }
62  
63  
  public int nrows() {
64  
      return nrows;
65  
  }
66  
67  
  public int ncols() {
68  
      return ncols;
69  
  }
70  
71  
  /** Returns A(i, j). Useful in Scala. */
72  
  public int apply(int i, int j) {
73  
      return A[i*ncols + j];
74  
  }
75  
76  
  /** Returns A(i, j). */
77  
  public int get(int i, int j) {
78  
      return A[i*ncols + j];
79  
  }
80  
81  
  /** Sets A(i, j). */
82  
  public int set(int i, int j, int x) {
83  
      return A[i*ncols + j] = x;
84  
  }
85  
86  
  public int add(int i, int j, int x) {
87  
      return A[i*ncols + j] += x;
88  
  }
89  
90  
  public int sub(int i, int j, int x) {
91  
      return A[i*ncols + j] -= x;
92  
  }
93  
94  
  public int mul(int i, int j, int x) {
95  
      return A[i*ncols + j] *= x;
96  
  }
97  
98  
  public int div(int i, int j, int x) {
99  
      return A[i*ncols + j] /= x;
100  
  }
101  
102  
  public IntArray2D add(IntArray2D b) {
103  
      if (nrows() != b.nrows() || ncols() != b.ncols()) {
104  
          throw new IllegalArgumentException("Matrix is not of same size.");
105  
      }
106  
107  
      for (int i = 0; i < A.length; i++) {
108  
          A[i] += b.A[i];
109  
      }
110  
      return this;
111  
  }
112  
113  
  public IntArray2D sub(IntArray2D b) {
114  
      if (nrows() != b.nrows() || ncols() != b.ncols()) {
115  
          throw new IllegalArgumentException("Matrix is not of same size.");
116  
      }
117  
118  
      for (int i = 0; i < A.length; i++) {
119  
          A[i] -= b.A[i];
120  
      }
121  
      return this;
122  
  }
123  
124  
  public IntArray2D mul(IntArray2D b) {
125  
      if (nrows() != b.nrows() || ncols() != b.ncols()) {
126  
          throw new IllegalArgumentException("Matrix is not of same size.");
127  
      }
128  
129  
      for (int i = 0; i < A.length; i++) {
130  
          A[i] *= b.A[i];
131  
      }
132  
      return this;
133  
  }
134  
135  
  public IntArray2D div(IntArray2D b) {
136  
      if (nrows() != b.nrows() || ncols() != b.ncols()) {
137  
          throw new IllegalArgumentException("Matrix is not of same size.");
138  
      }
139  
140  
      for (int i = 0; i < A.length; i++) {
141  
          A[i] /= b.A[i];
142  
      }
143  
      return this;
144  
  }
145  
146  
  public IntArray2D add(int x) {
147  
      for (int i = 0; i < A.length; i++) {
148  
          A[i] += x;
149  
      }
150  
151  
      return this;
152  
  }
153  
154  
  public IntArray2D sub(int x) {
155  
      for (int i = 0; i < A.length; i++) {
156  
          A[i] -= x;
157  
      }
158  
159  
      return this;
160  
  }
161  
162  
  public IntArray2D mul(int x) {
163  
      for (int i = 0; i < A.length; i++) {
164  
          A[i] *= x;
165  
      }
166  
167  
      return this;
168  
  }
169  
170  
  public IntArray2D div(int x) {
171  
      for (int i = 0; i < A.length; i++) {
172  
          A[i] /= x;
173  
      }
174  
175  
      return this;
176  
  }
177  
178  
  public long sum() {
179  
      long s = 0;
180  
      for (int i = 0; i < A.length; i++) {
181  
          s += A[i];
182  
      }
183  
184  
      return s;
185  
  }
186  
187  
  @Override
188  
  public String toString() {
189  
      return toString(false);
190  
  }
191  
192  
  /**
193  
   * Returns the string representation of matrix.
194  
   * @param full Print the full matrix if true. Otherwise,
195  
   *             print only top left 7 x 7 submatrix.
196  
   */
197  
  public String toString(boolean full) {
198  
      return full ? toString(nrows(), ncols()) : toString(7, 7);
199  
  }
200  
201  
  /**
202  
   * Returns the string representation of matrix.
203  
   * @param m the number of rows to print.
204  
   * @param n the number of columns to print.
205  
   */
206  
  public String toString(int m, int n) {
207  
      StringBuilder sb = new StringBuilder();
208  
      m = Math.min(m, nrows());
209  
      n = Math.min(n, ncols());
210  
211  
      String newline = n < ncols() ? "...\n" : "\n";
212  
213  
      for (int i = 0; i < m; i++) {
214  
          for (int j = 0; j < n; j++) {
215  
              sb.append(String.format("%d  ", get(i, j)));
216  
          }
217  
          sb.append(newline);
218  
      }
219  
220  
      if (m < nrows()) {
221  
          sb.append("  ...\n");
222  
      }
223  
224  
      return sb.toString();
225  
  }
226  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1026089
Snippet name: IntArray2D [from smile]
Eternal ID of this version: #1026089/2
Text MD5: a87701d367d7063ca728200298cab77f
Transpilation MD5: ba51f90ea4092c6b4ef9d14a3ad57962
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-27 02:38:18
Source code size: 5598 bytes / 226 lines
Pitched / IR pitched: No / No
Views / Downloads: 250 / 630
Version history: 1 change(s)
Referenced in: [show references]