peachsim 0.1.1692 → 0.1.1700

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/cli/bridge-flow-runner.ts +61 -39
  2. package/cli/commands/agent-wrapper.ts +21 -10
  3. package/cli/commands/control.ts +27 -7
  4. package/cli/commands/coverage.ts +1 -1
  5. package/cli/commands/detect.ts +17 -4
  6. package/cli/commands/maestro-compare.ts +25 -99
  7. package/cli/help-core.ts +3 -4
  8. package/cli/maestro-compare.ts +172 -4
  9. package/cli/outbound-endpoints.ts +2 -10
  10. package/detox/index.ts +15 -0
  11. package/detox/launch-permissions.ts +169 -0
  12. package/dist-lib/agent-daemon-client.cjs +2 -2
  13. package/dist-lib/agent-events.cjs +2 -2
  14. package/dist-lib/attached-projects.cjs +2 -2
  15. package/dist-lib/auth/shared-session.cjs +2 -2
  16. package/dist-lib/backend-origin.cjs +2 -2
  17. package/dist-lib/beta.cjs +2 -2
  18. package/dist-lib/beta.mjs +1 -1
  19. package/dist-lib/bridge-constants.cjs +2 -2
  20. package/dist-lib/bridge-contract-input.cjs +2 -2
  21. package/dist-lib/bridge-contract-input.mjs +1 -1
  22. package/dist-lib/bridge-contract.cjs +2 -2
  23. package/dist-lib/bridge-contract.mjs +1 -1
  24. package/dist-lib/bundle-channel.cjs +18273 -15925
  25. package/dist-lib/capture-contract.cjs +2 -2
  26. package/dist-lib/capture-contract.mjs +1 -1
  27. package/dist-lib/cloud-contract.cjs +2 -2
  28. package/dist-lib/cloud-contract.mjs +1 -1
  29. package/dist-lib/cloud-test-args.cjs +2 -2
  30. package/dist-lib/cloud-test-args.mjs +1 -1
  31. package/dist-lib/cloud.cjs +2 -2
  32. package/dist-lib/cloud.mjs +1 -1
  33. package/dist-lib/config.cjs +2 -2
  34. package/dist-lib/config.mjs +1 -1
  35. package/dist-lib/detox/index.cjs +104 -3
  36. package/dist-lib/dev-bundle-resolution.cjs +208 -44
  37. package/dist-lib/expo-swift-bundle.cjs +2349 -1778
  38. package/dist-lib/expo-swift-metadata.cjs +2 -2
  39. package/dist-lib/expo-swift-metadata.mjs +1 -1
  40. package/dist-lib/home-paths.cjs +2 -2
  41. package/dist-lib/host/bridge-host.cjs +7885 -3334
  42. package/dist-lib/host/fetch-proxy-handler.cjs +331 -39
  43. package/dist-lib/host/fetch-proxy-overrides.cjs +2 -2
  44. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  45. package/dist-lib/host/replacement-module-handler.cjs +2 -2
  46. package/dist-lib/host/websocket-proxy.cjs +2 -2
  47. package/dist-lib/index.cjs +297 -77
  48. package/dist-lib/jump-to-source-babel.cjs +2 -2
  49. package/dist-lib/jump-to-source-native.cjs +2 -2
  50. package/dist-lib/kotlin-host.cjs +2 -2
  51. package/dist-lib/menu.cjs +2 -2
  52. package/dist-lib/menu.mjs +1 -1
  53. package/dist-lib/metro-fingerprint-registry.cjs +2 -2
  54. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  55. package/dist-lib/metro-production-bundle.cjs +2 -2
  56. package/dist-lib/metro-production-bundle.mjs +1 -1
  57. package/dist-lib/metro.cjs +294 -74
  58. package/dist-lib/native-app-identity-project.cjs +48 -37
  59. package/dist-lib/native-app-identity-project.mjs +47 -36
  60. package/dist-lib/native-app-identity.cjs +2 -2
  61. package/dist-lib/native-app-identity.mjs +1 -1
  62. package/dist-lib/native-firebase-metadata.cjs +2 -2
  63. package/dist-lib/native-firebase-metadata.mjs +1 -1
  64. package/dist-lib/plist.cjs +2 -2
  65. package/dist-lib/plist.mjs +1 -1
  66. package/dist-lib/profiles.cjs +2 -2
  67. package/dist-lib/public-brand.cjs +2 -2
  68. package/dist-lib/public-origins-lib-esm.json +24 -23
  69. package/dist-lib/public-origins-lib.json +199 -79
  70. package/dist-lib/react-native-host-modules.cjs +2 -2
  71. package/dist-lib/react-native-host-modules.mjs +1 -1
  72. package/dist-lib/reap-browser.cjs +2 -2
  73. package/dist-lib/scripts/dev-server-scanner.cjs +206 -31
  74. package/dist-lib/sdk.cjs +2 -2
  75. package/dist-lib/sdk.mjs +1 -1
  76. package/dist-lib/skills.cjs +19600 -15992
  77. package/dist-lib/swift-compile.cjs +640 -402
  78. package/dist-lib/swift-compile.mjs +595 -357
  79. package/dist-lib/swift-package.cjs +24 -20
  80. package/dist-lib/swift-package.mjs +23 -19
  81. package/dist-lib/swift-submit.cjs +2 -2
  82. package/dist-lib/swift-submit.mjs +1 -1
  83. package/dist-lib/swift-toolchain.cjs +31 -4
  84. package/dist-lib/swift.cjs +2 -2
  85. package/dist-lib/vite-swift.cjs +1559 -1208
  86. package/dist-lib/vite.cjs +251 -42
  87. package/dist-lib/worklet-executable.cjs +2 -2
  88. package/dist-lib/worklet-executable.mjs +1 -1
  89. package/dist-types/packages/peach-engine/src/engine/font-config.d.ts +1 -1
  90. package/dist-types/packages/peach-engine/src/engine/node.d.ts +1 -0
  91. package/dist-types/packages/peach-engine/src/engine/scroll-pan-contacts.d.ts +3 -1
  92. package/dist-types/packages/peach-engine/src/engine/scroll.d.ts +7 -0
  93. package/dist-types/packages/peach-engine/src/engine/types.d.ts +2 -1
  94. package/dist-types/packages/peach-engine/src/remote-stream/cloud-display.d.ts +2 -2
  95. package/dist-types/packages/peach-engine/src/render-worker/host-service-protocol.d.ts +3 -0
  96. package/dist-types/packages/peach-engine/src/render-worker/protocol.d.ts +1 -0
  97. package/dist-types/packages/peach-engine/src/render-worker/scroll-mirror.d.ts +17 -0
  98. package/dist-types/packages/peach-engine/src/render-worker/worker-input-runtime.d.ts +2 -1
  99. package/dist-types/packages/peach-engine/src/render-worker/worklet-runtime.d.ts +3 -0
  100. package/dist-types/packages/peach-globals/src/cameraBridgeContract.d.ts +1 -0
  101. package/dist-types/packages/peach-globals/src/replayBridgeContract.d.ts +1 -0
  102. package/dist-types/packages/peach-globals/src/shellHostBridgeContract.d.ts +3 -0
  103. package/dist-types/packages/peach-kit/src/materials/types.d.ts +1 -0
  104. package/package.json +1 -1
  105. package/runtime-companion.json +1 -1
  106. package/scripts/dev-server-scanner.ts +4 -0
  107. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Alpha.h +2282 -0
  108. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/BasicImageTypes.h +55 -0
  109. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Conversion.h +10551 -6
  110. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Convolution.h +4236 -3
  111. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Geometry.h +827 -8
  112. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Histogram.h +1369 -0
  113. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Morphology.h +1243 -0
  114. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Transform.h +1747 -0
  115. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage.h +291 -2
  116. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_CVUtilities.h +1283 -0
  117. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_Types.h +811 -91
  118. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_Utilities.h +1342 -25
  119. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns.h +2738 -0
  120. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns_constants.h +1685 -0
  121. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns_graph.h +913 -0
  122. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns_structures.h +1917 -0
  123. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/LinearAlgebra.h +23 -0
  124. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/arithmetic.h +209 -0
  125. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/base.h +104 -0
  126. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/linear_systems.h +77 -0
  127. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/matrix.h +554 -0
  128. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/norms.h +58 -0
  129. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/object.h +186 -0
  130. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/splat.h +86 -0
  131. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/vector.h +364 -0
  132. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Quadrature/Integration.h +195 -0
  133. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Quadrature/Quadrature.h +44 -0
  134. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/BLAS/BLAS.h +2330 -0
  135. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/BLAS/Types.h +123 -0
  136. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/Solve.h +8339 -0
  137. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/SolveImplementation.h +520 -0
  138. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/SolveImplementationTyped.h +1893 -0
  139. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/Sparse.h +10 -0
  140. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/blas_new.h +1098 -0
  141. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/cblas.h +859 -0
  142. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/cblas_new.h +1134 -0
  143. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/clapack.h +10555 -0
  144. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/fortran_blas.h +695 -0
  145. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/lapack.h +38526 -0
  146. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/lapack_types.h +60 -0
  147. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/lapack_version.h +20 -0
  148. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/thread_api.h +50 -0
  149. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vBasicOps.h +1218 -0
  150. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vBigNum.h +1458 -0
  151. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vDSP.h +10377 -0
  152. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vDSP_translate.h +167 -0
  153. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vForce.h +1243 -0
  154. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vecLib.h +114 -0
  155. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vecLibTypes.h +146 -0
  156. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vectorOps.h +1106 -0
  157. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vfp.h +294 -0
  158. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Headers/Accelerate.h +25 -1
  159. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFArray.h +673 -0
  160. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFAvailability.h +232 -0
  161. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFBase.h +827 -0
  162. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFCalendar.h +121 -0
  163. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFCharacterSet.h +387 -0
  164. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFData.h +79 -0
  165. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFDate.h +122 -0
  166. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFDictionary.h +674 -0
  167. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFError.h +182 -0
  168. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFLocale.h +213 -0
  169. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFNotificationCenter.h +82 -0
  170. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFNumber.h +129 -0
  171. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFSet.h +491 -0
  172. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFString.h +883 -2
  173. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFTimeZone.h +98 -0
  174. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h +11 -229
  175. package/sdk/ios/System/Library/Frameworks/CoreGraphics.framework/Headers/CoreGraphics.h +2 -6
  176. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h +2 -1
  177. package/sdk/ios/System/Library/Frameworks/QuartzCore.framework/Headers/QuartzCore.h +1 -0
  178. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecBase.h +906 -3
  179. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecCertificate.h +613 -0
  180. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecItem.h +1284 -29
  181. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecPolicy.h +441 -0
  182. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecTrust.h +845 -0
  183. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/Security.h +4 -0
  184. package/sdk/ios/usr/include/AvailabilityMacros.h +3985 -0
  185. package/sdk/ios/usr/include/copyfile.h +143 -0
  186. package/sdk/ios/usr/include/malloc/_malloc.h +143 -0
  187. package/sdk/ios/usr/include/malloc/_malloc_type.h +153 -0
  188. package/sdk/ios/usr/include/malloc/_platform.h +80 -0
  189. package/sdk/ios/usr/include/malloc/_ptrcheck.h +43 -0
  190. package/sdk/ios/usr/include/malloc/malloc.h +768 -0
  191. package/sdk/ios/usr/include/os/availability.h +168 -6
  192. package/sdk/ios/usr/include/os/base.h +351 -0
  193. package/sdk/ios/usr/include/os/clock.h +18 -0
  194. package/sdk/ios/usr/include/os/lock.h +232 -10
  195. package/sdk/ios/usr/include/os/log.h +521 -38
  196. package/sdk/ios/usr/include/os/object.h +320 -3
  197. package/sdk/ios/usr/include/os/trace.h +492 -0
  198. package/sdk/ios/usr/include/os/trace_base.h +167 -0
  199. package/sdk/ios/usr/include/os/workgroup.h +37 -0
  200. package/sdk/ios/usr/include/os/workgroup_base.h +91 -0
  201. package/sdk/ios/usr/include/os/workgroup_interval.h +156 -0
  202. package/sdk/ios/usr/include/os/workgroup_object.h +359 -0
  203. package/sdk/ios/usr/include/os/workgroup_parallel.h +76 -0
  204. package/sdk/ios/usr/include/sys/cdefs.h +38 -0
  205. package/sdk/ios/wasm/include/unistd.h +3 -0
  206. package/skills/contrast/SKILL.md +3 -0
  207. package/src/codex-client.ts +11 -6
  208. package/src/dev-bundle-resolution.ts +124 -48
  209. package/src/expo-native-workspace.ts +176 -0
  210. package/src/expo-swift-host.ts +161 -26
  211. package/src/expo-swift-ready.ts +14 -28
  212. package/src/expoSwiftContract.ts +4 -1
  213. package/src/generated/swift-c-predefines.json +68 -12
  214. package/src/host/bridge-host.ts +5 -0
  215. package/src/installed-package-versions.ts +14 -1
  216. package/src/metro-project-root.ts +44 -33
  217. package/src/native-app-identity-project.ts +1 -0
  218. package/src/nitro-adapter.lock.json +916 -10
  219. package/src/nitro-adapter.ts +33 -24
  220. package/src/nitro-bundle.ts +108 -22
  221. package/src/nitro-compile.ts +32 -5
  222. package/src/nitro-source.ts +13 -3
  223. package/src/nitroAdapterContract.ts +100 -20
  224. package/src/node-http-fetch.ts +116 -0
  225. package/src/packageVersionsContract.ts +46 -0
  226. package/src/serverDiscoveryContract.ts +1 -0
  227. package/src/settingsContract.ts +0 -30
  228. package/src/swift-binary-modules.ts +20 -5
  229. package/src/swift-compile.ts +168 -48
  230. package/src/swift-coverage/xcode-assets.ts +4 -3
  231. package/src/swift-host.ts +14 -3
  232. package/src/swift-objc-compile.ts +32 -5
  233. package/src/swift-objc-metadata.ts +2 -2
  234. package/src/swift-objc-platform.ts +2 -2
  235. package/src/swift-objc-standin.ts +43 -13
  236. package/src/swift-package.ts +33 -26
  237. package/src/swift-source-pods.ts +126 -41
  238. package/src/swift-standin-digest.ts +77 -0
  239. package/src/swift-standin-emit.ts +40 -4
  240. package/src/swift-standins.ts +68 -19
  241. package/src/swift-toolchain.ts +38 -0
  242. package/src/swiftContract.ts +8 -0
  243. package/src/vite-plugin.ts +0 -10
  244. package/src/xcode-project.ts +62 -42
  245. package/src/settings-flags.ts +0 -107
