BaseMath icon indicating copy to clipboard operation
BaseMath copied to clipboard

Missing negation operation for FloatVector?

Open kechan opened this issue 7 years ago • 1 comments

I couldn't seem to find the protocol and its extension implementation of neg(...) for FloatVector. This would allow me to do this:

let z = exp(-x)

And internally apply a Unary negation follow by an exp(...). So I like to have a concise expression for a sigmoid that run fast, and this is very often needed for machine learning.

I would do some quick test later when i have a chance and will close this in case it is defined somewhere.

kechan avatar Jan 21 '19 18:01 kechan

I confirmed this is indeed missing: "let v2 = -v1" where v1 and v2 are Array<Float> compiler error for missing op.

A simple fix seemed to be:

In FloatVector.swift,

(1) add

extension SupportsBasicMath {
... 
@inlinable public static func neg(_ n:Int, _ pSrc:PtrT, _ pDst:MutPtrT) { for i in 0..<n { pDst[i] = -pSrc[i]} }
}

(2) add

extension FloatVector {
...
    @inlinable public func neg ()->Self { return new_call(neg) }
    @inlinable public func neg(_ dest:Self) { Element.neg(count, self.p, dest.p) }
    @inlinable public func neg_() { neg(self) }
...
    @inlinable prefix public static func -(x: Self) -> Self { return x.neg() }
}

The following xctest should pass:

func testNeg() {
    let exp = T(v1.map {-$0})
    let r1 = v1.neg()
    XCTAssertEqual(r1, exp)
    let r2 = v1.copy()
    v1.neg(r2)
    XCTAssertEqual(r2, exp)
    v1.neg_()
    XCTAssertEqual(v1, exp)
}

On inspecting the project, I noticed what look like python templates that will generate the swift code. I guess if I do a PR, I will have to update those as well. This neg for unary - doesn't seem to fall into existing coding pattern in those templates. Please advice.

An alternative will be that if this doesn't fit in BaseMath, this can easily be added as an further extension to SupportsBasicMath and FloatVector in a separate src file outside of the main library.

kechan avatar Jan 22 '19 04:01 kechan