C++ code for Delphi `in` set operator - delphi

I could not fully understand set membership in the help files. Please explain how in is handled in C++ for the following code:
if s1[1] in['0'..'9'] then
begin
ii := StrToInt(s1)+1;
s1 := IntToStr(ii);
if Length(s1)<2 then s1 := '0'+s1;
Edit_deneyismi.text := copy(s,1,i)+s1;
end
else Edit_deneyismi.text := 'Yeni_Deney_01';

Delphi sets are implemented in C++Builder using the Set<> template class, which has a Contains() method to support in operations, eg:
Set<char, '0', '9'> Digits;
for (char c = '0'; c <= '9'; ++c)
Digits << c;
if (Digits.Contains(s1[1]))
{
ii = StrToInt(s1)+1;
s1 = IntToStr(ii);
if (s1.Length() < 2) s1 = "0" + s1;
Edit_deneyismi->Text = s.SubString(1, i) + s1;
}
else
Edit_deneyismi->Text = "Yeni_Deney_01";
Otherwise, use the C isdigit() function, or the RTL Character::IsDigit() function. Or just compare the char values manually like Michael suggested.

Related

Mathematica's ListLinePlot in wxMaxima

I have the following functions:
P[t_] := P[t] = P[t-1] +a*ED[t-1];
ED[t_] := ED[t] = DF[t] + DC[t];
DF[t_] := DF[t] = b (F - P[t]);
DC[t_] := DC[t] = c (P[t] - F);
And the following parameters:
a=1;
c=0.2;
b = 0.75;
F=100;
In Mathematica I use the function "ListLinePlot" in order to plot P[t] and F:
ListLinePlot[{Table[P[t], {t, 0, 25}], Table[F, {t, 0, 25}]}, PlotStyle → {Black, Red},Frame → True, FrameLabel → {"time", "price"}, AspectRatio → 0.4, PlotRange → All]
How can I do this in wxMaxima? Is there a similar function or an alternative to ListLinePlot?
This is my attempt in wxMaxima:
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*[F-P[t]];
DC[t] := c*[P[t]-F];
a=1;
c=0.2;
b=0.75;
F=100;
And then I tried:
draw2d(points(P[t], [t,0,25]))
The plotted function should look like this:
OK, I've adapted the code you showed above. This works for me. I'm working with Maxima 5.44 on macOS.
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*(F-P[t]);
DC[t] := c*(P[t]-F);
a:1;
c:0.2;
b:0.75;
F:100;
P[0]: F + 1;
Pt_list: makelist (P[t], t, 0, 25);
load (draw);
set_draw_defaults (terminal = qt);
draw2d (points_joined = true, points(Pt_list));
Notes. (1) There needs to be a base case for the recursion on P. I put P[0]: F + 1. (2) Assignments are : instead of =. Note that x = y is a symbolic equation instead of an assignment. (3) Square brackets [ ] are only for subscripts and lists. Use parentheses ( ) for grouping expressions. (4) Syntax for draw2d is a little different, I fixed it up. (I put a default for terminal since the built-in value is incorrect for Maxima on macOS; if you are working on Linux or Windows, you can omit that.)
EDIT: Try this to draw a horizontal line as well.
draw2d (points_joined = true, points(Pt_list),
color = red, points([[0, F], [25, F]]),
yrange = [F - 1, P[0] + 1]);

check condition only if a condition is met within an if then

I have an example of a code and not sure what way is the best to use.
For example I have
if (x = 1) and (y = 2) and (if abc = false then check if z = 3) then
begin
...
check only
if x = 1
if y = 2
if abc = false check z = 3. if abc = true then dont check z = 3
i am not sure if i am explaining the best but hopefuly people will understand.
I want to know if this is possible or the best way to do it. Keeping in mind that rather than in example where its x, y, z and abc. there can be more in my use.
I currently have structure as...which i dont think is practical, and think theres a better way but i am not sure
if (abc = false) then
begin
if (x = 1) and (y = 2) and (z = 3) then
begin
...
end
else
begin
if (x = 1) and (y = 2) then
begin
...
Thanks in advance
I think you're looking for or. Now you will check that x must be 1, y must be 2, and if abc is false, z must be 3.
If abc = true, z can still be three, but it won't be checked.
Note that I just wrote abc instead of abc = true. Since it's a Boolean (true/false) already, that's allowed.
Also note how the operations are grouped using parentheses. The total sub-expression abc or (z=3) must return true for the total expression to return true.
Furthermore the sequence of the terms is significant - they are evaluated left-to-right. If the term (abc or (z=3)) is replaced by the logically-equivalent term ((z=3) or abc) then z=3 will be evaluated.
if (x = 1) and (y = 2) and (abc or (z = 3)) then
// Your magic goes here
Test program body to prove sequence is important
function z : Integer;
begin
writeln('Z being evaluated');
result := x + y;
end;
begin
x := 1;y := 2;
abc := true;
if (x=1) and (y=2) and (abc or (z=3)) then
writeln ('evaluated true')
else
writeln ('evaluated false');
writeln('done');
readln;
end.
Neither of your code samples compile, because neither is using the proper syntax.
This should get you started:
if (x = 1) and (y = 2) then
begin
if (abc) then
// Handle abc = True
else
begin
if (z = 3) then
// Handle abc = false and z = 3
else
// Handle abc = false and z <> 3
end;
end;

Dafny generic type array error

