-
Notifications
You must be signed in to change notification settings - Fork 17
/
binary_operation.swift
761 lines (687 loc) · 28.3 KB
/
binary_operation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//
// bioperator.swift
// Matft
//
// Created by Junnosuke Kado on 2020/02/27.
// Copyright © 2020 jkado. All rights reserved.
//
import Foundation
import Accelerate
extension Matft{
//infix
/**
Element-wise addition of two mfarray
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func add(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let (l_mfarray, r_mfarray, rettype) = biop_broadcast_to(l_mfarray, r_mfarray)
switch MfType.storedType(rettype){
case .Float:
return biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vadd)
case .Double:
return biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vaddD)
}
}
/**
Element-wise addition of mfarray and scalar
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func add<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let r_mfype = MfType.mftype(value: r_scalar)
let retmftype = MfType.priority(l_mfarray.mftype, r_mfype)
var l_mfarray = l_mfarray
if retmftype != l_mfarray.mftype{
l_mfarray = l_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_vs_by_vDSP(l_mfarray, Float.from(r_scalar), vDSP_vsadd)
case .Double:
return biop_vs_by_vDSP(l_mfarray, Double.from(r_scalar), vDSP_vsaddD)
}
}
/**
Element-wise addition of mfarray and scalar
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func add<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let l_mfype = MfType.mftype(value: l_scalar)
let retmftype = MfType.priority(l_mfype, r_mfarray.mftype)
var r_mfarray = r_mfarray
if retmftype != r_mfarray.mftype{
r_mfarray = r_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_vs_by_vDSP(r_mfarray, Float.from(l_scalar), vDSP_vsadd)
case .Double:
return biop_vs_by_vDSP(r_mfarray, Double.from(l_scalar), vDSP_vsaddD)
}
}
/**
Element-wise subtraction right mfarray from left mfarray
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func sub(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let (l_mfarray, r_mfarray, rettype) = biop_broadcast_to(l_mfarray, r_mfarray)
switch MfType.storedType(rettype){
case .Float:
return biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vsub)
case .Double:
return biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vsubD)
}
}
/**
Element-wise subtraction of mfarray and scalar
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func sub<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let r_mfype = MfType.mftype(value: r_scalar)
let retmftype = MfType.priority(l_mfarray.mftype, r_mfype)
var l_mfarray = l_mfarray
if retmftype != l_mfarray.mftype{
l_mfarray = l_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_vs_by_vDSP(l_mfarray, -Float.from(r_scalar), vDSP_vsadd)
case .Double:
return biop_vs_by_vDSP(l_mfarray, -Double.from(r_scalar), vDSP_vsaddD)
}
}
/**
Element-wise subtraction of mfarray and scalar
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func sub<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let l_mfype = MfType.mftype(value: l_scalar)
let retmftype = MfType.priority(l_mfype, r_mfarray.mftype)
var r_mfarray = r_mfarray
if retmftype != r_mfarray.mftype{
r_mfarray = r_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_vs_by_vDSP(-r_mfarray, Float.from(l_scalar), vDSP_vsadd)
case .Double:
return biop_vs_by_vDSP(-r_mfarray, Double.from(l_scalar), vDSP_vsaddD)
}
}
/**
Element-wise multiplication of two mfarray
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func mul(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let (l_mfarray, r_mfarray, rettype) = biop_broadcast_to(l_mfarray, r_mfarray)
switch MfType.storedType(rettype){
case .Float:
return biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vmul)
case .Double:
return biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vmulD)
}
}
/**
Element-wise multiplication of mfarray and scalar
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func mul<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let r_mfype = MfType.mftype(value: r_scalar)
let retmftype = MfType.priority(l_mfarray.mftype, r_mfype)
var l_mfarray = l_mfarray
if retmftype != l_mfarray.mftype{
l_mfarray = l_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_vs_by_vDSP(l_mfarray, Float.from(r_scalar), vDSP_vsmul)
case .Double:
return biop_vs_by_vDSP(l_mfarray, Double.from(r_scalar), vDSP_vsmulD)
}
}
/**
Element-wise multiplication of mfarray and scalar
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func mul<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let l_mfype = MfType.mftype(value: l_scalar)
let retmftype = MfType.priority(l_mfype, r_mfarray.mftype)
var r_mfarray = r_mfarray
if retmftype != r_mfarray.mftype{
r_mfarray = r_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_vs_by_vDSP(r_mfarray, Float.from(l_scalar), vDSP_vsmul)
case .Double:
return biop_vs_by_vDSP(r_mfarray, Double.from(l_scalar), vDSP_vsmulD)
}
}
/**
Element-wise division left mfarray by right mfarray
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func div(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let (l_mfarray, r_mfarray, rettype) = biop_broadcast_to(l_mfarray, r_mfarray)
switch MfType.storedType(rettype){
case .Float:
let ret = biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vdiv)
ret.mfdata._mftype = .Float
return ret
case .Double:
return biop_vv_by_vDSP(l_mfarray, r_mfarray, vDSP_func: vDSP_vdivD)
}
}
/**
Element-wise division of mfarray and scalar
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func div<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let r_mfype = MfType.mftype(value: r_scalar)
let retmftype = MfType.priority(l_mfarray.mftype, r_mfype)
var l_mfarray = l_mfarray
if retmftype != l_mfarray.mftype{
l_mfarray = l_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_vs_by_vDSP(l_mfarray, Float.from(r_scalar), vDSP_vsdiv)
case .Double:
return biop_vs_by_vDSP(l_mfarray, Double.from(r_scalar), vDSP_vsdivD)
}
}
/**
Element-wise division of mfarray and scalar
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func div<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let l_mfype = MfType.mftype(value: l_scalar)
let retmftype = MfType.priority(l_mfype, r_mfarray.mftype)
var r_mfarray = r_mfarray
if retmftype != r_mfarray.mftype{
r_mfarray = r_mfarray.astype(retmftype)
}
switch MfType.storedType(retmftype) {
case .Float:
return biop_sv_by_vDSP(Float.from(l_scalar), r_mfarray, vDSP_svdiv)
case .Double:
return biop_sv_by_vDSP(Double.from(l_scalar), r_mfarray, vDSP_svdivD)
}
}
/**
Matrix multiplication
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func matmul(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
return _matmul_operation(l_mfarray, r_mfarray)
}
/**
Inner product
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func inner(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
return _inner_operation(l_mfarray, r_mfarray)
}
/**
Cross product
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func cross(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
return _cross_operation(l_mfarray, r_mfarray)
}
/**
Check equality in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func equal(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
return _equal_operation(l_mfarray, r_mfarray)
}
/**
Check equality in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func equal<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
return _equal_operation(l_mfarray, Matft.nums(r_scalar, shape: [1]))
}
/**
Check equality in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func equal<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
return _equal_operation(Matft.nums(l_scalar, shape: [1]), r_mfarray)
}
/**
Check NOT equality in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func not_equal(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
return Matft.logical_not(_equal_operation(l_mfarray, r_mfarray))
}
/**
Check equality in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func not_equal<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
return Matft.logical_not(_equal_operation(l_mfarray, Matft.nums(r_scalar, shape: [1])))
}
/**
Check equality in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func not_equal<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
return Matft.logical_not(_equal_operation(Matft.nums(l_scalar, shape: [1]), r_mfarray))
}
/**
Check left mfarray's elements are less than right ones in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func less(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let diff = r_mfarray - l_mfarray
return to_Bool(diff.clip(min: 0, max: nil))
}
/**
Check left mfarray's elements are less than right scalar in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func less<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let diff = r_scalar - l_mfarray
return to_Bool(diff.clip(min: 0, max: nil))
}
/**
Check left scalar is less than right mfarray's elements in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func less<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let diff = r_mfarray - l_scalar
return to_Bool(diff.clip(min: 0, max: nil))
}
/**
Check left mfarray's elements are less equal than right ones in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func less_equal(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let diff = r_mfarray - l_mfarray
return to_Bool(diff.sign() + Float(1))
}
/**
Check left mfarray's elements are less equal than right scalar in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func less_equal<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let diff = r_scalar - l_mfarray
return to_Bool(diff.sign() + Float(1))
}
/**
Check left scalar is less equal than right mfarray's elements in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func less_equal<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let diff = r_mfarray - l_scalar
return to_Bool(diff.sign() + Float(1))
}
/**
Check left mfarray's elements are greater than right ones in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func greater(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let diff = l_mfarray - r_mfarray
return to_Bool(diff.clip(min: 0, max: nil))
}
/**
Check left scalar is greater than right mfarray's elements in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func greater<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let diff = l_mfarray - r_scalar
return to_Bool(diff.clip(min: 0, max: nil))
}
/**
Check left scalar is greater than right mfarray's elements in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func greater<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let diff = l_scalar - r_mfarray
return to_Bool(diff.clip(min: 0, max: nil))
}
/**
Check left mfarray's elements are greater equal than right ones in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func greater_equal(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let diff = l_mfarray - r_mfarray
return to_Bool(diff.sign() + Float(1))
}
/**
Check left scalar is greater equal than right mfarray's elements in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_mfarray: left mfarray
- r_scalar: right scalar conformed to MfTypable
*/
public static func greater_equal<T: MfTypable>(_ l_mfarray: MfArray, _ r_scalar: T) -> MfArray{
let diff = l_mfarray - r_scalar
return to_Bool(diff.sign() + Float(1))
}
/**
Check left scalar is greater equal than right mfarray's elements in element-wise. Returned mfarray's type will be bool.
- parameters:
- l_scalar: left scalar conformed to MfTypable
- r_mfarray: right mfarray
*/
public static func greater_equal<T: MfTypable>(_ l_scalar: T, _ r_mfarray: MfArray) -> MfArray{
let diff = l_scalar - r_mfarray
return to_Bool(diff.sign() + Float(1))
}
/**
Check equality in element-wise, and then when all of elements are true, return true, otherwise false
- parameters:
- l_mfarray: left mfarray
- r_mfarray: right mfarray
*/
public static func allEqual(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> Bool{
return _equalAll_operation(l_mfarray, r_mfarray)
}
}
/*
>>> a = np.arange(100).reshape(10,2,5)
>>> b = np.arange(135).reshape(5,3,9)
>>> np.matmul(a,b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 5)
>>> a = np.arange(100).reshape(10,2,5)
>>> b = np.arange(135).reshape(3,5,9)
>>> np.matmul(a,b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (10,2,5)->(10,newaxis,newaxis) (3,5,9)->(3,newaxis,newaxis) and requested shape (2,9)
For N dimensions it is a sum product over the last axis of a and the second-to-last of b:
>>> a = np.arange(2 * 2 * 4).reshape((2, 2, 4))
>>> b = np.arange(2 * 2 * 4).reshape((2, 4, 2))
>>> np.matmul(a,b).shape
(2, 2, 2)
>>> np.matmul(a, b)[0, 1, 1]
98
>>> sum(a[0, 1, :] * b[0 , :, 1])
98
//nice reference: https://stackoverflow.com/questions/34142485/difference-between-numpy-dot-and-python-3-5-matrix-multiplication
>>>From the above two definitions, you can see the requirements to use those two operations. Assume a.shape=(s1,s2,s3,s4) and b.shape=(t1,t2,t3,t4)
To use dot(a,b) you need
t3=s4;
To use matmul(a,b) you need
t3=s4
t2=s2, or one of t2 and s2 is 1 // <- for broadcast
t1=s1, or one of t1 and s1 is 1 // <- for broadcast
*/
//very dirty code....
fileprivate func _matmul_operation(_ lmfarray: MfArray, _ rmfarray: MfArray) -> MfArray{
precondition(lmfarray.ndim > 1, "cannot get an inverse matrix from 1-d mfarray")
precondition(rmfarray.ndim > 1, "cannot get an inverse matrix from 1-d mfarray")
//preprocessing
//type
var lmfarray = lmfarray
var rmfarray = rmfarray
if lmfarray.mftype != rmfarray.mftype{
let returnedType = MfType.priority(lmfarray.mftype, rmfarray.mftype)
if returnedType != lmfarray.mftype{
lmfarray = lmfarray.astype(returnedType)
}
else{
rmfarray = rmfarray.astype(returnedType)
}
}
// order
// must be row or column major
//let retorder = _matmul_convorder(&lmfarray, &rmfarray)
//broadcast
_matmul_broadcast_to(&lmfarray, &rmfarray)
/*
print(lmfarray.shape, lmfarray.strides)
print(lmfarray.data)
print(rmfarray.shape, rmfarray.strides)
print(rmfarray.data)
print(lmfarray)
print(rmfarray)*/
//run
switch MfType.storedType(lmfarray.mftype) {
case .Float:
return matmul_by_cblas(&lmfarray, &rmfarray, cblas_func: cblas_sgemm)
case .Double:
return matmul_by_cblas(&lmfarray, &rmfarray, cblas_func: cblas_dgemm)
}
}
//Note that this function is slighly different from biobiop_broadcast_to for precondition and checked axis
//TODO: gather this function and biobiop_broadcast_to
fileprivate func _matmul_broadcast_to(_ lmfarray: inout MfArray, _ rmfarray: inout MfArray){
var lshape = lmfarray.shape
var lstrides = lmfarray.strides
var rshape = rmfarray.shape
var rstrides = rmfarray.strides
precondition(lshape[lmfarray.ndim - 1] == rshape[rmfarray.ndim - 2], "Last 2 dimensions of the input mfarray must be lmfarray:(...,l,m) and rmfarray:(...,m,n)")
// broadcast
let retndim: Int
if lmfarray.ndim < rmfarray.ndim{ // l has smaller dim
retndim = rmfarray.ndim
lshape = Array<Int>(repeating: 1, count: rmfarray.ndim - lmfarray.ndim) + lshape // the 1 concatenated elements means broadcastable
lstrides = Array<Int>(repeating: 0, count: rmfarray.ndim - lmfarray.ndim) + lstrides// the 0 concatenated elements means broadcastable
}
else if lmfarray.ndim > rmfarray.ndim{// r has smaller dim
retndim = lmfarray.ndim
rshape = Array<Int>(repeating: 1, count: lmfarray.ndim - rmfarray.ndim) + rshape // the 1 concatenated elements means broadcastable
rstrides = Array<Int>(repeating: 0, count: lmfarray.ndim - rmfarray.ndim) + rstrides// the 0 concatenated elements means broadcastable
}
else{
retndim = lmfarray.ndim
}
let (l_mfstructure, r_mfstructure) = withDummy2ShapeStridesMBPtr(retndim){
l_shapeptr, l_stridesptr, r_shapeptr, r_stridesptr in
//move
lshape.withUnsafeMutableBufferPointer{
l_shapeptr.baseAddress!.moveAssign(from: $0.baseAddress!, count: retndim)
}
lstrides.withUnsafeMutableBufferPointer{
l_stridesptr.baseAddress!.moveAssign(from: $0.baseAddress!, count: retndim)
}
rshape.withUnsafeMutableBufferPointer{
r_shapeptr.baseAddress!.moveAssign(from: $0.baseAddress!, count: retndim)
}
rstrides.withUnsafeMutableBufferPointer{
r_stridesptr.baseAddress!.moveAssign(from: $0.baseAddress!, count: retndim)
}
for axis in (0..<retndim-2).reversed(){
if l_shapeptr[axis] == r_shapeptr[axis]{
continue
}
else if l_shapeptr[axis] == 1{
l_shapeptr[axis] = r_shapeptr[axis] // aligned to r
l_stridesptr[axis] = 0 // broad casted 0
}
else if r_shapeptr[axis] == 1{
r_shapeptr[axis] = l_shapeptr[axis] // aligned to l
r_stridesptr[axis] = 0 // broad casted 0
}
else{
preconditionFailure("Broadcast error: cannot calculate matrix multiplication due to broadcasting error. hint: For all dim < ndim-2, left.shape[dim] or right.shape[dim] is one, or left.shape[dim] == right.shape[dim]")
}
}
}
//print(Array<Int>(UnsafeBufferPointer<Int>(start: l_mfstructure._shape, count: l_mfstructure._ndim)))
//print(Array<Int>(UnsafeBufferPointer<Int>(start: r_mfstructure._shape, count: r_mfstructure._ndim)))
lmfarray = MfArray(base: lmfarray, mfstructure: l_mfstructure, offset: lmfarray.offsetIndex)
rmfarray = MfArray(base: rmfarray, mfstructure: r_mfstructure, offset: rmfarray.offsetIndex)
}
fileprivate func _cross_operation(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
var (l_mfarray, r_mfarray, rettype) = biop_broadcast_to(l_mfarray, r_mfarray)
let orig_shape_for3d = l_mfarray.shape
let lastdim = orig_shape_for3d[l_mfarray.ndim - 1]
//convert shape to calculate
l_mfarray = l_mfarray.reshape([-1, lastdim])
r_mfarray = r_mfarray.reshape([-1, lastdim])
if lastdim == 2{
let ret = l_mfarray[0~<,0] * r_mfarray[0~<,1] - l_mfarray[0~<,1]*r_mfarray[0~<,0]
return ret
}
else if lastdim == 3{
let ret = Matft.nums(0, shape: [l_mfarray.shape[0], lastdim], mftype: rettype)
ret[0~<,0] = l_mfarray[0~<,1] * r_mfarray[0~<,2] - l_mfarray[0~<,2]*r_mfarray[0~<,1]
ret[0~<,1] = l_mfarray[0~<,2] * r_mfarray[0~<,0] - l_mfarray[0~<,0]*r_mfarray[0~<,2]
ret[0~<,2] = l_mfarray[0~<,0] * r_mfarray[0~<,1] - l_mfarray[0~<,1]*r_mfarray[0~<,0]
return ret.reshape(orig_shape_for3d)
}
else{
preconditionFailure("Last dimension must be 2 or 3")
}
}
//uncompleted
fileprivate func _inner_operation(_ l_mfarray: MfArray, _ r_mfarray: MfArray) -> MfArray{
let lastdim = l_mfarray.shape[l_mfarray.ndim - 1]
precondition(lastdim == r_mfarray.shape[r_mfarray.ndim - 1], "Last dimension must be same")
let retShape = Array(l_mfarray.shape.prefix(l_mfarray.ndim - 1) + r_mfarray.shape.prefix(r_mfarray.ndim - 1))
let rettype = l_mfarray.mftype
//convert shape to calculate
let l_mfarray = l_mfarray.reshape([-1, lastdim])
let l_calcsize = l_mfarray.shape[0]
let r_mfarray = r_mfarray.reshape([-1, lastdim])
let r_calcsize = r_mfarray.shape[0]
let ret = Matft.nums(0, shape: [l_calcsize*r_calcsize], mftype: rettype)
for lind in 0..<l_calcsize{
for rind in 0..<r_calcsize{
ret[lind*r_calcsize + rind] = (l_mfarray[lind] * r_mfarray[rind]).sum()
}
}
return ret.reshape(retShape.count != 0 ? retShape : [1])
}
fileprivate func _equal_operation(_ l_mfarray: MfArray, _ r_mfarray: MfArray, thresholdF: Float = 1e-5, thresholdD: Double = 1e-10) -> MfArray{
let diff = l_mfarray - r_mfarray
//print(diff)
/*
let diff = l_mfarray - r_mfarray
print(diff)
diff.withDataUnsafeMRPtr{
dataptr in
var bytes = UnsafeMutableRawBufferPointer(start: dataptr, count: diff.storedByteSize).map{ ~<$0 }
bytes.withUnsafeMutableBufferPointer{
dataptr.copyMemory(from: $0.baseAddress!, byteCount: diff.storedByteSize)
}
}
print(diff)*/
return !to_Bool(diff, thresholdF: thresholdF, thresholdD: thresholdD)
}
fileprivate func _equalAll_operation(_ l_mfarray: MfArray, _ r_mfarray: MfArray, thresholdF: Float = 1e-5, thresholdD: Double = 1e-10) -> Bool{
//print(diff)
if l_mfarray.shape != r_mfarray.shape{
return false
}
let diff = l_mfarray - r_mfarray
// diff must be 0 if all of elements are same
switch diff.storedType {
case .Float:
if let data = diff.data as? [UInt8]{
return data.allSatisfy{ $0 == UInt8.zero }
}
else if let data = diff.data as? [UInt16]{
return data.allSatisfy{ $0 == UInt8.zero }
}
else if let data = diff.data as? [UInt32]{
return data.allSatisfy{ $0 == UInt32.zero }
}
else if let data = diff.data as? [UInt64]{
return data.allSatisfy{ $0 == UInt64.zero }
}
else if let data = diff.data as? [UInt]{
return data.allSatisfy{ $0 == UInt.zero }
}
else if let data = diff.data as? [Int8]{
return data.allSatisfy{ $0 == Int8.zero }
}
else if let data = diff.data as? [Int16]{
return data.allSatisfy{ $0 == Int16.zero }
}
else if let data = diff.data as? [Int32]{
return data.allSatisfy{ $0 == Int32.zero }
}
else if let data = diff.data as? [Int64]{
return data.allSatisfy{ $0 == Int64.zero }
}
else if let data = diff.data as? [Int]{
return data.allSatisfy{ $0 == Int.zero }
}
else if let data = diff.data as? [Float]{
return data.allSatisfy{ abs($0) <= thresholdF }
}
else{
// bool
guard let data = diff.astype(.Float).data as? [Float] else{
return false
}
return data.allSatisfy{ $0 == Float.zero }
}
case .Double:
if let data = diff.data as? [Double]{
return data.allSatisfy{ abs($0) <= thresholdD }
}
else{
return false
}
}
}