Protected execution, 2 cases - kdb

Why does in first following case protected execution work, but in second does not?:
q)t:([]a:1 2;b:3 4);
q)#[#[cols t; ; :; `bb]; (cols t)?`b; `columnNotFound]
`a`bb
q)#[#[cols t; ; :; `cc]; (cols t)?`c; `columnNotFound] // 1. works perfectly
`columnNotFound
q)#[#[cols t; (cols t)?`c; :; `cc]; `; `columnNotFound] // 2. exception does not handled
'length
[0] #[#[cols t; (cols t)?`c; :; `cc]; `; `columnNotFound]
^
Upd:
Hmm, I suspect something after trying:
q)#[{#[cols t; (cols t)?`c; :; `cc]}; `; `columnNotFound]
`columnNotFound

The protected execution is using the argument you're supplying. The first two examples are projections but the last is not, so it fails on execution.
q){#[cols t;x;:;`bb]}(cols t)?`b
`a`bb
q){#[cols t;x;:;`cc]}(cols t)?`c / thrown into error trap
'length
[1] {#[cols t;x;:;`cc]}
^
q))\
q)#[cols t;(cols t)?`c;:;`cc] / fails on execution
'length
[0] #[cols t;(cols t)?`c;:;`cc]
^
q)
In your upd, making the # apply a function is forcing the argument in the protected execution to be used.
q){#[cols t;(cols t)?`c;:;`cc]}`
'length
[1] {#[cols t;(cols t)?`c;:;`cc]}
^
q))

Related

Cross-module reference resolution error from SV wait statement

I wanted to wait on the output variable of a task.
eg: wait(user_defined_task_name(output_variable_type_name) == 1)
In this example shown below, my intention is to make wait statement to be active from 0ns to 3ns (basically from the beginning of timestamp till t2=1)
Here is a working example;
class cl;
task run(output bit t);
$display("time=%0t , t=%0b",$realtime, t);
#1;
$display("time=%0t , t=%0b",$realtime, t);
#2;
t = 1;
$display("time=%0t , t=%0b",$realtime, t);
endtask
endclass
class c2 extends cl;
bit t2;
task run1();
wait(run(t2) == 1); // error from this line, what am i violating here?
$display("t2 working t2=%0b time = %0t", t2, $realtime);
endtask
endclass
module tmp;
c2 c2_h=new;
initial begin
c2_h.run1();
$display("test msg");
end
endmodule
eda output log:
Top Level Modules:
tmp TimeScale is 1 ns / 1 ns
Error-[XMREF] Cross-module reference resolution error testbench.sv, 19
Cross-module reference resolution error is found. Function is
expected, but actual target is not a function. Source info:
run(this.t2)
1 error CPU time: .116 seconds to compile Exit code expected: 0,
received: 1
A couple of problems with your code. A task does not return a value and cannot be used in an expression. You can only call it as a stand alone statement. But if you change run to a function functions cannot consume time.
In your particular example, you do not change the value of the t argument until the end of the task, and output arguments are copied out upon exiting the task, so you might as well just call the task run(t2) as a statement and it will block until returning.
task run1();
run(t2);
$display("t2 working t2=%0b time = %0t", t2, $realtime);
endtask
If on the otherhand run set the t argument somewhere in the middle of the task, and you want to continue the run1 task as soon as that happened, the you would have use a fork/join_none and a ref argument instead.
class cl;
task run(ref bit t);
$display("time=%0t , t=%0b",$realtime, t);
#1;
$display("time=%0t , t=%0b",$realtime, t);
#2;
t = 1;
#2;
$display("time=%0t , t=%0b",$realtime, t);
endtask
endclass
class c2 extends cl;
bit t2;
task run1();
fork
run(t2);
join_none
wait(t2 == 1);
$display("t2 working t2=%0b time = %0t", t2, $realtime);
endtask
endclass
When I run your code on the Cadence simulator, I get a different message:
xmvlog: *E,INVCTX The task 'run' cannot be used in this context.
Using the nchelp utility to get more information on that message:
A task or void function cannot be passed as an actual argument
because they do not return a value that can be used. They also cannot
be used as part of an expression.
In your simple example, there seems to be no need to use wait. You can simply call the task on its own:
task run1();
run(t2);
$display("t2 working t2=%0b time = %0t", t2, $realtime);
endtask

