Animation in wxmaxima - animation

How can I implement animation in wxmaxima? Suppose I have a function f(x) = x^a and I want to plot a graph in the form of a gif animation with a variable parameter a. The documentation says:
draw(
delay = 100,
file_name = "zzz",
terminal = 'animated_gif,
gr2d(explicit(x^2,x,-1,1)),
gr2d(explicit(x^3,x,-1,1)),
gr2d(explicit(x^4,x,-1,1)));
there will be three frames with a delay of 1 second (100 delay = 1 sec). In Maxima, you can use the 'for' loop. How to insert the for loop correctly into the draw() environment so that the number of frames can be adjusted by the cycle counter and the function being drawn depends on the counter?

I don't think draw recognizes for loops. Try building up the list of frames via map and/or makelist. Append any additional arguments to the list of frames, and then say apply('draw, mylist). Something like:
myfunctions: makelist (x^i, i, 1, n);
myframes: map (lambda ([e], gr2d (explicit (e, x, -1, 1))), myfunctions);
mylist: append ([delay = 100, file_name = "zzz", terminal = 'animated_gif], myframes);
apply ('draw, mylist);
where n is the number of frames you want.

Related

How can I remove the "Corr" text from the correlation matrix done with GGally?

I have used the following codes for the correlation matrix
library(ggplot2)
library(GGally)
ggpairs(CorrelationBINA, title="Correlation matrix of BINA dhan7",
upper = list(continuous= wrap("cor", size = 10)),
lower = list(continuous ="smooth"))
and got the following Correlation matrix. From the upper triangle of the matrix, I want to remove the word "Corr" and want to keep only the correlation value.
This takes a user-defined function, calculates the correlation, rounds to two decimals, and then has this text display instead of the correlation value default that has the "Corr" term:
#This function identifies correlation for each pair of variables that will go into ggpairs command written later
cor_func <- function(data, mapping, method, ...){
x <- eval_data_col(data, mapping$x)
y <- eval_data_col(data, mapping$y)
corr <- cor(x, y, method=method, use='complete.obs')
ggally_text(
label = as.character(round(corr, 2)),
mapping = aes(),
xP = 0.5, yP = 0.5,
color = 'black',
...
)
}
ggpairs(iris[-5],
upper = list(continuous = wrap(cor_func,
method = 'spearman')))

Julia JuMP array variable constraint

I am trying to model a non-linear problem involving vector rotation using JuMP in Julia. I need a constraint, which looks something like v[1:3] == rotate(v) If I write it like this, it does not work, since "Nonlinear expressions may contain only scalar expressions". How can I work around this?
I could say something like v[1] == rotate(v)[1] and same for v[2] and v[3], but then I would have to compute rotate(v) three times as often. I could also try to split the rotate function into three functions which compute one element each, but the actual constraint is a bit more complicated than a simple rotation, so this could prove to be tricky.
Are there any other ways to do this? Maybe to have something like an auxiliary variable which can be computed as a vector and then in the constraint only compare the elements of the two vectors (essentialy the first approach, but without computing the function three times)?
See here for a suggested work-around:
https://discourse.julialang.org/t/how-to-set-up-nlconstraints-from-multi-output-user-defined-function/42309/5?u=odow
using JuMP
using Ipopt
function myfun(x)
return sum(xi for xi in x), sum(xi^2 for xi in x)
end
function memoized()
cache = Dict{UInt, Any}()
fi = (i, x) -> begin
h = hash((x, typeof(x)))
if !haskey(cache, h)
cache[h] = myfun(x)
end
return cache[h][i]::Real
end
return (x...) -> fi(1, x), (x...) -> fi(2, x)
end
model = Model(Ipopt.Optimizer)
f1, f2 = memoized()
register(model, :f1, 3, f1; autodiff = true)
register(model, :f2, 3, f2; autodiff = true)
#variable(model, x[1:3] >= 0, start = 0.1)
#NLconstraint(model, f1(x...) <= 2)
#NLconstraint(model, f2(x...) <= 1)
#objective(model, Max, sum(x))
optimize!(model)

How to convert a vector of arrays into 4D array?

I'm trying to play a little bit with Knet.jl and CNNs. Every example I found requires the input for CNN to be in the form of [dim1, dim2, n_of_channels, N] where N is a number of the actual images.
I'm a bit new to Julia and I don't know how to accomplish this.
I loaded images from some private directory and pushed them to a vector, so that their length is N.
images = Vector()
for img_file in readdir(dir)
img = load("$dir/$img_file")
images = vcat(images, [img])
end
typeof(image)
"320-element Array{Any,1}"
However in the following example xtrn is stored as 28x28x1x60000 Array and that is what I would like to accomplish with the private dataset.
using Knet; include(Knet.dir("data","mnist.jl"))
xtrn,ytrn,_,_= mnist()
typeof(xtrn)
Array{Float32,4}
I'm aware of functions as channelview, reshape and it's seems they should provide solution but I played with them a bit and got DimensionMismatch error all the time. I guess there's something I miss.
I don't have the files you are using in your example. But I would use cat in conjunction with a generator. Here's an example of something you can do:
julia> reduce((x,y)->cat(x, y, dims=4), rand(3,3) for _ in 1:3)
3×3×1×3 Array{Float64,4}:
[:, :, 1, 1] =
0.366818 0.847529 0.209042
0.281807 0.467918 0.68881
0.179162 0.222919 0.348935
[:, :, 1, 2] =
0.0418451 0.256611 0.609398
0.65166 0.281397 0.340405
0.11109 0.387638 0.974488
[:, :, 1, 3] =
0.454959 0.37831 0.554323
0.213613 0.980773 0.743419
0.133154 0.782516 0.669733
In order to do this with your files, this might work (untested):
images = reduce((x,y)->cat(x, y, dims=4), load(joinpath(dir, img_file)) for img_file in readdir(dir))
BTW. You should not initialize vectors like this:
images = Vector()
This makes an untyped container, which will have very bad performance. Write e.g.
images = Matrix{Float32}[]
This initializes an empty vector of Matrix{Float32}s.
Just to fill in the answer of DNF, this code results in Array in the form of [dim1, dim2, 1, N]:
images = reduce((x,y)->cat(x, y, dims=4), load(joinpath(dir, img_file)) for img_file in readdir(dir))
I wanted the 3rd dimension to be the channel and hence, the expected output is produced by:
images = reduce((x, y) -> cat(x, y, dims=4), permutedims(channelview(load(joinpath(dir, img_file))), (2, 3, 1)) for img in readdir(dir))

Logitech Gaming Software lua script code for pure random numbers

I've been trying for days to try find a way to make random numbers in the logitech gaming software (LGS) scripts. I know there is the
math.random()
math.randomseed()
but the thing is i need a changing value for the seed and the solutions from others are to add a os.time or tick() or GetRunningTime stuff which is NOT supported in the LGS scripts.
I was hoping some kind soul could help me by showing me a piece of code that makes pure random numbers. Because i don't want the pseudo random numbers because they are only random once. I need it to be random every time It runs the command. Like if i loop the math.randomI() one hundred times it will show a different number every time.
Thanks in advance!
Having a different seed won't guaratee you having a different number every time.
It will only ensure that you don't have the same random sequence every time you run your code.
A simple and most likely sufficient solution would be to use the mouse position as a random seed.
On a 4K screen that's over 8 Million different possible random seeds and it very unlikely that you hit the same coordinates within a reasonable time. Unless your game demands to click the same position over and over while you run that script.
This RNG receives entropy from all events.
Initial RNG state will be different on every run.
Just use random instead of math.random in your code.
local mix
do
local K53 = 0
local byte, tostring, GetMousePosition, GetRunningTime = string.byte, tostring, GetMousePosition, GetRunningTime
function mix(data1, data2)
local x, y = GetMousePosition()
local tm = GetRunningTime()
local s = tostring(data1)..tostring(data2)..tostring(tm)..tostring(x * 2^16 + y).."#"
for j = 2, #s, 2 do
local A8, B8 = byte(s, j - 1, j)
local L36 = K53 % 2^36
local H17 = (K53 - L36) / 2^36
K53 = L36 * 126611 + H17 * 505231 + A8 + B8 * 3083
end
return K53
end
mix(GetDate())
end
local function random(m, n) -- replacement for math.random
local h = mix()
if m then
if not n then
m, n = 1, m
end
return m + h % (n - m + 1)
else
return h * 2^-53
end
end
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
mix(event, arg) -- this line adds entropy to RNG
-- insert your code here:
-- if event == "MOUSE_BUTTON_PRESSED" and arg == 3 then
-- local k = random(5, 10)
-- ....
-- end
end

How to make a graph of a three-branches function in matlab

How can I make this function's graph in Matlab, so that its body is depicted in the same graph (plot or subplot)?
t0=0.15
x(t)= 1, if 0<=t<(t0/2)
-2, if (t0/2)<=t<=(3/2)*t0
0, else
The real question you should be asking is "How to define a function that has branches?", since plotting is easy once the function is defined.
Here's a way using anonymous functions:
x_t = #(t,t0)1*(0<=t & t<t0/2)-2*(t0/2<=t & t<=(3/2)*t0); %// the 1* is redundant, I only
%// left it there for clarity
Note that the & operator expects arrays and not scalars.
Here's a way using heaviside (aka step) functions (not exactly what you wanted, due to its behavior on the transition point, but worth mentioning):
x_t = #(t,t0)1*heaviside(t)+(-1-2)*heaviside(t-t0/2)+2*heaviside(t-t0*3/2);
Note that in this case, you need to "negate" the previous heaviside once you leave its area of validity.
After defining this function, simply evaluate and plot.
t0 = 0.15;
tt = -0.1:0.01:0.5;
xx = x_t(tt,t0);
plot(tt,xx); %// Or scatter(), or any other plotting function
BTW, t0 does not have to be an input to x_t - if it is defined before x_t, the value of t0 that exists in the workspace at that time will be captured and used, but this also means that if t0 changes later, this will not affect x_t.
I'm not sure of what you want, but would it be it?
clc
close all
clear
t0 = 0.15;
t = 0:0.01:0.15;
x = zeros(size(t));
x(0 <= t & t < (t0/2)) = 1;
x((t0/2) <= t & t <= (3/2)*t0) = -2;
figure, plot(t, x, 'rd')
which gives,
Everything depends on the final t, for example if the end t is 0.3, then you'll get,

Resources