I want to create five new variable K1 K2 K3 K4 K5 where the table below will return the content for each in their order of entry as shown on Fig 2
SN ID1 ID2 ID3 ID4 ID5 IE1 IE2 IE3 IE4 IE5
1 a b c d e
2 b a f c k
Fig 2
SN K1 K2 K3 K4 K5
1 a b c d e
2 b a f c k
Here's a possible way to do it:
(first recreating your example data to demonstrate on:)
data list list/ SN (f1) ID1 to ID5 IE1 to IE5 (10a1).
begin data
1, "a", "b", "c", , , "d", "e", , ,
2, "b", "a", , "f", , "c", "k", , ,
end data.
This is your example data, now you can run the following syntax, which will yield the results you expected:
string K1 to K5 (a1).
vector K=K1 to K5.
compute #x=1.
do repeat id=ID1 to IE5.
do if id<>"".
compute K(#x)=id. /* correction made here .
compute #x=#x+1.
end if.
end repeat.
Related
For the example, how to write code in z3py to maximize value of a+b+c+d as a,b,c,d are all chosen from list [1,2,3,4,5,6,7,8], which is given as the input. a, b, c, d are distinct values.
a, b, c, d = Ints("a b c d")
o = Optimize()
list = [1,2,3,4,5,6,7,8]
o.maximize(a+b+c+d)
how to write code corresponding to choose "a b c d" value from list. The correct out value of a+b+c+d is 26
Thanks!
Here's one way:
from z3 import *
a, b, c, d = Ints("a b c d")
o = Optimize()
list = [1,2,3,4,5,6,7,8]
vars = [a, b, c, d]
for v in vars:
o.add(Or([v == e for e in list]))
o.add(Distinct(*vars))
goal = Int("goal")
o.add(goal == a+b+c+d)
o.maximize(goal)
if o.check() == sat:
print o.model()
else:
print "not satisfiable"
When I run this, I get:
$ python a.py
[d = 5, c = 8, b = 6, a = 7, goal = 26]
below I have listed some ways to create variables/lists/functions via a for loop in Maxima,
but how, via a for loop, to do:
f1(x) := x^1$
f2(x) := x^2$
f3(x) := x^3$
example code:
for i : 1 thru 10 do
(x : concat ('a, i), x :: i)$
[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
for i : 1 thru 3 do
(x : concat ('L, i), x :: [(3*i)-2,(3*i)-1,3*i])$
[L1, L2, L3];
/* good, but not quite what I want to do */
for i : 1 thru 3 do
f[i](x) := x^i$
[f[1](2), f[2](2), f[3](2)];
/* is there a way, via a for loop, to create */
f1(x) := x^1$
f2(x) := x^2$
f3(x) := x^3$
[f1(2), f2(2), f3(2)];
EDIT: further code:
/* is there a way, via a for loop, to create */
f(x) := x^1$
g(x) := x^2$
h(x) := x^3$
[f(2), g(2), h(2)];
for tmp1 : 1 thru 10 do
(tmp2 : parse_string(ascii(96+tmp1)), tmp2 :: tmp1)$
[a, b, c, d, e, f, g, h, i, j];
for tmp1 : 1 thru 10 do
(tmp2 : concat(parse_string(ascii(96+tmp1)), tmp1), tmp2 :: tmp1)$
[a1, b2, c3, d4, e5, f6, g7, h8, i9, j10];
EDIT 2: original problems solved (any code improvements/simplifications welcome):
for i : 1 thru 3 do
eval_string(concat("f", i, "(x) := x^", i))$
[f1(2), f2(2), f3(2)];
for i : 1 thru 3 do
eval_string(concat(ascii(96+5+i), "(x) := x^", i))$
[f(2), g(2), h(2)];
sum:0$
for i : 1 thru 3 do
sum:sum + eval_string(concat("f", i, "(2)"))$
sum;
sum:0$
for i : 1 thru 3 do
sum:sum + eval_string(concat(ascii(96+5+i), "(2)"))$
sum;
What is your larger goal here? In general using subscripted symbols e.g. a[1] instead of a1 is to be preferred. When you define a subscripted function, you only define it once, not for every value of the subscript. E.g.
(%i1) f[i](x) := x^i $
(%i2) [f[1], f[2], f[3]];
2 3
(%o2) [lambda([x], x), lambda([x], x ), lambda([x], x )]
(%i3) [f[1](u), f[2](v), f[3](w)];
2 3
(%o3) [u, v , w ]
If that doesn't work for you, maybe you can explain about what you're trying to achieve.
I have collected here, various ways of creating multiple variables/lists/functions via a loop in Maxima,
as an answer to the question, '[f1(2), f2(2), f3(2)]' explicitly answers the original question:
for i : 1 thru 10 do
(x : concat ('a, i), x :: i)$
[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
for i : 1 thru 3 do
(x : concat ('L, i), x :: [(3*i)-2,(3*i)-1,3*i])$
[L1, L2, L3];
for tmp1 : 1 thru 10 do
(tmp2 : parse_string(ascii(96+tmp1)), tmp2 :: tmp1)$
[a, b, c, d, e, f, g, h, i, j];
for tmp1 : 1 thru 10 do
(tmp2 : concat(parse_string(ascii(96+tmp1)), tmp1), tmp2 :: tmp1)$
[a1, b2, c3, d4, e5, f6, g7, h8, i9, j10];
for i : 1 thru 3 do
eval_string(concat("f", i, "(x) := x^", i))$
[f1(2), f2(2), f3(2)];
for i : 1 thru 3 do
eval_string(concat(ascii(96+5+i), "(x) := x^", i))$
[f(2), g(2), h(2)];
f[i](x) := x^i$
[f[1], f[2], f[3]];
[f[1](2), f[2](2), f[3](2)];
[f[1](u), f[2](v), f[3](w)];
I'm reading the F# for C# developers book and there's this function that I can't seem to understand what are the effects of this function
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
the output is
triple is (1,2,3)
triple is (2,3,4)
triple is (3,4,5)
why a, b, c are initialized with tripleVariable? Is it because it was needed in the for loop to know their type (or its type, since it's a Tuple)?
The code snippet is using variable shadowing when defining the variables a, b and c. The variables are first initialized to the values of tripleVariable (line 2), but then they are shadowed by a new definition inside the for loop (line 4).
You can think of these as different variables - the code is equivalent to the following:
let tripleVariable = 1, "two", "three"
let a1, b1, c1 = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a2, b2, c2 in l do
printfn "triple is (%d,%d,%d)" a2 b2 c2
Variable shadowing simply lets you define a variable with a name that already exists in the scope. It hides the old variable and all subsequent code will only see the new one. In the above code snippet, the old (shadowed) variables b and c even have different types than the new ones.
The code contains 2 samples. The first one is
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
The 2nd one
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
They can be run independently.
The values a, b, and c in the for loop hide the a, b, and c defined outside of the loop. You can print a, b, and c after the loop to see that they still contain the values from tripleVariable:
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
printfn "tripleVariable is (%A,%A,%A)" a b c
Result:
triple is (1,2,3)
triple is (2,3,4)
triple is (3,4,5)
tripleVariable is (1,"two","three")
I just tried to create a histogram build upon a list of characters like this:
histogram [a c a c b c a c c c c c c a c c a c a c a a c c c a a b c b c]
But it doesn't show anything.
Is it just supposed to handle numbers or am I missing something?
You didnĀ“t use quotes in your histogram list, but I am assuming that you wanted to plot a list of strings like ["a" "b" "c" ...], right?
As far as I know, it is not possible to use categorical values (like strings) for a histogram in netlogo plots. This is also stated in the netlogo dictionary:
histogram [...] Any non-numeric values in the list are ignored. [...]
One way to solve this would be a conversion to a numeric list by just giving every string-character a specific number:
let m ["a" "c" "a" "c" "b" "c" "a" "a" "c" "c" "b" "b" "c" "c" "a" "a"]
let n [ ]
foreach m
[
if (? = "a") [set n lput 0 n]
if (? = "b") [set n lput 1 n]
if (? = "c") [set n lput 2 n]
;...
]
histogram n
histogram work as you said. It's not easy to help you with as little information about your code. For me the error can come from 2 ways :
the plot context definition need to be in bar (i.e image)
the x-range plot definition need to be defined
for the 2nd hypothese some thing like
set-plot-x-range 0 ( (max myliste) + 5)
histogram myliste
let maxbar modes myliste
let maxrange length filter [ ? = item 0 maxbar ] myliste
set-plot-y-range 0 1000
In the plot on code interface may work
If you choose the 2nd ways, you don't need tout defined the scale like on the screenshot
Is there a way in Maxima to assign values to a list of variables? Say I have two lists:
var : [a, b, c];
val : [1, 2, 3];
... and I want to assign 1 to a, 2 to b etc. Of course by iterating over the lists somehow, not "manually", i.e. a : 1; b : 2 ...
Thanks!
Use the :: operator.
(%i4) x : '[a, b, c];
(%o4) [a, b, c]
(%i5) x :: [11, 22, 33];
(%o5) [11, 22, 33]
(%i6) a;
(%o6) 11
(%i7) b;
(%o7) 22
(%i8) c;
(%o8) 33