@@ -0,0 +1,1893 @@
1
+ // This file expects the below preprocessor symbols to be defined prior to
2
+ // inclusion, which specifies which precision to generate implementations for.
3
+ #ifndef _SPARSE_IMPLEMENTATION_TYPE
4
+ # error "_SPARSE_IMPLEMENTATION_TYPE must be defined prior to inclusion"
5
+ #endif /* _SPARSE_IMPLEMENTATION_TYPE */
6
+ #ifndef _SPARSE_VARIANT
7
+ # error "_SPARSE_VARIANT must be defined prior to inclusion"
8
+ #endif /* _SPARSE_VARIANT */
9
+
10
+ /******************************************************************************
11
+ * External functions used to implement public API
12
+ ******************************************************************************/
13
+ #if defined __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ #if __has_feature(nullability)
18
+ #pragma clang assume_nonnull begin
19
+ #endif
20
+
21
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
22
+ extern _SPARSE_VARIANT(SparseMatrix) _SPARSE_VARIANT(_SparseConvertFromCoordinate)
23
+ (int m, int n, long nBlock, uint8_t blockSize, _SPARSE_ATTRIBUTES attributes, const int *row,
24
+ const int *col, const _SPARSE_IMPLEMENTATION_TYPE *val, char *storage, int *workspace);
25
+
26
+ #if defined __SPARSE_TYPES_H
27
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
28
+ extern _SPARSE_VARIANT(SparseMatrix) _SPARSE_VARIANT(_SparseConvertFromOpaque)(_SPARSE_OLDSTYLE(sparse_matrix) matrix);
29
+ #endif /* defined __SPARSE_TYPES_H */
30
+
31
+ #ifdef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
32
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
33
+ #else
34
+ API_AVAILABLE( macos(26.0), ios(26.0), watchos(26.0), tvos(26.0), visionos(26.0) )
35
+ #endif
36
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization)
37
+ _SPARSE_VARIANT(_SparseNumericFactorSymmetric)
38
+ (SparseOpaqueSymbolicFactorization *symbolicFactor,
39
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
40
+ const SparseNumericFactorOptions *options,
41
+ void *factorStorage,
42
+ void *workspace);
43
+
44
+ #ifndef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
45
+ API_AVAILABLE( macos(15.5), ios(18.5), watchos(11.5), tvos(18.5), visionos(2.5) )
46
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization)
47
+ _SPARSE_VARIANT(_SparseNumericFactorHermitian)
48
+ (SparseOpaqueSymbolicFactorization *symbolicFactor,
49
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
50
+ const SparseNumericFactorOptions *options,
51
+ void *factorStorage,
52
+ void *workspace);
53
+ #endif
54
+
55
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
56
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization) _SPARSE_VARIANT(_SparseNumericFactorQR)
57
+ (SparseOpaqueSymbolicFactorization *symbolicFactor,
58
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
59
+ const SparseNumericFactorOptions *options,
60
+ void *factorStorage,
61
+ void *workspace);
62
+
63
+ API_AVAILABLE( macos(15.5), ios(18.5), watchos(11.5), tvos(18.5), visionos(2.5) )
64
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization) _SPARSE_VARIANT(_SparseNumericFactorLU)
65
+ (SparseOpaqueSymbolicFactorization *symbolicFactor,
66
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
67
+ const SparseNumericFactorOptions *options,
68
+ void *factorStorage,
69
+ void *workspace);
70
+
71
+ #ifdef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
72
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
73
+ #else
74
+ API_AVAILABLE( macos(26.0), ios(26.0), watchos(26.0), tvos(26.0), visionos(26.0) )
75
+ #endif
76
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization)
77
+ _SPARSE_VARIANT(_SparseFactorSymmetric)
78
+ (SparseFactorization_t factorType,
79
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
80
+ const SparseSymbolicFactorOptions *sfoptions,
81
+ const SparseNumericFactorOptions *nfoptions);
82
+
83
+ #ifndef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
84
+ API_AVAILABLE( macos(15.5), ios(18.5), watchos(11.5), tvos(18.5), visionos(2.5) )
85
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization)
86
+ _SPARSE_VARIANT(_SparseFactorHermitian)
87
+ (SparseFactorization_t factorType,
88
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
89
+ const SparseSymbolicFactorOptions *sfoptions,
90
+ const SparseNumericFactorOptions *nfoptions);
91
+ #endif
92
+
93
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
94
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization) _SPARSE_VARIANT(_SparseFactorQR)
95
+ (SparseFactorization_t factorType,
96
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
97
+ const SparseSymbolicFactorOptions *sfoptions,
98
+ const SparseNumericFactorOptions *nfoptions);
99
+
100
+ API_AVAILABLE( macos(15.5), ios(18.5), watchos(11.5), tvos(18.5), visionos(2.5) )
101
+ extern _SPARSE_VARIANT(SparseOpaqueFactorization) _SPARSE_VARIANT(_SparseFactorLU)
102
+ (SparseFactorization_t factorType,
103
+ const _SPARSE_VARIANT(SparseMatrix) *Matrix,
104
+ const SparseSymbolicFactorOptions *sfoptions,
105
+ const SparseNumericFactorOptions *nfoptions);
106
+
107
+ #ifdef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
108
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
109
+ #else
110
+ API_AVAILABLE( macos(26.0), ios(26.0), watchos(26.0), tvos(26.0), visionos(26.0) )
111
+ #endif
112
+ extern void _SPARSE_VARIANT(_SparseRefactorSymmetric)
113
+ (const _SPARSE_VARIANT(SparseMatrix) *Matrix,
114
+ _SPARSE_VARIANT(SparseOpaqueFactorization) *Factorization,
115
+ const SparseNumericFactorOptions *nfoptions,
116
+ void *workspace);
117
+
118
+ #ifndef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
119
+ API_AVAILABLE( macos(15.5), ios(18.5), watchos(11.5), tvos(18.5), visionos(2.5) )
120
+ extern void _SPARSE_VARIANT(_SparseRefactorHermitian)
121
+ (const _SPARSE_VARIANT(SparseMatrix) *Matrix,
122
+ _SPARSE_VARIANT(SparseOpaqueFactorization) *Factorization,
123
+ const SparseNumericFactorOptions *nfoptions,
124
+ void *workspace);
125
+ #endif
126
+
127
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
128
+ extern void _SPARSE_VARIANT(_SparseRefactorQR)
129
+ (const _SPARSE_VARIANT(SparseMatrix) *Matrix,
130
+ _SPARSE_VARIANT(SparseOpaqueFactorization) *Factorization,
131
+ const SparseNumericFactorOptions *nfoptions,
132
+ void *workspace);
133
+
134
+ API_AVAILABLE( macos(15.5), ios(18.5), watchos(11.5), tvos(18.5), visionos(2.5) )
135
+ extern void _SPARSE_VARIANT(_SparseRefactorLU)
136
+ (const _SPARSE_VARIANT(SparseMatrix) *Matrix,
137
+ _SPARSE_VARIANT(SparseOpaqueFactorization) *Factorization,
138
+ const SparseNumericFactorOptions *nfoptions,
139
+ void *workspace);
140
+
141
+ API_AVAILABLE( macos(15.5), ios(18.5), watchos(11.5), tvos(18.5), visionos(2.5) )
142
+ extern void _SPARSE_VARIANT(_SparseUpdatePartialRefactorLU)
143
+ (_SPARSE_VARIANT(SparseOpaqueFactorization) *Opaque,
144
+ int updateCount,
145
+ const int *updatedIndices,
146
+ _SPARSE_VARIANT(SparseMatrix) newMatrix);
147
+
148
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
149
+ extern void _SPARSE_VARIANT(_SparseMultiplySubfactor)
150
+ (const _SPARSE_VARIANT(SparseOpaqueSubfactor) *Subfactor,
151
+ const _SPARSE_VARIANT(DenseMatrix) *_Nullable x,
152
+ const _SPARSE_VARIANT(DenseMatrix) *y,
153
+ char *workspace);
154
+
155
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
156
+ extern void _SPARSE_VARIANT(_SparseSolveSubfactor)
157
+ (const _SPARSE_VARIANT(SparseOpaqueSubfactor) *Subfactor,
158
+ const _SPARSE_VARIANT(DenseMatrix) *_Nullable b,
159
+ const _SPARSE_VARIANT(DenseMatrix) *x,
160
+ char *workspace);
161
+
162
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
163
+ extern void _SPARSE_VARIANT(_SparseSolveOpaque)
164
+ (const _SPARSE_VARIANT(SparseOpaqueFactorization) *Factored,
165
+ const _SPARSE_VARIANT(DenseMatrix) *_Nullable RHS,
166
+ const _SPARSE_VARIANT(DenseMatrix) *Soln,
167
+ void *workspace);
168
+
169
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
170
+ extern void _SPARSE_VARIANT(_SparseDestroyOpaqueNumeric)(_SPARSE_VARIANT(SparseOpaqueFactorization) *toFree);
171
+
172
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
173
+ extern void _SPARSE_VARIANT(_SparseRetainNumeric)
174
+ (_SPARSE_VARIANT(SparseOpaqueFactorization) *_Nonnull numericFactor);
175
+
176
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
177
+ extern SparseNumericFactorOptions _SPARSE_VARIANT(_SparseGetOptionsFromNumericFactor)(_SPARSE_VARIANT(SparseOpaqueFactorization) *factor);
178
+
179
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
180
+ extern void _SPARSE_VARIANT(_SparseGetWorkspaceRequired)
181
+ (SparseSubfactor_t Subfactor, _SPARSE_VARIANT(SparseOpaqueFactorization) Factor, size_t *workStatic,
182
+ size_t *workPerRHS);
183
+
184
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
185
+ extern size_t _SPARSE_VARIANT(_SparseGetIterativeStateSize)
186
+ (const SparseIterativeMethod *method, bool preconditioner, int m, int n, int nrhs);
187
+
188
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
189
+ extern void _SPARSE_VARIANT(_SparseCGIterate)
190
+ (const SparseCGOptions *options,
191
+ int iteration,
192
+ char *state,
193
+ const bool *converged,
194
+ _SPARSE_VARIANT(DenseMatrix) *X,
195
+ _SPARSE_VARIANT(DenseMatrix) *B,
196
+ _SPARSE_VARIANT(DenseMatrix) *R,
197
+ const _SPARSE_VARIANT(SparseOpaquePreconditioner) *_Nullable Preconditioner,
198
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)));
199
+
200
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
201
+ extern SparseIterativeStatus_t _SPARSE_VARIANT(_SparseCGSolve)
202
+ (const SparseCGOptions *options,
203
+ _SPARSE_VARIANT(DenseMatrix) *X,
204
+ _SPARSE_VARIANT(DenseMatrix) *B,
205
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)),
206
+ const _SPARSE_VARIANT(SparseOpaquePreconditioner) *_Nullable Preconditioner);
207
+
208
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
209
+ extern void _SPARSE_VARIANT(_SparseGMRESIterate)
210
+ (const SparseGMRESOptions *options,
211
+ int iteration,
212
+ char *state,
213
+ const bool *converged,
214
+ _SPARSE_VARIANT(DenseMatrix) *X,
215
+ _SPARSE_VARIANT(DenseMatrix) *B,
216
+ _SPARSE_VARIANT(DenseMatrix) *R,
217
+ const _SPARSE_VARIANT(SparseOpaquePreconditioner) *_Nullable Preconditioner,
218
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)));
219
+
220
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
221
+ extern SparseIterativeStatus_t _SPARSE_VARIANT(_SparseGMRESSolve)
222
+ (SparseGMRESOptions *options,
223
+ _SPARSE_VARIANT(DenseMatrix) *X,
224
+ _SPARSE_VARIANT(DenseMatrix) *B,
225
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)),
226
+ const _SPARSE_VARIANT(SparseOpaquePreconditioner) *_Nullable Preconditioner);
227
+
228
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
229
+ extern void _SPARSE_VARIANT(_SparseLSMRIterate)
230
+ (const SparseLSMROptions *options,
231
+ int iteration,
232
+ char *state,
233
+ const bool *converged,
234
+ _SPARSE_VARIANT(DenseMatrix) *X,
235
+ _SPARSE_VARIANT(DenseMatrix) *B,
236
+ _SPARSE_VARIANT(DenseMatrix) *R,
237
+ const _SPARSE_VARIANT(SparseOpaquePreconditioner) *_Nullable Preconditioner,
238
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)));
239
+
240
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
241
+ extern SparseIterativeStatus_t _SPARSE_VARIANT(_SparseLSMRSolve)
242
+ (SparseLSMROptions *options,
243
+ _SPARSE_VARIANT(DenseMatrix) *X,
244
+ _SPARSE_VARIANT(DenseMatrix) *B,
245
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)),
246
+ const _SPARSE_VARIANT(SparseOpaquePreconditioner) *_Nullable Preconditioner);
247
+
248
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
249
+ extern _SPARSE_VARIANT(SparseOpaquePreconditioner) _SPARSE_VARIANT(_SparseCreatePreconditioner)
250
+ (SparsePreconditioner_t type,
251
+ _SPARSE_VARIANT(SparseMatrix) *A);
252
+
253
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
254
+ extern void _SPARSE_VARIANT(_SparseReleaseOpaquePreconditioner)
255
+ (_SPARSE_VARIANT(SparseOpaquePreconditioner) *toFree);
256
+
257
+ API_AVAILABLE( macos(10.13), ios(11), watchos(4), tvos(11) )
258
+ extern void _SPARSE_VARIANT(_SparseSpMV)
259
+ (_SPARSE_IMPLEMENTATION_TYPE alpha,
260
+ _SPARSE_VARIANT(SparseMatrix) A,
261
+ _SPARSE_VARIANT(DenseMatrix) x,
262
+ bool accumulate,
263
+ _SPARSE_VARIANT(DenseMatrix) y);
264
+
265
+ #if __has_feature(nullability)
266
+ #pragma clang assume_nonnull end
267
+ #endif
268
+
269
+ #if defined __cplusplus
270
+ } /* extern "C" */
271
+ #endif
272
+
273
+ /******************************************************************************
274
+ * Implementation helper definitions and types
275
+ ******************************************************************************/
276
+ static inline __attribute__((__const__))
277
+ _SPARSE_VARIANT(DenseMatrix) _SPARSE_VARIANT(_DenseMatrixFromVector)(_SPARSE_VARIANT(DenseVector) x) {
278
+ return (_SPARSE_VARIANT(DenseMatrix)){
279
+ .rowCount = x.count,
280
+ .columnCount = 1,
281
+ .columnStride = x.count,
282
+ .data = x.data,
283
+ };
284
+ }
285
+
286
+ // This function just provides a shorthand way of delivering an invalid subfactor
287
+ static inline __attribute__((__const__))
288
+ _SPARSE_VARIANT(SparseOpaqueSubfactor) _SPARSE_VARIANT(_SparseInvalidSubfactor)(void) {
289
+ return (_SPARSE_VARIANT(SparseOpaqueSubfactor)) {
290
+ .contents = SparseSubfactorInvalid,
291
+ .factor = {
292
+ .status = SparseInternalError,
293
+ .symbolicFactorization = { .status = SparseInternalError },
294
+ }
295
+ };
296
+ }
297
+
298
+ // This function just provides a shorthand way of delivering a valid failed factor matrix
299
+ static inline __attribute__((__const__))
300
+ _SPARSE_VARIANT(SparseOpaqueFactorization) _SPARSE_VARIANT(_SparseFailedFactor)(SparseStatus_t status) {
301
+ return (_SPARSE_VARIANT(SparseOpaqueFactorization)) {
302
+ .status = status,
303
+ .symbolicFactorization = { .status = status },
304
+ };
305
+ }
306
+
307
+ // Null matrix for return on bad function calls
308
+ static const _SPARSE_VARIANT(SparseMatrix) _SPARSE_VARIANT(_SparseNullMatrix) = {
309
+ .structure = {
310
+ .rowCount = -1,
311
+ .columnCount = -1,
312
+ .blockSize = 0,
313
+ },
314
+ .data = NULL
315
+ };
316
+
317
+
318
+ /*! @abstract Return dimensions of matrix for the part of the factorization represented by Subfactor */
319
+ static inline
320
+ void _SPARSE_VARIANT(_SparseSubFactorGetDimn)(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, int *_Nonnull m, int *_Nonnull n) {
321
+ uint8_t blockSize = Subfactor.factor.symbolicFactorization.blockSize;
322
+ *m = Subfactor.factor.symbolicFactorization.rowCount * blockSize;
323
+ *n = Subfactor.factor.symbolicFactorization.columnCount * blockSize;
324
+
325
+ // We always factor a matrix where m>=n. Swap them so this is so.
326
+ if(*m < *n) {
327
+ int temp = *m;
328
+ *m = *n;
329
+ *n = temp;
330
+ }
331
+
332
+ // All Subfactors are n x n, except Q in SparseFactorizationQR, which is m x n
333
+ if(!(Subfactor.factor.symbolicFactorization.type == SparseFactorizationQR && Subfactor.contents == SparseSubfactorQ)) {
334
+ // Not the Q factor of QR => n x n
335
+ *m = *n;
336
+ }
337
+
338
+ // If Subfactor is transposed, swap m and n
339
+ if(Subfactor.attributes.transpose) {
340
+ int temp = *m;
341
+ *m = *n;
342
+ *n = temp;
343
+ }
344
+ }
345
+
346
+ /******************************************************************************
347
+ * Conversion From Other Formats
348
+ ******************************************************************************/
349
+
350
+ static inline SPARSE_PUBLIC_INTERFACE
351
+ _SPARSE_VARIANT(SparseMatrix) SparseConvertFromCoordinate(
352
+ int rowCount, int columnCount, long blockCount, uint8_t blockSize, _SPARSE_ATTRIBUTES attributes,
353
+ const int *_Nonnull row, const int *_Nonnull column, const _SPARSE_IMPLEMENTATION_TYPE *_Nonnull data) {
354
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions;
355
+ SPARSE_PARAMETER_CHECK(rowCount>=0, _SPARSE_VARIANT(_SparseNullMatrix),
356
+ "rowCount (%d) must be non-negative.\n", rowCount);
357
+ SPARSE_PARAMETER_CHECK(columnCount>=0, _SPARSE_VARIANT(_SparseNullMatrix),
358
+ "columnCount (%d) must be non-negative.\n", columnCount);
359
+ SPARSE_PARAMETER_CHECK(blockCount>=0, _SPARSE_VARIANT(_SparseNullMatrix),
360
+ "blockCount (%ld) must be non-negative.\n", blockCount);
361
+ SPARSE_PARAMETER_CHECK(attributes.kind==SparseOrdinary || rowCount==columnCount,
362
+ _SPARSE_VARIANT(_SparseNullMatrix),
363
+ "attributes.kind must be SparseOrdinary if matrix is not square.\n");
364
+ // NB: Whilst we request 48+ actual space from user, this is for alignment
365
+ // as malloc() returns 16-byte aligned data anyway, 2*16-4=28 is actually sufficient
366
+ char *storage = (char *)malloc(28 + (columnCount+1)*sizeof(long) + blockCount*sizeof(int) +
367
+ blockCount*blockSize*blockSize*sizeof(data[0]));
368
+ SPARSE_PARAMETER_CHECK(storage, _SPARSE_VARIANT(_SparseNullMatrix),
369
+ "Failed to allocate storage for result.\n");
370
+ int *workspace = (int *)malloc(rowCount*sizeof(int));
371
+ if(!workspace) free(storage);
372
+ SPARSE_PARAMETER_CHECK(workspace, _SPARSE_VARIANT(_SparseNullMatrix),
373
+ "Failed to allocate workspace of size %ld.\n", rowCount*sizeof(int));
374
+ _SPARSE_VARIANT(SparseMatrix) result = _SPARSE_VARIANT(_SparseConvertFromCoordinate)(rowCount, columnCount, blockCount, blockSize, attributes, row, column, data, storage, workspace);
375
+ result.structure.columnStarts = (long *)storage; // redundant to assignment in _SparseConvertFromCoordinate call, used to suppress static analyzer warning
376
+ free(workspace);
377
+ result.structure.attributes._allocatedBySparse = true;
378
+ return result;
379
+ }
380
+
381
+ static inline SPARSE_PUBLIC_INTERFACE
382
+ _SPARSE_VARIANT(SparseMatrix) SparseConvertFromCoordinate(int rowCount,
383
+ int columnCount, long blockCount, uint8_t blockSize, _SPARSE_ATTRIBUTES attributes,
384
+ const int *_Nonnull row, const int *_Nonnull column, const _SPARSE_IMPLEMENTATION_TYPE *_Nonnull data,
385
+ void *_Nonnull storage, void *_Nonnull workspace) {
386
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions;
387
+ SPARSE_PARAMETER_CHECK(rowCount>=0, _SPARSE_VARIANT(_SparseNullMatrix),
388
+ "rowCount (%d) must be non-negative.\n", rowCount);
389
+ SPARSE_PARAMETER_CHECK(columnCount>=0, _SPARSE_VARIANT(_SparseNullMatrix),
390
+ "columnCount (%d) must be non-negative.\n", columnCount);
391
+ SPARSE_PARAMETER_CHECK(blockCount>=0, _SPARSE_VARIANT(_SparseNullMatrix),
392
+ "blockCount (%ld) must be non-negative.\n", blockCount);
393
+ SPARSE_PARAMETER_CHECK(attributes.kind==SparseOrdinary || rowCount==columnCount,
394
+ _SPARSE_VARIANT(_SparseNullMatrix),
395
+ "attributes.kind must be SparseOrdinary if matrix is not square.\n");
396
+ return _SPARSE_VARIANT(_SparseConvertFromCoordinate)(rowCount, columnCount, blockCount, blockSize, attributes, row, column, data, (char*)storage, (int*)workspace);
397
+ }
398
+
399
+ #if defined __SPARSE_TYPES_H
400
+ static inline SPARSE_PUBLIC_INTERFACE
401
+ _SPARSE_VARIANT(SparseMatrix) SparseConvertFromOpaque(_Nonnull _SPARSE_OLDSTYLE(sparse_matrix) matrix) {
402
+ return _SPARSE_VARIANT(_SparseConvertFromOpaque)(matrix);
403
+ }
404
+ #endif /* defined __SPARSE_TYPES_H */
405
+
406
+ /******************************************************************************
407
+ * Matrix and Vector Operations (Sparse BLAS Wrappers)
408
+ ******************************************************************************/
409
+
410
+ /**** Multiplication **********************************************************/
411
+
412
+ static inline SPARSE_PUBLIC_INTERFACE
413
+ void SparseMultiply(_SPARSE_IMPLEMENTATION_TYPE alpha, _SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseMatrix) X, _SPARSE_VARIANT(DenseMatrix) Y) {
414
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
415
+
416
+ int Am = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
417
+ int An = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
418
+ SPARSE_CHECK_CONSISTENT_MAT_OUT_PLACE(Am, An, Y, X, /* no result */, "matrix A");
419
+ _SPARSE_VARIANT(_SparseSpMV)(alpha, A, X, false, Y);
420
+ }
421
+
422
+ static inline SPARSE_PUBLIC_INTERFACE
423
+ void SparseMultiply(_SPARSE_IMPLEMENTATION_TYPE alpha, _SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseVector) x, _SPARSE_VARIANT(DenseVector) y) {
424
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
425
+
426
+ int m = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
427
+ int n = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
428
+ SPARSE_PARAMETER_CHECK(n == x.count, /* No result */,
429
+ "Matrix dimensions (%dx%d) do not match x vector dimensions %dx%d\n",
430
+ m, n, x.count, 1);
431
+ SPARSE_PARAMETER_CHECK(m == y.count, /* No result */,
432
+ "Matrix dimensions (%dx%d) do not match y vector dimensions %dx%d\n",
433
+ m, n, y.count, 1);
434
+ _SPARSE_VARIANT(_SparseSpMV)(alpha, A, _SPARSE_VARIANT(_DenseMatrixFromVector)(x),
435
+ false, _SPARSE_VARIANT(_DenseMatrixFromVector)(y));
436
+ }
437
+
438
+ static inline SPARSE_PUBLIC_INTERFACE
439
+ void SparseMultiply(_SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseMatrix) X, _SPARSE_VARIANT(DenseMatrix) Y) {
440
+ SparseMultiply(1, A, X, Y);
441
+ }
442
+
443
+ static inline SPARSE_PUBLIC_INTERFACE
444
+ void SparseMultiply(_SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseVector) x, _SPARSE_VARIANT(DenseVector) y) {
445
+ SparseMultiply(1, A, x, y);
446
+ }
447
+
448
+ /**** Multipy-Add *************************************************************/
449
+
450
+
451
+ static inline SPARSE_PUBLIC_INTERFACE
452
+ void SparseMultiplyAdd(_SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseMatrix) X, _SPARSE_VARIANT(DenseMatrix) Y) {
453
+ SparseMultiplyAdd(1, A, X, Y);
454
+ }
455
+
456
+ static inline SPARSE_PUBLIC_INTERFACE
457
+ void SparseMultiplyAdd(_SPARSE_IMPLEMENTATION_TYPE alpha, _SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseMatrix) X, _SPARSE_VARIANT(DenseMatrix) Y) {
458
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
459
+
460
+ int Am = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
461
+ int An = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
462
+ SPARSE_CHECK_CONSISTENT_MAT_OUT_PLACE(Am, An, Y, X, /* no result */, "matrix A");
463
+ _SPARSE_VARIANT(_SparseSpMV)(alpha, A, X, true, Y);
464
+ }
465
+
466
+ static inline SPARSE_PUBLIC_INTERFACE
467
+ void SparseMultiplyAdd(_SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseVector) x, _SPARSE_VARIANT(DenseVector) y) {
468
+ SparseMultiplyAdd(1, A, x, y);
469
+ }
470
+
471
+ static inline SPARSE_PUBLIC_INTERFACE
472
+ void SparseMultiplyAdd(_SPARSE_IMPLEMENTATION_TYPE alpha, _SPARSE_VARIANT(SparseMatrix) A, _SPARSE_VARIANT(DenseVector) x, _SPARSE_VARIANT(DenseVector) y) {
473
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
474
+
475
+ int m = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
476
+ int n = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
477
+ SPARSE_PARAMETER_CHECK(n == x.count, /* No result */,
478
+ "Matrix dimensions (%dx%d) do not match x vector dimensions %dx%d\n",
479
+ m, n, x.count, 1);
480
+ SPARSE_PARAMETER_CHECK(m == y.count, /* No result */,
481
+ "Matrix dimensions (%dx%d) do not match y vector dimensions %dx%d\n",
482
+ m, n, y.count, 1);
483
+ _SPARSE_VARIANT(_SparseSpMV)(alpha, A, _SPARSE_VARIANT(_DenseMatrixFromVector)(x),
484
+ true, _SPARSE_VARIANT(_DenseMatrixFromVector)(y));
485
+ }
486
+
487
+ /******************************************************************************
488
+ * Transposition
489
+ ******************************************************************************/
490
+
491
+ static inline SPARSE_PUBLIC_INTERFACE _SPARSE_VARIANT(SparseMatrix) SparseGetTranspose(_SPARSE_VARIANT(SparseMatrix) Matrix) {
492
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
493
+ // if the matrix is marked as conjugate transposed, cannot perform transpose, since the net result is only conjugate
494
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
495
+ SPARSE_PARAMETER_CHECK(!(Matrix.structure.attributes.transpose && Matrix.structure.attributes.conjugate_transpose), Matrix,
496
+ "Cannot transpose matrix marked as conjugate transposed\n");
497
+ Matrix.structure.attributes.conjugate_transpose = false;
498
+ #endif
499
+ Matrix.structure.attributes.transpose = !Matrix.structure.attributes.transpose;
500
+ return Matrix;
501
+ }
502
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
503
+ static inline SPARSE_PUBLIC_INTERFACE _SPARSE_VARIANT(SparseMatrix) SparseGetConjugateTranspose(_SPARSE_VARIANT(SparseMatrix) Matrix) {
504
+ // if the matrix is marked as transposed, cannot perform conjugate transpose, since the net result is only conjugate
505
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
506
+ SPARSE_PARAMETER_CHECK(!(Matrix.structure.attributes.transpose && !Matrix.structure.attributes.conjugate_transpose), Matrix,
507
+ "Cannot conjugate transpose matrix marked as transposed\n");
508
+ Matrix.structure.attributes.conjugate_transpose = true;
509
+ Matrix.structure.attributes.transpose = !Matrix.structure.attributes.transpose;
510
+ return Matrix;
511
+ }
512
+ #endif
513
+ static inline SPARSE_PUBLIC_INTERFACE _SPARSE_VARIANT(SparseOpaqueFactorization) SparseGetTranspose(_SPARSE_VARIANT(SparseOpaqueFactorization) Factor) {
514
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
515
+ // if the factor is marked as conjugate transposed, cannot perform transpose, since the net result is only conjugate
516
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
517
+ SPARSE_PARAMETER_CHECK(!(Factor.attributes.transpose && Factor.attributes.conjugate_transpose), Factor,
518
+ "Cannot transpose factor marked as conjugate transposed\n");
519
+ Factor.attributes.conjugate_transpose = false;
520
+ #endif
521
+ Factor.attributes.transpose = !Factor.attributes.transpose;
522
+ _SPARSE_VARIANT(_SparseRetainNumeric)(&Factor); // we're taking an additional reference
523
+ return Factor;
524
+ }
525
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
526
+ static inline SPARSE_PUBLIC_INTERFACE _SPARSE_VARIANT(SparseOpaqueFactorization) SparseGetConjugateTranspose(_SPARSE_VARIANT(SparseOpaqueFactorization) Factor) {
527
+ // if the factor is marked as transposed, cannot perform conjugate transpose, since the net result is only conjugate
528
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
529
+ SPARSE_PARAMETER_CHECK(!(Factor.attributes.transpose && !Factor.attributes.conjugate_transpose), Factor,
530
+ "Cannot conjugate transpose factor marked as transposed\n");
531
+ Factor.attributes.conjugate_transpose = true;
532
+ Factor.attributes.transpose = !Factor.attributes.transpose;
533
+ _SPARSE_VARIANT(_SparseRetainNumeric)(&Factor); // we're taking an additional reference
534
+ return Factor;
535
+ }
536
+ #endif
537
+ static inline SPARSE_PUBLIC_INTERFACE _SPARSE_VARIANT(SparseOpaqueSubfactor) SparseGetTranspose(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor) {
538
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
539
+ // if the subfactor is marked as conjugate transposed, cannot perform transpose, since the net result is only conjugate
540
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
541
+ SPARSE_PARAMETER_CHECK(!(Subfactor.attributes.transpose && Subfactor.attributes.conjugate_transpose), Subfactor,
542
+ "Cannot transpose factor marked as conjugate transposed\n");
543
+ Subfactor.attributes.conjugate_transpose = false;
544
+ #endif
545
+ Subfactor.attributes.transpose = !Subfactor.attributes.transpose;
546
+ _SPARSE_VARIANT(_SparseRetainNumeric)(&Subfactor.factor); // we're taking an additional reference
547
+ return Subfactor;
548
+ }
549
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
550
+ static inline SPARSE_PUBLIC_INTERFACE _SPARSE_VARIANT(SparseOpaqueSubfactor) SparseGetConjugateTranspose(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor) {
551
+ // if the subfactor is marked as transposed, cannot perform conjugate transpose, since the net result is only conjugate
552
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
553
+ SPARSE_PARAMETER_CHECK(!(Subfactor.attributes.transpose && !Subfactor.attributes.conjugate_transpose), Subfactor,
554
+ "Cannot conjugate transpose factor marked as transposed\n");
555
+ Subfactor.attributes.conjugate_transpose = true;
556
+ Subfactor.attributes.transpose = !Subfactor.attributes.transpose;
557
+ _SPARSE_VARIANT(_SparseRetainNumeric)(&Subfactor.factor); // we're taking an additional reference
558
+ return Subfactor;
559
+ }
560
+ #endif
561
+
562
+ /******************************************************************************
563
+ * Sparse Factor Functions
564
+ ******************************************************************************/
565
+
566
+ /**** All-in-one Sparse Factor Functions **************************************/
567
+
568
+ static inline SPARSE_PUBLIC_INTERFACE
569
+ _SPARSE_VARIANT(SparseOpaqueFactorization) SparseFactor(SparseFactorization_t type,
570
+ _SPARSE_VARIANT(SparseMatrix) Matrix) {
571
+ return SparseFactor(type, Matrix, _SparseDefaultSymbolicFactorOptions,
572
+ _SPARSE_VARIANT(_SparseDefaultNumericFactorOptions));
573
+ }
574
+
575
+ static inline SPARSE_PUBLIC_INTERFACE
576
+ _SPARSE_VARIANT(SparseOpaqueFactorization) SparseFactor(SparseFactorization_t type,
577
+ _SPARSE_VARIANT(SparseMatrix) Matrix, SparseSymbolicFactorOptions options,
578
+ SparseNumericFactorOptions nfoptions) {
579
+ SPARSE_CHECK_VALID_MATRIX_STRUCTURE(Matrix.structure, _SPARSE_VARIANT(_SparseFailedFactor)(SparseParameterError));
580
+ switch(type) {
581
+ case SparseFactorizationCholeskyAtA:
582
+ case SparseFactorizationQR:
583
+ return _SPARSE_VARIANT(_SparseFactorQR)(type, &Matrix, &options, &nfoptions);
584
+ #if !0
585
+ case SparseFactorizationLU:
586
+ case SparseFactorizationLUUnpivoted:
587
+ case SparseFactorizationLUSPP:
588
+ case SparseFactorizationLUTPP:
589
+ if(__builtin_available(macOS 15.5, macCatalyst 18.5, iOS 18.5, watchOS 11.5, tvOS 18.5, visionOS 2.5, *)) {
590
+ return _SPARSE_VARIANT(_SparseFactorLU)(type, &Matrix, &options, &nfoptions);
591
+ } else {
592
+ __builtin_verbose_trap("unsupported", "LU factorization is not supported on this OS version");
593
+ }
594
+ #endif // !TARGET_OS_BRIDGE
595
+ default:
596
+ #if (defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL) || 0
597
+ SPARSE_PARAMETER_CHECK(Matrix.structure.attributes.kind == SparseSymmetric,
598
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseParameterError),
599
+ "Cannot perform symmetric matrix factorization of non-symmetric matrix.\n");
600
+ return _SPARSE_VARIANT(_SparseFactorSymmetric)(type, &Matrix, &options, &nfoptions);
601
+ #else
602
+ if (Matrix.structure.attributes.kind == SparseSymmetric) {
603
+ if(__builtin_available(macOS 26.0, macCatalyst 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)) {
604
+ return _SPARSE_VARIANT(_SparseFactorSymmetric)(type, &Matrix, &options, &nfoptions);
605
+ } else {
606
+ __builtin_verbose_trap("unsupported", "Complex Symmetric matrices are not supported on this OS version");
607
+ }
608
+ } else {
609
+ SPARSE_PARAMETER_CHECK(Matrix.structure.attributes.kind == SparseHermitian,
610
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseParameterError),
611
+ "Cannot perform Hermitian matrix factorization of non-Hermitian matrix.\n");
612
+ return _SPARSE_VARIANT(_SparseFactorHermitian)(type, &Matrix, &options, &nfoptions);
613
+ }
614
+ #endif
615
+ }
616
+ }
617
+
618
+ /**** Sparse Factor Functions using pre-calculated symbolic factors ***********/
619
+
620
+ static inline SPARSE_PUBLIC_INTERFACE
621
+ _SPARSE_VARIANT(SparseOpaqueFactorization) SparseFactor(SparseOpaqueSymbolicFactorization SymbolicFactor,
622
+ _SPARSE_VARIANT(SparseMatrix) Matrix) {
623
+ return SparseFactor(SymbolicFactor, Matrix, _SPARSE_VARIANT(_SparseDefaultNumericFactorOptions), NULL, NULL);
624
+ }
625
+
626
+ static inline SPARSE_PUBLIC_INTERFACE
627
+ _SPARSE_VARIANT(SparseOpaqueFactorization) SparseFactor(SparseOpaqueSymbolicFactorization SymbolicFactor,
628
+ _SPARSE_VARIANT(SparseMatrix) Matrix, SparseNumericFactorOptions nfoptions) {
629
+ return SparseFactor(SymbolicFactor, Matrix, nfoptions, NULL, NULL);
630
+ }
631
+
632
+ /**** Sparse Factor Functions with user-defined workspace *********************/
633
+
634
+ static inline SPARSE_PUBLIC_INTERFACE
635
+ _SPARSE_VARIANT(SparseOpaqueFactorization) SparseFactor(SparseOpaqueSymbolicFactorization symbolicFactor,
636
+ _SPARSE_VARIANT(SparseMatrix) Matrix, SparseNumericFactorOptions nfoptions,
637
+ void *_Nullable factorStorage, void *_Nullable workspace) {
638
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions;
639
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(symbolicFactor,
640
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseParameterError),
641
+ "Bad symbolic factor.\n");
642
+ options = _SparseGetOptionsFromSymbolicFactor(&symbolicFactor);
643
+ SPARSE_CHECK_MATCH_SYMB_FACTOR(Matrix, symbolicFactor,
644
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseParameterError));
645
+ // User may or may not have supplied storage for factors and workspace. If they haven't, we
646
+ // allocate our own, the following two variables track which case we're in.
647
+ bool userFactorStorage = (factorStorage != NULL);
648
+ bool userWorkspace = (workspace != NULL);
649
+ if(!userFactorStorage) {
650
+ factorStorage = options.malloc(_SPARSE_REAL_SIZE(symbolicFactor.factorSize));
651
+ SPARSE_PARAMETER_CHECK(factorStorage != NULL,
652
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseInternalError),
653
+ "Failed to allocate factor storage of size %ld bytes.",
654
+ _SPARSE_REAL_SIZE(symbolicFactor.factorSize));
655
+ }
656
+ if(!userWorkspace) {
657
+ workspace = options.malloc(_SPARSE_REAL_SIZE(symbolicFactor.workspaceSize));
658
+ if(!workspace && !userFactorStorage) options.free(factorStorage);
659
+ SPARSE_PARAMETER_CHECK(workspace != NULL,
660
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseInternalError),
661
+ "Failed to allocate workspace of size %ld bytes.",
662
+ _SPARSE_REAL_SIZE(symbolicFactor.workspaceSize));
663
+ }
664
+ _SPARSE_VARIANT(SparseOpaqueFactorization) result;
665
+ switch(symbolicFactor.type) {
666
+ case SparseFactorizationCholeskyAtA:
667
+ case SparseFactorizationQR:
668
+ result = _SPARSE_VARIANT(_SparseNumericFactorQR)(&symbolicFactor, &Matrix, &nfoptions, factorStorage, workspace);
669
+ break;
670
+ #if !0
671
+ case SparseFactorizationLU:
672
+ case SparseFactorizationLUUnpivoted:
673
+ case SparseFactorizationLUSPP:
674
+ case SparseFactorizationLUTPP:
675
+ if(__builtin_available(macOS 15.5, macCatalyst 18.5, iOS 18.5, watchOS 11.5, tvOS 18.5, visionOS 2.5, *)) {
676
+ result = _SPARSE_VARIANT(_SparseNumericFactorLU)(&symbolicFactor, &Matrix, &nfoptions, factorStorage, workspace);
677
+ } else {
678
+ __builtin_verbose_trap("unsupported", "LU factorization is not supported on this OS version");
679
+ }
680
+ break;
681
+ #endif // !TARGET_OS_BRIDGE
682
+ default:
683
+ #ifdef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
684
+ result = _SPARSE_VARIANT(_SparseNumericFactorSymmetric)(&symbolicFactor, &Matrix, &nfoptions, factorStorage, workspace);
685
+ #else
686
+ if (Matrix.structure.attributes.kind == SparseSymmetric) {
687
+ if(__builtin_available(macOS 26.0, macCatalyst 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)) {
688
+ result = _SPARSE_VARIANT(_SparseNumericFactorSymmetric)(&symbolicFactor, &Matrix, &nfoptions, factorStorage, workspace);
689
+ } else {
690
+ __builtin_verbose_trap("unsupported", "Complex Symmetric matrices are not supported on this OS version");
691
+ }
692
+ } else {
693
+ result = _SPARSE_VARIANT(_SparseNumericFactorHermitian)(&symbolicFactor, &Matrix, &nfoptions, factorStorage, workspace);
694
+ }
695
+ #endif
696
+ }
697
+ result.userFactorStorage = userFactorStorage; // Flag whether we should free memory on destruction or not
698
+ if(!userWorkspace) options.free(workspace); // If we allocated workspace, free it
699
+ return result;
700
+ }
701
+
702
+ /******************************************************************************
703
+ * Sparse Direct Solve Functions (DenseMatrix)
704
+ ******************************************************************************/
705
+
706
+ static inline SPARSE_PUBLIC_INTERFACE
707
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseMatrix) XB) {
708
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
709
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
710
+ "Factored does not hold a completed matrix factorization.\n");
711
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
712
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
713
+ SPARSE_CHECK_CONSISTENT_DS_MAT_IN_PLACE(Factored, XB, /* no result */);
714
+ int nrhs = (XB.attributes.transpose) ? XB.rowCount : XB.columnCount; // number of rhs
715
+ size_t lworkspace = Factored.solveWorkspaceRequiredStatic + nrhs*Factored.solveWorkspaceRequiredPerRHS;
716
+ void *workspace = options.malloc(lworkspace);
717
+ SPARSE_PARAMETER_CHECK(workspace, /*no result*/, "Failed to allocate workspace of size %ld for SparseSolve().\n", lworkspace);
718
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, NULL, &XB, workspace);
719
+ options.free(workspace);
720
+ }
721
+
722
+ static inline SPARSE_PUBLIC_INTERFACE
723
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X) {
724
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
725
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
726
+ "Factored does not hold a completed matrix factorization.\n");
727
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
728
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
729
+ SPARSE_CHECK_CONSISTENT_DS_MAT_OUT_PLACE(Factored, B, X, /* no result */);
730
+ int nrhs = (B.attributes.transpose) ? B.rowCount : B.columnCount; // number of rhs
731
+ size_t lworkspace = Factored.solveWorkspaceRequiredStatic + nrhs*Factored.solveWorkspaceRequiredPerRHS;
732
+ void *workspace = options.malloc(lworkspace);
733
+ SPARSE_PARAMETER_CHECK(workspace, /*no result*/,
734
+ "Failed to allocate workspace of size %ld for SparseSolve().\n",
735
+ lworkspace);
736
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, &B, &X, workspace);
737
+ options.free(workspace);
738
+ }
739
+
740
+ /**** Solving Systems with User Defined Workspace *****************************/
741
+
742
+ static inline SPARSE_PUBLIC_INTERFACE
743
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseMatrix) XB, void *_Nonnull workspace) {
744
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
745
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
746
+ "Factored does not hold a completed matrix factorization.\n");
747
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
748
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
749
+ SPARSE_CHECK_CONSISTENT_DS_MAT_IN_PLACE(Factored, XB, /* no result */);
750
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, NULL, &XB, workspace);
751
+ }
752
+
753
+ static inline SPARSE_PUBLIC_INTERFACE
754
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X, void *_Nonnull workspace) {
755
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
756
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
757
+ "Factored does not hold a completed matrix factorization.\n");
758
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
759
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
760
+ SPARSE_CHECK_CONSISTENT_DS_MAT_OUT_PLACE(Factored, B, X, /* no result */);
761
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, &B, &X, workspace);
762
+ }
763
+
764
+ /******************************************************************************
765
+ * Sparse Direct Solve Functions (DenseVector)
766
+ ******************************************************************************/
767
+
768
+ static inline SPARSE_PUBLIC_INTERFACE
769
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseVector) xb) {
770
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
771
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
772
+ "Factored does not hold a completed matrix factorization.\n");
773
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
774
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
775
+ SPARSE_CHECK_CONSISTENT_DS_VEC_IN_PLACE(Factored, xb, /* no result */);
776
+ _SPARSE_VARIANT(DenseMatrix) XB = _SPARSE_VARIANT(_DenseMatrixFromVector)(xb);
777
+ size_t lworkspace = Factored.solveWorkspaceRequiredStatic + 1*Factored.solveWorkspaceRequiredPerRHS;
778
+ void *workspace = options.malloc(lworkspace);
779
+ SPARSE_PARAMETER_CHECK(workspace, /*no result*/, "Failed to allocate workspace of size %ld for SparseSolve().\n", lworkspace);
780
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, NULL, &XB, workspace);
781
+ options.free(workspace);
782
+ }
783
+
784
+ static inline SPARSE_PUBLIC_INTERFACE
785
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseVector) b, _SPARSE_VARIANT(DenseVector) x) {
786
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
787
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
788
+ "Factored does not hold a completed matrix factorization.\n");
789
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
790
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
791
+ SPARSE_CHECK_CONSISTENT_DS_VEC_OUT_PLACE(Factored, b, x, /* no result */);
792
+ _SPARSE_VARIANT(DenseMatrix) B = _SPARSE_VARIANT(_DenseMatrixFromVector)(b);
793
+ _SPARSE_VARIANT(DenseMatrix) X = _SPARSE_VARIANT(_DenseMatrixFromVector)(x);
794
+ size_t lworkspace = Factored.solveWorkspaceRequiredStatic + 1*Factored.solveWorkspaceRequiredPerRHS;
795
+ void *workspace = options.malloc(lworkspace);
796
+ SPARSE_PARAMETER_CHECK(workspace, /*no result*/,
797
+ "Failed to allocate workspace of size %ld for SparseSolve().\n",
798
+ lworkspace);
799
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, &B, &X, workspace);
800
+ options.free(workspace);
801
+ }
802
+
803
+ /**** Solving Systems with User Defined Workspace *****************************/
804
+
805
+ static inline SPARSE_PUBLIC_INTERFACE
806
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseVector) xb, void *_Nonnull workspace) {
807
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
808
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
809
+ "Factored does not hold a completed matrix factorization.\n");
810
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
811
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
812
+ SPARSE_CHECK_CONSISTENT_DS_VEC_IN_PLACE(Factored, xb, /* no result */);
813
+ _SPARSE_VARIANT(DenseMatrix) XB = _SPARSE_VARIANT(_DenseMatrixFromVector)(xb);
814
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, NULL, &XB, workspace);
815
+ }
816
+
817
+ static inline SPARSE_PUBLIC_INTERFACE
818
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueFactorization) Factored, _SPARSE_VARIANT(DenseVector) b, _SPARSE_VARIANT(DenseVector) x, void *_Nonnull workspace) {
819
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
820
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored.symbolicFactorization, /* no result */,
821
+ "Factored does not hold a completed matrix factorization.\n");
822
+ options = _SparseGetOptionsFromSymbolicFactor(&Factored.symbolicFactorization);
823
+ SPARSE_CHECK_VALID_NUMERIC_FACTOR(Factored, /* no result */);
824
+ SPARSE_CHECK_CONSISTENT_DS_VEC_OUT_PLACE(Factored, b, x, /* no result */);
825
+ _SPARSE_VARIANT(DenseMatrix) B = _SPARSE_VARIANT(_DenseMatrixFromVector)(b);
826
+ _SPARSE_VARIANT(DenseMatrix) X = _SPARSE_VARIANT(_DenseMatrixFromVector)(x);
827
+ _SPARSE_VARIANT(_SparseSolveOpaque)(&Factored, &B, &X, workspace);
828
+ }
829
+
830
+ /******************************************************************************
831
+ * Advanced Solving Functions
832
+ ******************************************************************************/
833
+
834
+ /**** Symbolic Refactor Functions *********************************************/
835
+
836
+ static inline SPARSE_PUBLIC_INTERFACE
837
+ void SparseRefactor(_SPARSE_VARIANT(SparseMatrix) Matrix, _SPARSE_VARIANT(SparseOpaqueFactorization) *_Nonnull Factorization) {
838
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
839
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factorization->symbolicFactorization,
840
+ /* no result */,
841
+ "Factorization does not hold a completed matrix factorization.\n");
842
+ SparseNumericFactorOptions nfoptions = _SPARSE_VARIANT(_SparseGetOptionsFromNumericFactor)(Factorization);
843
+ SparseRefactor(Matrix, Factorization, nfoptions);
844
+ }
845
+
846
+ static inline SPARSE_PUBLIC_INTERFACE
847
+ void SparseRefactor(_SPARSE_VARIANT(SparseMatrix) Matrix, _SPARSE_VARIANT(SparseOpaqueFactorization) *_Nonnull Factorization, SparseNumericFactorOptions nfoptions) {
848
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
849
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factorization->symbolicFactorization,
850
+ /* no result */,
851
+ "Factorization does not hold a valid symbolic matrix factorization.\n");
852
+ options = _SparseGetOptionsFromSymbolicFactor(&Factorization->symbolicFactorization);
853
+ void *workspace = options.malloc(_SPARSE_REAL_SIZE(Factorization->symbolicFactorization.workspaceSize));
854
+ if(!workspace) Factorization->status = SparseInternalError;
855
+ SPARSE_PARAMETER_CHECK(workspace,
856
+ /* no return value */,
857
+ "Failed to allocate workspace of size %ld.",
858
+ _SPARSE_REAL_SIZE(Factorization->symbolicFactorization.workspaceSize));
859
+
860
+ SparseRefactor(Matrix, Factorization, nfoptions, workspace);
861
+ options.free(workspace);
862
+ }
863
+
864
+ static inline SPARSE_PUBLIC_INTERFACE
865
+ void SparseRefactor(_SPARSE_VARIANT(SparseMatrix) Matrix, _SPARSE_VARIANT(SparseOpaqueFactorization) *_Nonnull Factored, void *_Nonnull workspace) {
866
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
867
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(Factored->symbolicFactorization,
868
+ /* no result */,
869
+ "Factored does not hold a completed matrix factorization.\n");
870
+ SparseNumericFactorOptions nfoptions = _SPARSE_VARIANT(_SparseGetOptionsFromNumericFactor)(Factored);
871
+ return SparseRefactor(Matrix, Factored, nfoptions, workspace);
872
+ }
873
+
874
+ static inline SPARSE_PUBLIC_INTERFACE
875
+ void SparseRefactor(_SPARSE_VARIANT(SparseMatrix) Matrix, _SPARSE_VARIANT(SparseOpaqueFactorization) *_Nonnull Factored, SparseNumericFactorOptions nfoptions, void *_Nonnull workspace) {
876
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
877
+ SPARSE_CHECK_MATCH_SYMB_FACTOR(Matrix, Factored->symbolicFactorization, /* no result */);
878
+ switch(Factored->symbolicFactorization.type) {
879
+ case SparseFactorizationCholeskyAtA:
880
+ case SparseFactorizationQR:
881
+ _SPARSE_VARIANT(_SparseRefactorQR)(&Matrix, Factored, &nfoptions, workspace);
882
+ break;
883
+ #if !0
884
+ case SparseFactorizationLU:
885
+ case SparseFactorizationLUUnpivoted:
886
+ case SparseFactorizationLUSPP:
887
+ case SparseFactorizationLUTPP:
888
+ if(__builtin_available(macOS 15.5, macCatalyst 18.5, iOS 18.5, watchOS 11.5, tvOS 18.5, visionOS 2.5, *)) {
889
+ _SPARSE_VARIANT(_SparseRefactorLU)(&Matrix, Factored, &nfoptions, workspace);
890
+ } else {
891
+ __builtin_verbose_trap("unsupported", "LU factorization is not supported on this OS version");
892
+ }
893
+ break;
894
+ #endif // !TARGET_OS_BRIDGE
895
+ default:
896
+ #ifdef _SPARSE_IMPLEMENTATION_TYPE_IS_REAL
897
+ _SPARSE_VARIANT(_SparseRefactorSymmetric)(&Matrix, Factored, &nfoptions, workspace);
898
+ #else
899
+ if (Matrix.structure.attributes.kind == SparseSymmetric) {
900
+ if(__builtin_available(macOS 26.0, macCatalyst 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)) {
901
+ _SPARSE_VARIANT(_SparseRefactorSymmetric)(&Matrix, Factored, &nfoptions, workspace);
902
+ } else {
903
+ __builtin_verbose_trap("unsupported", "Complex Symmetric matrices are not supported on this OS version");
904
+ }
905
+ } else {
906
+ _SPARSE_VARIANT(_SparseRefactorHermitian)(&Matrix, Factored, &nfoptions, workspace);
907
+ }
908
+ #endif
909
+ }
910
+ }
911
+
912
+ /******************************************************************************
913
+ * Sparse Update Functions
914
+ ******************************************************************************/
915
+ static inline SPARSE_PUBLIC_INTERFACE
916
+ void SparseUpdateFactor(SparseUpdate_t updateAlgorithm,
917
+ _SPARSE_VARIANT(SparseOpaqueFactorization) *_Nonnull Factorization,
918
+ int updateCount,
919
+ const int *_Nonnull updatedIndices,
920
+ _SPARSE_VARIANT(SparseMatrix) Update) {
921
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
922
+ SPARSE_CHECK_MATCH_SYMB_FACTOR(Update, Factorization->symbolicFactorization, /* no result */);
923
+ switch(Factorization->symbolicFactorization.type) {
924
+ case SparseFactorizationLUUnpivoted:
925
+ case SparseFactorizationLUSPP:
926
+ case SparseFactorizationLUTPP:
927
+ _SPARSE_VARIANT(_SparseUpdatePartialRefactorLU)(Factorization, updateCount, updatedIndices, Update);
928
+ break;
929
+ default:
930
+ Factorization->status = SparseParameterError;
931
+ SPARSE_PARAMETER_CHECK(false, /* no result */, "SparseUpdate is only supported on LU factorizations");
932
+ }
933
+ }
934
+
935
+ /******************************************************************************
936
+ * Extracting Sub-factors of Factors
937
+ ******************************************************************************/
938
+
939
+ static inline SPARSE_PUBLIC_INTERFACE
940
+ _SPARSE_VARIANT(SparseOpaqueSubfactor) SparseCreateSubfactor(SparseSubfactor_t subfactor,
941
+ _SPARSE_VARIANT(SparseOpaqueFactorization) Factor) {
942
+ // Check user supplied a valid factorization object
943
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
944
+ SPARSE_PARAMETER_CHECK(Factor.symbolicFactorization.status == SparseStatusOK &&
945
+ Factor.symbolicFactorization.factorization &&
946
+ Factor.status == SparseStatusOK &&
947
+ Factor.numericFactorization,
948
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
949
+ "Bad factor.\n");
950
+ options = _SparseGetOptionsFromSymbolicFactor(&Factor.symbolicFactorization);
951
+ // Check user is requesting a valid combination of subfactor and Factor
952
+ _SPARSE_ATTRIBUTES attributes = {
953
+ .transpose = false,
954
+ .triangle = SparseLowerTriangle,
955
+ .kind = SparseOrdinary,
956
+ };
957
+ SparseFactorization_t type = Factor.symbolicFactorization.type;
958
+ switch(subfactor) {
959
+ case SparseSubfactorP:
960
+ break; // Valid for all factor types
961
+
962
+ case SparseSubfactorL:
963
+ // Valid for Cholesky and LDL^T only
964
+ attributes.kind = SparseTriangular;
965
+ SPARSE_PARAMETER_CHECK(type == SparseFactorizationCholesky ||
966
+ type == SparseFactorizationLDLTUnpivoted ||
967
+ type == SparseFactorizationLDLTSBK ||
968
+ type == SparseFactorizationLDLTTPP,
969
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
970
+ "Subfactor Type only valid for Cholesky and LDL^T factorizations.\n");
971
+ break;
972
+
973
+ case SparseSubfactorPLPS:
974
+ // Valid for Cholesky and LDL^T only
975
+ SPARSE_PARAMETER_CHECK(type == SparseFactorizationCholesky ||
976
+ type == SparseFactorizationLDLTUnpivoted ||
977
+ type == SparseFactorizationLDLTSBK ||
978
+ type == SparseFactorizationLDLTTPP,
979
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
980
+ "Subfactor Type only valid for Cholesky and LDL^T factorizations.\n");
981
+ break;
982
+
983
+ case SparseSubfactorS:
984
+ // Valid for LDL^T only
985
+ SPARSE_PARAMETER_CHECK(type == SparseFactorizationLDLTUnpivoted ||
986
+ type == SparseFactorizationLDLTSBK ||
987
+ type == SparseFactorizationLDLTTPP,
988
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
989
+ "Subfactor Type only valid for LDL^T factorization.\n");
990
+ break;
991
+
992
+ #if !0
993
+ case SparseSubfactorSr:
994
+ case SparseSubfactorSc:
995
+ // Valid for LDL^T only
996
+ SPARSE_PARAMETER_CHECK(type == SparseFactorizationLUUnpivoted ||
997
+ type == SparseFactorizationLUSPP ||
998
+ type == SparseFactorizationLUTPP,
999
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
1000
+ "Subfactor Type only valid for LU factorization.\n");
1001
+ break;
1002
+ #endif // !TARGET_OS_BRIDGE
1003
+
1004
+ case SparseSubfactorD:
1005
+ // Valid for LDL^T only
1006
+ SPARSE_PARAMETER_CHECK(type == SparseFactorizationLDLTUnpivoted ||
1007
+ type == SparseFactorizationLDLTSBK ||
1008
+ type == SparseFactorizationLDLTTPP,
1009
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
1010
+ "Subfactor Type only valid for LDL^T factorizations.\n");
1011
+ break;
1012
+
1013
+ case SparseSubfactorQ:
1014
+ // Valid for QR or LU only
1015
+ SPARSE_PARAMETER_CHECK(type == SparseFactorizationQR
1016
+ #if !0
1017
+ || type == SparseFactorizationLUUnpivoted
1018
+ || type == SparseFactorizationLUSPP
1019
+ || type == SparseFactorizationLUTPP
1020
+ #endif // !TARGET_OS_BRIDGE
1021
+ ,
1022
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
1023
+ "SparseSubfactorQ only valid for QR or LU factorizations.\n");
1024
+ break;
1025
+
1026
+ case SparseSubfactorR:
1027
+ case SparseSubfactorRP:
1028
+ // Valid for QR and CholeskyAtA only
1029
+ attributes.kind = SparseTriangular;
1030
+ attributes.triangle = SparseUpperTriangle;
1031
+ SPARSE_PARAMETER_CHECK(type == SparseFactorizationQR ||
1032
+ type == SparseFactorizationCholeskyAtA,
1033
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(),
1034
+ "Subfactor Type only valid for QR and CholeskyAtA factorizations.\n");
1035
+ break;
1036
+
1037
+ default:
1038
+ SPARSE_PARAMETER_CHECK(false, _SPARSE_VARIANT(_SparseInvalidSubfactor)(), "Invalid subfactor type.");
1039
+ }
1040
+ _SPARSE_VARIANT(_SparseRetainNumeric)(&Factor); // count new reference
1041
+ size_t workspaceRequiredStatic, workspaceRequiredPerRHS;
1042
+ _SPARSE_VARIANT(_SparseGetWorkspaceRequired)(subfactor, Factor, &workspaceRequiredStatic, &workspaceRequiredPerRHS);
1043
+ return (_SPARSE_VARIANT(SparseOpaqueSubfactor)) {
1044
+ .attributes = attributes,
1045
+ .contents = subfactor,
1046
+ .factor = Factor,
1047
+ .workspaceRequiredStatic = workspaceRequiredStatic,
1048
+ .workspaceRequiredPerRHS = workspaceRequiredPerRHS
1049
+ };
1050
+ }
1051
+
1052
+ /******************************************************************************
1053
+ * Sub-factor Multiplication and Solve Functions
1054
+ ******************************************************************************/
1055
+
1056
+ /**** Matrix solve functions **************************************************/
1057
+
1058
+ static inline SPARSE_PUBLIC_INTERFACE
1059
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) XB) {
1060
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1061
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1062
+ "Subfactor does not hold a valid factorization.\n");
1063
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1064
+ // We assume this from this point on.
1065
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1066
+ // Figure out expected dimensions of x and b
1067
+ int m, n;
1068
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1069
+ // Check XY agrees
1070
+ int XBCount = (XB.attributes.transpose) ? XB.rowCount : XB.columnCount;
1071
+ int XBsize = (XB.attributes.transpose) ? XB.columnCount : XB.rowCount;
1072
+ SPARSE_PARAMETER_CHECK(XBCount > 0, /* none */, "XB must have non-zero dimension.\n");
1073
+ int maxmn = (m > n) ? m : n;
1074
+ SPARSE_PARAMETER_CHECK(XBsize == maxmn, /* none */,
1075
+ "XB dimension (%d) must match maximum subfactor dimension (%d).\n",
1076
+ XBsize, maxmn);
1077
+ size_t lworkspace = Subfactor.workspaceRequiredStatic + XBCount*Subfactor.workspaceRequiredPerRHS;
1078
+ void *workspace = options.malloc(lworkspace);
1079
+ SPARSE_PARAMETER_CHECK(workspace, /* none */,
1080
+ "Failed to allocate workspace of size %ld.\n", lworkspace);
1081
+ _SPARSE_VARIANT(_SparseSolveSubfactor)(&Subfactor, NULL, &XB, (char*)workspace);
1082
+ options.free(workspace);
1083
+ }
1084
+
1085
+ static inline SPARSE_PUBLIC_INTERFACE
1086
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X) {
1087
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1088
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1089
+ "Subfactor does not hold a valid factorization.\n");
1090
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1091
+ // We assume this from this point on.
1092
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1093
+ // Figure out expected dimensions of x and y
1094
+ int m, n;
1095
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1096
+ SPARSE_CHECK_CONSISTENT_MAT_OUT_PLACE(m, n, B, X, /* no result */, "subfactor dimension");
1097
+ int nrhs = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1098
+ size_t lworkspace = Subfactor.workspaceRequiredStatic + nrhs*Subfactor.workspaceRequiredPerRHS;
1099
+ void *workspace = options.malloc(lworkspace);
1100
+ SPARSE_PARAMETER_CHECK(workspace, /* none */,
1101
+ "Failed to allocate workspace of size %ld.\n",
1102
+ lworkspace);
1103
+ _SPARSE_VARIANT(_SparseSolveSubfactor)(&Subfactor, &B, &X, (char*)workspace);
1104
+ options.free(workspace);
1105
+ }
1106
+
1107
+ /**** Matrix solve functions with user-supplied workspace *********************/
1108
+
1109
+ static inline SPARSE_PUBLIC_INTERFACE
1110
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) XB, void *_Nonnull workspace) {
1111
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1112
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1113
+ "Subfactor does not hold a valid factor subobject.\n");
1114
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1115
+ // We assume this from this point on.
1116
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1117
+ // Figure out expected dimensions of x and b
1118
+ int m, n;
1119
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1120
+ // Check XY agrees
1121
+ int XBCount = (XB.attributes.transpose) ? XB.rowCount : XB.columnCount;
1122
+ int XBsize = (XB.attributes.transpose) ? XB.columnCount : XB.rowCount;
1123
+ SPARSE_PARAMETER_CHECK(XBCount > 0, /* none */, "XB must have non-zero dimension.\n");
1124
+ int maxmn = (m > n) ? m : n;
1125
+ SPARSE_PARAMETER_CHECK(XBsize == maxmn, /* none */,
1126
+ "XB dimension (%d) must match maximum subfactor dimension (%d).\n",
1127
+ XBsize, maxmn);
1128
+ _SPARSE_VARIANT(_SparseSolveSubfactor)(&Subfactor, NULL, &XB, (char*)workspace);
1129
+ }
1130
+
1131
+ static inline SPARSE_PUBLIC_INTERFACE
1132
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X,
1133
+ void *_Nonnull workspace) {
1134
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1135
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1136
+ "Subfactor does not hold a valid factor subobject.\n");
1137
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1138
+ // We assume this from this point on.
1139
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1140
+ // Figure out expected dimensions of x and y
1141
+ int m, n;
1142
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1143
+ SPARSE_CHECK_CONSISTENT_MAT_OUT_PLACE(m, n, B, X, /* no result */, "subfactor dimension");
1144
+ _SPARSE_VARIANT(_SparseSolveSubfactor)(&Subfactor, &B, &X, (char*)workspace);
1145
+ }
1146
+
1147
+ /**** Vector solve ************************************************************/
1148
+
1149
+ static inline SPARSE_PUBLIC_INTERFACE
1150
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseVector) XB) {
1151
+ _SPARSE_VARIANT(DenseMatrix) XBMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(XB);
1152
+ SparseSolve(Subfactor, XBMatrix);
1153
+ }
1154
+
1155
+ static inline SPARSE_PUBLIC_INTERFACE
1156
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseVector) B, _SPARSE_VARIANT(DenseVector) X) {
1157
+ _SPARSE_VARIANT(DenseMatrix) BMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(B);
1158
+ _SPARSE_VARIANT(DenseMatrix) XMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(X);
1159
+ SparseSolve(Subfactor, BMatrix, XMatrix);
1160
+ }
1161
+
1162
+ /**** Vector solve functions with user-supplied workspace *********************/
1163
+
1164
+ static inline SPARSE_PUBLIC_INTERFACE
1165
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseVector) XB, void *_Nonnull workspace) {
1166
+ _SPARSE_VARIANT(DenseMatrix) XBMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(XB);
1167
+ SparseSolve(Subfactor, XBMatrix, workspace);
1168
+ }
1169
+
1170
+ static inline SPARSE_PUBLIC_INTERFACE
1171
+ void SparseSolve(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseVector) B, _SPARSE_VARIANT(DenseVector) X,
1172
+ void *_Nonnull workspace) {
1173
+ _SPARSE_VARIANT(DenseMatrix) BMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(B);
1174
+ _SPARSE_VARIANT(DenseMatrix) XMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(X);
1175
+ SparseSolve(Subfactor, BMatrix, XMatrix, workspace);
1176
+ }
1177
+
1178
+ /**** Matrix multiply functions ***********************************************/
1179
+
1180
+ static inline SPARSE_PUBLIC_INTERFACE
1181
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) XY) {
1182
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1183
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1184
+ "Subfactor does not hold a valid factor subobject.\n");
1185
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1186
+ // We assume this from this point on.
1187
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1188
+ // Figure out expected dimensions of x and y
1189
+ int m, n;
1190
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1191
+ // Check XY agrees
1192
+ int XYCount = (XY.attributes.transpose) ? XY.rowCount : XY.columnCount;
1193
+ int XYsize = (XY.attributes.transpose) ? XY.columnCount : XY.rowCount;
1194
+ SPARSE_PARAMETER_CHECK(XYCount > 0, /* none */, "XY must have non-zero dimension.\n");
1195
+ int maxmn = (m > n) ? m : n;
1196
+ SPARSE_PARAMETER_CHECK(XYsize == maxmn, /* none */,
1197
+ "XY dimension (%d) must match maximum subfactor dimension (%d).\n",
1198
+ XYsize, maxmn);
1199
+ size_t lworkspace = Subfactor.workspaceRequiredStatic + XYCount*Subfactor.workspaceRequiredPerRHS;
1200
+ void *workspace = options.malloc(lworkspace);
1201
+ SPARSE_PARAMETER_CHECK(workspace, /* none */,
1202
+ "Failed to allocate workspace of size %ld.\n", lworkspace);
1203
+ _SPARSE_VARIANT(_SparseMultiplySubfactor)(&Subfactor, NULL, &XY, (char*)workspace);
1204
+ options.free(workspace);
1205
+ }
1206
+
1207
+ static inline SPARSE_PUBLIC_INTERFACE
1208
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) X, _SPARSE_VARIANT(DenseMatrix) Y) {
1209
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1210
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1211
+ "Subfactor does not hold a valid factor subobject.\n");
1212
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1213
+ // We assume this from this point on.
1214
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1215
+ // Figure out expected dimensions of x and y
1216
+ int m, n;
1217
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1218
+ SPARSE_CHECK_CONSISTENT_MAT_OUT_PLACE(n, m, X, Y, /* no result */, "subfactor dimension");
1219
+
1220
+ int nrhs = (Y.attributes.transpose) ? Y.rowCount : Y.columnCount;
1221
+ size_t lworkspace = Subfactor.workspaceRequiredStatic + nrhs*Subfactor.workspaceRequiredPerRHS;
1222
+ void *workspace = options.malloc(lworkspace);
1223
+ SPARSE_PARAMETER_CHECK(workspace, /* none */,
1224
+ "Failed to allocate workspace of size %ld.\n", lworkspace);
1225
+ _SPARSE_VARIANT(_SparseMultiplySubfactor)(&Subfactor, &X, &Y, (char*)workspace);
1226
+ options.free(workspace);
1227
+ }
1228
+
1229
+ /**** Matrix multiply functions with user-supplied workspace ******************/
1230
+
1231
+ static inline SPARSE_PUBLIC_INTERFACE
1232
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) XY, void *_Nonnull workspace) {
1233
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1234
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1235
+ "Subfactor does not hold a valid factor subobject.\n");
1236
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1237
+ // We assume this from this point on.
1238
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1239
+ // Figure out expected dimensions of x and y
1240
+ int m, n;
1241
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1242
+ // Check XY agrees
1243
+ int XYCount = (XY.attributes.transpose) ? XY.rowCount : XY.columnCount;
1244
+ int XYsize = (XY.attributes.transpose) ? XY.columnCount : XY.rowCount;
1245
+ SPARSE_PARAMETER_CHECK(XYCount > 0, /* none */, "XY must have non-zero dimension.\n");
1246
+ int maxmn = (m > n) ? m : n;
1247
+ SPARSE_PARAMETER_CHECK(XYsize == maxmn, /* none */,
1248
+ "XY dimension (%d) must match maximum subfactor dimension (%d).\n",
1249
+ XYsize, maxmn);
1250
+ _SPARSE_VARIANT(_SparseMultiplySubfactor)(&Subfactor, NULL, &XY, (char*)workspace);
1251
+ }
1252
+
1253
+ static inline SPARSE_PUBLIC_INTERFACE
1254
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseMatrix) X, _SPARSE_VARIANT(DenseMatrix) Y, void *_Nonnull workspace) {
1255
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1256
+ SPARSE_PARAMETER_CHECK(Subfactor.contents != SparseSubfactorInvalid, /* none */,
1257
+ "Subfactor does not hold a valid factor subobject.\n");
1258
+ // The only way to construct a valid Subfactor is to have a valid factorization.
1259
+ // We assume this from this point on.
1260
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1261
+ // Figure out expected dimensions of x and y
1262
+ int m, n;
1263
+ _SPARSE_VARIANT(_SparseSubFactorGetDimn)(Subfactor, &m, &n);
1264
+ SPARSE_CHECK_CONSISTENT_MAT_OUT_PLACE(m, n, Y, X, /* no result */, "subfactor dimension");
1265
+ _SPARSE_VARIANT(_SparseMultiplySubfactor)(&Subfactor, &X, &Y, (char*)workspace);
1266
+ }
1267
+
1268
+ /**** Vector multiply functions ***********************************************/
1269
+
1270
+ static inline SPARSE_PUBLIC_INTERFACE
1271
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseVector) XY) {
1272
+ _SPARSE_VARIANT(DenseMatrix) XYMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(XY);
1273
+ SparseMultiply(Subfactor, XYMatrix);
1274
+ }
1275
+
1276
+ static inline SPARSE_PUBLIC_INTERFACE
1277
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor,
1278
+ _SPARSE_VARIANT(DenseVector) X, _SPARSE_VARIANT(DenseVector) Y) {
1279
+ _SPARSE_VARIANT(DenseMatrix) XMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(X);
1280
+ _SPARSE_VARIANT(DenseMatrix) YMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(Y);
1281
+ SparseMultiply(Subfactor, XMatrix, YMatrix);
1282
+ }
1283
+
1284
+ /**** Vector multiply functions with user-supplied workspace ******************/
1285
+
1286
+ static inline SPARSE_PUBLIC_INTERFACE
1287
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor, _SPARSE_VARIANT(DenseVector) XY, void *_Nonnull workspace) {
1288
+ _SPARSE_VARIANT(DenseMatrix) XYMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(XY);
1289
+ SparseMultiply(Subfactor, XYMatrix, workspace);
1290
+ }
1291
+
1292
+ static inline SPARSE_PUBLIC_INTERFACE
1293
+ void SparseMultiply(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor,
1294
+ _SPARSE_VARIANT(DenseVector) X, _SPARSE_VARIANT(DenseVector) Y,
1295
+ void *_Nonnull workspace) {
1296
+ _SPARSE_VARIANT(DenseMatrix) XMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(X);
1297
+ _SPARSE_VARIANT(DenseMatrix) YMatrix = _SPARSE_VARIANT(_DenseMatrixFromVector)(Y);
1298
+ SparseMultiply(Subfactor, XMatrix, YMatrix, workspace);
1299
+ }
1300
+
1301
+ /******************************************************************************
1302
+ * Preconditioners
1303
+ ******************************************************************************/
1304
+
1305
+ static inline SPARSE_PUBLIC_INTERFACE
1306
+ _SPARSE_VARIANT(SparseOpaquePreconditioner) SparseCreatePreconditioner(
1307
+ SparsePreconditioner_t type, _SPARSE_VARIANT(SparseMatrix) A) {
1308
+ SparseIterativeMethod nullMethod = {};
1309
+ struct _SparseIterativeMethodBaseOptions options = nullMethod.options.base;
1310
+ int Am = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
1311
+ int An = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
1312
+ SPARSE_PARAMETER_CHECK(Am>0 && An>0,
1313
+ (_SPARSE_VARIANT(SparseOpaquePreconditioner)) { .type = SparsePreconditionerNone },
1314
+ "Bad matrix dimensions %dx%d\n", Am, An);
1315
+
1316
+ return _SPARSE_VARIANT(_SparseCreatePreconditioner) (type, &A);
1317
+ }
1318
+
1319
+ /******************************************************************************
1320
+ * Iterative Sparse Solve Functions
1321
+ ******************************************************************************/
1322
+
1323
+ /**** Solve without preconditioner ********************************************/
1324
+
1325
+ static inline SPARSE_PUBLIC_INTERFACE
1326
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method, _SPARSE_VARIANT(SparseMatrix) A,
1327
+ _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X) {
1328
+
1329
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1330
+ int Am = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
1331
+ int An = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
1332
+ SPARSE_PARAMETER_CHECK(Am>0 && An>0, SparseIterativeParameterError,
1333
+ "Bad matrix dimensions %dx%d\n", Am, An);
1334
+
1335
+ int Xm = (X.attributes.transpose) ? X.columnCount : X.rowCount;
1336
+ int Xn = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1337
+ int Bm = (B.attributes.transpose) ? B.columnCount : B.rowCount;
1338
+ int Bn = (B.attributes.transpose) ? B.rowCount : B.columnCount;
1339
+ SPARSE_PARAMETER_CHECK(Xn == Bn, SparseIterativeParameterError,
1340
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1341
+ SPARSE_PARAMETER_CHECK(Xm==An, SparseIterativeParameterError,
1342
+ "Dimensions of A (%dx%d) and X (%dx%d) do not match.", Am, An, Xm, Xn);
1343
+ SPARSE_PARAMETER_CHECK(Bm==Am, SparseIterativeParameterError,
1344
+ "Dimensions of A (%dx%d) and B (%dx%d) do not match.", Am, An, Bm, Bn);
1345
+
1346
+ switch(method.method) {
1347
+ case _SparseMethodCG:
1348
+ // For CG A must be square, so X and B must have matching dimensions
1349
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1350
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1351
+ return _SPARSE_VARIANT(_SparseCGSolve)(&method.options.cg, &X, &B,
1352
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1353
+ // For CG, trans is only ever false (and requires A==A^T anyway)
1354
+ if(accumulate) SparseMultiplyAdd(A, X_, Y);
1355
+ else SparseMultiply(A, X_, Y);
1356
+ }, NULL);
1357
+ case _SparseMethodGMRES:
1358
+ // For GMRES A must be square, so X and B must have matching dimensions
1359
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1360
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1361
+ return _SPARSE_VARIANT(_SparseGMRESSolve)(&method.options.gmres, &X, &B,
1362
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1363
+ // For GMRES, trans is only ever false
1364
+ if(accumulate) SparseMultiplyAdd(A, X_, Y);
1365
+ else SparseMultiply(A, X_, Y); }, NULL);
1366
+ case _SparseMethodLSMR:
1367
+ return _SPARSE_VARIANT(_SparseLSMRSolve)(&method.options.lsmr, &X, &B,
1368
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1369
+ if(accumulate) {
1370
+ if(trans==CblasNoTrans) SparseMultiplyAdd(A, X_, Y);
1371
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
1372
+ else SparseMultiplyAdd(SparseGetConjugateTranspose(A), X_, Y);
1373
+ #else
1374
+ else SparseMultiplyAdd(SparseGetTranspose(A), X_, Y);
1375
+ #endif
1376
+ } else {
1377
+ if(trans==CblasNoTrans) SparseMultiply(A, X_, Y);
1378
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
1379
+ else SparseMultiply(SparseGetConjugateTranspose(A), X_, Y);
1380
+ #else
1381
+ else SparseMultiply(SparseGetTranspose(A), X_, Y);
1382
+ #endif
1383
+ }
1384
+ }, NULL);
1385
+ default:
1386
+ __builtin_unreachable(); // All options considered above.
1387
+ }
1388
+ }
1389
+
1390
+ static inline SPARSE_PUBLIC_INTERFACE
1391
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method, _SPARSE_VARIANT(SparseMatrix) A,
1392
+ _SPARSE_VARIANT(DenseVector) b, _SPARSE_VARIANT(DenseVector) x) {
1393
+ return SparseSolve(method, A, _SPARSE_VARIANT(_DenseMatrixFromVector)(b),
1394
+ _SPARSE_VARIANT(_DenseMatrixFromVector)(x));
1395
+ }
1396
+
1397
+
1398
+ static inline SPARSE_PUBLIC_INTERFACE
1399
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method,
1400
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)),
1401
+ _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X) {
1402
+
1403
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1404
+ int Xm = (X.attributes.transpose) ? X.columnCount : X.rowCount;
1405
+ int Xn = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1406
+ int Bm = (B.attributes.transpose) ? B.columnCount : B.rowCount;
1407
+ int Bn = (B.attributes.transpose) ? B.rowCount : B.columnCount;
1408
+ SPARSE_PARAMETER_CHECK(Xm > 0 && Xn > 0, SparseIterativeParameterError,
1409
+ "Bad dimensions for X (%dx%d)\n", Xm, Xn);
1410
+ SPARSE_PARAMETER_CHECK(Bm > 0 && Bn > 0, SparseIterativeParameterError,
1411
+ "Bad dimensions for B (%dx%d)\n", Bm, Bn);
1412
+ SPARSE_PARAMETER_CHECK(Xn == Bn, SparseIterativeParameterError,
1413
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1414
+
1415
+ switch(method.method) {
1416
+ case _SparseMethodCG:
1417
+ // For CG A must be square, so X and B must have matching dimensions
1418
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1419
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1420
+ return _SPARSE_VARIANT(_SparseCGSolve)(&method.options.cg, &X, &B, ApplyOperator, NULL);
1421
+ case _SparseMethodGMRES:
1422
+ // For GMRES A must be square, so X and B must have matching dimensions
1423
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1424
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1425
+ return _SPARSE_VARIANT(_SparseGMRESSolve)(&method.options.gmres, &X, &B, ApplyOperator, NULL);
1426
+ case _SparseMethodLSMR:
1427
+ return _SPARSE_VARIANT(_SparseLSMRSolve)(&method.options.lsmr, &X, &B, ApplyOperator, NULL);
1428
+ default:
1429
+ __builtin_unreachable(); // All options considered above.
1430
+ }
1431
+ }
1432
+
1433
+ static inline SPARSE_PUBLIC_INTERFACE
1434
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method,
1435
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans,_SPARSE_VARIANT(DenseVector), _SPARSE_VARIANT(DenseVector)),
1436
+ _SPARSE_VARIANT(DenseVector) b, _SPARSE_VARIANT(DenseVector) x) {
1437
+
1438
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1439
+ int Xm = x.count;
1440
+ int Bm = b.count;
1441
+ SPARSE_PARAMETER_CHECK(Xm > 0, SparseIterativeParameterError,
1442
+ "Bad dimension for x (%dx%d)\n", Xm, 1);
1443
+ SPARSE_PARAMETER_CHECK(Bm > 0, SparseIterativeParameterError,
1444
+ "Bad dimensions for b (%dx%d)\n", Bm, 1);
1445
+
1446
+ _SPARSE_VARIANT(DenseMatrix) X = _SPARSE_VARIANT(_DenseMatrixFromVector)(x);
1447
+ _SPARSE_VARIANT(DenseMatrix) B = _SPARSE_VARIANT(_DenseMatrixFromVector)(b);
1448
+ // Note: we rely on the fact that neither X nor Y is transpose in calls to ApplyOperator()
1449
+ // and Precondition()
1450
+ switch(method.method) {
1451
+ case _SparseMethodCG:
1452
+ // Must be symmetric for CG
1453
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1454
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1455
+ return _SPARSE_VARIANT(_SparseCGSolve)(&method.options.cg, &X, &B,
1456
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1457
+ ApplyOperator(accumulate, trans,
1458
+ (_SPARSE_VARIANT(DenseVector)) { .count=X_.rowCount, .data=X_.data },
1459
+ (_SPARSE_VARIANT(DenseVector)) { .count=Y.rowCount, .data=Y.data }
1460
+ );
1461
+ }, NULL );
1462
+ case _SparseMethodGMRES:
1463
+ // Must be square for GMRES
1464
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1465
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1466
+ return _SPARSE_VARIANT(_SparseGMRESSolve)(&method.options.gmres, &X, &B,
1467
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1468
+ ApplyOperator(accumulate, trans,
1469
+ (_SPARSE_VARIANT(DenseVector)) { .count=X_.rowCount, .data=X_.data },
1470
+ (_SPARSE_VARIANT(DenseVector)) { .count=Y.rowCount, .data=Y.data }
1471
+ );
1472
+ }, NULL );
1473
+ case _SparseMethodLSMR:
1474
+ return _SPARSE_VARIANT(_SparseLSMRSolve)(&method.options.lsmr, &X, &B,
1475
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1476
+ ApplyOperator(accumulate, trans,
1477
+ (_SPARSE_VARIANT(DenseVector)) { .count=X_.rowCount, .data=X_.data },
1478
+ (_SPARSE_VARIANT(DenseVector)) { .count=Y.rowCount, .data=Y.data }
1479
+ );
1480
+ }, NULL );
1481
+ default:
1482
+ __builtin_unreachable(); // All options considered above.
1483
+ }
1484
+ }
1485
+
1486
+ /**** Solve with preconditioner ***********************************************/
1487
+
1488
+ static inline SPARSE_PUBLIC_INTERFACE
1489
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method, _SPARSE_VARIANT(SparseMatrix) A,
1490
+ _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X, SparsePreconditioner_t Preconditioner) {
1491
+
1492
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1493
+ int Am = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
1494
+ int An = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
1495
+ SPARSE_PARAMETER_CHECK(Am>0 && An>0, SparseIterativeParameterError,
1496
+ "Bad matrix dimensions %dx%d\n", Am, An);
1497
+
1498
+ int Xm = (X.attributes.transpose) ? X.columnCount : X.rowCount;
1499
+ int Xn = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1500
+ int Bm = (B.attributes.transpose) ? B.columnCount : B.rowCount;
1501
+ int Bn = (B.attributes.transpose) ? B.rowCount : B.columnCount;
1502
+ SPARSE_PARAMETER_CHECK(Xn == Bn, SparseIterativeParameterError,
1503
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1504
+ SPARSE_PARAMETER_CHECK(Xm==An, SparseIterativeParameterError,
1505
+ "Dimensions of A (%dx%d) and X (%dx%d) do not match.", Am, An, Xm, Xn);
1506
+ SPARSE_PARAMETER_CHECK(Bm==Am, SparseIterativeParameterError,
1507
+ "Dimensions of A (%dx%d) and B (%dx%d) do not match.", Am, An, Bm, Bn);
1508
+
1509
+ SPARSE_PARAMETER_CHECK(Preconditioner != SparsePreconditionerNone &&
1510
+ Preconditioner != SparsePreconditionerUser, SparseIterativeParameterError,
1511
+ "Invalid preconditioner type for this call: for no preconditioner, omit "
1512
+ "the parameter. User-supplied preconditioners must supply apply() method.");
1513
+
1514
+ // Create preconditioner
1515
+ _SPARSE_VARIANT(SparseOpaquePreconditioner) P =
1516
+ _SPARSE_VARIANT(_SparseCreatePreconditioner) (Preconditioner, &A);
1517
+ if(P.type == SparsePreconditionerNone) return SparseIterativeInternalError;
1518
+
1519
+ // Call CG
1520
+ SparseIterativeStatus_t result;
1521
+ switch(method.method) {
1522
+ case _SparseMethodCG:
1523
+ result = _SPARSE_VARIANT(_SparseCGSolve)(&method.options.cg, &X, &B,
1524
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1525
+ // For CG, trans is only ever false
1526
+ if(accumulate) SparseMultiplyAdd(A, X_, Y);
1527
+ else SparseMultiply(A, X_, Y);
1528
+ }, &P);
1529
+ break;
1530
+ case _SparseMethodGMRES:
1531
+ result = _SPARSE_VARIANT(_SparseGMRESSolve)(&method.options.gmres, &X, &B,
1532
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1533
+ // For GMRES, trans is only ever false
1534
+ if(accumulate) SparseMultiplyAdd(A, X_, Y);
1535
+ else SparseMultiply(A, X_, Y);
1536
+ }, &P);
1537
+ break;
1538
+ case _SparseMethodLSMR:
1539
+ result = _SPARSE_VARIANT(_SparseLSMRSolve)(&method.options.lsmr, &X, &B,
1540
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1541
+ if(accumulate) {
1542
+ if(trans==CblasNoTrans) SparseMultiplyAdd(A, X_, Y);
1543
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
1544
+ else SparseMultiplyAdd(SparseGetConjugateTranspose(A), X_, Y);
1545
+ #else
1546
+ else SparseMultiplyAdd(SparseGetTranspose(A), X_, Y);
1547
+ #endif
1548
+ } else {
1549
+ if(trans==CblasNoTrans) SparseMultiply(A, X_, Y);
1550
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
1551
+ else SparseMultiply(SparseGetConjugateTranspose(A), X_, Y);
1552
+ #else
1553
+ else SparseMultiply(SparseGetTranspose(A), X_, Y);
1554
+ #endif
1555
+ }
1556
+ }, &P);
1557
+ break;
1558
+ default:
1559
+ __builtin_unreachable(); // All options considered above.
1560
+ }
1561
+
1562
+ // Release preconditioner
1563
+ _SPARSE_VARIANT(_SparseReleaseOpaquePreconditioner)(&P);
1564
+
1565
+ return result;
1566
+ }
1567
+
1568
+ static inline SPARSE_PUBLIC_INTERFACE
1569
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method, _SPARSE_VARIANT(SparseMatrix) A,
1570
+ _SPARSE_VARIANT(DenseVector) b, _SPARSE_VARIANT(DenseVector) x,
1571
+ SparsePreconditioner_t Preconditioner) {
1572
+ return SparseSolve(method, A, _SPARSE_VARIANT(_DenseMatrixFromVector)(b),
1573
+ _SPARSE_VARIANT(_DenseMatrixFromVector)(x));
1574
+ }
1575
+
1576
+ static inline SPARSE_PUBLIC_INTERFACE
1577
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method, _SPARSE_VARIANT(SparseMatrix) A,
1578
+ _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X,
1579
+ _SPARSE_VARIANT(SparseOpaquePreconditioner) Preconditioner) {
1580
+
1581
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1582
+
1583
+ int Am = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.columnCount : A.structure.rowCount);
1584
+ int An = A.structure.blockSize * ((A.structure.attributes.transpose) ? A.structure.rowCount : A.structure.columnCount);
1585
+ SPARSE_PARAMETER_CHECK(Am>0 && An>0, SparseIterativeParameterError,
1586
+ "Bad matrix dimensions %dx%d\n", Am, An);
1587
+ int Xm = (X.attributes.transpose) ? X.columnCount : X.rowCount;
1588
+ int Xn = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1589
+ int Bm = (B.attributes.transpose) ? B.columnCount : B.rowCount;
1590
+ int Bn = (B.attributes.transpose) ? B.rowCount : B.columnCount;
1591
+ SPARSE_PARAMETER_CHECK(Xn == Bn, SparseIterativeParameterError,
1592
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1593
+ SPARSE_PARAMETER_CHECK(Xm==An, SparseIterativeParameterError,
1594
+ "Dimensions of A (%dx%d) and X (%dx%d) do not match.", Am, An, Xm, Xn);
1595
+ SPARSE_PARAMETER_CHECK(Bm==Am, SparseIterativeParameterError,
1596
+ "Dimensions of A (%dx%d) and B (%dx%d) do not match.", Am, An, Bm, Bn);
1597
+
1598
+ switch(method.method) {
1599
+ case _SparseMethodCG:
1600
+ // For CG A must be square, so X and B must have matching dimensions
1601
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1602
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1603
+ return _SPARSE_VARIANT(_SparseCGSolve)(&method.options.cg, &X, &B,
1604
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1605
+ // For CG, trans is only ever false (and requires A==A^T anyway)
1606
+ if(accumulate) SparseMultiplyAdd(A, X_, Y);
1607
+ else SparseMultiply(A, X_, Y);
1608
+ }, &Preconditioner);
1609
+ case _SparseMethodGMRES:
1610
+ // For GMRES A must be square, so X and B must have matching dimensions
1611
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1612
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1613
+ return _SPARSE_VARIANT(_SparseGMRESSolve)(&method.options.gmres, &X, &B,
1614
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1615
+ // For GMRES, trans is only ever false
1616
+ if(accumulate) SparseMultiplyAdd(A, X_, Y);
1617
+ else SparseMultiply(A, X_, Y);
1618
+ }, &Preconditioner);
1619
+ case _SparseMethodLSMR:
1620
+ return _SPARSE_VARIANT(_SparseLSMRSolve)(&method.options.lsmr, &X, &B,
1621
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1622
+ if(accumulate) {
1623
+ if(trans==CblasNoTrans) SparseMultiplyAdd(A, X_, Y);
1624
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
1625
+ else SparseMultiplyAdd(SparseGetConjugateTranspose(A), X_, Y);
1626
+ #else
1627
+ else SparseMultiplyAdd(SparseGetTranspose(A), X_, Y);
1628
+ #endif
1629
+ } else {
1630
+ if(trans==CblasNoTrans) SparseMultiply(A, X_, Y);
1631
+ #if !defined _SPARSE_IMPLEMENTATION_TYPE_IS_REAL && !0
1632
+ else SparseMultiply(SparseGetConjugateTranspose(A), X_, Y);
1633
+ #else
1634
+ else SparseMultiply(SparseGetTranspose(A), X_, Y);
1635
+ #endif
1636
+ }
1637
+ }, &Preconditioner);
1638
+ default:
1639
+ __builtin_unreachable(); // All options considered above.
1640
+ }
1641
+ }
1642
+
1643
+ static inline SPARSE_PUBLIC_INTERFACE
1644
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method, _SPARSE_VARIANT(SparseMatrix) A,
1645
+ _SPARSE_VARIANT(DenseVector) b, _SPARSE_VARIANT(DenseVector) x,
1646
+ _SPARSE_VARIANT(SparseOpaquePreconditioner) Preconditioner) {
1647
+ return SparseSolve(method, A, _SPARSE_VARIANT(_DenseMatrixFromVector)(b),
1648
+ _SPARSE_VARIANT(_DenseMatrixFromVector)(x), Preconditioner);
1649
+ }
1650
+
1651
+ static inline SPARSE_PUBLIC_INTERFACE
1652
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method,
1653
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)) ,
1654
+ _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) X,
1655
+ _SPARSE_VARIANT(SparseOpaquePreconditioner) Preconditioner) {
1656
+
1657
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1658
+ int Xm = (X.attributes.transpose) ? X.columnCount : X.rowCount;
1659
+ int Xn = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1660
+ int Bm = (B.attributes.transpose) ? B.columnCount : B.rowCount;
1661
+ int Bn = (B.attributes.transpose) ? B.rowCount : B.columnCount;
1662
+ SPARSE_PARAMETER_CHECK(Xm > 0 && Xn > 0, SparseIterativeParameterError,
1663
+ "Bad dimensions for X (%dx%d)\n", Xm, Xn);
1664
+ SPARSE_PARAMETER_CHECK(Bm > 0 && Bn > 0, SparseIterativeParameterError,
1665
+ "Bad dimensions for B (%dx%d)\n", Bm, Bn);
1666
+ SPARSE_PARAMETER_CHECK(Xn == Bn, SparseIterativeParameterError,
1667
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1668
+
1669
+ switch(method.method) {
1670
+ case _SparseMethodCG:
1671
+ // For CG A must be square, so X and B must have matching dimensions
1672
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1673
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1674
+ return _SPARSE_VARIANT(_SparseCGSolve)(&method.options.cg, &X, &B, ApplyOperator, &Preconditioner);
1675
+ case _SparseMethodGMRES:
1676
+ // For GMRES A must be square, so X and B must have matching dimensions
1677
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1678
+ "Dimensions of X (%dx%d) and B (%dx%d) do not match.", Xm, Xn, Bm, Bn);
1679
+ return _SPARSE_VARIANT(_SparseGMRESSolve)(&method.options.gmres, &X, &B, ApplyOperator, &Preconditioner);
1680
+ case _SparseMethodLSMR:
1681
+ return _SPARSE_VARIANT(_SparseLSMRSolve)(&method.options.lsmr, &X, &B, ApplyOperator, &Preconditioner);
1682
+ default:
1683
+ __builtin_unreachable(); // All options considered above.
1684
+ }
1685
+ }
1686
+
1687
+
1688
+ static inline SPARSE_PUBLIC_INTERFACE
1689
+ SparseIterativeStatus_t SparseSolve(SparseIterativeMethod method,
1690
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseVector), _SPARSE_VARIANT(DenseVector)),
1691
+ _SPARSE_VARIANT(DenseVector) b, _SPARSE_VARIANT(DenseVector) x,
1692
+ _SPARSE_VARIANT(SparseOpaquePreconditioner) Preconditioner) {
1693
+
1694
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1695
+ int Xm = x.count;
1696
+ int Bm = b.count;
1697
+
1698
+ SPARSE_PARAMETER_CHECK(Xm > 0, SparseIterativeParameterError,
1699
+ "Bad dimension for x (%dx%d)\n", Xm, 1);
1700
+ SPARSE_PARAMETER_CHECK(Bm > 0, SparseIterativeParameterError,
1701
+ "Bad dimensions for b (%dx%d)\n", Bm, 1);
1702
+
1703
+ _SPARSE_VARIANT(DenseMatrix) X = _SPARSE_VARIANT(_DenseMatrixFromVector)(x);
1704
+ _SPARSE_VARIANT(DenseMatrix) B = _SPARSE_VARIANT(_DenseMatrixFromVector)(b);
1705
+
1706
+ // Note: we rely on the fact that neither X nor Y is transpose in calls to ApplyOperator()
1707
+ // and Precondition() (true as input matrices are not-transpose)
1708
+ switch(method.method) {
1709
+ case _SparseMethodCG:
1710
+ // For CG A must be square, so X and B must have matching dimensions
1711
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1712
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1713
+ return _SPARSE_VARIANT(_SparseCGSolve)(&method.options.cg, &X, &B,
1714
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1715
+ ApplyOperator(accumulate, trans,
1716
+ (_SPARSE_VARIANT(DenseVector)) { .count=X_.rowCount, .data=X_.data },
1717
+ (_SPARSE_VARIANT(DenseVector)) { .count=Y.rowCount, .data=Y.data }
1718
+ );
1719
+ }, &Preconditioner );
1720
+ case _SparseMethodGMRES:
1721
+ // For GMRES A must be square, so X and B must have matching dimensions
1722
+ SPARSE_PARAMETER_CHECK(Xm == Bm, SparseIterativeParameterError,
1723
+ "Counts of X (%d) and B (%d) do not match.", Xm, Bm);
1724
+ return _SPARSE_VARIANT(_SparseGMRESSolve)(&method.options.gmres, &X, &B,
1725
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1726
+ ApplyOperator(accumulate, trans,
1727
+ (_SPARSE_VARIANT(DenseVector)) { .count=X_.rowCount, .data=X_.data },
1728
+ (_SPARSE_VARIANT(DenseVector)) { .count=Y.rowCount, .data=Y.data }
1729
+ );
1730
+ }, &Preconditioner );
1731
+ case _SparseMethodLSMR:
1732
+ return _SPARSE_VARIANT(_SparseLSMRSolve)(&method.options.lsmr, &X, &B,
1733
+ ^void(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix) X_, _SPARSE_VARIANT(DenseMatrix) Y) {
1734
+ ApplyOperator(accumulate, trans,
1735
+ (_SPARSE_VARIANT(DenseVector)) { .count=X_.rowCount, .data=X_.data },
1736
+ (_SPARSE_VARIANT(DenseVector)) { .count=Y.rowCount, .data=Y.data }
1737
+ );
1738
+ }, &Preconditioner );
1739
+ default:
1740
+ __builtin_unreachable(); // All options considered above.
1741
+ }
1742
+ }
1743
+
1744
+ /******************************************************************************
1745
+ * Sparse Single-step Iteration Functions
1746
+ ******************************************************************************/
1747
+
1748
+ static inline SPARSE_PUBLIC_INTERFACE
1749
+ size_t _SPARSE_VARIANT(SparseGetStateSize)(SparseIterativeMethod method, bool preconditioner, int m, int n, int nrhs) {
1750
+ return _SPARSE_VARIANT(_SparseGetIterativeStateSize)(&method, preconditioner, m, n, nrhs);
1751
+ }
1752
+
1753
+ static inline SPARSE_PUBLIC_INTERFACE
1754
+ void SparseIterate(SparseIterativeMethod method, int iteration, const bool *_Nonnull converged, void *_Nonnull state,
1755
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)),
1756
+ _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) R, _SPARSE_VARIANT(DenseMatrix) X,
1757
+ _SPARSE_VARIANT(SparseOpaquePreconditioner) Preconditioner) {
1758
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1759
+ int Xm = (X.attributes.transpose) ? X.columnCount : X.rowCount;
1760
+ int Xn = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1761
+ SPARSE_PARAMETER_CHECK(Xm>0 && Xn>0, /* no result */, "Invalid size for X (%dx%d).", Xm, Xn);
1762
+ int Bm = (B.attributes.transpose) ? B.columnCount : B.rowCount;
1763
+ int Bn = (B.attributes.transpose) ? B.rowCount : B.columnCount;
1764
+ SPARSE_PARAMETER_CHECK(Bm>0 && Bn>0, /* no result */, "Invalid size for B (%dx%d).", Bm, Bn);
1765
+ int Rm = (R.attributes.transpose) ? R.columnCount : R.rowCount;
1766
+ int Rn = (R.attributes.transpose) ? R.rowCount : R.columnCount;
1767
+ SPARSE_PARAMETER_CHECK(Rm>0 && Rn>0, /* no result */, "Invalid size for R (%dx%d).", Rm, Rn);
1768
+ SPARSE_PARAMETER_CHECK(Xn==Bn && Bn==Rn, /* no result */,
1769
+ "Sizes of X (%dx%d), B (%dx%d) and R (%dx%d) are inconsistent.", Xm, Xn, Bm, Bn, Rm, Bn);
1770
+ SPARSE_PARAMETER_CHECK(Rm>=Bm, /* no result */,
1771
+ "Sizes of residual matrix R(%dx%d) must be at least as large as right-hand side B (%dx%d).",
1772
+ Rm, Rn, Bm, Rn);
1773
+
1774
+ switch(method.method) {
1775
+ case _SparseMethodCG:
1776
+ _SPARSE_VARIANT(_SparseCGIterate) (&method.options.cg, iteration, (char*)state, converged,
1777
+ &X, &B, &R, &Preconditioner, ApplyOperator);
1778
+ break;
1779
+ case _SparseMethodGMRES:
1780
+ _SPARSE_VARIANT(_SparseGMRESIterate) (&method.options.gmres, iteration, (char*)state, converged,
1781
+ &X, &B, &R, &Preconditioner, ApplyOperator);
1782
+ break;
1783
+ case _SparseMethodLSMR:
1784
+ _SPARSE_VARIANT(_SparseLSMRIterate) (&method.options.lsmr, iteration, (char*)state, converged,
1785
+ &X, &B, &R, &Preconditioner, ApplyOperator);
1786
+ break;
1787
+ default:
1788
+ __builtin_unreachable(); // All options considered above.
1789
+ }
1790
+ }
1791
+
1792
+ static inline SPARSE_PUBLIC_INTERFACE
1793
+ void SparseIterate(SparseIterativeMethod method, int iteration, const bool *_Nonnull converged, void *_Nonnull state,
1794
+ void (^_Nonnull ApplyOperator)(bool accumulate, enum CBLAS_TRANSPOSE trans, _SPARSE_VARIANT(DenseMatrix), _SPARSE_VARIANT(DenseMatrix)),
1795
+ _SPARSE_VARIANT(DenseMatrix) B, _SPARSE_VARIANT(DenseMatrix) R, _SPARSE_VARIANT(DenseMatrix) X) {
1796
+ struct _SparseIterativeMethodBaseOptions options = method.options.base;
1797
+ int Xm = (X.attributes.transpose) ? X.columnCount : X.rowCount;
1798
+ int Xn = (X.attributes.transpose) ? X.rowCount : X.columnCount;
1799
+ SPARSE_PARAMETER_CHECK(Xm>0 && Xn>0, /* no result */, "Invalid size for X (%dx%d).", Xm, Xn);
1800
+ int Bm = (B.attributes.transpose) ? B.columnCount : B.rowCount;
1801
+ int Bn = (B.attributes.transpose) ? B.rowCount : B.columnCount;
1802
+ SPARSE_PARAMETER_CHECK(Bm>0 && Bn>0, /* no result */, "Invalid size for B (%dx%d).", Bm, Bn);
1803
+ int Rm = (R.attributes.transpose) ? R.columnCount : R.rowCount;
1804
+ int Rn = (R.attributes.transpose) ? R.rowCount : R.columnCount;
1805
+ SPARSE_PARAMETER_CHECK(Rm>0 && Rn>0, /* no result */, "Invalid size for R (%dx%d).", Rm, Rn);
1806
+ SPARSE_PARAMETER_CHECK(Xn==Bn && Bn==Rn, /* no result */,
1807
+ "Sizes of X (%dx%d), B (%dx%d) and R (%dx%d) are inconsistent.", Xm, Xn, Bm, Bn, Rm, Bn);
1808
+ SPARSE_PARAMETER_CHECK(Rm>=Bm, /* no result */,
1809
+ "Sizes of residual matrix R(%dx%d) must be at least as large as right-hand side B (%dx%d).",
1810
+ Rm, Rn, Bm, Rn);
1811
+
1812
+ switch(method.method) {
1813
+ case _SparseMethodCG:
1814
+ _SPARSE_VARIANT(_SparseCGIterate) (&method.options.cg, iteration, (char*)state, converged,
1815
+ &X, &B, &R, NULL, ApplyOperator);
1816
+ break;
1817
+ case _SparseMethodGMRES:
1818
+ _SPARSE_VARIANT(_SparseGMRESIterate) (&method.options.gmres, iteration, (char*)state, converged,
1819
+ &X, &B, &R, NULL, ApplyOperator);
1820
+ break;
1821
+ case _SparseMethodLSMR:
1822
+ _SPARSE_VARIANT(_SparseLSMRIterate) (&method.options.lsmr, iteration, (char*)state, converged,
1823
+ &X, &B, &R, NULL, ApplyOperator);
1824
+ break;
1825
+ default:
1826
+ __builtin_unreachable(); // All options considered above.
1827
+ }
1828
+ }
1829
+
1830
+ /******************************************************************************
1831
+ * Memory Management
1832
+ ******************************************************************************/
1833
+
1834
+ /**** Retaining resources *****************************************************/
1835
+
1836
+ static inline SPARSE_PUBLIC_INTERFACE
1837
+ _SPARSE_VARIANT(SparseOpaqueFactorization) SparseRetain(_SPARSE_VARIANT(SparseOpaqueFactorization) NumericFactor) {
1838
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1839
+ SPARSE_CHECK_VALID_SYMBOLIC_FACTOR(NumericFactor.symbolicFactorization,
1840
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseParameterError),
1841
+ "Can only retain valid numeric factorizations.\n")
1842
+ options = _SparseGetOptionsFromSymbolicFactor(&NumericFactor.symbolicFactorization);
1843
+ SPARSE_PARAMETER_CHECK(NumericFactor.status == SparseStatusOK && NumericFactor.numericFactorization,
1844
+ _SPARSE_VARIANT(_SparseFailedFactor)(SparseParameterError),
1845
+ "Can only retain valid numeric factorizations.\n");
1846
+ _SPARSE_VARIANT(_SparseRetainNumeric)(&NumericFactor);
1847
+ return NumericFactor;
1848
+ }
1849
+
1850
+ static inline SPARSE_PUBLIC_INTERFACE
1851
+ _SPARSE_VARIANT(SparseOpaqueSubfactor) SparseRetain(_SPARSE_VARIANT(SparseOpaqueSubfactor) Subfactor) {
1852
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // We need default error reporting until we verify valid factor
1853
+ SPARSE_PARAMETER_CHECK(Subfactor.factor.symbolicFactorization.status == SparseStatusOK &&
1854
+ Subfactor.factor.symbolicFactorization.factorization,
1855
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(), "Can only retain valid objects.\n");
1856
+ options = _SparseGetOptionsFromSymbolicFactor(&Subfactor.factor.symbolicFactorization);
1857
+ SPARSE_PARAMETER_CHECK(Subfactor.factor.status == SparseStatusOK && Subfactor.factor.numericFactorization,
1858
+ _SPARSE_VARIANT(_SparseInvalidSubfactor)(), "Can only retain valid objects.\n");
1859
+ _SPARSE_VARIANT(_SparseRetainNumeric)(&Subfactor.factor);
1860
+ return Subfactor;
1861
+ }
1862
+
1863
+ /**** Cleaning up resources ***************************************************/
1864
+
1865
+ static inline SPARSE_PUBLIC_INTERFACE
1866
+ void SparseCleanup(_SPARSE_VARIANT(SparseOpaqueFactorization) toFree) {
1867
+ _SPARSE_VARIANT(_SparseDestroyOpaqueNumeric)(&toFree);
1868
+ }
1869
+
1870
+ static inline SPARSE_PUBLIC_INTERFACE
1871
+ void SparseCleanup(_SPARSE_VARIANT(SparseOpaqueSubfactor) toFree) {
1872
+ _SPARSE_VARIANT(_SparseDestroyOpaqueNumeric)(&toFree.factor);
1873
+ }
1874
+
1875
+ static inline SPARSE_PUBLIC_INTERFACE
1876
+ void SparseCleanup(_SPARSE_VARIANT(SparseMatrix) toFree) {
1877
+ SparseSymbolicFactorOptions options = _SparseDefaultSymbolicFactorOptions; // Default error reporting.
1878
+ SPARSE_PARAMETER_CHECK(toFree.structure.attributes._allocatedBySparse == true, ,
1879
+ "Attempting to call SparseCleanup on a matrix that was not allocated by the Sparse library.\n");
1880
+ free(toFree.structure.columnStarts);
1881
+ }
1882
+
1883
+ static inline SPARSE_PUBLIC_INTERFACE
1884
+ void SparseCleanup(_SPARSE_VARIANT(SparseOpaquePreconditioner) Preconditioner) {
1885
+ SparseIterativeMethod nullMethod = {};
1886
+ struct _SparseIterativeMethodBaseOptions options = nullMethod.options.base;
1887
+ SPARSE_PARAMETER_CHECK(Preconditioner.type!=SparsePreconditionerNone && Preconditioner.type!=SparsePreconditionerUser,
1888
+ /* no result */, "Cannot cleanup this kind of Preconditioner.");
1889
+
1890
+ _SPARSE_VARIANT(_SparseReleaseOpaquePreconditioner) (&Preconditioner);
1891
+ }
1892
+
1893
+ /*********************************** END **************************************/