# New LANumerics package for numerical linear algebra in Swift

**URL:** <https://forums.swift.org/t/new-lanumerics-package-for-numerical-linear-algebra-in-swift/36822>\
**Category:** Swift Numerics\
**Created:** [May 24, 2020, 10:42am UTC](https://forums.swift.org/t/new-lanumerics-package-for-numerical-linear-algebra-in-swift/36822 "2020-05-24T10:42:01Z")\
**Posts on this page:** 2\
**Page:** 3

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [July 18, 2020, 2:40pm UTC](https://forums.swift.org/t/new-lanumerics-package-for-numerical-linear-algebra-in-swift/36822/41 "2020-07-18T14:40:05Z")

</div>

> [@Andrew\_Trick](#):
>
> But you have would have five levels of nested `withMemoryRebound` calls. Unless @Karoy_Lorentey or @scanon can think of a better way to do this.

It might be nice to have a free function that could do multiple rebinds at once to avoid the nesting:

```swift
withReboundMemory(to:
  (a, __CLPK_complex.self), 
  (s, __CLPK_complex.self) // SE-0257 :'(
) { a_recast, s_recast in
  ...
}

```

(of course you'd need variadics to define it "properly")

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [July 29, 2020, 5:19am UTC](https://forums.swift.org/t/new-lanumerics-package-for-numerical-linear-algebra-in-swift/36822/42 "2020-07-29T05:19:44Z")

</div>

Sorry for the delayed response. You are using `recast`, which is a wrapper around `assumingMemoryBound(to:)`, in two interesting cases:

The first is to reconcile a pointer-type mismatch between C APIs. You're passing a pointer to Numerics' Complex type into Accelerate's \_\_CLPK\_complex type:

```swift
DSPSplitComplex(realp: recast(&real), ...

```

The sanctioned way to do this is to call `withMemoryRebound(to:capacity:)`. Then as long as you follow the usual rules for pointer-generating closures (don't escape the pointer out of the closure), then you're safe. As time goes on, the compiler will get better at enforcing that rule to catch mistakes.

Even though your code is using an unsafe approach that can't be checked by the compiler, it actually safe in practice. It cannot be miscompiled as-is because you are following the following unwritten rules for type punning across compilation boundaries:

- Swift code is only accessing Numerics' Complex type. It never loads or stores using  
Accelerate's type.

- You only use `recast` within an argument expression of a call into  
Accelerate. So the only code accessing that type is inside  
Accelerate.

* * *

In the second case, you have a pointer to a generic type

```swift
X : UnsafePointer<Self>

```

And dynamically know the concrete type:

```swift
UnsafePointer<Float> = recast(X)

```

That is original motivation for the `assumingMemoryBound(to:)`, and if I'm reading the code correctly, what you're doing is perfectly safe.

[Previous page](https://forums.swift.org/t/new-lanumerics-package-for-numerical-linear-algebra-in-swift/36822.md?page=2)
