creating a list from a discrete function in mathematica - list

I'm trying to make a list of lists {{a},{b}...}, but instead I'm building a list of non-list terms {{{a}},{{b}}...}
First, I started with a discrete function:
f[n_]:=RandomReal[BinormalDistribution[{c[[n, 3]], c[[n, 1]]}, ...........
Second, I made a list of lists by:
d = Array[f, 100]
Outputs: {{{1.64219, 0.0231185}}, {{0.690885, 0.00599381}},......
Which can not be read by SmoothDensityHistogram:
SmoothDensityHistogram::ldata: {{1.64219,0.0231185}} is not a valid dataset or list of datasets.

You can Flatten a single level in your list of lists. Essentialy you're squeezing out a singleton dimension in your 3d array, making it 2d:
In[22]:= mylist = {{{1.64219, 0.0231185}}, {{0.690885, 0.00599381}}}
Out[22]= {{{1.64219, 0.0231185}}, {{0.690885, 0.00599381}}}
In[23]:= Dimensions[mylist]
Out[23]= {2, 1, 2}
In[24]:= mymatrix = Flatten[mylist, 1]
Out[24]= {{1.64219, 0.0231185}, {0.690885, 0.00599381}}
In[25]:= Dimensions[mymatrix]
Out[25]= {2, 2}

Related

How to display interaction element with order way of list one in Kotlin

I have two lists and I want to return a result in the following way:
the result should contain elements that are in list one and list two
output should be same order as per first list
Input :
val first = listOf(1, 2, 3, 4, 5,7,9,15,11)
val second = listOf(2, 15 , 4,3, 11)
Output:
val output = listOf(2,3,4,15,11)
Please help me to learn how to get common values in both lists in order of list first in Kotlin.
You can do
val output = first.filter { second.contains(it) }
What you are looking for is the intersection of the two lists:
val output = first.intersect(second)
As pointed out by #Ivo the result is a Set which can be turned into a list with output.toList(). However, since the result is a set, it contains no duplicates, e.g. if first is listOf(1,2,3,1,2,3) and second is listOf(2,4,2,4), the result will be equal to setOf(2).
If this is not acceptable, the solution of #Ivo should be used instead.

Tensorflow: list of tuples as placeholder

I want to use compute_gradients and generate local gradients. These gradients are to be averaged with multiple local gradients from other machines after which apply_gradients will be called. I am using 2 session.runs with a feed_dict in the second one that accepts gradients. Since apply_gradients expects a list of tuples, I am looking for an efficient way to do this.
This is how I am generating the list of tuples placeholder :
grads = cifar10.train_part1(loss, global_step)
xx = [tf.placeholder(tf.float32, shape=grads[0][0].shape) for i in range(10)]
yy = [tf.placeholder(tf.float32, shape=grads[0][0].shape) for i in range(10)]
xyz = zip(xx,yy)
train_op = cifar10.train_part2(loss,global_step, xyz)
I get the following error :
NotImplementedError: ('Trying to optimize unsupported type ', tf.Tensor 'Placeholder_10:0' shape=(5, 5, 3, 64) dtype=float32)

Python Immutable Tuple - What Am I Doing Wrong?

Apologies if this is obvious but I'm pretty new to Python and I cannot get my head around this problem. In the following code I have populated a tuple with a series of lists and I am trying to create a new list with items from this tuple. I was hoping that the final result will be that test_raw remains unchanged and test_working will look like the following:
[['aa', 1, 2, 99.5, ['bb', 1, 2, 27.2]],
['aa', 5, 5, 74.2, ['bb', 5, 5, 37]]]
However, in the process, I seem to be appending the 'bb' lists to my tuple as well. I thought that once a tuple is constructed, it cannot be changed but obviously not. Any idea what is happening?
test_raw = (['aa',1,2,99.5],
['bb',1,2,27.2],
['aa',5,5,74.2],
['bb',5,5,37])
test_working = []
for i in range(len(test_raw)):
if test_raw[i][0] == "aa":
test_working.append(test_raw[i])
for i in range(len(test_raw)):
if test_raw[i][0] == "bb":
for j in range(len(test_working)):
if test_working[j][1:3] == test_raw[i][1:3]:
test_working[j].append(test_raw[i])
break
print(test_raw)
(['aa', 1, 2, 99.5, ['bb', 1, 2, 27.2]], ['bb', 1, 2, 27.2], ['aa',.....)
You are not appending to the tuple itself, but the lists inside tuple. I won't debug your code for you but when you run your code, you'll notice that your first list (originally ['aa',1,2,99.5]) has a new element in it (['bb', 1, 2, 27.2])
You aren't appending to the tuple, you are just changing the lists that are inside that tuple
Consider this simple example
my_tuple = (1,2,3, [4,5,6])
my_tuple[3].append(7)
This doesn't add onto my_tuple, just the list that is the last element of it

prolog, i have list of structure how to Extract data and return new list of other structure?

I implementing Family program in prolog i have a problem to implement some rules.
first of all i implement this rule:
number_of_children_couple(_list):-
findall(children(_f,_m,_n),children(_f,_m,_n),_list).
return list:
19 ?- number_of_children_couple(_list).
_list = [children(mordechai, miriam, 1), children(salax, naima, 1), children(eli, bella, 2), children(..., ..., ...)|...].
my problem is how to implement :
number_of_children_person(_list28,list_person):-
first argument:
_list28 = _list //the above list ,that return from the rule
and second argument is :
list_person = [children(mordechai, 1), children(salax, 1),children(eli, 2),children(..., ...)|...]
and i also use with:
%_num is number of children for couple
children(_father,_mother,_num):-
couple(_mother,_father),
findall(_child,parents(_mother,_father,_child),_children),
length1(_children,_num).
%_num is number of children for _person
children(_person,_num):-
gender(_person,_),
findall(_child,parent(_person,_child),_list),
length1(_list,_num).
if what you want is to drop an argument from children/3 structure, can be done in a number of ways, the simpler being
number_of_children_person([],[]).
number_of_children_person([children(A,_,C)|R],[children(A,C)|T]) :-
number_of_children_person(R,T).
or more succintly
number_of_children_person(L3,L2) :-
findall(children(A,C), member(children(A,B,C),L3), L2).

How to transform a matrix with 2 columns into a multimap like structure?

I am wondering if there is a way to transform a matrix of 2 columns into a multimap or list of list.
The first column of the matrix is an id (with possibly duplicated entries) and the 2nd column is some value.
For example,
if I have to following matrix
m <- matrix(c(1,2,1,3,2,4), c(3,2))
I would like to transform it into the following list
[[1]]
3,4
[[2]]
2
With base functions, you can do something like this:
tapply(m[,2], m[,1], `[`) # outputs an array
by(m, m[,1], function(m) m[,2]) # outputs a by object, which is a list
You could use plyr:
dlply(m, 1, function(m) m[,2]) # outputs a list
dlply(m, 1, `[`, 2) # another way to do it...