maple plotting a set of data points from a file - maple

I have the following data list in a file, say mydata.txt
0 -2.900125720 -253.200 \ 1 -5.512974510 -253.800 \ 9 -398.4569435
-253.16 \ 10 -748.4988836 -253.19 \
I read the file in as readdata(mydata.txt, float, 3) and this generates a data list, say
mydatalist := [[0., -2.900125720, -2531.200], [1.0, -5.512974510,
-253.800], [2.0, -8.690644103, -253.168], [9.0, -398.4569435, -253.16], [10.0, -748.4988836, -253.19]]
How do I plot with Maple (say) the third column against the first column (i.e. first column are x values, third column are y values)?

There are more powerful ways to read in large amounts of data such as the command ImportMatrix, which can get you to a float[8] Matrix (more efficiently used than a listlist).
But, starting with such a list of lists,
mydatalist := [[0., -2.900125720, -253.200],
[1.0, -5.512974510, -253.800],
[2.0, -8.690644103, -253.168],
[9.0, -398.4569435, -253.16],
[10.0, -748.4988836, -253.19]]:
M := Matrix(mydatalist,datatype=float[8]):
M[..,[1,3]];
plot(M[..,[1,3]]);
plot(M[..,[1,3]], style=point);

Related

Calculate 4 previous quarters from a date - pyspark

I am using the below to get the previous quarter from 'new_date' and it works great. How do I calculate 4 quarters back?
(F.expr("date_add(date_trunc('quarter', cast(new_date as date)), -1)"))
Here's one way to do it using transform.
You get the previous quarter's ending date using your approach (or any approach), and create an array using that date 4 times (using array_repeat). Then use transform with add_months to subtract 3, 6, 9 months and use last_day to get the quarter ending date.
data_sdf. \
withColumn('q1_back', func.date_add(func.date_trunc('quarter', 'dt'), -1)). \
withColumn('qtr_arr',
func.expr('transform(array_repeat(q1_back, 4), (x, i) -> last_day(add_months(x, i*-3)))')
). \
show(truncate=False)
# +----------+----------+------------------------------------------------+
# |dt |q1_back |qtr_arr |
# +----------+----------+------------------------------------------------+
# |2018-12-31|2018-09-30|[2018-09-30, 2018-06-30, 2018-03-31, 2017-12-31]|
# |2019-12-31|2019-09-30|[2019-09-30, 2019-06-30, 2019-03-31, 2018-12-31]|
# |2018-12-31|2018-09-30|[2018-09-30, 2018-06-30, 2018-03-31, 2017-12-31]|
# |2018-11-10|2018-09-30|[2018-09-30, 2018-06-30, 2018-03-31, 2017-12-31]|
# +----------+----------+------------------------------------------------+
The binary function provided in transform can access the 0-based index which can be used to subtract 3, 6, 9 months from the first quarter [0*-3, 1*-3, 2*-3, 3*-3].

K-means clustering error: only 0's may be mixed with negative subscripts

I am trying to do kmeans clustering on IRIS data in R. I want to use KKZ option for the seed selection (starting points of clusters).
If i dont standardize the data i have no issues with the KKZ command:
library(inaparc)
res<- kkz(x=iris[,1:4], k=3)
seed <- res$v # this gives me the cluster seeds based on KKZ method
k1 <- kmeans(iris[,1:4], seed, iter.max=1000)
However, when i scale the data first, then kkz command gives me the error:
library(ClusterR)
dat <- center_scale(iris[1:4], mean_center = TRUE, sd_scale = TRUE) # scale iris data
res2 <- kkz(x=dat, k=3)
**Error in x[-x[i, ], ] : only 0's may be mixed with negative subscripts**
I think this is an array indexing thing but not sure what it is and how to solve it.
For some reason, kkz cannot take in anything with a mixture of positive and negative values. I have a lot of problems running it, for example:
#ok
set.seed(1000)
kkz(matrix(rnorm(1000,5,1),100,10),3)
# not ok
kkz(matrix(rnorm(1000,0,1),100,10),3)
Error in x[-x[i, ], ] : only 0's may be mixed with negative subscripts
You don't really need to center your values, so you can do:
dat <- center_scale(iris[1:4], mean_center = FALSE, sd_scale = TRUE)
res2 <- kkz(x=dat, k=3)
I would be quite cautious about using this package..until you figure out why it is so..

Microsoft SEAL : Required negative values as a result after subtraction of two PolyCRT composed ciphertexts

