How to declare a matrix or a vector giving its dimensions? - maxima

Is it possible in Maxima for me to declare a matrix with a given number of rows and of columns without initializing it? The closest thing I found is the function zeromatrix. And how to declare vectors like that?
I want to have them declared so I can fill them in loops.
Thank you.

You don't need to declare things in Maxima prior to using them.
For instance, you could create some lists containing the rows, as in
for i:1 thru 6 do myrow[i]:makelist(i+j,j,1,5);
Then you could construct the matrix with
M[i,j]:=myrow[i][j]
and
genmatrix(M,6,5)
Of course, this can be made more precise if you provide more details of what you are trying to do...

Related

How to recode first two digits SPSS

I have two variables, A and B, that both use a 5-digit code starting with 50. So variable A might be 50123 to designate orange and B might be 50123 to designate apple. The codes were originally constructed to be concatenated together to create a single hierarchy code. I now need to consider them independently and must be able to distinguish between a code for variable A and variable B.
Bottom line is I want to recode variable A items from 50123 to 60123 and variable B items from 50123 to 70123. What's the best way to do this in SPSS without having to manually enter items either in syntax or the recode dialog box?
Thanks
If variables A and B are numbers, you could try
compute A=A+10000.
compute B=B+20000.
If they are strings, this should work:
compute A=concat("6", char.substr(A,2,5)).
compute B=concat("7", char.substr(B,2,5)).

How to dynamically define multiple polynomials inside a loop in Maxima

So...I want to create five different polynomials inside a loop in order to make a Sturm sequence, but I don't seem to be able to dynamically name a set of polynomials with different names.
For example:
In the first iteration it would define p1(x):whatever
Then, in the second iteration it would define p2(x):whatever
Lastly, in the Nth iteration it would define pn(x):whatever
So far, I have managed to simply store them in a list and call them one by one by its position. But surely there is a more professional way to accomplish this?
Sorry for the non-technical language :)
I think a subscripted variable is appropriate here. Something like:
for k:1 thru 5 do
p[k] : make_my_polynomial(k);
Then p[1], ..., p[5] are your polynomials.
When you assign to a subscripted variable e.g. something like foo[bar]: baz, where foo hasn't been defined as a list or array already, Maxima creates what it calls an "undeclared array", which is just a lookup table.
EDIT: You can refer to subscripted variables without assigning them any values. E.g. instead of x^2 - 3*x + 1 you could write u[i]^2 - 3*u[i] + 1 where u[i] is not yet assigned any value. Many (most?) functions treat subscripted variables the same as non-subscripted ones, e.g. diff(..., u[i]) to differentiate w.r.t. u[i].

Using List.map to create a list of odd numbers

My professor is having us create a series of functions relating to approximating pi and e based on continuing fractions. In order to set this up, he is having us create a function that takes an integer and maps that many odd numbers squared, starting from 1. For instance, here is the desired behavior:
oddSquares 6;;
val it : int list = [1.0; 9.0; 25.0; 49.0; 81.0; 121.0]
I can see that one mapping will likely be used to square all the values in the list, but I can't figure out a way to map the number to a list of numbers. I don't want to ask anybody to write code for me, but methodically, what am I trying to do when I'm assembling the base list?
It feels like the best method is to work backwards, starting from the base number of 6 terms. We then evaluate the 6th odd term (11, or 2x-1 practically), but then require some method of recursion to continue to evaluate smaller values of oddSquares. I also think this is against the spirit of trying to map the number into these values. Can someone give me some guidance as to the first translation from the number into the list form?
F# offers special neat syntax for creating lists of successive numbers:
let oneToSix = [1..6]
This is a special case of something called "list comprehension". They can be more complex than just successive numbers - they can include multiple generators, filters, projections, Cartesian products, etc. In particular, your whole task of generating first N odd numbers can be expressed as one list comprehension. However, since you explicitly asked not to write the code for you, I won't.

How to obtain a list in a list (aka matrix)?

How to make an n-dimensional datastructure within lab.open-roberta.org using calliope as system? It seems there is no way to declare a variable as a list within a list. As a workaround one could think of making a list of String and then read the characters the string consists of - but it seems there is no way to do that either.
The Image is a fine matrix but it's fixed to 5x5 elements.
Has anybody an idea how to make a more complex datastructure (I know an n-dimensional list can be stored in a one-dimesional list but it's so hard to read that as a human).

Manipulating Unbounded Integers using custom data structure

Full disclosure, my question is pertaining to a project I am working on for my Data Structures class. I know this is usually frowned upon, but I am hoping it may be okay due to the fact that I have the data structure itself done and I'm just seeking assistance in creating a method.
The project is to implement a custom data structure to represent unbounded integers using a custom linked list. I cannot use the BigInteger nor LinkedList classes. I implemented the data structure using the IntNode class provided from the project.
The class takes in a string of numbers, breaks it into 3 character chunks, converts those chunks into integers and stores each chunk in a custom "linked list" of IntNode objects.
For example: 123456789123 represented as 4 IntNodes, <123> <456> <789> <123>
The method I am having difficulty implementing is:
UnboundedInt multiply (UnboundedInt )
A method that multiplies the current UnboundedInt with a passed in one. The return is a new UnboundedInt.
There is also an 'add' method which was easy to implement and I do realize I could use to handle multiplication by looping the 'add' method as many times as one of the UnboundedInt objects, however, how would I handle the loop variable when it, itself, breaches the limit of an integer?
I do realize I could use to handle multiplication by looping the 'add' method as many times as one of the UnboundedInt objects
That's not going to be the answer, because it would be too slow if either operand is non-trivial.
There is also an 'add' method which was easy to implement
That's good, because that's going to be part of the solution.
How did you implement that?
Probably following the steps you would do if you had to do it on paper.
You can implement multiplication the same way.
How do you multiply two numbers on paper?
You multiply the number on the left with each digit on the right, one by one.
After you have the multiples by one digits, you add them.
For example, let's say you were to multiply 123456789123 with 234. It would go like this:
123456789123 * 234
------------
246913578246
370370367369
+ 493827156492
==============
28888888654782
Multiplying an IntNode by 1-digit numbers should be easy,
and you already have the implementation of add, so the complete solution is not far away. To sum it up, what you still need to implement:
Multiply by a 1-digit number
Multiply by 10
Combine the above two to compute the total

Resources