0

I am practicing with Swift 3.x and I need to plot some data. The problem is that I only really have IBM's online Swift sandbox to work with. The purpose of the plotting is to understand how single-precision code is affected by summations:

enter image description here

I wrote some code to do this, but now I have no clue how to plot this. I doubt Swift can somehow bring up a window for plotting, let alone do so when run through the online sandbox.

Side note: I might be able to VNC into a Mac computer at my university to use Xcode. If I paste the same code into an Xcode project, could it make plots?

Here is the code in case you wanted to see it. I need to now run this code for N=1 to N=1,000,000.

import Foundation

func sum1(N: Int) -> Float {

    var sum1_sum: Float = 0.0
    var n_double: Double = 0.0

    for n in 1...(2*N) {

        n_double = Double(n)
        sum1_sum += Float(pow(-1.0,n_double)*(n_double/(n_double+1.0)))
    }

    return sum1_sum
}

func sum2(N: Int) -> Float {

    var sum2_sum: Float = 0.0
    var n_double: Double = 0.0
    var sum2_firstsum: Float = 0.0
    var sum2_secondsum: Float = 0.0

    for n in 1...N {

        n_double = Double(n)
        sum2_firstsum += Float((2.0*n_double - 1)/(2.0*n_double))
        sum2_secondsum += Float((2.0*n_double)/(2.0*n_double + 1))

    }

    sum2_sum = sum2_secondsum - sum2_firstsum //This is where the subtractive cancellation occurs
    return sum2_sum
}

func sum3(N: Int) -> Float {

    var sum3_sum: Float = 0.0
    var n_double: Double = 0.0

    for n in 1...N {

        n_double = Double(n)
        sum3_sum += Float(1/(2.0*n_double*(2.0*n_double + 1)))
    }

    return sum3_sum
}

print("Sum 1:", sum1(N: 1000000))
print("Sum 2:", sum2(N: 1000000))
print("Sum 3:", sum3(N: 1000000))
whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50
  • Unfortunately, you are correct in thinking that the IBM Swift Sandbox does not have graphical capabilities. You should be able to plot something in a Swift Playground, though, if you can get to a Mac somehow. Take a look at the XCPlayground package. – TheSoundDefense Feb 04 '17 at 01:08
  • @TheSoundDefense I can VNC into a Mac at school. Is it safe to say that XCode has this capability? – whatwhatwhat Feb 04 '17 at 01:51
  • yes, any reasonably current version of Xcode (from the past two years or so) should have a playground built in. – TheSoundDefense Feb 04 '17 at 02:08
  • @TheSoundDefense ok I know how to find the playground in XCode but how do I begin plotting? I tried looking for a Swift function to do so but I couldn't find one – whatwhatwhat Feb 04 '17 at 02:52
  • There are some functions in the XCPlayground package that can let you do this. XCCaptureValue is one of them. – TheSoundDefense Feb 04 '17 at 18:42

1 Answers1

1

Yes, @TheSoundDefense is right. There is no plotting output from the Swift Sandbox directly. However, I recommend that you still use the Swift Sandbox. Just run the code, and copy and paste the output in comma-delimited format to Excel or MATLAB to plot it. I did some tweaking to your sum2 as an example, while also making it a bit more functional in the process:

   func sum2(N: Int) -> Float {

    let a: Float = (1...N).reduce(0) {
        let nDouble = Double($1)
        return Float((2.0 * nDouble - 1) / (2.0 * nDouble)) + $0
    }

    let b: Float = (1...N).reduce(0) {
        let nDouble = Double($1)
        return Float((2.0 * nDouble) / (2.0 * nDouble + 1)) + $0
    }

    return b - a
}

let N = 10
let out = (1...N).map(){ sum2(N: $0)}
let output = out.reduce(""){$0 + "\($1), "}
print(output)

0.166667, 0.216667, 0.240476, 0.254365, 0.263456, 0.269867, 0.274629, 0.278306, 0.28123, 0.283611,

  • And Google isn't turning up anything useful to resolve this. – whatwhatwhat Feb 04 '17 at 04:12
  • Wait, I'm confused- I thought that you did not have access to Xcode for this assignment. Anyhow, I composed the code in the Xcode playground- and also copied it into the IBM Swift Sandbox and it works just fine both ways. BTW, I think the question wants the plot on a log-log scale? – Robert F. Dickerson Feb 04 '17 at 15:00
  • Yes, it does have to be on a log-log scale. I can VNC into a MAC, but it looks like there's an error and I'm not sure how to fix it. – whatwhatwhat Feb 04 '17 at 16:06
  • There is a chance that that Mac that you are VNC'd to does not have Xcode 8.2 installed. Maybe an older version. If you don't want to use the Swift sandbox, you can always download the Linux toolchain here, and put it in an Ubuntu virtual machine: https://swift.org/download/ – Robert F. Dickerson Feb 04 '17 at 16:08
  • I was hoping to get it to work on XCode because I need to run this code for all values from `N=1` to `N=1000000` and the IBM sandbox aborts the process after `N=12100` – whatwhatwhat Feb 04 '17 at 16:08
  • Hmmm that might work. Do you think running this on a virtual machine might let it actually run for 1000000 loops? – whatwhatwhat Feb 04 '17 at 16:10
  • Yes, just install VirtualBox and add a guest machine with Ubuntu 16.04 installed. You can run any number of iterations if you're running it on your own machine. You don't have the same resource limits or runtime quota you will see on the Sandbox. Plus- you can pipe your output to a file and load it up in your graphing application. Since you're using Linux, now, get to know the Swift package manager, to scaffold out your executable: `swift package init --type executable` – Robert F. Dickerson Feb 04 '17 at 16:53