Swift and Mathematics

Hi all?

I am an electrical engineer who has recently learnt Swift for a new project.

I know Swift is an OO language but I can't resist making comparisons to other languages I have used previously in science and academia.

The applications I am writing relies heavily on mathematics and I really wish Swift had more built in support for basic mathematical constructs.

I was very disappointed to see Apple reserving the ^ keyword for a bitwise operation (XOR) rather than a power function as is pretty standard elsewhere.

If I could have one built in function added to Swift it would be the Power function using preferably the ^ keyword.

After that, why not throw in the basic trigonometric functins too? Would that be too much to ask? I don't condone bloating Swift by adding every single mathematical function in existance, just a few of these basics to make the easy stuff a whole lot quicker and more portable.

Yes I know you can use overload operators and subscripts, but this just adds more custom code which kills any portability of your core mathematics.

A far better solution would be to provide language level support for these basic mathematical constructs.

Is there any way of contacting the language development group to give feedback about improved built-in support for mathematical equations?

Yes I know you can link to other classes that provide these functions (and I'm not expecting MATLAB like readability from Swift) but if just linking in the functionality is your answer then you really must not do very much technical computing because writing out complex mathematics using objects and .syntax is a nightmare for productivity and readability.

If anyone can help me get in touch with the language development group, I will be happy to provide examples of what I am talking about.

I am the only one with these feelings about Swift?

Cheers,

Paul

Yes, Swift should provide more built in functions.

For maths, it should speak the language of mathematicians, not of computer scientist.


I posted a similar comment for set related functions, where syntax is at odds with the language used for set theory.


I expect it from Apple, they learn the (math) language to the machine and not force users to learn machine language.


PS: similar for string manipulation which remains very cumbersome with the range ; I would appreciate a more direct manipulation.

Perhaps I am misunderstanding your question, but all mathematical functions from

https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/math.3.html#//apple_ref/doc/man/3/math

are available in Swift, for example:


import Foundation
let x = sin(M_PI/2)
let y = atan2(0.0, 1.0)
let z = exp(1.0)
let p = pow(2.0, 3.0)

Swift and math are just fine. Your expectations on how the syntax should look may be throwing you off but once you get past using, for example, pow instead of ^, it becomes much easier. You'll discover any number of libraries and utilities such as Accelerate and CoreGraphics that not only support mathematical expression and problem solving but make them fun!

Already by allowing operator overloading and Unicode variable names, Swift is able to provide fairly decent mathematical support. However,

pow(x,y) // doesn't really hack it.


Someone who does not have experience coding math-centric code will probably wonder what the fuss is about. Imagine having to do:

add(x,y) // ... especially on a long and involved equation.

And consider that in the realm of signals processing, raising a complex number to some power is as common as adding two integers.


One solution I would like to see considered would be allowing Unicode operators. (A carefully chosen subset of Unicode). This would allow succinct set theoretic notation for a start.


I don't think we should constrain ourselves to what is underneath our obsolete QWERTY keyboards. Digging out a Unicode character would only take a few seconds, and produce beautiful code. Given the hours I spend staring at ugly code, I would be very happy to invest.


Have a look at https://www.youtube.com/watch?v=a9xAKttWgP4 -- APL have gone down this road, and produced something very impressive. Pity Dyalog did not release it into the wild, it has since died in captivity.

“One solution I would like to see considered would be allowing Unicode operators. (A carefully chosen subset of Unicode). This would allow succinct set theoretic notation for a start.”


You're in luck. This already exists in Swift, exactly as you describe.

Swift and Mathematics
 
 
Q