PyFinitDiff 2.0.3.post0__py3-none-any.whl → 2.1.2.post0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PyFinitDiff/_version.py +2 -2
- PyFinitDiff/finite_difference_1D/boundaries.py +118 -69
- PyFinitDiff/finite_difference_1D/derivative.py +30 -16
- PyFinitDiff/finite_difference_1D/diagonals.py +155 -82
- PyFinitDiff/finite_difference_1D/finite_difference.py +104 -55
- PyFinitDiff/finite_difference_1D/utils.py +37 -19
- PyFinitDiff/finite_difference_2D/boundaries.py +116 -65
- PyFinitDiff/finite_difference_2D/derivative.py +31 -15
- PyFinitDiff/finite_difference_2D/diagonals.py +122 -78
- PyFinitDiff/finite_difference_2D/finite_difference.py +112 -62
- PyFinitDiff/finite_difference_2D/utils.py +47 -26
- PyFinitDiff/triplet.py +312 -33
- {PyFinitDiff-2.0.3.post0.dist-info → PyFinitDiff-2.1.2.post0.dist-info}/METADATA +53 -28
- PyFinitDiff-2.1.2.post0.dist-info/RECORD +26 -0
- {PyFinitDiff-2.0.3.post0.dist-info → PyFinitDiff-2.1.2.post0.dist-info}/WHEEL +1 -1
- PyFinitDiff/tools/__init__.py +0 -0
- PyFinitDiff-2.0.3.post0.dist-info/RECORD +0 -27
- /PyFinitDiff/{tools/directories.py → directories.py} +0 -0
- {PyFinitDiff-2.0.3.post0.dist-info → PyFinitDiff-2.1.2.post0.dist-info}/LICENSE +0 -0
- {PyFinitDiff-2.0.3.post0.dist-info → PyFinitDiff-2.1.2.post0.dist-info}/top_level.txt +0 -0
|
@@ -4,28 +4,38 @@
|
|
|
4
4
|
import numpy
|
|
5
5
|
from PyFinitDiff.triplet import DiagonalTriplet
|
|
6
6
|
from dataclasses import dataclass
|
|
7
|
+
from typing import Tuple
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
@dataclass
|
|
10
11
|
class MeshInfo:
|
|
11
12
|
"""
|
|
12
|
-
|
|
13
|
+
Represents the mesh information for a finite-difference grid.
|
|
13
14
|
|
|
14
|
-
Attributes
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
Attributes
|
|
16
|
+
----------
|
|
17
|
+
n_x : int
|
|
18
|
+
Number of points in the x direction.
|
|
19
|
+
n_y : int
|
|
20
|
+
Number of points in the y direction.
|
|
21
|
+
dx : float, optional
|
|
22
|
+
Infinitesimal displacement in the x direction. Defaults to 1.
|
|
23
|
+
dy : float, optional
|
|
24
|
+
Infinitesimal displacement in the y direction. Defaults to 1.
|
|
25
|
+
size : int
|
|
26
|
+
Total number of points in the mesh.
|
|
27
|
+
shape : Tuple[int, int]
|
|
28
|
+
Shape of the mesh as a tuple.
|
|
21
29
|
"""
|
|
22
30
|
n_x: int
|
|
23
31
|
n_y: int
|
|
24
32
|
dx: float = 1
|
|
25
33
|
dy: float = 1
|
|
26
34
|
|
|
27
|
-
def __post_init__(self):
|
|
28
|
-
"""
|
|
35
|
+
def __post_init__(self) -> None:
|
|
36
|
+
"""
|
|
37
|
+
Post-initialization to calculate mesh size and shape.
|
|
38
|
+
"""
|
|
29
39
|
self.size = self.n_x * self.n_y
|
|
30
40
|
self.shape = (self.n_x, self.n_y)
|
|
31
41
|
|
|
@@ -37,21 +47,35 @@ def get_circular_mesh_triplet(
|
|
|
37
47
|
x_offset: float = 0,
|
|
38
48
|
y_offset: float = 0,
|
|
39
49
|
value_out: float = 0,
|
|
40
|
-
value_in: float = 1) ->
|
|
50
|
+
value_in: float = 1) -> DiagonalTriplet:
|
|
41
51
|
"""
|
|
42
|
-
|
|
52
|
+
Generates a DiagonalTriplet representing a 2D mesh with a circular structure.
|
|
43
53
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
radius (float): The radius of the circular structure.
|
|
48
|
-
x_offset (float, optional): The x offset of the circular structure. Defaults to 0.
|
|
49
|
-
y_offset (float, optional): The y offset of the circular structure. Defaults to 0.
|
|
50
|
-
value_in (float, optional): The value inside the circular structure. Defaults to 1.
|
|
51
|
-
value_out (float, optional): The value outside the circular structure. Defaults to 0.
|
|
54
|
+
The mesh is represented with `value_in` inside a circle of a given `radius`,
|
|
55
|
+
and `value_out` outside the circle. The circular structure is centered at
|
|
56
|
+
a specific (x, y) offset.
|
|
52
57
|
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
n_x : int
|
|
61
|
+
The number of points in the x-axis.
|
|
62
|
+
n_y : int
|
|
63
|
+
The number of points in the y-axis.
|
|
64
|
+
radius : float
|
|
65
|
+
The radius of the circular structure.
|
|
66
|
+
x_offset : float, optional
|
|
67
|
+
The x offset of the circular structure. Defaults to 0.
|
|
68
|
+
y_offset : float, optional
|
|
69
|
+
The y offset of the circular structure. Defaults to 0.
|
|
70
|
+
value_out : float, optional
|
|
71
|
+
The value outside the circular structure. Defaults to 0.
|
|
72
|
+
value_in : float, optional
|
|
73
|
+
The value inside the circular structure. Defaults to 1.
|
|
74
|
+
|
|
75
|
+
Returns
|
|
76
|
+
-------
|
|
77
|
+
DiagonalTriplet
|
|
78
|
+
The 2D circular mesh triplet.
|
|
55
79
|
"""
|
|
56
80
|
y, x = numpy.mgrid[
|
|
57
81
|
-100:100:complex(n_y),
|
|
@@ -62,7 +86,4 @@ def get_circular_mesh_triplet(
|
|
|
62
86
|
mesh = numpy.ones(x.shape) * value_out
|
|
63
87
|
mesh[r < radius] = value_in
|
|
64
88
|
|
|
65
|
-
return DiagonalTriplet(mesh, shape=
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
# -
|
|
89
|
+
return DiagonalTriplet(mesh, shape=(n_x, n_y))
|
PyFinitDiff/triplet.py
CHANGED
|
@@ -7,9 +7,21 @@ from scipy.sparse import coo_matrix
|
|
|
7
7
|
from dataclasses import dataclass
|
|
8
8
|
import matplotlib.pyplot as plt
|
|
9
9
|
|
|
10
|
-
|
|
11
10
|
@dataclass
|
|
12
11
|
class Triplet:
|
|
12
|
+
"""
|
|
13
|
+
A class representing a sparse matrix in triplet (COO) format, useful for finite-difference operations.
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
shape : tuple of int
|
|
18
|
+
Shape of the matrix (rows, columns).
|
|
19
|
+
array : numpy.ndarray
|
|
20
|
+
A 2D array with three columns representing row indices, column indices, and values.
|
|
21
|
+
add_extra_column : bool, optional
|
|
22
|
+
If True, adds an extra column of ones to the array (default is False).
|
|
23
|
+
|
|
24
|
+
"""
|
|
13
25
|
shape: Tuple[int, int]
|
|
14
26
|
array: numpy.ndarray
|
|
15
27
|
add_extra_column: bool = False
|
|
@@ -30,79 +42,221 @@ class Triplet:
|
|
|
30
42
|
|
|
31
43
|
@property
|
|
32
44
|
def index(self) -> numpy.ndarray:
|
|
33
|
-
"""
|
|
45
|
+
"""
|
|
46
|
+
Return the first two columns of the array, representing the indices.
|
|
47
|
+
|
|
48
|
+
Returns
|
|
49
|
+
-------
|
|
50
|
+
numpy.ndarray
|
|
51
|
+
Array of row and column indices.
|
|
52
|
+
"""
|
|
34
53
|
return self.array[:, :2].astype(int)
|
|
35
54
|
|
|
36
55
|
@property
|
|
37
56
|
def rows(self) -> numpy.ndarray:
|
|
38
|
-
"""
|
|
57
|
+
"""
|
|
58
|
+
Return the row indices (first column).
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
numpy.ndarray
|
|
63
|
+
Array of row indices.
|
|
64
|
+
"""
|
|
39
65
|
return self.array[:, 0].astype(int)
|
|
40
66
|
|
|
41
67
|
@property
|
|
42
68
|
def columns(self) -> numpy.ndarray:
|
|
43
|
-
"""
|
|
69
|
+
"""
|
|
70
|
+
Return the column indices (second column).
|
|
71
|
+
|
|
72
|
+
Returns
|
|
73
|
+
-------
|
|
74
|
+
numpy.ndarray
|
|
75
|
+
Array of column indices.
|
|
76
|
+
"""
|
|
44
77
|
return self.array[:, 1].astype(int)
|
|
45
78
|
|
|
46
79
|
@property
|
|
47
80
|
def values(self) -> numpy.ndarray:
|
|
48
|
-
"""
|
|
81
|
+
"""
|
|
82
|
+
Return the values (third column).
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
numpy.ndarray
|
|
87
|
+
Array of values.
|
|
88
|
+
"""
|
|
49
89
|
return self.array[:, 2]
|
|
50
90
|
|
|
51
91
|
@values.setter
|
|
52
92
|
def values(self, value: Union[float, numpy.ndarray]) -> None:
|
|
53
|
-
"""
|
|
93
|
+
"""
|
|
94
|
+
Set the values in the array.
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
value : float or numpy.ndarray
|
|
99
|
+
The new value(s) to set in the third column of the array.
|
|
100
|
+
"""
|
|
54
101
|
self.array[:, 2] = value
|
|
55
102
|
|
|
56
103
|
@property
|
|
57
104
|
def size(self) -> int:
|
|
58
|
-
"""
|
|
105
|
+
"""
|
|
106
|
+
Return the number of entries in the triplet.
|
|
107
|
+
|
|
108
|
+
Returns
|
|
109
|
+
-------
|
|
110
|
+
int
|
|
111
|
+
Number of entries.
|
|
112
|
+
"""
|
|
59
113
|
return self.array.shape[0]
|
|
60
114
|
|
|
61
115
|
def remove_below_i(self, i_value: int) -> 'Triplet':
|
|
62
|
-
"""
|
|
116
|
+
"""
|
|
117
|
+
Remove all entries with row indices below a specified value.
|
|
118
|
+
|
|
119
|
+
Parameters
|
|
120
|
+
----------
|
|
121
|
+
i_value : int
|
|
122
|
+
Threshold for row indices. Entries with row indices below this value are removed.
|
|
123
|
+
|
|
124
|
+
Returns
|
|
125
|
+
-------
|
|
126
|
+
Triplet
|
|
127
|
+
The modified Triplet instance.
|
|
128
|
+
"""
|
|
63
129
|
self.array = self.array[self.rows > i_value]
|
|
64
130
|
return self
|
|
65
131
|
|
|
66
132
|
def remove_above_i(self, i_value: int) -> 'Triplet':
|
|
67
|
-
"""
|
|
133
|
+
"""
|
|
134
|
+
Remove all entries with row indices above a specified value.
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
i_value : int
|
|
139
|
+
Threshold for row indices. Entries with row indices above this value are removed.
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
-------
|
|
143
|
+
Triplet
|
|
144
|
+
The modified Triplet instance.
|
|
145
|
+
"""
|
|
68
146
|
self.array = self.array[self.rows < i_value]
|
|
69
147
|
return self
|
|
70
148
|
|
|
71
149
|
def remove_below_j(self, j_value: int) -> 'Triplet':
|
|
72
|
-
"""
|
|
150
|
+
"""
|
|
151
|
+
Remove all entries with column indices below a specified value.
|
|
152
|
+
|
|
153
|
+
Parameters
|
|
154
|
+
----------
|
|
155
|
+
j_value : int
|
|
156
|
+
Threshold for column indices. Entries with column indices below this value are removed.
|
|
157
|
+
|
|
158
|
+
Returns
|
|
159
|
+
-------
|
|
160
|
+
Triplet
|
|
161
|
+
The modified Triplet instance.
|
|
162
|
+
"""
|
|
73
163
|
self.array = self.array[self.columns > j_value]
|
|
74
164
|
return self
|
|
75
165
|
|
|
76
166
|
def remove_above_j(self, j_value: int) -> 'Triplet':
|
|
77
|
-
"""
|
|
167
|
+
"""
|
|
168
|
+
Remove all entries with column indices above a specified value.
|
|
169
|
+
|
|
170
|
+
Parameters
|
|
171
|
+
----------
|
|
172
|
+
j_value : int
|
|
173
|
+
Threshold for column indices. Entries with column indices above this value are removed.
|
|
174
|
+
|
|
175
|
+
Returns
|
|
176
|
+
-------
|
|
177
|
+
Triplet
|
|
178
|
+
The modified Triplet instance.
|
|
179
|
+
"""
|
|
78
180
|
self.array = self.array[self.columns < j_value]
|
|
79
181
|
return self
|
|
80
182
|
|
|
81
183
|
def delete(self, index: numpy.ndarray) -> None:
|
|
82
|
-
"""
|
|
184
|
+
"""
|
|
185
|
+
Delete entries at the given indices.
|
|
186
|
+
|
|
187
|
+
Parameters
|
|
188
|
+
----------
|
|
189
|
+
index : numpy.ndarray
|
|
190
|
+
Indices of entries to delete.
|
|
191
|
+
"""
|
|
83
192
|
self.array = numpy.delete(self.array, index.astype(int), axis=0)
|
|
84
193
|
|
|
85
194
|
def append(self, other: 'Triplet') -> None:
|
|
86
|
-
"""
|
|
195
|
+
"""
|
|
196
|
+
Append another triplet to this one.
|
|
197
|
+
|
|
198
|
+
Parameters
|
|
199
|
+
----------
|
|
200
|
+
other : Triplet
|
|
201
|
+
Another Triplet instance to append.
|
|
202
|
+
"""
|
|
87
203
|
self.array = numpy.r_[self.array, other.array]
|
|
88
204
|
|
|
89
205
|
def append_array(self, array: numpy.ndarray) -> None:
|
|
90
|
-
"""
|
|
206
|
+
"""
|
|
207
|
+
Append a NumPy array to this triplet.
|
|
208
|
+
|
|
209
|
+
Parameters
|
|
210
|
+
----------
|
|
211
|
+
array : numpy.ndarray
|
|
212
|
+
The array to append to the triplet.
|
|
213
|
+
"""
|
|
91
214
|
self.array = numpy.r_[self.array, array]
|
|
92
215
|
|
|
93
216
|
def __add__(self, other: 'Triplet') -> 'Triplet':
|
|
94
|
-
"""
|
|
217
|
+
"""
|
|
218
|
+
Concatenate two triplets and merge any duplicate indices.
|
|
219
|
+
|
|
220
|
+
Parameters
|
|
221
|
+
----------
|
|
222
|
+
other : Triplet
|
|
223
|
+
Another Triplet instance to concatenate with this one.
|
|
224
|
+
|
|
225
|
+
Returns
|
|
226
|
+
-------
|
|
227
|
+
Triplet
|
|
228
|
+
A new Triplet instance with merged values for duplicate indices.
|
|
229
|
+
"""
|
|
95
230
|
combined_array = numpy.r_[self.array, other.array]
|
|
96
231
|
return Triplet(array=combined_array, shape=self.shape).remove_duplicate()
|
|
97
232
|
|
|
98
233
|
def __mul__(self, factor: float) -> 'Triplet':
|
|
99
|
-
"""
|
|
234
|
+
"""
|
|
235
|
+
Multiply all values in the triplet by a factor.
|
|
236
|
+
|
|
237
|
+
Parameters
|
|
238
|
+
----------
|
|
239
|
+
factor : float
|
|
240
|
+
The factor to multiply all values by.
|
|
241
|
+
|
|
242
|
+
Returns
|
|
243
|
+
-------
|
|
244
|
+
Triplet
|
|
245
|
+
A new Triplet instance with values scaled by the factor.
|
|
246
|
+
"""
|
|
100
247
|
return Triplet(array=self.array * [1, 1, factor], shape=self.shape)
|
|
101
248
|
|
|
102
249
|
__rmul__ = __mul__
|
|
103
250
|
|
|
104
251
|
def remove_duplicate(self) -> 'Triplet':
|
|
105
|
-
"""
|
|
252
|
+
"""
|
|
253
|
+
Remove duplicate entries by summing their values.
|
|
254
|
+
|
|
255
|
+
Returns
|
|
256
|
+
-------
|
|
257
|
+
Triplet
|
|
258
|
+
A new Triplet instance with duplicate entries removed.
|
|
259
|
+
"""
|
|
106
260
|
new_array = self.array
|
|
107
261
|
index_to_delete = []
|
|
108
262
|
duplicate = self.get_duplicate_index()
|
|
@@ -121,26 +275,68 @@ class Triplet:
|
|
|
121
275
|
return Triplet(array=triplet_array, shape=self.shape)
|
|
122
276
|
|
|
123
277
|
def coincide_i(self, mask: 'Triplet') -> None:
|
|
124
|
-
"""
|
|
278
|
+
"""
|
|
279
|
+
Remove entries whose row indices do not coincide with those in the mask.
|
|
280
|
+
|
|
281
|
+
Parameters
|
|
282
|
+
----------
|
|
283
|
+
mask : Triplet
|
|
284
|
+
The mask triplet used to filter rows.
|
|
285
|
+
"""
|
|
125
286
|
mask_i_unique = numpy.unique(mask.rows[mask.values != 0])
|
|
126
287
|
self.array = numpy.vstack([self.array[self.rows == i] for i in mask_i_unique])
|
|
127
288
|
|
|
128
289
|
def __sub__(self, other: 'Triplet') -> 'Triplet':
|
|
129
|
-
"""
|
|
290
|
+
"""
|
|
291
|
+
Subtract another triplet by removing matching row indices.
|
|
292
|
+
|
|
293
|
+
Parameters
|
|
294
|
+
----------
|
|
295
|
+
other : Triplet
|
|
296
|
+
Another Triplet instance to subtract.
|
|
297
|
+
|
|
298
|
+
Returns
|
|
299
|
+
-------
|
|
300
|
+
Triplet
|
|
301
|
+
A new Triplet instance with non-matching rows.
|
|
302
|
+
"""
|
|
130
303
|
non_matching_indices = ~numpy.isin(self.rows, other.rows)
|
|
131
304
|
return Triplet(array=self.array[non_matching_indices], shape=self.shape)
|
|
132
305
|
|
|
133
306
|
def __iter__(self):
|
|
134
|
-
"""
|
|
307
|
+
"""
|
|
308
|
+
Iterator to yield the indices and values as tuples.
|
|
309
|
+
|
|
310
|
+
Yields
|
|
311
|
+
------
|
|
312
|
+
tuple
|
|
313
|
+
A tuple containing row index, column index, and value.
|
|
314
|
+
"""
|
|
135
315
|
return iter((int(i), int(j), value) for i, j, value in self.array)
|
|
136
316
|
|
|
137
317
|
def enumerate(self, start: int = 0, stop: int = None):
|
|
138
|
-
"""
|
|
318
|
+
"""
|
|
319
|
+
Enumerate over the entries, yielding an index and the entry values.
|
|
320
|
+
|
|
321
|
+
Parameters
|
|
322
|
+
----------
|
|
323
|
+
start : int, optional
|
|
324
|
+
Starting index for enumeration (default is 0).
|
|
325
|
+
stop : int, optional
|
|
326
|
+
Stopping index for enumeration (default is None).
|
|
327
|
+
|
|
328
|
+
Yields
|
|
329
|
+
------
|
|
330
|
+
tuple
|
|
331
|
+
Index and a tuple of row index, column index, and value.
|
|
332
|
+
"""
|
|
139
333
|
for idx, (i, j, value) in enumerate(self.array[start:stop], start=start):
|
|
140
334
|
yield idx, (int(i), int(j), value)
|
|
141
335
|
|
|
142
336
|
def merge_duplicate(self) -> None:
|
|
143
|
-
"""
|
|
337
|
+
"""
|
|
338
|
+
Merge duplicate entries by summing their values.
|
|
339
|
+
"""
|
|
144
340
|
duplicates = self.get_duplicate_index()
|
|
145
341
|
if duplicates.size == 0:
|
|
146
342
|
return
|
|
@@ -150,52 +346,129 @@ class Triplet:
|
|
|
150
346
|
self.delete(duplicate[1:])
|
|
151
347
|
|
|
152
348
|
def get_duplicate_index(self) -> numpy.ndarray:
|
|
153
|
-
"""
|
|
349
|
+
"""
|
|
350
|
+
Get indices of duplicate entries.
|
|
351
|
+
|
|
352
|
+
Returns
|
|
353
|
+
-------
|
|
354
|
+
numpy.ndarray
|
|
355
|
+
Array of duplicate indices.
|
|
356
|
+
"""
|
|
154
357
|
unique_indices, inverse_indices, counts = numpy.unique(self.index, axis=0, return_inverse=True, return_counts=True)
|
|
155
358
|
duplicate_indices = numpy.where(counts > 1)[0]
|
|
156
359
|
return numpy.array([numpy.where(inverse_indices == idx)[0] for idx in duplicate_indices], dtype=object)
|
|
157
360
|
|
|
158
361
|
@property
|
|
159
362
|
def max_i(self) -> int:
|
|
160
|
-
"""
|
|
363
|
+
"""
|
|
364
|
+
Return the maximum row index.
|
|
365
|
+
|
|
366
|
+
Returns
|
|
367
|
+
-------
|
|
368
|
+
int
|
|
369
|
+
Maximum row index.
|
|
370
|
+
"""
|
|
161
371
|
return self.rows.max()
|
|
162
372
|
|
|
163
373
|
@property
|
|
164
374
|
def max_j(self) -> int:
|
|
165
|
-
"""
|
|
375
|
+
"""
|
|
376
|
+
Return the maximum column index.
|
|
377
|
+
|
|
378
|
+
Returns
|
|
379
|
+
-------
|
|
380
|
+
int
|
|
381
|
+
Maximum column index.
|
|
382
|
+
"""
|
|
166
383
|
return self.columns.max()
|
|
167
384
|
|
|
168
385
|
@property
|
|
169
386
|
def min_i(self) -> int:
|
|
170
|
-
"""
|
|
387
|
+
"""
|
|
388
|
+
Return the minimum row index.
|
|
389
|
+
|
|
390
|
+
Returns
|
|
391
|
+
-------
|
|
392
|
+
int
|
|
393
|
+
Minimum row index.
|
|
394
|
+
"""
|
|
171
395
|
return self.rows.min()
|
|
172
396
|
|
|
173
397
|
@property
|
|
174
398
|
def min_j(self) -> int:
|
|
175
|
-
"""
|
|
399
|
+
"""
|
|
400
|
+
Return the minimum column index.
|
|
401
|
+
|
|
402
|
+
Returns
|
|
403
|
+
-------
|
|
404
|
+
int
|
|
405
|
+
Minimum column index.
|
|
406
|
+
"""
|
|
176
407
|
return self.columns.min()
|
|
177
408
|
|
|
178
409
|
@property
|
|
179
410
|
def diagonal(self) -> numpy.ndarray:
|
|
180
|
-
"""
|
|
411
|
+
"""
|
|
412
|
+
Return the diagonal elements of the triplet.
|
|
413
|
+
|
|
414
|
+
Returns
|
|
415
|
+
-------
|
|
416
|
+
numpy.ndarray
|
|
417
|
+
Diagonal elements.
|
|
418
|
+
"""
|
|
181
419
|
return self.array[self.rows == self.columns]
|
|
182
420
|
|
|
183
421
|
def shift_diagonal(self, value: float) -> 'Triplet':
|
|
184
|
-
"""
|
|
422
|
+
"""
|
|
423
|
+
Shift the diagonal elements by a specified value.
|
|
424
|
+
|
|
425
|
+
Parameters
|
|
426
|
+
----------
|
|
427
|
+
value : float
|
|
428
|
+
Value to add to the diagonal elements.
|
|
429
|
+
|
|
430
|
+
Returns
|
|
431
|
+
-------
|
|
432
|
+
Triplet
|
|
433
|
+
A new Triplet instance with the shifted diagonal.
|
|
434
|
+
"""
|
|
185
435
|
diagonal_triplet = DiagonalTriplet(numpy.ones(min(self.max_i, self.max_j)) * value, shape=self.shape)
|
|
186
436
|
return self + diagonal_triplet
|
|
187
437
|
|
|
188
438
|
def update_elements(self, other_triplet: 'Triplet', i_range: slice) -> 'Triplet':
|
|
189
|
-
"""
|
|
439
|
+
"""
|
|
440
|
+
Update elements within a specified range.
|
|
441
|
+
|
|
442
|
+
Parameters
|
|
443
|
+
----------
|
|
444
|
+
other_triplet : Triplet
|
|
445
|
+
The Triplet instance whose elements will be used for updating.
|
|
446
|
+
i_range : slice
|
|
447
|
+
Range of rows to update.
|
|
448
|
+
|
|
449
|
+
Returns
|
|
450
|
+
-------
|
|
451
|
+
Triplet
|
|
452
|
+
The updated Triplet instance.
|
|
453
|
+
"""
|
|
190
454
|
self.array[i_range, :] = other_triplet.array[i_range, :]
|
|
191
455
|
return self
|
|
192
456
|
|
|
193
457
|
def to_dense(self) -> numpy.ndarray:
|
|
194
|
-
"""
|
|
458
|
+
"""
|
|
459
|
+
Convert the triplet to a dense matrix representation.
|
|
460
|
+
|
|
461
|
+
Returns
|
|
462
|
+
-------
|
|
463
|
+
numpy.ndarray
|
|
464
|
+
Dense matrix representation of the triplet.
|
|
465
|
+
"""
|
|
195
466
|
return self.to_scipy_sparse().toarray()
|
|
196
467
|
|
|
197
468
|
def plot(self) -> NoReturn:
|
|
198
|
-
"""
|
|
469
|
+
"""
|
|
470
|
+
Plot the dense matrix representation of the triplet.
|
|
471
|
+
"""
|
|
199
472
|
plt.figure()
|
|
200
473
|
plt.title('Finite-difference coefficients structure')
|
|
201
474
|
plt.xlabel('Columns')
|
|
@@ -209,7 +482,13 @@ class Triplet:
|
|
|
209
482
|
plt.show()
|
|
210
483
|
|
|
211
484
|
def to_scipy_sparse(self) -> coo_matrix:
|
|
212
|
-
"""
|
|
485
|
+
"""
|
|
486
|
+
Convert the triplet to a SciPy sparse matrix.
|
|
487
|
+
|
|
488
|
+
Returns
|
|
489
|
+
-------
|
|
490
|
+
coo_matrix: The scipy sparse matrix representation.
|
|
491
|
+
"""
|
|
213
492
|
return coo_matrix((self.values, (self.rows, self.columns)), shape=(numpy.prod(self.shape),) * 2)
|
|
214
493
|
|
|
215
494
|
|