Round brackets for latex(Matrix(…)) - sympy

By default, latex(Matrix([[1, 2], [3, 4]]) gives a matrix surrounded by square brackets. How can I use round brackets instead?

You can use
latex(Matrix([[1, 2], [3, 4]]), mat_delim='(', mat_str='matrix')

Related

Prolog: Finding 2 times of a list numbers and putting the number and its double in a list

I'm new to Prolog and I've been trying to create a predicate in Prolog which finds the double of a list of numbers and putting the number and its double in one single list. For example:
?- double_list([2,3,6,10], List).
List = [[2, 4], [3, 6], [6, 12], [10, 20]].
This is the code I've done so far:
double_list([],[]).
double_list([H|T], [[H, double]| List]):-
double is (H*2),
double_list(T, List).
I don't know where I went wrong. I keep on getting false. when trying to run the program.
Variables in Prolog start with either an underscore or an upper case letter. The issue in your code is that you wrote double instead of Double. Correcting your code to:
double_list([],[]).
double_list([H|T], [[H, Double]| List]):-
Double is H*2,
double_list(T, List).
gives you the expected answer to your query:
| ?- double_list([2,3,6,10], List).
List = [[2, 4], [3, 6], [6, 12], [10, 20]]
yes
Some Prolog systems are able to detect the error in your code. For example, using SICStus Prolog, we get:
* invalid LHS in arithmetic expression: double

How to update numpy column where column condition met?

If I have the following:
import numpy as np
a = np.array([[0, 1],
[1, 3],
[4, 4]])
And want to update the column value if a column condition is met. For example if the 2nd column value is greater than 2, then replace only that column value with 9.
a = [[0, 1],
[1, 9],
[4, 9]]
I would have thought this would work, but it updates all the values in that row.
a[a[:,1] > 2] = 9
But it replaces all the values in the row.
a =[[0, 1],
[9, 9],
[9, 9]]
I'm guessing I'm missing some understanding of how the boolean indexing is being created here.
You need:
import numpy as np
a = np.array([[0, 1],
[1, 3],
[4, 4]])
a[:,1]= np.where(a[:,1]>2, 9, a[:,1])
print(a)
Output:
array([[0, 1],
[1, 9],
[4, 9]])
why your code not working
try printing out print(a[a[:,1] > 2])
it will give output as:
[[1 3]
[4 4]]
It will check for 2nd index if it is greater than 2 it will return an entire row.

Understanding Prolog lists and recursion

reverse([], X, X).
reverse([H|Original], Result, Reverse) :-
reverse(Original, Result, [H|Reverse]).
?- reverse([1,2,3], X, []).
X = [3,2,1].
So above is a fairly simple Prolog program to reverse a list. I'm just looking for someone to explain to me how the Reverse variable in the reverse rule ends up with the result. From my thinking, I'm adding the head of the original list to the head of a new list Reverse thus reversing the list. Nowhere am I interacting with the variable Result, so why does it hold anything at all?
I've been really stumped on this for a few days now and would be really grateful if anyone could clear this up!
PS. I'm also struggling with why reverse([], X, X). is required for this to work.
If you activate tracing …
trace(reverse).
… then you can see what happens:
?- reverse([1,2,3],X,[]).
T Call: (7) reverse([1, 2, 3], _G1003, [])
T Call: (8) reverse([2, 3], _G1003, [1])
T Call: (9) reverse([3], _G1003, [2, 1])
T Call: (10) reverse([], _G1003, [3, 2, 1])
T Exit: (10) reverse([], [3, 2, 1], [3, 2, 1])
T Exit: (9) reverse([3], [3, 2, 1], [2, 1])
T Exit: (8) reverse([2, 3], [3, 2, 1], [1])
T Exit: (7) reverse([1, 2, 3], [3, 2, 1], [])
X = [3, 2, 1].
In your example the second rule builds the reversed list and the first rule "copies" the Reverse list to the Result variable.

convert a 2d tuple into a list

i'm not very familiar with python, but i need to convert a 2d tuple into a nested list, i searched on stack i couldn't find an answer, Example:
Tuple = {(1,3),(3,5),(5,6)}
i need it to be a list:
List = [[1,3],[3,5],[5,6]]
why i need to convert a tuple, tuple wont allow me to use .replace on the content of the tuple
i tried to uselist() as stated on the internet but it didn't convert the tuple, thank you.
You can try like this,
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> [list(item) for item in Tuple]
[[5, 6], [1, 3], [3, 5]]
Or, you can use map
>>> list(map(list, Tuple))
[[5, 6], [1, 3], [3, 5]]
You can simply use map function which performs better when you want to apply a built-in function on an iterable :
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> list(map(list,Tuple))
[[5, 6], [1, 3], [3, 5]]
You can try this:
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> [list(item) for item in Tuple]
[[5, 6], [1, 3], [3, 5]]
or You can use iterloops imap for better performance
>>>import itertools
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> list(itertools.imap(list, Tuple))
[[5, 6], [1, 3], [3, 5]]

Define a function createSquareMat_sumpos(...) which receives one argument, dim, an integer number greater than

Define a function createSquareMat_sumpos(...) which receives one argument, dim, an integer number greater than 0, and it returns a list of lists which represents a matrix of dimension dim x dim, so that each element in the matrix has as content the sum of the column + the row position.
As an example, the following code fragment:
print (createSquareMat_sumpos(2))
should produce the output:
[[0, 1], [1, 2]]
def createSquareMat_sumpos(dim):
list=[[]]*dim
for i in range(len(list)):
list[i]=[i,(i+1)] #### not sure what to write here ####
return list
this works for
print (createSquareMat_sumpos(2)) and gives [[0, 1], [1, 2]]
but print (createSquareMat_sumpos(3)) I need it to give me [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
instead of
[[0, 1], [1, 2], [2, 3]]