Matrix and vector operations in IOS and IOS

Source: Internet
Author: User

Matrix and vector operations in IOS and IOS

Add the import file Accelerate. framework for the project

In the swift file to be calculated

import Accelerate
1. Vector and constant operations

Function Form

vDSP_vs***D(vector, 1, &scalar, &result, 1, length_of_vector)

Here, 1 represents the operation on all vector elements. If it is 2, the operation is performed at intervals. The vast majority of cases are 1.

Specific instance

1. Adding vectors and constants

<pre name="code" class="plain">var v = [4.0, 5.0]var s = 3.0var vsresult= [Double](count : v.count, repeatedValue : 0.0)vDSP_vsaddD(v, 1, &s, &vsresult, 1, vDSP_Length(v.count))vsresult    // returns [7.0, 8.0]

 
2. Multiplication of vectors and constants 

vDSP_vsmulD(v, 1, &s, &vsresult, 1, vDSP_Length(v.count))vsresult    // returns [12.0, 15.0]

3. Division of vectors and constants

vDSP_vsdivD(v, 1, &s, &vsresult, 1, vDSP_Length(v.count))vsresult    // returns [1.333333333333333, 1.666666666666667]


2. calculation between vectors

vDSP_v***D(vector_1, 1, vector_2, 1, &result, 1, length_of_vector)
1 is the same as in 1, which indicates that each element is operated.

1. Adding Vectors

var v1 = [1.0, 2.0]var v2 = [3.0, 4.0]var vvresult = [Double](count : 2, repeatedValue : 0.0)vDSP_vaddD(v1, 1, v2, 1, &vvresult, 1, vDSP_Length(v1.count))vvresult    // returns [4.0, 6.0]

2. Multiplication of Vectors

vDSP_vmulD(v1, 1, v2, 1, &vvresult, 1, vDSP_Length(v1.count))vvresult    // returns [3.0, 8.0]

3. Division of Vectors

vDSP_vdivD(v1, 1, v2, 1, &vvresult, 1, vDSP_Length(v1.count))vvresult    // returns [3.0, 2.0]
4. Vector point Multiplication

var v3 = [1.0, 2.0]var v4 = [3.0, 4.0]var dpresult = 0.0vDSP_dotprD(v3, 1, v4, 1, &dpresult, vDSP_Length(v3.count))dpresult    // returns 11.0

Iii. Matrix Operations

Since this library uses a one-dimensional array for matrix operations, addition and subtraction operations are the same as vectors.

1. Matrix Multiplication

vDSP_mmulD(matrix_1, 1, matrix_2, 1, &result, 1,                      rows_of_matrix_1, columns_of_matrix_2,                      columns_of_matrix_1_or_rows_of_matrix_2)
Note: because the two matrices are multiplied, the number of columns in the previous matrix must be equal to the number of rows in the next matrix.

var m1 = [ 3.0, 2.0, 4.0, 5.0, 6.0, 7.0 ]var m2 = [ 10.0, 20.0, 30.0, 30.0, 40.0, 50.0]var mresult = [Double](count : 9, repeatedValue : 0.0)vDSP_mmulD(m1, 1, m2, 1, &mresult, 1, 3, 3, 2)mresult    // returns [90.0, 140.0, 190.0, 280.0, 370.0, 270.0, 400.0, 530.0]

2. Inverse Matrix

func invert(matrix : [Double]) -> [Double] {    var inMatrix = matrix    var pivot : __CLPK_integer = 0    var workspace = 0.0    var error : __CLPK_integer = 0    var N = __CLPK_integer(sqrt(Double(matrix.count)))    dgetrf_(&N, &N, &inMatrix, &N, &pivot, &error)    if error != 0 {        return inMatrix    }    dgetri_(&N, &inMatrix, &N, &pivot, &workspace, &N, &error)    return inMatrix}<pre name="code" class="plain">var m = [1.0, 2.0, 3.0, 4.0]invert(m)    // returns [-2.0, 1.0, 1.5, -0.5]

 

3. Matrix transpose

vDSP_mtransD(matrix, 1, &result, 1, number_of_rows_of_result, number_of_columns_of_result)
Example

var t = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]var mtresult = [Double](count : 6, repeatedValue : 0.0)vDSP_mtransD(t, 1, &mtresult, 1, 3, 2)mtresult    // returns [1.0, 4.0, 2.0, 5.0, 3.0, 6.0]


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.