differentiating and taking limit of complex vector valued function with maple - maple

I would like to do the following with maple
let
omega := z -> 2*<Re(z), Im(z), 1>/(1+abs(z)^2):
and
phi := -> z (l*z+a)/(1-l*conjugate(a)*z):
where a is complex and l is real.
I consider Omega=omega( phi(z) ) and i would like to evaluate diff(Omega,x) diff(Omega,y) but also compute some limit like
> expr := omega(phi(1/e));
> Omega := simplify(map(limit, expr, e = 0));
> expr2 := (omega(phi(1/(e^2*conjugate(z))))-Omega)/e^2;
> H := limit(expr2, e = 0);
Unfortunately i have tried every thing (Vector Calculus , Complex...) and i have always a probleme either because i work with vector or because the variable is complex.
Does someone have any idea of the good way to code such a problem?
Thx

[edited:] I cannot tell whaht your original definition of operator phi is, as there is a syntax error in the original post (invalid syntax). In particular I cannot tell whether you meant,
phi := z -> (l*z+a)/(1-l*conjugate(a)*z):
or,
phi := z -> z (l*z+a)/(1-l*conjugate(a)*z):
I used the former below. The results will depend upon the choice, of course.
In a previous question by you an answer involved using evalc, under which all unknowns would be treated as real.
But now you seem to have a mix, where l is to be taken as real while a may be complex.
As shown in your earlier question another approach is to use assumptions on the unknowns, which in this case can give finer control over your mix.
Note that a will get treated as being possibly complex, by default. So we can use an assumption on just l.
restart:
omega := z -> 2*<Re(z), Im(z), 1>/(1+abs(z)^2):
phi := z -> (l*z+a)/(1-l*conjugate(a)*z):
expr := omega(phi(1/e)):
map(limit,expr,e=0) assuming l::real:
map(simplify,%);
[ 2 Re(a) ]
[- ----------]
[ 2 ]
[ | a | + 1]
[ ]
[ 2 Im(a) ]
[- ----------]
[ 2 ]
[ | a | + 1]
[ ]
[ 2 ]
[ 2 | a | ]
[ ---------- ]
[ 2 ]
[ | a | + 1 ]
Here is another way to get a result. We could let a=S+T*I and use evalc to handle altogether the assumptions that S and T (and l) are purely real.
map(limit,subs(a=S+T*I,expr),e=0) assuming l::real:
simplify(map(evalc,%));
[ 2 S ]
[- -----------]
[ 2 2 ]
[ S + T + 1]
[ ]
[ 2 T ]
[- -----------]
[ 2 2 ]
[ S + T + 1]
[ ]
[ 2 2 ]
[ 2 (S + T ) ]
[ ----------- ]
[ 2 2 ]
[ S + T + 1 ]

Related

Applying a list of functions on a list of argument lists

