sumit
6/1/2019 - 6:52 AM

simd. A module for performing computations on small vectors and matrices.

import simd // importing module

let arr = [2.0, 2.0]
// initializing simd
let vector = simd_double2(x: arr[0], y:arr[1]) // initializing
let normalized_vector = simd_normalize(vector) // normalized vector
let gfd : Double = normalized_vector.x // fetching values

import simd // importing module

let arr = [2.0, 2.0]
// initializing simd
let vector = simd_double2(x: arr[0], y:arr[1]) // initializing
let normalized_vector = simd_normalize(vector) // normalized vector
let gfd : Double = normalized_vector.x // fetching values
let transposed_matrix = simd_double3x3(rows: rows).transpose //transpose of matrix

//*** Multiply vectors -----------
import simd
let rows = [
    simd_double3(2,      0, 0),
    simd_double3(     1, 2, 0),
    simd_double3(     0,      0, 1)
]
let RotationMatrix = simd_double3x3(rows: rows)
let positionVector = simd_double3(x: 3, y: 2, z: 1)
print(RotationMatrix)
print(RotationMatrix*positionVector)
//------------------------------***

//*** Cross multiplication of vectors---------
let two = simd_double3(x: 3, y: 2, z: 12)
let three = simd_double3(x: 14, y: 22, z: 10)
print(cross(two, three))
//-----------------------------------------***

//*** converting matrix into string----------
let RotationMatrix = simd_double3x3(rows: rows)
var rotationMatrix_string = ""
for i in 0...2{
    for col in [RotationMatrix.columns.0, RotationMatrix.columns.1, RotationMatrix.columns.2]
    {
        rotationMatrix_string += String(col[i]) + ", "
    }
    rotationMatrix_string += "\n"
}

rotationMatrix_string
print(rotationMatrix_string)
//---------------------------------------*****


//finding angle between two vectors
import simd
var vector1 = float3(0.0, 1.0, 0.0) // VECTOR 1
var vector2 = float3(1.0, 1.0, 0.0) // VECTOR 2
var dotProduct = dot(vector1, vector2)
var a = dotProduct / (length(vector1) * length(vector2))
a = Float(round(1000*a)/1000)
let angle = acos(a)*(360.0/3.14) // angle is in degrees

//