alexandertar / laswift Goto Github PK
View Code? Open in Web Editor NEWLinear Algebra library for Swift language
License: BSD 3-Clause "New" or "Revised" License
Linear Algebra library for Swift language
License: BSD 3-Clause "New" or "Revised" License
I notice that matrixMatrixOperation
has precondition(A.rows == B.rows && A.cols == B.cols, "Matrices must be of same dimensions")
. However, when multiplying matricies all we need is A.cols == B.rows
. Are there any plans to support uneven matrix multiplication?
Hi,
as in tittle:
"Cannot subscript a value of type 'Matrix' with an argument of type '(ClosedRange, ClosedRange)'"
I've tried doing something simple as
let P = zeros(15,15)
P[0..<3, 0..<3] = eye(3,3)
Are you planning on adding Linux support or it's just focused on pure macOS ecosystem?
In my understanding the eigenvectors are in the columns of V and not as stated here "by rows"
/// Compute eigen values and vectors of a given square matrix.
///
/// A precondition error is thrown if the algorithm fails to converge.
///
/// - Parameters:
/// - A: square matrix to calculate eigen values and vectors of
/// - Returns: eigenvectors matrix (by rows) and diagonal matrix with eigenvalues on the main diagonal
public func eig(_ A: Matrix) -> (V: Matrix, D: Matrix) {
I would rather return the right eigenvectors returned in vr. Most commonly right eigenvectors are used in physics and engineering when nothing in particular is mentioned. This likely would also avoid the transposition.
I need to calculate a matrix of pairwise distances given a set of 1D vectors. It's L2/Euclidean distance.
Can I do better with LASwift than this?
for i in 0..<embeddings.rows {
for j in 0..<embeddings.rows {
let diff: Vector = embeddings[row:j] - embeddings[row:i]
let distance_i_to_j = (sum(diff .^ 2)).squareRoot()
}
}
I have tried to count eigenvalues and eigenvectors of this simple matrix
after that i noticed that both values and vectors are wrong
you can check it in this article
http://lpsa.swarthmore.edu/MtrxVibe/EigMat/MatrixEigen.html
i have tried to count that by my own and same result as with mathlab
you are using dsyevd_ function but i tried with dgeev_ and its work great
any ideas?
Currently I'm calculating determinants by multiplying all eigenvalues returned from the eig
function. However, this method is imprecise for some matrices, specially rather large ones.
A more robust and idiomatic way to calculate the matrix determinants would be by calling LAPACK's dgetrf function, which performs LU factorisation, which can be utilised to compute the determinant as such.
After adding LASwift to my cartfile, only an iOS version of LASwift gets built. When telling Carthage to build for Mac OS, it says the project doesn't support that platform.
after migrating to swift 4 and changing build settings to recommended, occurred an error in inv(_ A: Matrix)
found a solution, works for inv() but we need better solution
https://stackoverflow.com/questions/47114737/overlapping-accesses-pointer
In the file Matrix.swift:
402 let srcPtr = srcBuf.baseAddress! + row.lowerBound * rows + col.lowerBound
417 let dstPtr = dstBuf.baseAddress! + row.lowerBound * rows + col.lowerBound
should be:
402 let srcPtr = srcBuf.baseAddress! + row.lowerBound * cols + col.lowerBound
417 let dstPtr = dstBuf.baseAddress! + row.lowerBound * cols + col.lowerBound
Hi,
I'm trying to convert a layer calculation from Python to Swift using your library. But I think there is a calculation problem in your solution for singular value decomposition.
This is an example matrix I'm getting on first calculation loop. Both python and Swift response with the same matrix:
[
[ 6.5 -0.5 0. 0.5 -6.5 ],
[-0.5 7.25 6.25 5.25 0.5 ],
[ 0. 6.25 6.25 6.25 0. ],
[ 0.5 5.25 6.25 7.25 -0.5 ],
[-6.5 0.5 0. -0.5 6.5 ]
]
With numpy I'm calculating svd as:
u, d, v_t = np.linalg.svd( cov_matrix, full_matrices=True )
For LASwift library I'm simply calling svd function as there are no arguments for the method. Numpy returns the response as u, d, v_t
, but LASwift, even though the namings are the same returns a response as v, d, u
.
Most important is, the response starts to differ after a few rows and/or colums.
Last two columns are different.
0.00000, -0.70425, 0.06350, 0.24434, 0.66355
-0.57735, 0.06350, 0.70425, -0.38310, 0.14107
-0.57735, -0.00000, 0.00000, 0.76620, -0.28214
-0.57735, -0.06350, -0.70425, -0.38310, 0.14107
-0.00000, 0.70425, -0.06350, 0.24434, 0.66355
0.00000, -0.70425, 0.06350, 0.44473, -0.54974
-0.57735, 0.06350, 0.70425, 0.31739, 0.25677
-0.57735, -0.00000, -0.00000, -0.63478, -0.51353
-0.57735, -0.06350, -0.70425, 0.31739, 0.25677
0.00000, 0.70425, -0.06350, 0.44473, -0.54974
Same, but needs a collapse (not important).
18.75000, 13.09017, 1.90983, 0.00000, 0.00000
18.75000, 0.00000, 0.00000, 0.00000, 0.00000
0.00000, 13.09017, 0.00000, 0.00000, 0.00000
0.00000, 0.00000, 1.90983, 0.00000, 0.00000
0.00000, 0.00000, 0.00000, 0.00000, 0.00000
0.00000, 0.00000, 0.00000, 0.00000, 0.00000
Last two rows are different.
0.00000, -0.57735, -0.57735, -0.57735, 0.00000
-0.70425, 0.06350, -0.00000, -0.06350, 0.70425
0.06350, 0.70425, 0.00000, -0.70425, -0.06350
0.54102, -0.26286, 0.52573, -0.26286, 0.54102
0.45529, 0.31236, -0.62472, 0.31236, 0.45529
0.00000, -0.57735, -0.57735, -0.57735, -0.00000
-0.70425, 0.06350, -0.00000, -0.06350, 0.70425
0.06350, 0.70425, 0.00000, -0.70425, -0.06350
-0.13761, -0.40044, 0.80089, -0.40044, -0.13761
0.69359, -0.07945, 0.15889, -0.07945, 0.69359
Why the response changes and is there a way to solve this?
Suggesting enhancement to Matrix.swift. All changes are modeled after NumPy functionality but adapted to Swift. Added zeros(like:), ones(like:), .T for transpose, hstack() and vstack(). I haven't been able to get the LASwift Xcode project completely working on my system so I do not feel comfortable changing GitHub sources myself.
Someone should probably review where I implemented ".T". I debated if it would be better to put in an extension but decided it would be better to put the property in the main Matrix class.
The Pods and test cases is one area I am having problems with on my system so I have not provided test cases although I have tested my implementations with print statements and manually verified.
Enclosed updated Matrix.swift and differences (diff -u) Matrix.swift.diff.
Matrix.swift.diff.zip
Matrix.swift.zip
The current versions of Nimble and Quick do not match between the Example/Podfile
and Package.swift
.
Can you please update the version in the Swift Package as well?
It causes dependency version conflicts in my project and Swift Package Manager is very unforgiving, when it comes to that.
I created a func lstsq() for LASwift to compute the least squares solution to an overdetermined linear system A*X=B. This is roughly equivalent to the NumPy function with the same name. My implementation uses Accelerate framework LAPACK dgels(). I put lstsq() in a new file MatrixLeastSquare.swift but it could probably put in MatrixAlgebra.swift since it is similar to svd(). I felt more comfortable having it in its own file so I could be more verbose with comments. Note that this implementation depends on a transpose shortcut (.T) that I submitted in a separate enhancement suggestion.
Enclosed are the sources MatrixLeastSquare.swift, lstsq_test.swift (a documented test case) and numpy_lstsq_example3.py (roughly equivalent to lstsq_test.swift for verification).
MatrixLeastSquare.swift.zip
lstsq_test.swift.zip
numpy_lstsq_example3.py.zip
Is there the possibility to include the Covariance matrix calculation? I have seen in this other library but it is not updated to swift 4?
CAUTION. This is an obvious problem and others apparently do not have it so it might be due to something in my environment or something I have done wrong.
When I try compile I get the error
./MatrixExponent.swift:39:13: Operator implementation without matching operator declaration
In ./Operators.swift I do not see a definition for ".^" nor have I found one elsewhere. I solved my problem by making the following changes in ./Operators.swift
diff --git a/LASwift/Operators.swift b/LASwift/Operators.swift
index 71630e2..34cf140 100755
--- a/LASwift/Operators.swift
+++ b/LASwift/Operators.swift
@@ -6,6 +6,13 @@
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
+precedencegroup ExponentiationPrecedence {
+ associativity: right
+ higherThan: MultiplicationPrecedence
+}
+
+infix operator .^ : ExponentiationPrecedence
+
infix operator .* : MultiplicationPrecedence
infix operator ./ : MultiplicationPrecedence
infix operator ./. : MultiplicationPrecedence
I am using macOS 10.13.6, Xcode 10.1 with Swift 4.2. Yes I know it is a little old but my Power Mac doesn't support newer macOS. I am building for a macOS (x86_64) command line app. Because I didn't want to deal with the pods and test cases I have copied the sources into my own new Xcode project. Once I made this change it compiled and seemed to work well. I really like LASwift.
Why am I seeing this and no one else? Did I loose something by creating a new project? Do other versions of Swift either have the operator defined, have a default, or don't care?
Hi,
as in tittle, I've tried:
let P = zeros(15,15)
P[0,0] = eye(3,3)
and I've got this error:
"Cannot assign value of type 'Matrix' to subscript of type 'Double'"
I am not sure if this is aligned with the package purposes, but it would be helpful if there are rank and unique method to see if a matrix is collinear or not.
I assume this type of checks will be helpful for SVD.
something that looks like
LASwift.unique(matrix)
LASwift.unique.rank(matrix)
Hi, there.
I've started using your library, and thanks for this great tool.
I am trying to get a dot product of two Matrix objects, but could not find a way to accomplish this without writing custom code.
Is there a possible solution in the library that I'm missing?
Thanks again.
After I install LASwift with Cocoapod
pod 'LASwift', '~> 0.1.0'
There are compiling error in MatrixAlgebra.swift
within function inv
. The error info is
Overlapping accesses to 'N', but modification requires exclusive access; consider copying to a local variable
The lines with errors are:
dgetrf_(&N, &N, &(B.flat), &N, &pivot, &error)
// ...
dgetri_(&N, &(B.flat), &N, &pivot, &wkOpt, &lWork, &error)
// ...
dgetri_(&N, &(B.flat), &N, &pivot, &work, &lWork, &error)
I find out that all these functions use &N
more than ones, so I change the function into this:
public func inv(_ A: Matrix) -> Matrix {
precondition(A.rows == A.cols, "Matrix dimensions must agree")
let B = Matrix(A)
var N = __CLPK_integer(A.rows)
var M = __CLPK_integer(A.cols)
var LDA = N
var pivot = [__CLPK_integer](repeating: 0, count: Int(N))
var wkOpt = __CLPK_doublereal(0.0)
var lWork = __CLPK_integer(-1)
var error: __CLPK_integer = 0
dgetrf_(&N, &M, &(B.flat), &LDA, &pivot, &error)
precondition(error == 0, "Matrix is non invertible")
/* Query and allocate the optimal workspace */
dgetri_(&N, &(B.flat), &M, &pivot, &wkOpt, &lWork, &error)
lWork = __CLPK_integer(wkOpt)
var work = Vector(repeating: 0.0, count: Int(lWork))
/* Compute inversed matrix */
dgetri_(&N, &(B.flat), &M, &pivot, &work, &lWork, &error)
precondition(error == 0, "Matrix is non invertible")
return B
}
Then all errors are gone.
This library still works but when compiled for macOS 13.3 or later generates lots of deprecation warnings. I am compiling for macOS 14.2 and see warnings similar to:
'dgetrf_' was deprecated in macOS 13.3: The CLAPACK interface is deprecated. Please compile with -DACCELERATE_NEW_LAPACK to access the new lapack headers.
The current Accelerate/BLAS help says
For Swift projects, specify ACCELERATE_NEW_LAPACK=1 and ACCELERATE_LAPACK_ILP64=1 as preprocessor macros in Xcode build settings.
When I did that all occurrences of __CLPK_int
and __CLPK_doublereal
generate undefined errors. The current web and Xcode 15.1 documentation seems broken with respect to LAPACK documentation so I could not find how the new LAPACK defines things. I did find in Apple's Solving systems of linear equations with LAPACK sample code uses __LAPACK_int
. I assume this is the equivalent to __CLPK_int
. I didn't find an equivalent to __CLPK_doublereal
. Changing all __CLPK_int
to __LAPACK_int
and __CLPK_doublereal
to Double
compiled without errors and a simple test case produced the expected results. To confirm this more testing is needed.
I am not sure of the best way to solve in the long term. Maybe make new typealias that selects the appropriate alias for OS version? I want to think about this more before making any changes. Hoping to see Apple improve the LAPACK documentation so we can make more informed decisions. No urgency for a fix but sometime in the future it will break.
A related annoyance is I haven't found a way to turn off deprecation warnings in Xcode 15.1. The deprecation warnings generate a lot of clutter that could obscure more important warnings.
Looking to use asin or atan (to calculate distance between points on the sphere).
I don't see anything I could use in LASwift. Not sure if there is any advantage of having this in LASwift vs using Swift's own. Accelerate framework appears to have asin, for example: https://developer.apple.com/documentation/accelerate/asin
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.