Math operations with data from a file
Code: "io_math.py". Programming language: Python DMelt Version 1. Last modified: 03/13/2016. License: Pro
https://datamelt.org/code/cache/io_math_1069.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.



from  java.io import *
from  java.lang.Math import *

from  jhplot.math.DoubleArray import *
from  jhplot.math.IntegerArray import *
from  jhplot.math.LinearAlgebra import *
from  jhplot.math.io.ArrayString import *
from  jhplot.math.StatisticSample  import *


# create data set
a =[1.0, 2.0, 3.0, 4.0]
b =[ [1.0, 2.0, 3.0, 4.0], [11.0, 12.0, 13.0, 14.0] ]

# write files in ASCII
from  jhplot.math.io.ASCIIFile import *;
writeDoubleArray(File("a.txt"), a)
writeDoubleArray(File("b.txt"), b)

# read these files
a_read = readDouble1DArray(File("a.txt"));
b_read = readDoubleArray(File("b.txt"));

# print  arrays
print "a_read =  \n" + printDoubleArray(a_read)
print "b_read =  \n" + printDoubleArray(b_read)


# - binary format ---
# NOTE THAT ONLY 1D ARRAYS ARE SUPPORTED IN BINARY FORMAT
a = [1.0, 2.0, 3.0, 4.0]

# write file in binary
from  jhplot.math.io.BinaryFile import *
writeDoubleArray(File("a.dat"), a, "LITTLE_ENDIAN")

#read these file
a_read = readDoubleArray(File("a.dat"), "LITTLE_ENDIAN");
 
# print
print  "a_read from binary file = \n" + printDoubleArray(a_read)


from  jhplot.math.DoubleArray import *
# random 4 x 3 matrix
A = random(4,3)
print "RANDOM =  \n" + printDoubleArray(A)

# linear algebra
print "a TIMES 10 =  \n" + printDoubleArray( times(a, 10)  )
K= plus(a,20)
print "a PLUSE a =  \n" + printDoubleArray( K )
L=divide(K,a)
print "a DIVIDE a =  \n" + printDoubleArray( L  )
#print "MIN=", min(L)
print "SUM=", sum(L)
print "PRODUCT=", product(L)
print "SORTED=",  printDoubleArray(sort(L))


print "MEAN=", mean(L)
print "Standard deviation=", stddeviation(L)


Ads help maintain this website.