Suppose I have two vectors x = [1,2,3,4] and y = [5,1,2,6].
I composed and encrypted the two array using PolyCRTBuilder ( Ciphertextx and Ciphertexty ) .
If I subtract the two ciphertexts ( Ciphertextx MINUS Ciphertexty ), I should get Result = [-4, 1, 1, -2] but after the homomorphic subtraction I am getting ResultDecrypted = [40957, 1, 1, 40959] .
I understood that because the plaintext is only defined modulo plain_modulus, we got that result. But i want the resultant negative values to be used for the next computation how can i assign the resultant negative values to a vector and use the same for the further computations
You are using a pretty old version of SEAL if it still has PolyCRTBuilder; in newer versions of the library this has been renamed to BatchEncoder and it supports encoding to/from std::vector<std::int64_t> which, I believe, is what you want.

pure data [hist] implementation

No idea how to use [hist] in Pure Data.
And the three arguments of [hist] is:
the value of first class,
the value of last class,
the number of classes.
I cannot figure out the first and second argument meaning? And how am I going to pass the output of [hist] to [tabwrite] and generate an array diagram in Pure Data.
It seems you are using the [hist] object from smlib.
The histogram will contain <number of classes> bins of equal size, with the first bin being equivalent to the <value of first class> and the last bin being equivalent to <value of last class>-1 (the offset is arguably a bug).
So, the value of first class is the minimum expected input value (x>=min), and the value of last class is the maximum expected input value (x<<max).
Any input value exceeding those boundaries will be clipped.
Examples:
[3, absolute(
|
[hist 2 5 3]
|
[print]
This will create a 3-bin histogram, with the bins 2±0.5 (with clipping this means x<2.5), 3±0.5 and 4±0.5 (with clipping that is 3.5<x).
The input 3 will be filed into the second bin, so the absolute histogram is 0 1 0.
Similarily:
[3, absolute(
|
[hist 3 6 3]
|
[print]
This will create a 3-bin histogram, with the bins 3±0.5, 4±0.5 and 5±0.5.
The input 3 will be now filed into the first bin, so the absolute histogram is 1 0 0.
Displaying the histogram:
You can set the table-values by sending a list of number to the table, prefixed with the starting index:
[relative(
|
[hist 0 100 100]
|
[list prepend 0]
|
[s $0-histo]
[table $0-histo 100]
Alternatively check the [array] object (which also can be accessed via [tabread] and the like)

Selecting random values in a set in mathematica

I have a set which has {0} and other 8 elements, total 9 elements. I want to choose random 3 value in this set and create a 3x1 column matrix. This will repeat all possible choices in the set. How can I do?
As #Picket said in comment,
The way RandomSample works will ensure it will not output the same choice twice in a single call
If your list is small, you can generate all subsets and sample it.
Example
RandomSample[Subsets[{a, b, c, d, e, f}, {3}], 7]
will generate all (20) subsets with 3 (distinct) elements and then pick 7 different uniformly (there are options to weight each member differently, chose the random generator, etc.).
RandomSample[Flatten[Permutations /# Subsets[{a, b, c, d, e, f}, {3}], 1], 13]
will generate all (120) possible ordered selections of 3 distinct elements among a set of 6 elements and give a sample of 13 distinct elements of this list.
If what you want is a random ordering of all possible subsets of size 3, or of all ordered selections without duplicate of size 3 just ask the same way but with the exact number of such sets.
myset = { foo, foo2, foo3, foo5 };
RandomSample[Subsets[myset, {3}], Binomial[Length[myset],3 ]]
RandomSample[Flatten[Permutations /# Subsets[myset, {3}], 1], 3!*Binomial[Length[myset],3 ] ]
(if you ask more than the exact number of possibilities, RandomSample will complain)
Now if your initial set is large so that the set of subsets is impractical for generation time and memory, take advantage of representing set composition by numbers, even if it is not perfect in term of uniform distribution. Say that your initial set has 20 distinct elements. A three digit number in base 20 can represent any selection of 3. If you account for the need to filter out the few with one digit appearing more than once
20^3/(3!*Binomial[20, 3]) // N
1.16959
You are probably safe by generating 25% more numbers than what you need and filtering the ones with repetition:
Cases[IntegerDigits[RandomSample[0 ;; 20^3-1, Ceiling[31*(1 + 1/4)] ], 20, 3], _?(Length[Union[#]] == 3 &), 1, 31]
This generates a random sample of 39 distinct 3-digit numbers in base 20 and select the first 31 with no duplicates in the form of a list of 3-coordinates vectors.