Why matrix changes value one line after its assignment? - Maple - math

two values of the matrix A are changing as you can see in the picture. Why is this happening? What can I do to solve it?
I tried 'Execute the entire worksheet' but could not solve it.

The Matrix which you first assign to A contains the name C in the formulas used for both the A[2,3] and A[3,2] entries.
And then you assign a Vector to the name C.
So, of course, that affects those two entries of A.
If you don't want that to happen then choose use two different names for the two different purposes, eg. C for the Vector and CP in the entries, or vice versa.
You will always run into trouble if you try to use the same name to mean two different things. That's not just a Maple issue: it'll happen to you in math, and in other programming languages.

Related

How do I create an N-Queens Problem with Genetic Algorithm child with constrained row and column?

For the N-Queen problem found here, I am trying to implement a genetic algorithm to solve it.
However, let's say that I am trying to constrain the problem. We know that to get an attacking value of 0, you can't have queens in the same row and column. I limit the boards to always have a different row and column for each queen. I want the genetic algorithm to find a solution where the diagonals are also not attacking.
My problem is with creating a child for this solution using a genetic algorithm. What is a good way to generate a child from two parent boards that follows that the children must not have queens in overlapping rows and columns?
Avoiding both overlapping rows and columns is difficult in a genetic algorithm. The typical approach is to implicitly represent the columns by the index in an array, and then have the queens represented by numbers 1..N.
So, a solution to the 8-queen problem would be represented by (5 1 8 4 2 7 3 6).
If you take any subset of the array you can mix it with another array and be guaranteed that there is one queen in each column (or row - however you prefer to think of this).
You can avoid both column and row overlap by using combinatorics (so there are N! arrangements instead of N^N), but the issue is that the representation required to do this (you can essentially use integers to represent full configurations) doesn't work as well for crossover operations. You will also run into the limit of integer representations. Using an array as above works fairly well, so I would suggest exploring that approach first.

closed/fixed:Interpertation of basic R code

I have a basic question in regards to the R programming language.
I'm at a beginners level and I wish to understand the meaning behind two lines of code I found online in order to gain a better understanding. Here is the code:
as.data.frame(y[1:(n-k)])
as.data.frame(y[(k+1):n])
... where y and n are given. I do understand that the results are transformed into a data frame by the function as.data.frame() but what about the rest? I'm still at a beginners level so pardon me if this question is off-topic or irrelevant in this forum. Thank you in advance, I appreciate every answer :)
Looks like you understand the as.data.frame() function so let's look at what is happening inside of it. We're looking at y[1:(n-k)]. Here, y is a vector which is a collection of data points of the same type. For example:
> y <- c(1,2,3,4,5,6)
Try running that and then calling back y. What you get are those numbers listed out. Now, consider the case you want to just call out the number 1 in that vector. How would you do that? Well, this is where the brackets come into play. If you wanted to just call the number 1 in y:
> y[1]
[1] 1
Therefore, the brackets are a way of calling out or indexing specific items in the vector. Note that the indexing starts at the value 1 and goes up to the number of items in the vector, or length. One last thing before we go back to the example you gave. What if we want to index the numbers 1, 2, and 3 from the vector but not the rest?
> y[1:3]
[1] 1 2 3
This is where the colon comes into play. It allows us to reference a subset of the numbers. However, it will reference all the numbers between the index left of the colon and right of it. Try this out for yourself in R! Play around and see what happens.
Finally going back to your example:
y[1:(n-k)]
How would this work based on what we discussed? Well, the colon means that we are indexing all values in the vector y from two index values. What are those values? Well, they are the numbers to the left and right of the colon. Therefore, we are asking R to give us the values from the first position (index of 1) to the (n-k) position. Therefore, it's important to know what n and k are. If n is 4 and k is 1 then the command becomes:
y[1:3]
The same logic can apply to the second as.data.frame() command in your question. Essentially, R is picking out different numbers from a vector y and multiplying them together.
Hope this helps. The best way to learn R is to play around with a command, throw different numbers at it, guess what will happen, and then see what happens!