I have a list of anonymous functions that I need to map over a list of argument lists. My desire is to obtain a list containing lists whose elements are the functions in the functions list, evaluated at each argument list.
My first thought was to map the functions list over the argument lists and then apply the functions in the functions list on each element in the arguments list.
;; definitions for k functions
let fj [ [x1 x2 ... xn ] -> <body j> ]
...
let args [ [ ... ] [ ... ] ... [ ... ] ] ;; list of arguments
let f-list ( list f1 f2 ... fk )
map [ [ arg ] -> map [ [ f ] -> ( runresult f arg ) f-list ] ] args
There is one two problem with this code:
Mapping the f-list on list arg does not behave the way I expected it to behave. Namely, running the code produces run-time errors because the functions in f-list are not defined over a list but are instead defined over a number of arguments.
In order to overcome this latter difficulty, I have redefined the functions in f-list so as to receive a list as an argument and use item in the body of the functions in order to retrieve individual parameters.
This solution, apart from the fact that, it too, feels like a hack, it is also highly impractical because it increases the code needed to obtain a given result, in a way that is highly error prone, not to mention extremely tedious ( consider having to redefine a modest number of functions with a moderate number of arguments ).
In Mathematica it is possible to transform a list of arguments to a function into a sequence ( or tuple ) of arguments by using Apply as in Apply[f, args] where args is a list of argument values.
This facilitates the application of functions on lists of argument ( lists ). Practically, one can write f ### { args1, args2, ..., argsm } and obtain a list { f[x11, x12, ..., x1n], f[x21, x22, ..., x2n], ..., f[xm1, xm2, ..., xmn] } ( where ### is just the infix notation that corresponds to Apply[f, args, 1] ).
I guess what I'm looking for is how to achieve the same result in native NetLogo code without having to resort to hacks like the one reported in the former part of this question.
some actual code
globals [ f-list args ]
to setup
let f1 [ [ arg ] -> ( ( item 0 arg ) - 5 ) ^ 2 + ( ( item 1 arg ) - 5 ) ^ 2 + 0 * ( ( item 2 arg ) - 5 ) ^ 2 ]
let f2 [ [ arg ] -> 4 * ( item 1 arg ) ^ 2 + 4 * ( item 0 arg ) ^ 2 + 0 * ( item 2 arg ) ^ 2 ]
let f3 [ [ arg ] -> 2 + ( ( item 1 arg ) - 2 ) ^ 2 + ( ( item 2 arg ) - 1 ) ^ 2 + 0 * ( item 0 arg ) ]
let f4 [ [ arg ] -> 9 * ( item 0 arg ) - ( ( item 2 arg ) - 1 ) ^ 2 + 0 * ( item 1 arg ) ]
set f-list ( list f1 f2 f3 f4 )
set args [ [ 2.04 3.09 -1.32 ] [ 5.57 -3.9 4.0 ] [ -1.1 -0.432 8.0 ] [ 1.32 -2.3 -9.103 ] ]
show map [ [ arg ] -> ( map [ [ f ] -> ( runresult f arg ) ] f-list ) ] args
end
using a setup button that calls the procedure above produces the following output:
observer: [[12.4097 54.8388 8.570500000000001 12.977599999999999] [79.53490000000001 184.9396 45.81 41.13] [66.716624 5.586496 56.914624 -58.9] [66.83239999999999 28.129599999999996 122.56060899999999 -90.190609]]
Thanks to Bryan Head, NetLogo now offers this functionality via the (experimental and undocumented) __apply and __apply-result primitives.
In your case, __apply-result is the one you need:
globals [ f-list args ]
to setup
let f1 [ [x y z] -> (x - 5) ^ 2 + (y - 5) ^ 2 + 0 * (z - 5) ^ 2 ]
let f2 [ [x y z] -> 4 * y ^ 2 + 4 * x ^ 2 + 0 * z ^ 2 ]
let f3 [ [x y z] -> 2 + (y - 2) ^ 2 + (x - 1) ^ 2 + 0 * x ]
let f4 [ [x y z] -> 9 * x - (z - 1) ^ 2 + 0 * y ]
set f-list (list f1 f2 f3 f4)
set args [[2.04 3.09 -1.32] [5.57 -3.9 4.0] [-1.1 -0.432 8.0] [1.32 -2.3 -9.103]]
print map [ arg -> map [ f -> __apply-result f arg ] f-list ] args
end
As you can see, __apply-result takes two arguments, an anonymous reporter and a list, and automatically passes the items in the list as arguments to the anonymous reporter. The __apply primitive is similar, but takes an anonymous command instead of an anonymous reporter.

NetLogo addition of nominal values giving unusual results