In trying to verify a generic FIFO queue backed by an array I ran into a confusing error. The queue was found in this paper, authored by the creator of Dafny.
The error in question is:
unless an initializer is provided for the array elements, a new array of 'Data' must have empty size
which relates to both lines allocating an array via new Data[whatever] in the constructor and the enqueue method.
Dafny version: Dafny 2.0.0.00922 technical preview 0
Full code for reference.
class {:autocontracts} SimpleQueue<Data>
{
ghost var Contents: seq<Data>;
var a: array<Data>;
var m: int, n: int;
predicate Valid() {
a != null && a.Length != 0 && 0 <= m <= n <= a.Length && Contents == a[m..n]
}
constructor ()
ensures Contents == [];
{
a := new Data[10];
m := 0;
n := 0;
Contents := [];
}
method Enqueue(d: Data)
ensures Contents == old(Contents) + [d];
{
if n == a.Length {
var b := a;
if m == 0 {
b := new Data[2 * a.Length];
}
forall (i | 0 <= i < n - m) {
b[i] := a[m + i];
}
a, m, n := b, 0, n - m;
}
a[n], n, Contents := d, n + 1, Contents + [d];
}
method Dequeue() returns (d: Data)
requires Contents != [];
ensures d == old(Contents)[0] && Contents == old(Contents)[1..];
{
assert a[m] == a[m..n][0];
d, m, Contents := a[m], m + 1, Contents[1..];
}
}
method Main()
{
var q := new SimpleQueue();
q.Enqueue(5); q.Enqueue(12);
var x := q.Dequeue();
assert x == 5;
}
Since the time of writing that paper, Dafny's type system has been generalized to support types that are not "default initializable". This has led to some backwards incompatibilities.
The easiest fix is to change
class SimpleQueue<Data>
to
class SimpleQueue<Data(0)>
which means that the type variable Data can only be instantiated with default-initializable types.
Another fix is to change the constructor to accept a default value for type Data as an argument. Then you can allocate an array using an initializer function, as in
new Data[10] (_ => d)

Translate VHDL to Verilog

I have a problem with translating VHDL to Verilog.
It's part of my source code on VHDL.
With I/O I somehow understood, but have some problems to translate this string
ib1 <= std_logic_vector(to_unsigned(i,ib1'length));
to verilog?
COMPONENT GenerateModel
PORT(
ib1 : IN std_logic_vector(3 downto 0);
);
END COMPONENT;
--Inputs
signal ib1 : std_logic_vector(3 downto 0) := (others => '0');
BEGIN
uut: GenerateModel PORT MAP (
ib1 => ib1,
);
process
begin
for i in 0 to 15 loop
ib1 <= std_logic_vector(to_unsigned(i,ib1'length));
wait for 10 ns;
end loop;
end process;
end;
To extend into Verilog from Paebbels' comment, the line you are looking at does an explicit conversion from the type of the loop variable i to the port variable ib1. In Verilog, that explicit conversion is not needed, you can just assign the port variable directly. So, for example (in Verilog IEEE 1364-1995 compatible):
integer i;
...
for (i = 0; i < 16; i = i + 1) begin
ib1 = i; // <-- The line
#10; // -- Assume 1 step is 1 ns, can specific timescale if needed
end
If you want, you can even loop through the variable directly if its of type reg (ie, not a net):
for (ib1 = 0; ib1 < 15; ib1 = ib1 + 1) begin
#10;
end
#10;
[Note that as Greg mentioned, you need to be sure you dont create an infinite loop as if ib1 is 4-bits wide, it will always be less than 16, thus I fixed the example above to loop until ib1 is 15 (4'b1111)]

How do I Save[] definitions associated to a symbol in Mathematica without saving subsidiary definitions?

The built-in Mathematica command Save[file, symbol] uses FullDefinition[] to look up the definition symbol and all of the subsidiary definitions.
For example, the commands
a:=b
c:=2a+b
Save[ToFileName[NotebookDirectory[],"test.dat"],c]
produces the file test.dat containing
c := 2*a + b
a := b
I have a program with a lot of prettifying MakeBoxes type definitions that I do not want to be saved when I Save[] the many separate results.
In terms of the simple example above, I do not want the a := b definition saved to the file. Does anyone know a neat way to make this happen?
According to the documentation, Save uses FullDefinition while what you want is for it to use Definition. Using a Block we can override the global definition of any symbol, and in particular replace FullDefinition with Definition while running Save:
Block[{FullDefinition},
FullDefinition = Definition;
Save[filename, c]
];
FilePrint[filename]
DeleteFile[filename]
The magic works:
c := 2*a + b
EDIT. Wrapping things up with the right attributes:
SetAttributes[truncatedSave, HoldRest]
truncatedSave[filename_, args__] := Block[{FullDefinition},
FullDefinition = Definition;
Save[filename, args]];
I think
DumpSave["test1", c]
Does that.
Sample code:
a := b;
c := 2 a + b;
DumpSave["test1", c];
Clear[a, c];
<< test1
?a
?c
Out
_____________________
Global`a
_____________________
Global`c
c:=2 a+b
Warning - Warning - I don't know what I am doing
Just found this browsing the help system randomly.
Never before used RunThrough ... anyway seems to do what you want.
Clear["Global`*"];
a := b;
c := 2 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt", Unevaluated[Put[Definition[c], "c:\\rtout"]]]
FilePrint[outputfile]
Clear[a, c];
<< "c:\\rtout"
DeleteFile[outputfile]
?c
Out
c := 2*a + b
_______________________________
Global`c
c:=2 a+b
Edit.. Works on lists with a little Hold-Fu
Clear["Global`*"];
(*Trick here *)
f[l_] := Definition ## HoldPattern /# Unevaluated#l;
SetAttributes[f, HoldFirst];
a := b;
c := 2 a + b;
d := 3 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt",Unevaluated[Put[Evaluate[f#{c, d}], "c:\\rtout"]]]
(* test *)
FilePrint[outputfile]
Clear[a, c, d];
<< "c:\\rtout"
DeleteFile[outputfile]
?c
?d

Resources