how to to create a mulitline macro in julia?

macro Estruct(name,arg,type,max=100,min=0,descritpion="")
:(struct $(esc(name))
$((esc(arg)))
$(esc(name))($(esc(arg)))=new(
check($(esc(type)),$(esc(arg)),$(esc(name)),$(esc(max)),$(esc(min)),$(esc(descritpion))))
end)
end
how can I imeplent this macro like this:
#Estruct begin
B
arg1
Float64
200.0
5.0
"this"
end
I don't know how to make a multiline macro. I thought I just have to add begin and end, but I'm always getting: MethodError: no method matching var"#Estruct"(::LineNumberNode, ::Module, ::Expr)
There's no such thing as a multiline macro, that's just a macro that takes a block as an argument. You can see how the macro gets invoked by writing a dummy version of it that just returns its arguments:
macro Estruct(args...); args; end
Now you can invoke that the way you want to call it and it will return its arguments as a tuple:
julia> args = #Estruct begin
B
arg1
Float64
200.0
5.0
"this"
end
(quote
#= REPL[12]:2 =#
B
#= REPL[12]:3 =#
arg1
#= REPL[12]:4 =#
Float64
#= REPL[12]:5 =#
200.0
#= REPL[12]:6 =#
5.0
#= REPL[12]:7 =#
"this"
end,)
julia> typeof(args)
Tuple{Expr}
julia> dump(args[1])
Expr
head: Symbol block
args: Array{Any}((12,))
1: LineNumberNode
line: Int64 2
file: Symbol REPL[12]
2: Symbol B
3: LineNumberNode
line: Int64 3
file: Symbol REPL[12]
4: Symbol arg1
5: LineNumberNode
line: Int64 4
file: Symbol REPL[12]
...
8: Float64 200.0
9: LineNumberNode
line: Int64 6
file: Symbol REPL[12]
10: Float64 5.0
11: LineNumberNode
line: Int64 7
file: Symbol REPL[12]
12: String "this"
julia> args[1].args
12-element Vector{Any}:
:(#= REPL[12]:2 =#)
:B
:(#= REPL[12]:3 =#)
:arg1
:(#= REPL[12]:4 =#)
:Float64
:(#= REPL[12]:5 =#)
200.0
:(#= REPL[12]:6 =#)
5.0
:(#= REPL[12]:7 =#)
"this"
This tells you that the macro gets called with a single argument which is an Expr with head type :block, i.e. the one argument is a quoted block. To implement your "multiline macro" you need to implement the macro body to accept a quoted block expression and process it, presumably by looking at its .args field which is where all the expressions you're interested in are. You'll probably want to ignore all the LineNumberNode objects in there and just process the other items in the block.

Error on print_usage and fzero while Running Matlab Script in Octave

I am trying to run inputfile_calrel_example1 FERUM Matlab scripts from https://www.sigma-clermont.fr/en/ferum in Octave-5.1.0.0 but run into errors with respect to print_usage and fzero as follows:
error: Invalid call to fzero. Correct usage is:
-- fzero (FUN, X0)
-- fzero (FUN, X0, OPTIONS)
-- [X, FVAL, INFO, OUTPUT] = fzero (...)
error: called from
print_usage at line 91 column 5
fzero at line 133 column 5
drho0_dthetaf_integral at line 75 column 22
mod_corr_solve at line 99 column 54
form at line 90 column 58
ferum at line 129 column 33
>>
Looking through print_usage.m file reveals line 91 as follows:
error ("Octave:invalid-fun-call", msg);
while lines 78 to 92:
if (at_toplev)
error ("Octave:invalid-fun-call",
"Invalid call to %s. Correct usage is:\n\n%s\n%s",
name, usage_string, __additional_help_message__ ());
else
msg = sprintf ("Invalid call to %s. Correct usage is:\n\n%s",
name, usage_string);
## Ensure that the error doesn't end up with a newline, as that disables
## backtraces.
if (msg(end) == "\n")
msg(end) = " ";
endif
error ("Octave:invalid-fun-call", msg);
endif
and fzero lines 132 to 134 are as follows:
if (nargin < 2 | nargin > 3)
print_usage (mfilename ());
end
I would like to have hints as to how to resolve the above error messages.
Best regards
Aliyu Aziz
As stated in the comments, fzero was called with the following arguments.
drho0_dthetafi.mu = fzero( ...
'betadrho0_dthetaf' ...
, 0 ...
, optimset('fzero') ...
, dF_dthetafi.mu ...
, PHI2 ...
, F ...
, dPHI2_dZi ...
, dZi_dthetafi.mu ...
, dPHI2_drho0 ...
, detJ ...
, WIP ...
);
From the documentation (help fzero) you can see that the above call is not a valid fzero call:
-- fzero (FUN, X0, OPTIONS)
Find a zero of a univariate function
FUN is a function handle, inline function, or string containing the
name of the function to evaluate.
X0 should be a two-element vector specifying two points which
bracket a zero. If X0 is a single scalar then several nearby and distant
values are probed in an attempt to obtain a valid bracketing. If this is not
successful, the function fails.
OPTIONS is a structure specifying additional options.
To initialize an options structure with default values for 'fzero'
use 'options = optimset ("fzero")'.
So as you see, the extra arguments after 'optimset' trigger an error.
I'm assuming that the extra arguments were intended to be arguments to the betadrho0_dthetaf function. In general the function FUN expects a single argument (since it is univariate). If your betadrho0_dthetaf function expects a number of other parameters, then instead of using it in fzero via string, wrap it around an anonymous function handle which does only take a single argument, and uses your intended function internally to calculate the intended result, e.g.
drho0_dthetafi.mu = fzero( ...
#(x) betadrho0_dthetaf( ...
x ...
, dF_dthetafi.mu ...
, PHI2 ...
, F ...
, dPHI2_dZi ...
, dZi_dthetafi.mu ...
, dPHI2_drho0 ...
, detJ ...
, WIP ...
) ...
, 0 ...
, optimset('fzero') ...
);
or something along those lines, depending on how you would call that beta function.

ultisnips: How to "freeze" vim.current.window.cursor value for snippet

I had a snippet that used to work well (neovim 0.2.0)
snippet #= "comment ===" b
# `!p snip.rv = '=' * (78 - vim.current.window.cursor[1])`
# ${1:comments}
# `!p snip.rv = '=' * (78 - vim.current.window.cursor[1])`
endsnippet
This snippet is basically writing python comments block when triggered,
where the length of "=" depends on the position of the cursor.
For a few days now (I don't know which update makes it failing), the length of "=" is decreasing as long as I type my comment.
It looks like vim.current.window.cursor[1] is constantly re-evaluated.
Any idea how to "freeze" the value?
I finally found:
snippet #= "comment ===" b
`!p
if not snip.c:
width = int(vim.eval("78 - virtcol('.')"))
snip.rv = '# ' + '=' * width`
# ${1:comments}
`!p snip.rv = '# ' + '=' * width`
endsnippet

Debugging GNU octave: Using dbnext after dbup

I'm using GNU octave 3.6.4. According to the changelog (v 3.2):
Moving up and down the call stack with
dbup and dbdown now works.
However, when I'm in debug mode and excecute dbup followed by dbnext, the next line in the lower frame will be executed. Why is this so and how can it be avoided?
octave -q
octave:1> myfunc_base(2,3)
stopped in /home/seb/octave/myfunc.m at line 5
5: keyboard
debug> dbstack
stopped in:
--> myfunc at line 5 [/home/seb/octave/myfunc.m]
myfunc_base at line 4 [/home/seb/octave/myfunc_base.m]
debug> dbup
stopped in myfunc_base at line 4 % <-- looks good!
debug> dbnext
stopped in /home/seb/octave/myfunc.m at line 6 % <-- damn this is the old frame!
6: sp = a + temp;
debug>
The two test functions:
myfunc.m
function sp = myfunc (a, b, c)
temp = b+c;
keyboard
sp = a + temp;
end
myfunc_base.m
function sp = myfunc_base (aa, bb)
temp = myfunc(aa, aa, bb);
sp = aa + temp;
end
To step out you have to use dbstep out. This matches the behaviour of matlab and everything else would be very stange. You can not step to the next line on any level of the stack if an exception occurs.