How to create map (std::map) like structure in maple? - maple

I need a container with API like:
addKeyValuePair(containerRef, [key, value])
searchByKey(containerRef, key) #that would return NULL or `value`
How to create such thing in Maple (15)?

You might just want usual indexing into a table. Note that previously unassigned name Y below becomes a table just by virtue of assigning to some indexed entry Y[a].
restart:
Y[a]:=211:
Y[a];
211
Y[b];
Y[b]
assigned(Y[a]);
true
assigned(Y[b]);
false
eval(Y);
table([a = 211])
Having said all that, it's only a small bit of wrapping to get the functionality you've described, where accessing Y[b] returns NULL when Y[b] is not yet assigned, etc.
You can of customize (or remove) the error checking and restriction that key be of type symbol, as Maple allows a table to be indexed more generally.
restart:
addKeyValuePair:=proc(containerRef::{indexed,symbol}, L::list)
if nops(L) <> 2 then
error "expecting list with two entries"; end if;
if not type(L[1], symbol) then
error "expecting list with symbol as first entry"; end if;
containerRef[L[1]]:=L[2];
NULL;
end proc:
searchByKey:=proc(containerRef::{indexed,symbol}, key::name)
if assigned(containerRef[key]) then
containerRef[key];
else
NULL;
end if;
end proc:
addKeyValuePair( Y, [a, 4.5] );
searchByKey( Y, a );
4.5
searchByKey( Y, b );
searchByKey( Q, b );
addKeyValuePair( Y, [a, 211] );
searchByKey( Y, a );
211
addKeyValuePair( Y, [c] );
Error, (in addKeyValuePair) expecting list with two entries
addKeyValuePair( Y, [2.34, 211] );
Error, (in addKeyValuePair) expecting list with symbol as first entry
A table is a mutable data structure. It is of type last_name_eval and in consequence it (effectively) gets passed "by reference" as argument to a procedure call.

Related

Converting arrays from signed to integer in VHDL?