When a variable is being incremented by very small value (say 0.01), it is not giving proper (precise) results as expected.
Looking for suggestions(if any) to deal with the same.
globals[p]
to go
set p 0
while[p <= 1]
[
print p
set p p + 0.01
]
end
First read this:
http://floating-point-gui.de/
Then to subdivide an interval into n pieces, do this:
to-report subdivide [#xmin #xmax #n]
let ps n-values (#n + 1) [? / #n]
report map [#xmin + ? * (#xmax - #xmin)] ps
end

How make a list of cumulative sum in netlogo

How can i make a list of cumulative sum of a other list?
i tried it that way:
;;all temperatrue-values around the turtle saved in list
set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors)
;;build cumulative value of temperatures and put each value in list
let tempsum 0
set tempsum_list []
foreach temperature_values
[set tempsum (tempsum + ? )
set tempsum_list fput tempsum tempsum_list
]
but it doesn't work. can anyone fix this problem? it says that "+ excepted a input but gets a list instead".
your code for a cumulative sum works (except that I think you need lput rather than fput. You can see it with this:
to test
let ll [1 2 3 4]
let tempsum 0
let tempsum_list []
foreach ll
[ set tempsum (tempsum + ? )
set tempsum_list lput tempsum tempsum_list
]
print tempsum_list
end
Did the error highlight the line set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors)? Try putting a space after between ) and ^. NetLogo is picky about space around mathematical operators.
As Jen suggested, you can use foreach. Another nice approach is reduce:
to-report partial-sums [#lst]
set #lst (fput [0] #lst) ;;prepare for reduce
report butfirst reduce [lput (?2 + last ?1) ?1] #lst
end
Similar to Alan's solution (Just an update for the recent version of NetLogo that replaces ? with -> for anonymous procedures.)
to-report partial-sums [lst]
report butfirst reduce [[result-so-far next-item] -> lput (next-item + last
result-so-far) result-so-far] fput [0] lst
end
This is like Alan's solution, just abstracted a bit further. (Perhaps too far, depending on your taste! I like JenB's solution as well.)
Let's first define a thing like reduce, but that keeps all the intermediate results:
to-report scan [fn xs]
report reduce [lput (runresult fn ?2 last ?1) ?1]
(fput (list first xs) butfirst xs)
end
Now we can use it to compute partial sums:
observer> show scan task + [1 2 3 4 5]
observer: [1 3 6 10 15]
but we are also free to swap in a different operation:
observer> show scan task * [1 2 3 4 5]
observer: [1 2 6 24 120]

Testing inequalities on lists

I have tried several different ways using different list primitives, but cannot find a way to test an inequality for each item of a list. For example, my list [1 2 -1 -2] could be tested for > 0, and give [1 2 0 0 ]. Can someone please help with this simple task.
There's a couple ways to do this, depending on the result you want. For your example, you can do:
map [ ifelse-value (? > 0) [ ? ] [ 0 ] ] my-list
map creates a new list by applying the given reporter task to each item of the given list. If you haven't used tasks in NetLogo before, ? represents the argument to the task. So, in this case doing:
map [ ifelse-value (? > 0) [ ? ] [ 0 ] ] [1 2 -1 -2]
basically does:
(list ifelse-value (1 > 0) [ 1 ] [ 0 ]
ifelse-value (2 > 0) [ 2 ] [ 0 ]
ifelse-value (-1 > 0) [ -1 ] [ 0 ]
ifelse-value (-2 > 0) [ -2 ] [ 0 ])
ifelse-value is like ifelse, except that it returns the value in the block that runs.
If, you just want to get rid of all items in the list that fail the inequality, you can use filter
filter [ ? < 0 ] my-list
map and filter are very powerful reporters, but they can take a little while to master. But, if you need to do something fancy with lists, chances are you can do it with map, filter, or (in more extreme cases) reduce.

Nested foreach in NetLogo

I am trying to calculate the Gini coefficient of a set of numbers. The Gini coefficient is half the mean absolute difference. That is, for every possible pair of numbers in the list, I need to take their absolute difference and add these differences together (and some other stuff). This is my code
to-report calc-Gini [list-Values]
let sumdiff 0
foreach list-Values
[ foreach list-Values
[ set sumdiff sumdiff + abs ( ?1 - ?2 )
]
]
report 0.5 * sumdiff / (mean list-Values * (length list-Values) ^ 2)
end
When I test it (eg show calc-Gini (list 1 2 3)) I get an error "task expected 2 inputs, but only got 1" on the second foreach.
I think the problem is that NetLogo wants to run through the foreach loops simultaneously. So if the list length is N, then it creates only N pairs (that is, first item in list1 and first item in list2, then the second item in each list etc) which is where the requirement for equal length lists comes from. But I need it to work with the N^2 pairs obtained by crossing the lists.
How can I make the nested foreach do what I want and/or is some other primitive more appropriate?
NetLogo doesn't have a mechanism for binding ?1 and ?2 to an outer and an inner task. When it sees ?1 and ?2 in your code, it expects that both inputs will come from the inner task. And since the inner foreach only provides one input, NetLogo complains.
You can get around that problem by simply assigning the input of the outer foreach to a local variable:
to-report calc-Gini [list-Values]
let sumdiff 0
foreach list-Values
[ let v ?
foreach list-Values
[ set sumdiff sumdiff + abs ( v - ? )
]
]
report 0.5 * sumdiff / (mean list-Values * (length list-Values) ^ 2)
end
That being said, here is an alternative implementation:
to-report calc-gini [ xs ]
report 0.5 * sum map [ sum-diff ? xs ] xs / (mean xs * (length xs) ^ 2)
end
to-report sum-diff [ x xs ]
report sum map [ abs (x - ?) ] xs
end
I can't solve your nested foreach approach, but this might be an alternative way to do your calculation:
If you use ordered data, you can use this equation for the Gini coefficient (given a vector $y$ with $y_i$, $i=1,...,n$)
$$ G(y) = \frac{1}{n} (n + 1 - 2 * \frac{ \sum_{i=1}^{n} (n + 1 - i) y_{i} }{ \sum_{i=1}^{n} y_i} $$
and the following reporter should deliver the result in NetLogo:
to-report calc-Gini [list-Values]
let values sort list-Values ; making sure values are in a non-decreasing order
let n length values
let i 1
let numerator []
foreach values
[ set numerator lput ( (n + 1 - i) * ? ) numerator
set i i + 1
]
report 1 / n * ( n + 1 - 2 * (sum(numerator) / sum(values)) )
end