How to combine two sparsematrix using java colt lib? - colt

I'm using java colt library for sparse matrix.
Inputs are the two sparse matrix with the same row number, e.g.,
SparseDoubleMatrix1D v1; //[1 2]
SparseDoubleMatrix1D v2; // [3, 4]
After some calculation of v1, and v2. I want to append the result of v2 to v1 or they can also be assigned to another new matrix, e.g.,
v3 = [v1; v2] ; // [1 2 3 4]
Any one know how to achieve this with colt? Is there an existing method for it?

Since no one answers, I find some solution by using DoubleFactory2D.sparse.appendColumns method in colt lib. details as below:
SparseDoubleMatrix2D v1 = new SparseDoubleMatrix2D(new double[][]{{0, 1, 2}, {1, 1, 3}, {4, 5, 6}});
SparseDoubleMatrix2D v2 = new SparseDoubleMatrix2D(new double[][]{{0, 1}, {1, 1}});
SparseDoubleMatrix2D matrix = (SparseDoubleMatrix2D) DoubleFactory2D.sparse.appendColumns(v1, v2);

Related

How can I configure Maxima to index lists from 0 instead of 1?

If I create a list in Maxima:
(%i1) a: [2, 3, 5, 7, 11];
(a) [2, 3, 5, 7, 11]
Then if I index into that list and ask for element 4:
(%i2) a[4];
(%o2) 7
This shows that Maxima uses 1-indexing rather than 0-indexing. I would prefer to use 0-indexing for lists rather than 1-indexing. How can I do this?
It is not possible to change the indexing for lists in Maxima; it always starts at 1.

How do I Filter elements of a PCollection with a ParDo with Apache Beam Python SDK

I have a PCollection, and I would like to use a ParDo to filter out some elements from it.
Is there a place where I can find an example for this?
In the Apache Beam Python SDK, there is a Filter transform that receives a lambda, and filters out all elements that return False. Here is an example:
filtered_collection = (beam.Create([1, 2, 3, 4, 5])
beam.Filter(lambda x: x % 2 == 0))
In this case, filtered_collection will be a PCollection that contains 2, and 4.
If you want to code this as a DoFn that is passed to a ParDo transform, you would do something like this:
class FilteringDoFn(beam.DoFn):
def process(self, element):
if element % 2 == 0:
yield element
else:
return # Return nothing
and you can apply it like so:
filtered_collection = (beam.Create([1, 2, 3, 4, 5])
beam.ParDo(FilteringDoFn()))
where, like before, filtered_collection is a PCollection that contains 2, and 4.

Dijkstra algorithm under constraint

I have N vertices one being the source. I would like to find the shortest path that connects all the vertices together (so a N-steps path) with the constraint that all the vertices cannot be visited at whichever step.
A network is defined by N the number of vertices, the source, the cost to travel between each pair of vertices and, for each step the list of vertices that can be visited
For example, if N=5 and the vertices are 1(the source),2,3,4 and 5, the list [[2, 3, 4], [2, 3, 4, 5], [2, 3, 4, 5], [3, 4, 5]] means that for step 2 only vertices 2,3 and 4 can be visited and so forth...
I can't figure out how to adapt the Dijkstra algorithm to my problem. I would really like some ideas Or maybe a better solution is to find something else, are there others algorithm that can handle this problem ?
Note : I posted the same question at math.stackexchange, I apologize if it is considered as a duplicate
You don't need any adaptation. Dijkstra algorithm will work fine under these constraints.
Following your example:
Starting from the vertex 1 we can get to 2 (let's suppose distance d = 2), 3 (d = 7) and 4 (d = 11) - current values of distance is [0, 2, 7, 11, N/A]
Next, pick the vertex with the shortest distance (vertex 2) - we can get from it to 2 again (shouldn't be counted), 3 (d = 3), 4 (d = 4) or 5 (d = 9). We see, that we can get to the vertex 3 with distance 2 + 3 = 5 < 7, which is shorter than 7, so update the value. The same is for the vertex 4 (2 + 4 = 6 < 11) - current values are [0, 2, 5, 6, 9]
Mark all the vertices we visited and follow the algorithm until all the vertices are selected.

How to compute the mean over rows till a variable changes and repeat?

Given a very huge table of the following format (e.g. snippet):
Subject, Condition, VPH, Task, Round, Item, Decision, Self, Other, RT
1, 1, 1, SVO, 0, 0, 4, 2.5, 2.0, 8.598
1, 1, 1, SVO, 1, 5, 3, 4.1, 3.4, 7.785
1, 1, 1, SVO, 2, 4, 3, 3.2, 3.4, 15.713
2, 2, 1, SVO, 0, 0, 4, 2.5, 2.0, 15.439
2, 2, 1, SVO, 1, 2, 7, 4.9, 2.3, 30.777
2, 2, 1, SVO, 2, 3, 8, 4.3, 4.3, 13.549
3, 3, 1, SVO, 0, 0, 5, 2.8, 1.5, 9.066
... (And so on)
Needed: Compute the mean over all rounds for self and others for each subject.
What i have so far:
I sorted the about 100mb .txt file using bash sort so the subject and the related rounds appear after each other (like the example shows). After that i imported the .txt file into SPSS24. Right now i have no idea to write a function that computes for each subject the mean of variable self and others over the three rounds. E.g.: (some pseudo-code)
for n = 1 to last_subject do:
get row self where lines have line_subject as n
compute mean over these content
write result as new variable self_mean as new variable after variabel RT at line n
increase n by one
As i am totally new to SPSS i really appreciate detailed help. I am also satisfied with references that specifically attend to computation over rows (i found lots of stuff over columns).
Thank you very much!
Edit: example output
After computing the table should look like this:
Subject, Mean_Self, Mean_Others
1, 3.27, 2.9
2, ..., ...
3,
... (And so on)
So now we computed the Mean_Self from the top example like so:
mean(2.5 + 4.1 + 3.2)
where:
2.5 was used from line 1 of Variable Self
4.1 was used from line 2 of Variable Self
3.2 was used from line 3 of Variable Self
2.5 was not used from line 4 of Variable Self because Variable Subject changed, there for we want to repeat the process with the new Subject (here 2) until it changes again. The results should create a table like the one above. Same procedure for Variable Other.
If I understand right what you need is the aggregate command. aggregate can create a new dataset/file with your aggregated data, or add the aggregated data to your active dataset, like you described above:
AGGREGATE
/OUTFILE=* MODE=ADDVARIABLES
/BREAK=Subject
/Self_mean=MEAN(Self)
/Other_mean=MEAN(Other).
In order to get the new variables in a new, separate tabe, look up other AGGREGATE options, e.g. /OUTFILE=* (removing MODE=ADDVARIABLES) will result in the new aggregated data replacing the original file in the window, while /OUTFILE="path/filename" will save the aggregated data to a file.

Add flags on condition

How can I add a flag, where the value of x - 1 is different then value of x, can it be done directly on the chart?
x is [1, 1, 1, 3, 3] so when I get the pair 1, 3 I want to put a flag on 3, and say that something has changed.

Resources