coeffs returns coefficients in a very weird order - maple

I have a polynomial like this one:
p := -1.604609130*10^(-11)*z^9+.1111140258*z^8+3.210741142*10^(-11)*z^7-.3955586214*z^6-2.108471910*10^(-11)*z^5+.6692726719*z^4+5.024523477*10^(-12)*z^3-.8174429322*z^2-3.142106870*10^(-13)*z+.9089252367
You see, just a regular polynomial with numeric coefficients. Then I call coeffs on it and get this:
> coeffs(p, z, 't'); t
-11
0.9089252367, -0.3955586214, -2.108471910 10 , 0.6692726719,
-12 -11
5.024523477 10 , -0.8174429322, -1.604609130 10 ,
-11 -13
0.1111140258, 3.210741142 10 , -3.142106870 10
6 5 4 3 2 9 8 7
1, z , z , z , z , z , z , z , z , z
Why on Earth it returns coefficients in such order?! I would expect it to be either from higher powers to lower powers (like in MATLAB) or from lower powers to higher powers (like in Mathematica), but Maple does something absolutely weird. My program depends on the order of coefficients extracted by coeffs, so I just can't use it.
Is there a way in Maple to extract coefficients in some sane order?

You should use the CoefficientList or CoefficientVector commands for this.
Note the comments about efficiency relative to using coeffs for this, in that help page.
Those commands also have an option for returning the coefficients in reverse order.
restart:
p := -1.604609130*10^(-11)*z^9 + .1111140258*z^8 + 3.210741142*10^(-11)*z^7
-.3955586214*z^6 - 2.108471910*10^(-11)*z^5 + .6692726719*z^4
+ 5.024523477*10^(-12)*z^3 - .8174429322*z^2 - 3.142106870*10^(-13)*z
+ .9089252367:
V := PolynomialTools:-CoefficientVector( p, z );
[ 0.9089252367]
[ ]
[ -13]
[-3.142106870 10 ]
[ ]
[ -0.8174429322]
[ ]
[ -12]
[ 5.024523477 10 ]
[ ]
[ 0.6692726719]
[ ]
V := [ -11]
[-2.108471910 10 ]
[ ]
[ -0.3955586214]
[ ]
[ -11]
[ 3.210741142 10 ]
[ ]
[ 0.1111140258]
[ ]
[ -11]
[-1.604609130 10 ]
L := PolynomialTools:-CoefficientList( p, z );
[ -13 -12
L := [0.9089252367, -3.142106870 10 , -0.8174429322, 5.024523477 10 ,
-11 -11
0.6692726719, -2.108471910 10 , -0.3955586214, 3.210741142 10 ,
-11]
0.1111140258, -1.604609130 10 ]

Related

How can I shuffle the order of a matrix in NetLogo?

How can I shuffle the contents of a matrix and preserve it as a matrix? The shuffle function works for lists but not for matrices
show shuffle [1 2 3 4 5]
1 2 3 5 4
set m matrix:from-row-list
[[1 2 3 4 5]]
show shuffle m
SHUFFLE expected input to be a list but got the
org.nlogo.extensions.matrix.MatrixExtension$LogoMatrix {{matrix: [ [
1 2 3 4 5 ] ]}} instead.
There may be an easier way, but you could jump to a list, shuffle, then come back to a matrix based on the dimensions of your original matrix.
extensions [ matrix ]
to setup
ca
let m matrix:from-row-list [ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ]
let sm shuffled-matrix m
print matrix:pretty-print-text sm
reset-ticks
end
; Reporter returns a shuffled matrix
to-report shuffled-matrix [ mat ]
; Get the number of columns
let cols last matrix:dimensions mat
; Shuffle the matrix values as a list
let shuf-vals shuffle reduce sentence matrix:to-row-list mat
; Use the shuffled values to generate a new matrix
; with the same dimensions as the original
report matrix:from-row-list ( subsetter shuf-vals cols )
end
; Reporter returns a list cut into sublists
; based on the len value passed
to-report subsetter [ ls len ]
; Generate subsetting indices for the sublists
let vals ( range 0 ( length ls ) len )
; Make subsets of ls based on the subsetting indices
report map [ i -> sublist ls i ( i + len ) ] vals
end
A few example outputs from setup:
[[ 1 6 7 ]
[ 9 5 8 ]
[ 3 4 2 ]]
[[ 4 6 8 ]
[ 1 9 2 ]
[ 7 5 3 ]]
[[ 2 9 4 ]
[ 6 3 8 ]
[ 5 1 7 ]]

How can I multiply n elements of a matrix by a number in NetLogo?