Set value for multiple variables at once?

While programming a sample program in my TI-84 calculator, I was wondering if there were a way to initialize multiple variables to a single value in one line?
I tried doing
0 -> A,B,C,D
However this did not work for the calculator. I know you can do each one on an individual line, but my question is, is it possible to initialize multiple variables to a single value at once in TI-BASIC?
No. You cannot initialize multiple variables to a single value at once in TI-Basic.
However, you can:
Set all values in an array or matrix to the same value at once.
Initialize variables to zero in three bytes instead of four using DelVar (variable) and then leaving off the following colon. For example, instead of doing :0->A:0->B:Disp A,B you can do :DelVar ADelVar BDisp A,B.
Remember that uninitialized variables are treated as zero when first used.
No, you can't set multiple variables at once with one value. But if you just want them on the same line, you can use a colon like this 0→A:0→B. If you really feel like it, you could make another program that zeroes out every variable and just call it from within your program like this.
PROGRAM:ZERO
:0→A
:0→B
:0→C
...
PROGRAM:OTHER
:prgmZERO
...

Finding a closest looking segment of data in another sequence

I am doing image processing, in which I came across a situation, where I have to compare two vectors and find an instance of the smaller vector in the larger vector.
Say the two vectors are A: with 100 elements (or entries)
and B; with 10 elements. B is a model and it may not be present exactly as it is' in the vector A. I can compare 10 elements at a time and find the difference. Ideal case is that the B is present somewhere and the difference is zero. Otherwise a minimum will result at some random location, and i am missing the location.
Please help me in giving an algorithm such that the i can find Bs' closest instance in A.
What you are looking for is the cross-correlation function.The peak the the cross correlation of the two vectors will be the point were vector B is most similar to vector A.
You may want to get an explanation of how it is implemented in matlab HERE as it gives an easier explanation of how this operation can be implemented in software.

Fortran90 created allocatable arrays but elements incorrect

Trying to create an array from an xyz data file. The data file is arranged so that x,y,z of each atom is on a new line and I want the array to reflect this.
Then to use this array to find find the distance from each atom in the list with all the others.
To do this the array has been copied such that atom1 & atom2 should be identical to the input file.
length is simply the number of atoms in the list.
The write statement: WRITE(20,'(3F12.9)') atom1 actually gives the matrix wanted but when I try to find individual elements they're all wrong!
Any help would be really appreciated!
Thanks guys.
DOUBLE PRECISION, DIMENSION(:,:), ALLOCATABLE ::atom1,atom2'
ALLOCATE(atom1(length,3),atom2(length,3))
READ(10,*) ((atom1(i,j), i=1,length), j=1,3)
atom2=atom1
distn=0
distc=0
DO n=1,length
x1=atom1(n,1)
y1=atom1(n,2) !1st atom
z1=atom1(n,3)
DO m=1,length
x2=atom2(m,1)
y2=atom2(m,2) !2nd atom
z2=atom2(m,3)`
Your READ statement reads all the x coordinates for all atoms from however many records, then all the y coordinates, then all the z coordinates. That's inconsistent with your description of the input file. You have the nesting of the io-implied-do's in the READ statement around the wrong way - it should be ((atom1(i,j),j=1,3),i=1,length).
Similarly, as per the comment, your diagnostic write mislead you - you were outputting all x ordinates, followed by all y ordinates, etc. Array element order of a whole array reference varies the first (leftmost) dimension fastest (colloquially known as column major order).
(There are various pitfalls associated with list directed formatting that mean I wouldn't recommend it for production code (or perhaps for input specifically written with the knowledge of and defence against those pitfalls). One of those pitfalls is that the READ under list directed formatting will pull in as many records as it requires to satisfy the input list. You may have detected the problem earlier if you were using an explicit format that nominated the number of fields per record.)

Resources