Maxima, how to do something like Table in Mathematica?
In Mathematica I can do
listA=Table[k,{k,1,100}]
How do I do the same thing in maxima?
I think makelist is the function you want. E.g. listA : makelist(i, i, 1, 100) to make a list [1, 2, 3, ..., 100]. You can nest makelist to make a nested list. See also create_list and genmatrix.
Related
I’m not quite sure how to explain this, but I’m trying to find a way to return all the values between 1 to 4 , that are not the value provided.
For example, let say I provide value ‘2’, I want the process to return 1,3,4 for me to process individually.
To give you a more specific explanation, I’m putting together a script to retrieve the input in use against each output of a 4x4 HDMI matrix. I can retrieve the input in use e.g 2, and enable that button on a UI, but I can’t work out to get the other 3 values, to request that those buttons are turned off on the UI too.
Discover what’s on..(value returned 2)
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input2", ‘true’,
Turn off the others..
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input1", ‘false’,
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input3", ‘false’,
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input4", ‘false’,
Hope that helps someone to help me ?
function foo(n)
tbl = {1, 2, 3, 4}
table.remove(tbl, n)
return tbl
end
or to also call those functions
function foo(n)
tbl = {1, 2, 3, 4}
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input"..table.remove(tbl, n), ‘true’)
for num, _ in ipairs(tbl) do
luup.variable_set("urn:upnp-net:serviceId:Matrix1", "input"..num, ‘false’)
end
end
Both the collection-for-in operation and .map() method can return some manipulation of elements from a previous collection. Is there ever any reason to prefer using one over the other?
var myList = [1,2,3];
var alteredList1 = [for(int i in myList) i + 2]; //[3,4,5]
var alteredList2 = myList.map((e) => e + 2).toList(); //[3,4,5]
Use whichever is easier and more readable.
That's a deliberately vague answer, because it depends on what you are doing.
Any time you have something ending in .toList() I'd at least consider making it into a list literal. If the body of the map or where is simple, you can usually rewrite it directly to a list literal using for/in (plus if for where).
And then, sometimes it gets complicated, you need to use the same variable twice, or the map computation uses a while loop, or something else doesn't just fit into the list literal syntax.
Then you can either keep the helper function and do [for (var e in something) helperFunction(e)] or just do something.map((e) { body of helper function }).toList(). In many cases the latter is then more readable.
So, consider using a list literal if your iterable code ends in toList, but if the literal gets too convoluted, don't feel bad about using the .map(...).toList() approach.
Readability is all that really matters.
Not an expert but personally I prefer the first method. Some reasons:
You can include other elements (independent from the for loop) in the same list:
var a = [1, 2, 3];
bool include5 = true;
var b = [
1,
for (var i in a) i + 1,
if (include5) 5,
];
print(b); // [1, 2, 3, 4, 5]
Sometimes when mapping models to a list of Widgets the .map().toList() method will produce a List<dynamic>, implicit casting won't work. When you come across such an error just avoid the second method.
how to write a code snippet Ruby that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4]. Please don't use any built-in flatten functions in either language.
Here's one solution without using the built-in flatten method. It involves recursion
def flattify(array)
array.each_with_object([]) do |element, flattened|
flattened.push *(element.is_a?(Array) ? flattify(element) : element)
end
end
I tested this out in irb.
flattify([[1,2,[3],4])
=> [1,2,3,4]
arr = [[1,2,[3]],4]
If, as in you example, arr contains only numbers, you could (as opposed to "should") do this:
eval "[#{arr.to_s.delete('[]')}]"
=> [1, 2, 3, 4]
How do I perform a function on every element in a list? For example say I have a list:
(1 x q)
and I was to use my-function on 1, x, q. Is there a predefined function for this? Similarly to foreach in a "higher" level language? Or do I have to manually step through it using car and cdr?
Any help would be much appreciated!
If you want to construct a fresh list of results of my-function, use mapcar:
(mapcar #'my-function my-list)
If you do not want the results, use mapc or dolist or loop.
See also
What's the best way to learn LISP?
The #' in common lisp
Why #' is used before lambda in Common Lisp?
Of course there is.
Look a the hyperspec symbol index and read about the following:
map, map-into
mapcar, mapcan, mapcon, mapc, mapl, maplist
dolist
loop: (loop :for element :in list #|...|#)
If you need the results as another list of the same length: map, map-into, mapcar, loop.
If you want to do it by side effects: dolist, loop.
I was trying to see if I can collect information from list based on the depth of nesting of sub-lists.
But I realized that it should be accomplish-able only by tree. I learned programming binary tree in lisp to some extent but it won't be helpful and I will have to try tree with any number of child nodes.
I also found out that there is 'sequence' function in lisp which can make the the work easier as I can do
li:[g,f,t,h,[a,b,c],[[t,y,u]]]
/*after filtering out non-list elements li=[[a,b,c],[[t,y,u]]]*/
IN::map(lambda([x],apply(sequence,[x]),li)
OUT:: [a,b,c,[t,y,u]]
/*Again filtering out and repeating the process will leave me with [t,y,u].*/
In this way I can collect sub-lists at different labels. I couldn't see this sequence function in Maxima. Is there any certain reason it wasn't included ?
Not sure what you want to accomplish. Maybe append has the desired effect?
(%i1) li : [[a, b, c], [[t, y, u]]] $
(%i2) apply (append, li);
(%o2) [a, b, c, [t, y, u]]