I have a matrix m and I want to have a user defined function which allows me to control the elements that are multiplied by a value.
The function matrix:set-and-report looks promising but I'm not sure how to implement this for multiple elements.
For example, I would like to multiply the first 3 elements of the matrix by -1 to move from this:
let m matrix:from-row-list [1 2 3 4 5 6]
print m
to this:
let n matrix:from-row-list [-1 -2 -3 4 5 6]
With matrix:set-and-report you were indeed pretty close to a solution. Please check the example, I hope this is what you were looking for. The report function has matrix as an input. Than you specify the row, than the index were you want to start the multiplication, where to end it, and finally the multiplier.
Extensions [
matrix
]
to test
let m matrix:from-row-list [ [1 2 3 4 5 6] [1 2 3 4 5 6] ]
print (word "original matrix " m)
print (word "modified matrix " matrix-row-manipulation m 0 0 3 -1)
end
to-report matrix-row-manipulation [matrix row columen-index-start columen-index-end multiplier]
let index (range columen-index-start columen-index-end 1)
foreach index [ i ->
set matrix matrix:set-and-report matrix row i (matrix:get matrix row i * multiplier )
]
report matrix
end
This will return you:
observer> test
original matrix {{matrix: [ [ 1 2 3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}
modified matrix {{matrix: [ [ -1 -2 -3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}

How can I change the elements of a matrix if they satisfy some condition?

I'm working with the matrix extension in NetLogo. I want to be able to modify specific elements of the matrix if they equal some number.
For instance if the value is 0.95 I want to run random 2 on it so it comes out as a 1 or a 0. And if it's a 1.75 it comes out as a 1 or a 2 with random (3 - 1) + 1
This would change my matrix m from this:
let m matrix:from-row-list [[1 0.95 0.95] [2 1 1.75] [1 2 1] ]
to this:
[[1 1 0] [.05 1 2] [.05 .25 1] ]
Thanks
I'm not sure if I understand your updated matrix example- for example, why does the 2 in the second row become 0.05 in the output? I'm assuming you have some other rules for dealing with those numbers. Anyway, I think you can use matrix:map to accomplish what you're after- you may just have to set up the rules in your anonymous reporter to reflect what you're after. Here is an example using the rules you supplied for values of 0.95 and 1.75:
extensions [ matrix ]
to matrix-manipulation
let m matrix:from-row-list [[1 0.95 0.95] [2 1 1.75] [1 2 1] ]
let m2 matrix:map [ i -> val-change i ] m
print matrix:pretty-print-text m2
end
to-report val-change [ val ]
if val = 0.95 [
report random 2
]
if val = 1.75 [
report 1 + random 2
]
report val
end
Output becomes:
[[ 1 0 1 ]
[ 2 1 2 ]
[ 1 2 1 ]]

Netlogo: How to get the weight of the link

I got a set of turtles with links connected to each other. I wanted to retrieve the weight of the link between two nodes, i've tried searching but couldn't find any info on how to do it. I'm not using nw cause i don't want the shortest path. Any ideas? This is a section of my code:
to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
let node-link link-with turtle b
;Then retrieve weight link to do adding
]
] )
print weighted-dist
end
enter image description here
The S is my starting point (25 in the list) and E is end (24 in the list) I wanted to calculate the weight of this "orange path"
Jen's answer about how to get the weight of a link is correct, but I would suggest an alternative way of computing the sum of these weights: using the sum primitive!
This requires turning your foreach into a map, but aside from that, it's pretty straightforward:
let weighted-dist sum (map [ [a b] ->
[ [ weight ] of link-with turtle b ] of turtle a
] (but-last oldList) (but-first oldList))
Another small comment: using a list of who numbers might not be the best way to approach things, but I don't know enough about your problem to suggest an alternative...
Assuming you called the weight weight (in your links-own statement that you haven't shown) then something like this should work:
to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
let node-link link-with turtle b
set weighted-dist weighted-dist + [weight] of node-link
]
] )
print weighted-dist
end
Getting the attribute value for a link is exactly the same as getting the attribute value for a turtle or patch, you use of

NetLogo histogram data

I tried posting this to the NetLogo user group on Yahoo but wasn't successful in getting the post accepted. So I'm trying here.
NetLogo can plot histograms. Is there any way to get access to the histogram data, i.e., the data generated for the histogram plot? Thanks.
Happy Holidays, Russ!
I don't think it's possible to get the values. Though if you wanted to implement your own histogram for data, you could use something like:
to-report calc-histogram [ aList numBars aMaxValue ]
let minValue min aList
let interval (aMaxValue - minValue) / numBars
let hist []
foreach n-values numBars [?] [
let lowerBound minValue + (? * interval)
let upperBound lowerBound + interval
let x (lowerBound + upperBound) / 2
let y length filter [? >= lowerBound and ? < upperBound] aList
set hist lput (list x y ) hist
]
report hist
end
example usage:
observer> calc-histogram [0 1 18 2 3 4 5 6 7 7 7 9 10 7 15 7 17 18 19 ] 5 20
observer: [[2 4] [6 8] [10 2] [14 1] [18 4]]