Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : ORIX Sheet10 * File : Packages/SheetAnalytics/Sources/CAccelShim/shim.c11 * Module : CAccelShim12 * Repository : https://git.spboucher.ai/orix-sheet13 * Created : 2026-11-2114 * Last Modified : 2026-11-2115 *16 * =============================================================================17 * AUTHOR & PROFESSIONAL AFFILIATION18 * =============================================================================19 *20 * Author : Simon-Pierre Boucher21 * Position : Professor of Finance22 * Institution : Université du Québec en Outaouais (UQO)23 * Department : Département des sciences administratives24 * Organization : SP Boucher25 *26 * Website : https://spboucher.ai27 * Git : https://git.spboucher.ai28 *29 * =============================================================================30 * OFFICIAL CONTACT DIRECTORY31 * =============================================================================32 *33 * General : info@spboucher.ai34 * Professional : contact@spboucher.ai35 * Direct : simon@spboucher.ai36 * Research : research@spboucher.ai37 * Development : dev@spboucher.ai38 * Support : support@spboucher.ai39 * Security/Admin : admin@spboucher.ai40 * Notifications : notifications@spboucher.ai41 *42 * Preferred Contact: support@spboucher.ai43 *44 * =============================================================================45 * COPYRIGHT & INTELLECTUAL PROPERTY46 * =============================================================================47 *48 * Copyright (c) 202649 * Simon-Pierre Boucher50 *51 * All rights reserved where applicable.52 *53 * This file is governed by the applicable project license,54 * copyright notices, institutional agreements and relevant law.55 *56 * Unless expressly authorized by the applicable license or57 * rights holder, copying, redistribution, sublicensing,58 * publication and commercial exploitation are not permitted.59 *60 * Existing third-party rights and notices remain unaffected.61 *62 * =============================================================================63 * CONFIDENTIALITY & SECURITY64 * =============================================================================65 *66 * Classification : Public67 * Distribution : Public repository; use is governed by the project license68 * Security Contact : admin@spboucher.ai69 *70 * Access to this source does not independently grant permission71 * to redistribute or disclose confidential materials.72 *73 * Do not embed credentials, private keys, access tokens,74 * certificates or confidential personal information.75 *76 * =============================================================================77 * LICENSE & MAINTENANCE78 * =============================================================================79 *80 * License : None specified (no license file in this repository)81 * Maintainer : Simon-Pierre Boucher82 * Technical Contact: dev@spboucher.ai83 *84 * =============================================================================85 * END OF SP BOUCHER SOURCE HEADER86 * =============================================================================87 */8889#include <Accelerate/Accelerate.h>90#include <stdlib.h>91#include "CAccelShim.h"9293static int imin(int a, int b) { return a < b ? a : b; }94static int imax(int a, int b) { return a > b ? a : b; }9596int orix_dgeqrf(int m, int n, double *a, double *tau) {97 __LAPACK_int M = m, N = n, lda = imax(1, m), info = 0, lwork = -1;98 double query = 0;99 dgeqrf_(&M, &N, a, &lda, tau, &query, &lwork, &info);100 if (info != 0) return (int)info;101 lwork = (__LAPACK_int)query;102 if (lwork < 1) lwork = imax(1, n);103 double *work = malloc(sizeof(double) * (size_t)lwork);104 if (!work) return -1000;105 dgeqrf_(&M, &N, a, &lda, tau, work, &lwork, &info);106 free(work);107 return (int)info;108}109110int orix_dorgqr(int m, int n, int k, double *a, const double *tau) {111 __LAPACK_int M = m, N = n, K = k, lda = imax(1, m), info = 0, lwork = -1;112 double query = 0;113 dorgqr_(&M, &N, &K, a, &lda, tau, &query, &lwork, &info);114 if (info != 0) return (int)info;115 lwork = (__LAPACK_int)query;116 if (lwork < 1) lwork = imax(1, n);117 double *work = malloc(sizeof(double) * (size_t)lwork);118 if (!work) return -1000;119 dorgqr_(&M, &N, &K, a, &lda, tau, work, &lwork, &info);120 free(work);121 return (int)info;122}123124int orix_dgesdd(int m, int n, double *a, double *s, double *u, double *vt) {125 int k = imin(m, n);126 __LAPACK_int M = m, N = n, lda = imax(1, m), ldu = imax(1, m), ldvt = imax(1, k), info = 0, lwork = -1;127 char jobz = 'S';128 double query = 0;129 __LAPACK_int *iwork = malloc(sizeof(__LAPACK_int) * (size_t)imax(1, 8 * k));130 if (!iwork) return -1000;131 dgesdd_(&jobz, &M, &N, a, &lda, s, u, &ldu, vt, &ldvt, &query, &lwork, iwork, &info);132 if (info != 0) { free(iwork); return (int)info; }133 lwork = (__LAPACK_int)query;134 if (lwork < 1) lwork = 1;135 double *work = malloc(sizeof(double) * (size_t)lwork);136 if (!work) { free(iwork); return -1000; }137 dgesdd_(&jobz, &M, &N, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, iwork, &info);138 free(work);139 free(iwork);140 return (int)info;141}142143int orix_dsyev(int n, double *a, double *w) {144 __LAPACK_int N = n, lda = imax(1, n), info = 0, lwork = -1;145 char jobz = 'V', uplo = 'U';146 double query = 0;147 dsyev_(&jobz, &uplo, &N, a, &lda, w, &query, &lwork, &info);148 if (info != 0) return (int)info;149 lwork = (__LAPACK_int)query;150 if (lwork < 1) lwork = imax(1, 3 * n);151 double *work = malloc(sizeof(double) * (size_t)lwork);152 if (!work) return -1000;153 dsyev_(&jobz, &uplo, &N, a, &lda, w, work, &lwork, &info);154 free(work);155 return (int)info;156}157158int orix_dpotrf(int n, double *a) {159 __LAPACK_int N = n, lda = imax(1, n), info = 0;160 char uplo = 'L';161 dpotrf_(&uplo, &N, a, &lda, &info);162 return (int)info;163}164165int orix_dgesv(int n, int nrhs, double *a, double *b) {166 __LAPACK_int N = n, NRHS = nrhs, lda = imax(1, n), ldb = imax(1, n), info = 0;167 __LAPACK_int *ipiv = malloc(sizeof(__LAPACK_int) * (size_t)imax(1, n));168 if (!ipiv) return -1000;169 dgesv_(&N, &NRHS, a, &lda, ipiv, b, &ldb, &info);170 free(ipiv);171 return (int)info;172}173174int orix_dgetri(int n, double *a) {175 __LAPACK_int N = n, lda = imax(1, n), info = 0, lwork = -1;176 __LAPACK_int *ipiv = malloc(sizeof(__LAPACK_int) * (size_t)imax(1, n));177 if (!ipiv) return -1000;178 dgetrf_(&N, &N, a, &lda, ipiv, &info);179 if (info != 0) { free(ipiv); return (int)info; }180 double query = 0;181 dgetri_(&N, a, &lda, ipiv, &query, &lwork, &info);182 if (info != 0) { free(ipiv); return (int)info; }183 lwork = (__LAPACK_int)query;184 if (lwork < 1) lwork = imax(1, n);185 double *work = malloc(sizeof(double) * (size_t)lwork);186 if (!work) { free(ipiv); return -1000; }187 dgetri_(&N, a, &lda, ipiv, work, &lwork, &info);188 free(work);189 free(ipiv);190 return (int)info;191}192193void orix_dgemm(int m, int n, int k, const double *a, const double *b, double *c) {194 if (m == 0 || n == 0) return;195 if (k == 0) { for (long i = 0; i < (long)m * n; i++) c[i] = 0; return; }196 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, m, n, k, 1.0, a, m, b, k, 0.0, c, m);197}198