The fast equivalent of Python fragment assignment

In Python, you can have a list (similar to an array in swift):

>>> li=[0,1,2,3,4,5]

And do the slice assignment for any / all list:

>>> li[2:]=[99]         # note then end index is not needed if you mean 'to the end'
>>> li
[0, 1, 99]

Swift has a similar slice assignment (this is in the interactive shell swift):

  1> var arr=[0,1,2,3,4,5]
arr: [Int] = 6 values {
  [0] = 0
  [1] = 1
  [2] = 2
  [3] = 3
  [4] = 4
  [5] = 5
}
  2> arr[2...arr.endIndex-1]=[99]
  3> arr
$R0: [Int] = 3 values {
  [0] = 0
  [1] = 1
  [2] = 99
}

So far so good. But there are a few problems.

Firstly, swift does not work for an empty list or index after endIndex. Python adds if the slice index after the end index:

>>> li=[]             # empty
>>> li[2:]=[6,7,8]
>>> li
[6, 7, 8]
>>> li=[0,1,2]
>>> li[999:]=[999]
>>> li
[0, 1, 2, 999]

The equivalent in swift is a mistake:

  4> var arr=[Int]()
arr: [Int] = 0 values
  5> arr[2...arr.endIndex-1]=[99]
fatal error: Can't form Range with end < start

It is easy to check and copy.

The second problem is the killer: it is very slow. Consider this Python code to do the exact summation of a list of floats:

def msum(iterable):
    "Full precision summation using multiple floats for intermediate values"
    # Rounded x+y stored in hi with the round-off stored in lo.  Together
    # hi+lo are exactly equal to x+y.  The inner loop applies hi/lo summation
    # to each partial so that the list of partial sums remains exact.
    # Depends on IEEE-754 arithmetic guarantees.  See proof of correctness at:
    # www-2.cs.cmu.edu/afs/cs/project/quake/public/papers/robust-arithmetic.ps

    partials = []               # sorted, non-overlapping partial sums
    for x in iterable:
        i = 0
        for y in partials:
            if abs(x) < abs(y):
                x, y = y, x
            hi = x + y
            lo = y - (hi - x)
            if lo:
                partials[i] = lo
                i += 1
            x = hi
        partials[i:] = [x]
    return sum(partials, 0.0)

, hi/lo, msum([.1]*10) 1.0 , 0.9999999999999999. C- msum Python.

:

func msum(it:[Double])->Double {
    // Full precision summation using multiple floats for intermediate values 
    var partials=[Double]()
    for var x in it {
        var i=0
        for var y in partials{
            if abs(x) < abs(y){
                (x, y)=(y, x)
            }
            let hi=x+y
            let lo=y-(hi-x)
            if abs(lo)>0.0 {
                partials[i]=lo
                i+=1
            }
            x=hi
        }
        // slow part trying to replicate Python slice assignment partials[i:]=[x]
        if partials.endIndex>i {
            partials[i...partials.endIndex-1]=[x]
        }
        else {
            partials.append(x)
        }    
    } 
    return partials.reduce(0.0, combine: +)
}

:

import Foundation
var arr=[Double]()
for _ in 1...1000000 {
    arr+=[10, 1e100, 10, -1e100]
    }

print(arr.reduce(0, combine: +))    // will be 0.0
var startTime: CFAbsoluteTime!
startTime = CFAbsoluteTimeGetCurrent()
print(msum(arr), arr.count*5)          // should be arr.count * 5
print(CFAbsoluteTimeGetCurrent() - startTime)

7 . Python native msum 2,2 ( 4 ), fsum 0,09 ( 90 )

partials[i...partials.endIndex-1]=[x] arr.removeRange(i..<arr.endIndex), . , .

:

  • : partials[i...partials.endIndex-1]=[x]
  • / ?
+4
2

( ), Swift ( " " "-O", "" " " ), , "Release" . ( "Release" , ).

:

var arr = [0,1,2,3,4,5]
arr[2 ..< arr.endIndex] = [99]
print(arr) // [0, 1, 99]

, , ( , Python):

var arr = [Int]()
arr[0 ..< arr.endIndex] = [99]
print(arr) // [99]

So

if partials.endIndex > i {
    partials[i...partials.endIndex-1]=[x]
}
else {
    partials.append(x)
} 

 partials[i ..< partials.endIndex] = [x]
 // Or: partials.replaceRange(i ..< partials.endIndex, with: [x])

. , Swift Swift .

partials.replaceRange(i ..< partials.endIndex, with: [])
partials.append(x)

1,25 0,75 .

+5

@MartinR, replaceRange , slice.

( ), - :

partials.replaceRange(i..<partials.endIndex, with: CollectionOfOne(x))

CollectionOfOne , [x], inline , .

+4

Source: https://habr.com/ru/post/1619753/


All Articles