[Swift-commit] r6721 - SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r
wozniak at ci.uchicago.edu
wozniak at ci.uchicago.edu
Fri Aug 2 16:52:56 CDT 2013
Author: wozniak
Date: 2013-08-02 16:52:56 -0500 (Fri, 02 Aug 2013)
New Revision: 6721
Added:
SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/dets.swift
SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/numpy.swift
Log:
Swift example
Added: SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/dets.swift
===================================================================
--- SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/dets.swift (rev 0)
+++ SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/dets.swift 2013-08-02 21:52:56 UTC (rev 6721)
@@ -0,0 +1,32 @@
+
+import io;
+import math;
+import python;
+import string;
+import R;
+
+import numpy;
+
+global const int N = 3;
+global const int U = 7;
+
+main
+{
+ // Define our collection of determinants:
+ float dets[];
+ foreach i in [1:U-1]
+ {
+ // For U, i, construct a matrix via Numpy:
+ A = matrix_add(matrix_scale(eye(N), itof(i)), ones(N));
+ B = matrix_set(A, 2, 0, (U-i+1)**3);
+ // Obtain its determinant via Numpy:
+ v = determinant(B);
+ // Store the determinant in a Swift array:
+ dets[i] = abs_float(v);
+ }
+
+ // Build a fragment of R code with the dets:
+ code = sprintf("max(%s)", string_from_floats(dets));
+ r = R(code);
+ printf("max: %f", r);
+}
Added: SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/numpy.swift
===================================================================
--- SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/numpy.swift (rev 0)
+++ SwiftTutorials/ATPESC_2013-08-06/part11-swift-py-r/numpy.swift 2013-08-02 21:52:56 UTC (rev 6721)
@@ -0,0 +1,58 @@
+
+global const string numpy = "from numpy import *\n\n";
+
+typedef matrix string;
+
+(matrix A) eye(int n)
+{
+ string command = sprintf("repr(eye(%i))", n);
+ string code = numpy+command;
+ matrix t = python(code);
+ A = replace_all(t, "\n", "", 0);
+}
+
+(matrix A) ones(int n)
+{
+ string command = sprintf("repr(ones(%i))", n);
+ string code = numpy+command;
+ matrix t = python(code);
+ A = replace_all(t, "\n", "", 0);
+}
+
+(matrix M) matrix_scale(matrix A, float s)
+{
+ string command = sprintf("repr(%s*%s)", A, s);
+ string code = numpy+command;
+ matrix t = python(code);
+ M = replace_all(t, "\n", "", 0);
+}
+
+(matrix M) matrix_set(matrix A, int row, int column, float s)
+{
+ string fragment =
+ """
+A = %s
+A[%i,%i] = %f
+repr(A)
+""";
+ string command = sprintf(fragment, A, row, column, s);
+ string code = numpy+command;
+ matrix t = python(code);
+ M = replace_all(t, "\n", "", 0);
+}
+
+(matrix M) matrix_add(matrix A1, matrix A2)
+{
+ string command = sprintf("repr(%s+%s)", A1, A2);
+ string code = numpy+command;
+ matrix t = python(code);
+ M = replace_all(t, "\n", "", 0);
+}
+
+(float d) determinant(matrix A)
+{
+ string command = sprintf("repr(linalg.det(%s))", A);
+ string code = numpy+command;
+ string t = python(code);
+ d = tofloat(t);
+}
More information about the Swift-commit
mailing list