You can pass MfType as MfArray’s argument mftype: .Hoge . It is similar to dtype.
※Note that stored data type will be Float or Double only even if you set MfType.Int.
So, if you input big number to MfArray, it may be cause to overflow or strange results in any calculation (+, -, *, /,… etc.). But I believe this is not problem in practical use.
MfType’s list is below
public enum MfType: Int{
case None // Unsupportted
case Bool
case UInt8
case UInt16
case UInt32
case UInt64
case UInt
case Int8
case Int16
case Int32
case Int64
case Int
case Float
case Double
case Object // Unsupported
}
Below is Matft’s function list. As I mentioned above, almost functions are similar to Numpy. Also, these function use Accelerate framework inside, the perfomance may keep high.
* means method function exists too. Shortly, you can use a.shallowcopy() where a is MfArray.
^ means method function only. Shortly, you can use a.tolist()notMatft.tolist where a is MfArray.
Matft supports only natural cubic spline. I’ll implement other boundary condition later.
Matft
Numpy
Matft.interp1d.cubicSpline
scipy.interpolation.CubicSpline
Image
Matft
Numpy
Matft.image.cgimage2mfarray
N/A
Matft.image.mfarray2cgimage
N/A
Matft
OpenCV
Matft.image.color
cv2.cvtColor
Matft.image.resize
cv2.resize
Matft.image.warpAffine
cv2.warpAffine
Performance
I use Accelerate framework, so all of MfArray operation may keep high performance.
let a = Matft.arange(start: 0, to: 10*10*10*10*10*10, by: 1, shape: [10,10,10,10,10,10])
let aneg = Matft.arange(start: 0, to: -10*10*10*10*10*10, by: -1, shape: [10,10,10,10,10,10])
let aT = a.T
let b = a.transpose(axes: [0,3,4,2,1,5])
let c = a.transpose(axes: [1,2,3,4,5,0])
let posb = a > 0
import numpy as np
a = np.arange(10**6).reshape((10,10,10,10,10,10))
aneg = np.arange(0, -10**6, -1).reshape((10,10,10,10,10,10))
aT = a.T
b = a.transpose((0,3,4,2,1,5))
c = a.transpose((1,2,3,4,5,0))
posb = a > 0
Arithmetic test
Matft
time
Numpy
time
let _ = a+aneg
596μs
a+aneg
1.04ms
let _ = b+aT
4.46ms
b+aT
4.31ms
let _ = c+aT
5.31ms
c+aT
2.92ms
Math test
Matft
time
Numpy
time
let _ = Matft.math.sin(a)
2.14ms
np.sin(a)
14.7ms
let _ = Matft.math.sin(b)
7.02ms
np.sin(b)
15.8ms
let _ = Matft.math.sign(a)
3.09ms
np.sign(a)
1.37ms
let _ = Matft.math.sign(b)
8.33ms
np.sign(b)
1.42ms
Bool test
Matft
time
Numpy
time
let _ = a > 0
4.63ms
a > 0
855μs
let _ = a > b
17.8ms
a > b
1.83ms
let _ = a === 0
4.65ms
a == 0
603μs
let _ = a === b
19.7ms
a == b
1.78ms
Indexing test
Matft
time
Numpy
time
let _ = a[posb]
1.21ms
a[posb]
1.29ms
Matft achieved almost same performance as Numpy!!!
※Swift’s performance test was conducted in release mode
However, as you can see the above table, Matft’s boolean operation is toooooooo slow…(Issue #18)
So, a pull request is very welcome!!
Installation
SwiftPM
Import
Project > Build Setting > +
Select Rules
Update
File >Swift Packages >Update to Latest Package versions
Important!!! the below installation is outdated. Please install Matft via swiftPM!!!
Matft
Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy.
INFO: Support Complex!!
Note: You can use [Protocol version(beta version)](https://github.com/jjjkkkjjj/Matft/tree/protocol) too.Feature & Usage
Many types
Pretty print
Indexing
Slicing
View
Conversion
Univarsal function reduction
Mathematic
Complex
Image Conversion
…etc.
See Function List for all functions.
Declaration
MfArray
The MfArray such like a numpy.ndarray
MfType
You can pass MfType as MfArray’s argument
mftype: .Hoge
. It is similar todtype
.※Note that stored data type will be Float or Double only even if you set MfType.Int. So, if you input big number to MfArray, it may be cause to overflow or strange results in any calculation (+, -, *, /,… etc.). But I believe this is not problem in practical use.
MfType’s list is below
Also, you can convert MfType easily using
astype
Subscription
MfSlice
You can set MfSlice (see below’s list) to subscript.
(Positive) Indexing
Normal indexing
Slicing
If you replace
:
with~<
, you can get sliced mfarray. Note that usea[0~<]
instead ofa[:]
to get all elements along axis.Negative Indexing
Negative indexing is also available That’s implementation was hardest for me…
Boolean Indexing
You can use boolean indexing.
Caution! I don’t check performance, so this boolean indexing may be slowUnfortunately, Matft is too slower than numpy…
(numpy is 1ms, Matft is 7ms…)
Fancy Indexing
You can use fancy indexing!!!
View
Note that returned subscripted mfarray will have
base
property (is similar toview
in Numpy). See numpy doc in detail.Complex
Matft supports Complex!!
But this is beta version. so, any bug may be ocurred.
Please report me by issue! (Progres
TODO
sin,cos,tan,exp,log
)Image
You can acheive an image processing by Matft! (Beta version) Please refer to the example here.
For more complex conversion, see OpenCV code.
Function List
Below is Matft’s function list. As I mentioned above, almost functions are similar to Numpy. Also, these function use Accelerate framework inside, the perfomance may keep high.
*
means method function exists too. Shortly, you can usea.shallowcopy()
wherea
isMfArray
.^
means method function only. Shortly, you can usea.tolist()
notMatft.tolist
wherea
isMfArray
.#
means support complex operationOperation
Line 2 is infix (prefix) operator.
+
+
-
-
/
.
*
*
*+
n/a
*^
n/a
*&
@
===
==
!==
!=
<
<
<=
<=
>
>
>=
>=
==
n/a
-
-
e.g.) Matft.ufuncReduce(a, Matft.add)
e.g.) numpy.add.reduce(a)
e.g.) Matft.ufuncAccumulate(a, Matft.add)
e.g.) numpy.add.accumulate(a)
Matft supports only natural cubic spline. I’ll implement other boundary condition later.
Performance
I use
Accelerate
framework, so all of MfArray operation may keep high performance.let _ = a+aneg
596μs
a+aneg
1.04ms
let _ = b+aT
4.46ms
b+aT
4.31ms
let _ = c+aT
5.31ms
c+aT
2.92ms
let _ = Matft.math.sin(a)
2.14ms
np.sin(a)
14.7ms
let _ = Matft.math.sin(b)
7.02ms
np.sin(b)
15.8ms
let _ = Matft.math.sign(a)
3.09ms
np.sign(a)
1.37ms
let _ = Matft.math.sign(b)
8.33ms
np.sign(b)
1.42ms
let _ = a > 0
4.63ms
a > 0
855μs
let _ = a > b
17.8ms
a > b
1.83ms
let _ = a === 0
4.65ms
a == 0
603μs
let _ = a === b
19.7ms
a == b
1.78ms
let _ = a[posb]
1.21ms
a[posb]
1.29ms
Matft achieved almost same performance as Numpy!!!
※Swift’s performance test was conducted in release mode
However, as you can see the above table, Matft’s boolean operation is toooooooo slow…(Issue #18)
So, a pull request is very welcome!!
Installation
SwiftPM
Important!!! the below installation is outdated. Please install Matft via swiftPM!!!
Carthage
Set Cartfile
Import Matft.framework made by above process to your project
CocoaPods
Create Podfile (Skip if you have already done)
Write
pod 'Matft'
in Podfile such like belowInstall Matft
Contact
Feel free to ask this project or anything via junnosuke.kado.git@gmail.com