I have declared an array
type datastream is array(0 to 10) of signed (5 downto 0);
For simulation, I want to display the array as integer-numbers
So I created
type datastream_int is array(0 to 10) of integer;
and
signal DIN_ARRAY: datastream;
signal DIN_ARRAY_int: datastream_int;
...
DIN_ARRAY_real <= datastream_int(DIN_ARRAY);
But it fails. How to convert it? Dont want to use a for loop
The numeric_std package, that I assume you are using, provides a to_integer function to convert from a single signed value to a single integer object. For an array, you're going to have to use a for loop. Something like this:
for i in DIN_ARRAY'range loop
DIN_ARRAY_int <= to_integer(DIN_ARRAY(i));
end loop;
You could also provide a conversion function (it will also contain a for loop)
function datastream_to_datastream_int( d : datastream ) return datastream_int is
variable r : datastream_int;
begin
for i in d'range loop
r(i) := to_integer(d(i));
end loop;
return r;
end function;
....
--no loop required
DIN_ARRAY_int <= datastream_to_datastream_int(DIN_ARRAY);
So, there will be a for loop somewhere.
Your code fails because you have attempted a type conversion, which is only allowed between similar types - ie. array or record types where the element types match between the two types.
PS. VHDL 2008 provides an integer_vector type in the std.standard library (which is included by default) which may help by allowing you to do this:
signal DIN_ARRAY_int: integer_vector(DIN_ARRAY'range);
If you did decide to keep datastream_int as per your original, you could type convert it to an integer_vector, because the types are similar:
my_iv <= integer_vector(DIN_ARRAY_int);

Check if indexed variable has a value assigned somewhere

Part 1
If we have this
[> restart; a[5]:=23: a[7]:=41:
then
[> about(a); whattype(a);
a:
nothing known about this object
symbol
but
[> print(a);
table([5 = 23, 7 = 41])
and
[> convert(a,list);
[23, 41]
So Maple does have enough information about variable a and its indexes somewhere. How can we check that information without printing?
For small indexes it's not a problem, but if b[123457891234578912345789]:=789; then how do we check if there is some index i for which b[i] has a defined value and what that index i is (without printing, i. e. not manually)?
Part 2
I'd like to be able to work with such variables inside a function and return such variable.
For example, let's say I want to increase all indexes by 1. If I know which indexes exist then I can do that
[> a[5]:=23: a[7]:=41:
f:=proc(x) local y;
y[6]:=x[5]; y[8]:=x[7]; y;
end proc:
b:=f(a); b[6]; b[8];
b:=y
23
41
but often I don't know what indexes variable a has.
There are several programmatic queries you can make,
restart;
a[5]:=23: a[7]:=41:
# test whether a is assigned
assigned('a');
true
# test whether a is assigned a table
type(a, table);
true
# test whether table a has any indexed values
evalb( nops([indices(a, 'nolist')]) > 0 );
true
assigned('a'[7]);
true
If you wish you can make such queries prior to attempting to access and utilize the indexed reference (ie. to check that it has an assigned value). For example,
if type(a,table) and assigned('a'[7]) then
a[7];
end if;
41
if type(a, table) then
[indices(a, 'nolist')];
end if;
[5, 7]

"operator symbol not allowed for generic subprogram" from Ada

I want to make subprogram for adding array's elements with Ada.
subprogram "Add_Data" have 3 parameters-
first parameter = generic type array (array of INTEGER or array of REAL)
second parameter = INTEGER (size of array)
third parameter = generic type sum (array of INTEGER -> sum will be INTEGER, array of REAL -> sum will be REAL)
I programmed it from ideone.com.
(I want to see just result by array of INTEGER. After that, I will test by array of REAL)
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
procedure test is
generic
type T is private;
type Tarr is array (INTEGER range <>) of T;
--function "+" (A,B : T) return T;
--function "+" (A, B : T) return T is
--begin
-- return (A+B);
--end "+";
procedure Add_Data(X : in Tarr; Y : in INTEGER; Z : in out T);
procedure Add_Data(X : in Tarr; Y : in INTEGER; Z : in out T) is
temp : T;
count : INTEGER;
begin
count := 1;
loop
temp :=temp+ X(count); //<-This is problem.
count := count + 1;
if count > Y then
exit;
end if;
end loop;
Z:=temp;
end Add_Data;
type intArray is array (INTEGER range <>) of INTEGER;
intArr : intArray := (1=>2, 2=>10, 3=>20, 4=>30, 5=>8);
sum : INTEGER;
procedure intAdd is new Add_Data(Tarr=>intArray, T=>INTEGER);
begin
sum := 0;
intAdd(intArr, 5, sum);
put (sum);
end test;
when I don't overload operator "+", It makes error.
"There is no applicable operator "+" for private type "T" defined."
What can I do for this?
If a generic’s formal type is private, then nothing in the generic can assume anything about the type except that it can be assigned (:=) and that it can be compared for equality (=) and inequality (/=). In particular, no other operators (e.g. +) are available in the generic unless you provide them.
The way to do that is
generic
type T is private;
with function "+" (L, R : T) return T is <>;
This tells the compiler that (a) there is a function "+" which takes two T’s and returns a T; and (b) if the actual T has an operator "+" which matches that profile, to allow it as the default.
So, you could say
procedure intAdd is new Add_Data (T => Integer, ...
or, if you didn’t feel like using the default,
procedure intAdd is new Add_Data (T => Integer, "+" => "+", ...
In addition to not knowing how to declare a generic formal subprogram (Wright has shown how to do this for functions), your code has a number of other issues that, if addressed, might help you move from someone who thinks in another language and translates it into Ada into someone who actually uses Ada. Presuming that you want to become such a person, I will point some of these out.
You declare your array types using Integer range <>. It's more common in Ada to use Positive range <>, because people usually refer to positions starting from 1: 1st, 2nd, 3rd, ...
Generics are used for code reuse, and in real life, such code is often used by people other than the original author. It is good practice not to make unstated assumptions about the values clients will pass to your operations. You assume that, for Y > 0, for all I in 1 .. Y => I in X'range and for Y < 1, 1 in X'range. While this is true for the values you use, it's unlikely to be true for all uses of the procedure. For example, when an array is used as a sequence, as it is here, the indices are immaterial, so it's more natural to write your array aggreate as (2, 10, 20, 30, 8). If I do that, Intarr'First = Integer'First and Intarr'Last = Integer'First + 4, both of which are negative. Attempting to index this with 1 will raise Constraint_Error.
Y is declared as Integer, which means that zero and negative values are acceptable. What does it mean to pass -12 to Y? Ada's subtypes help here; if you declare Y as Positive, trying to pass non-positive values to it will fail.
Z is declared mode in out, but the input value is not referenced. This would be better as mode out.
Y is not needed. Ada has real arrays; they carry their bounds around with them as X'First, X'Last, and X'Length. Trying to index an array outside its bounds is an error (no buffer overflow vulnerabilities are possible). The usual way to iterate over an array is with the 'range attribute:
for I in X'range loop
This ensures that I is always a valid index into X.
Temp is not initialized, so it will normally be initialized to "stack junk". You should expect to get different results for different calls with the same inputs.
Instead of
if count > Y then
exit;
end if;
it's more usual to write exit when Count > Y;
Since your procedure produces a single, scalar output, it would be more natural for it to be a function:
generic -- Sum
type T is private;
Zero : T;
type T_List is array (Positive range <>) of T;
with function "+" (Left : T; Right : T) return T is <>;
function Sum (X : T_List) return T;
function Sum (X : T_List) return T is
Result : T := Zero;
begin -- Sum
Add_All : for I in X'range loop
Result := Result + X (I);
end loop Add_All;
return Result;
end Sum;
HTH

Matlab set param from array

In Matlab, in order to change the value of a block I do
set_param('model/V','Amplitude','100')
and the value of V is 100. But if I do
for i=1:10
set_param('model/V','Amplitude','P(i)')
...
end
The it stores the value of V as P(i). But in order to access the i-th element of the 20-by-1 P matrix, I need to refer to it by P(i). What is my error?
Change the value to string using:-
set_param('model/V','Amplitude',num2str(P(i)) );
Also it will set the value of 'model/V' to P(20) i.e. last one.
You might want to loop through the current blocks too
Something like : (just example)
set_param(['model/V' num2str(i)],'Amplitude',num2str(P(i)) );
for model/V1, model/V2,...model/V20 .

Trying to learn Prolog, can't figure out what's wrong

Doing a small example, I want to insert a symbol within a list at a certain pozition
domains
element = symbol
list = element*
position = integer
predicates
insert (element, position, list, list) %(input,input,input,output)
clauses
insert (E,_,[],[E]).
insert (E, 1,[H|T],[E|[H|T]]). %(I insert E on the "first" position)
% I get errors how E and T are not bound. I have no idea how to make them bound
insert (E,P,[H|T],[H1|T1]) :-
P > 1,
P1 = P - 1,
insert (E,P1,T,T1).
It doesn't work ... but I don't know why. Well it kinda works. I would like it to show me outputList = [NEW_LIST] instead of showing every symbolName=_ and then outputList = [_,_,_,_].
Apparently there is some problem with symbol data type, if I use integer, it fixes itself.
Also, this is esential:
domains
element = integer
list = element*
position = integer
predicates
insert (element, position, list, list) %(input,input,input,output)
clauses
insert (E,_,[],[E]).
insert (E, 1,[H|T],[E|[H|T]]).
insert (E,P,[H|T],[H|T1]) :- % has to be "H" (or anything else) in both
P > 1, % so prolog understands what we are trying to do
P1 = P - 1, % don't really understand why though
insert (E,P1,T,T1). % I might be wrong