Skip to content

Instantly share code, notes, and snippets.

@gradyzhuo
Created September 6, 2015 05:13
Show Gist options
  • Save gradyzhuo/1467571cd0227704726c to your computer and use it in GitHub Desktop.
Save gradyzhuo/1467571cd0227704726c to your computer and use it in GitHub Desktop.
MatrixDemoByBLAS - Accelerate Framework
//
// main.m
// Matrix
//
// Created by Grady Zhuo on 2015/9/6.
// Copyright © 2015年 Grady Zhuo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Accelerate/Accelerate.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
/*
* 求解聯立方程組:
x + 2y = 6
3 + 4y = 10
| 1 2 | | x | | 6 |
=> | | | | = | |
| 3 4 | | y | | 10 |
寫成 擴充矩陣
| 1 2 | 6 |
=> | | |
| 3 4 | 10 |
| 1 0 | -2 |
欲求解 => | | |
| 0 1 | 4 |
*/
/* buffer
* | 1 2 |
* | |
* | 3 4 |
*/
double buffer[] = { 1.0, 2.0, 3.0, 4.0 };
la_object_t matrix = la_matrix_from_double_buffer(buffer, 2, 2, 2, LA_NO_HINT, LA_DEFAULT_ATTRIBUTES);
/* buffer
* | 6 |
* | |
* | 10 |
*/
double buffer2[] = { 6, 10};
la_object_t vector = la_matrix_from_double_buffer(buffer2, 2, 1, 1, LA_NO_HINT, LA_DEFAULT_ATTRIBUTES);
/* solvedMatrix
* |-2 |
* | |
* | 4 |
*/
//解出來的聯立解
la_object_t solvedMatrix = la_solve(matrix, vector);
double solvedBuffer[] = { 0, 0};
//從Matrix物件 轉回 double[]
la_status_t status = la_matrix_to_double_buffer(solvedBuffer, 1, solvedMatrix);
if (status == LA_SUCCESS) {
NSLog(@"success: x:%lf, y:%lf", solvedBuffer[0], solvedBuffer[1]);
}else{
NSLog(@"Wrong");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment