Converge (fixed point) syntax difference in q and k - kdb

We should use square brackets when flattering all levels in list:
q)b:(1 2;(3 4;5 6);7;8)
q)raze/[b] / flatten all levels
1 2 3 4 5 6 7 8
q)raze/b
'/
[0] raze/b
But why one forced to use raze/[b] for Converge syntax instead of simple raze/b?
Upd
Why this syntax works in k, for example {x+y}/1 2 3 but doesn't work in q?
My assumption that it's been made to prevent qbies errors when using / adverb instead of %. I think there may be a discussion about it in some dev channel, but I've found only Shakti discussion group for now at https://groups.google.com/forum/#!forum/shaktidb, and kx.com also shutted down community wiki, so I don't know where to find an additional info - asking here
Upd2
The / is quite overloaded in k too: see (not official ref though) https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md#over - over, fixedpoint, for and while. Pretty the same as in q, right? But why the interpreter 'ban' the k syntax in q context, - is there a technical reason why q can't recognise a user intention as it k does?

The reason that, say, cos/1 works in k but not q is that q has no ambivalence. That is to say that all q operators are not overloaded on valence, as noted in section 6.1.2 in q4m.
With any of q's adverbs (each:', over:/ scan:\, prior::, eachright:/:, eachleft:\:) the resulting derivative function is interpreted by q to be dyadic, unless you uses []'s to specifically pass one argument.
For the example above q interprets cos/ as do rather than converge and so requires the left argument specifying the number of iterations (Note the results of 0 cos/ 1, 1 cos/ 1, 2 cos/ 1, etc.). The preferred way to resolve is to use []'s: cos/[1].
(cos/) 1 works because user defined functions can never use infix notation, so the expression is automatically interpreted as applying monadically. This is also why 2 (cos/) 1 fails. To resolve this you would again have to use []'s: (cos/)[2;1].

You don't necessarily need square brackets here. You could use
(raze/)b
if you do not want to use square brackets around b. The way you are using over ( or /) without the brackets around b requires the parenthesis around raze/b if you do not specify the initial value of the accumulator. This is because the q interpreter needs to know that you are applying raze/ to the list b rather than applying / to the list first (which is why a '/ error is thrown) then raze after (reading the code from right to left).
More info on using / can be found here: https://code.kx.com/q4m3/6_Functions/#676-over-for-accumulation

Related

How do I encode this in answer set programming?

I am a total newbie to answer set programming, and I am struggling with a rather simple question. The program needs to be written in clingo.
So here is the question:
An abstract argumentation framework consists of a set A of arguments
and an attack relation R ⊆ A X A between them. For any two arguments
a1 and a2, if (a1, a2) ∈ R then we say that a1 attacks a2: if one
admits argument a1 then it casts doubts on argument a2. Formally, a
subset of arguments E ⊆ A is stable if the following two conditions
hold:
no arguments in E attack any other argument of E.
any argument outside of E is attacked by an argument from E.
Write an ASP program that identifies stable subsets of arguments in a
given instance through answer sets. The instance will be provided via
two predicates argument/1 and attack/2 corresponding to A and R
respectively.
Here is an example:
argument (a).
argument (b).
argument (c).
argument (d).
attack (a,b).
attack (b,c).
attack (d,c).
Valid output:
choose (a) choose (d)
This what I tried, which is obviously wrong:
choose(X) :- argument(X), attack(X,Y).
I don't know how to approach this at all.
Please help.
A simple 3 Step solving approach is the following:
describe the facts (check)
generate what you want as a result, but leave the program a choice
give rules which solutions do not apply
So start with 2:
generate possible outcomes. Think of it in simple words: For every argument I choose it or not.
The may or may not part can be solved with a subsum {}.
{choose(X)} :- argument(X).
or even simpler: I choose a subsum from the arguments
{choose(X):argument(X)}.
Lets check the solutions with Potassco and #show choose/1., resoning mode enumerate all:
Answer: 1
Answer: 2
choose(b)
Answer: 3
choose(c).
..
Answer: 15
choose(a) choose(b) choose(c)
Answer: 16
choose(a) choose(b) choose(c) choose(d)
SATISFIABLE
All combinations are found. Time to remove the wrong stuff. Again: think of it in simple words: It is not possible that I choose two arguments where one attacks the other. (If the head is left open, this is read a False.)
:- choose(X), attack(X,Y), choose(Y).
Now check it again:
Answer: 1
Answer: 2
choose(a)
Answer: 3
choose(d)
Answer: 4
choose(a) choose(d)
Answer: 5
choose(c)
Answer: 6
choose(a) choose(c)
Answer: 7
choose(b)
Answer: 8
choose(b) choose(d)
SATISFIABLE
Now we need to make sure every not choosen argument is attacked by a at least one choosen element:
1 {choose(Y):attack(Y,X)} :- argument(X), not choose(X).
Reads: For every argument X, which is not choosen, the number of choosen arguments which attack it, is at least one.
Lets check it:
Answer: 1
choose(a) choose(d)
SATISFIABLE
Nice.
Since the contraints are normally formulated with an empty head, lets reformulate the last rule:
:- argument(X), not choose(X), {choose(Y):attack(Y,X)} 0.
Reads: There is no argument X, which is not choosen and has maximum 0 choosen arguments, which attack X. It gives the same output.
Complete code:
argument (a;b;c;d).
attack (a,b).
attack (b,c).
attack (d,c).
{choose(X):argument(X)}.
:- choose(X), attack(X,Y), choose(Y).
:- argument(X), not choose(X), {choose(Y):attack(Y,X)} 0.
#show choose/1.

regress with a constraint/condition

I want to execute a regression but I need a condition/constraint in the command but mine doesn't work. My dependent variable is COMP_STD and my independent is BGroup. BGroup has the following values: 1 2 3 4 5.
I want to run 5 different regressions with the variable BGroup, so that this variable equals 1, 2, and so on. This is the syntax that I tried:
regress COMP_STD if inrange (BGroup, 1)
but Stata says inrange not found so I guess it has to be another syntax which I can't find.
inrange() shouldn't be presented as inrange (whatever), i.e. with a following space. There is a hint that Stata can't understand you; otherwise why is it asking about inrange? The reason for that puzzlement is that --when separated from its arguments -- a bare inrange looks like the name of a variable (or scalar), and you have no such variable (or scalar).
You didn't get that far, but the inrange() call was illegal for another reason. It needs three arguments, e.g. if inrange(BGroup, 1, 1).
But if BGroup == 1 is much simpler.
Note that regress is irrelevant here as the problem is entirely about selecting observations. That said, statsby is a command possibly useful here for getting several regressions at once.

How to use OR condition in LibreOffice?

I am trying to use the formula below to set conditions in LibreOffice but I keep getting an error. What am I doing wrong with the statement below:
=IF(G2<=2,'negative',IF(OR(G2>2 & G2<=3,'neutral',IF(OR(G2>=4,'positive))))))
Thanks
It seems, that in your formula is missing the last ':
'positive))))))
should be 'positive'))))))
Also the
&
is string-concatenation in LibreOffice, so you need here the equivalent to OR() and that is AND().
But you can simplify your formula to
=IF(G2<=2,'negative',IF(AND(G2>2,G2<=3),'neutral','positive'))
The first test is if the number is lower than 2 (negative),
the second test is if the number is between 2 and 3 (neutral)
and then there is no further test needed as it is the only remainig possiblity.
For a different locale, a slightly shorter, and I'd say simpler, version that also avoids the need for OR/AND:
=IF(G2<=2,"negative",IF(G2<=3,"neutral","positive"))
Once <=2 first test is handled (either but outputting negative or by proceeding to the 'result if FALSE') there is no longer the possibility of 2 or less, so the AND is not necessary.
The above though does fill a gap left by OP between 3 and 4.

How to avoid a meta argument warning in SICStus SPIDER?

This is probably related to a comp.lang.prolog-discussion.
I'm getting several warnings like this using Eclipse with the SICStus SPIDER:
The plain meta argument (Y) is passed as a closure argument
(with 0 suppressed arguments) to the callee.
Here is a code sample:
% Prologs set_of is baroque %% RS-140614 130sec runtime vs. 28sec runtime
:- meta_predicate set_of(+,:,+) .
set_of(X,Y,Z):- %%
setof(X,Y^Y,Z),!; %% Trick to avoid alternatives
Z=[]. %% What is wrong with empty sets ?
How can I get rid of the SPIDER warnings?
I'm not really interested in simply suppressing the warnings.
I'm using the latest version of SPIDER IDE (0.0.51), and SICStus Prolog 4.2.3.
There are several issues in the code you show.
Bad meta argument
First, the built-in predicate setof/3 has the following properties:
?- predicate_property(setof(A,B,C),P).
P = (meta_predicate setof(?,0,?))
; P = built_in
; P = jittable.
which closely corresponds to the ISO declarations in ISO/IEC 13211-1:
8.10.3.2 Template and modes
setof(?term, +callable_term, ?list)
The second argument is a goal to be executed by call/1. No extra arguments are needed. This is what the 0 tells us.
On the other hand, your code you show contains a different meta predicate declaration:
:- meta_predicate(set_of(+,:,+)) .
Here, the second argument is a :. In SICStus, YAP, and SWI, the : means: This argument will be automatically qualified with the current module, such that the module information can be passed further on. Think of asserta(:). Here, the argument is not a goal but a clause.
So what you need to fix this, is to replace : by 0. And you might indicate this fact in the variable name used. That is, Goal_0 for call(Goal_0), Goal_1 for call(Goal_1, Arg1), Goal_2for call(Goal_2, Arg1, Arg2) etc.
Bad modes
The + in the first and third argument is inappropriate. The 3rd argument is commonly an uninstantiated variable to be unified with the resulting list.
Prolog's setof/3 baroque?
% Prologs set_of is baroque
The comment probably wants to say that setof/3 contains superfluous ornaments. In fact, setof/3 is much more versatile than mentioned set_of/3. Take this recent question or that. Often you first think about a very specific situation. Say, you want the list of actors of a particular movie. Then, later on you want to ask what movies there are. It is this generalization which works very smoothly with setof/3 whereas it is extremely complex if you do not have it.
Another very useful way to use setof/3 is when you want to eliminate redundant answers:
?- (X=2;X=1;X=2).
X = 2
; X = 1
; X = 2.
?- setof(t, (X=2;X=1;X=2), _).
X = 1
; X = 2.
Try to emulate that efficiently.
Runtime overheads
They are next to negligible. If you really believe that there are overheads, simply use setof/3 with a single goal. In this manner preprocessing is next to naught.

"How to apply Red to compile-time optimization of this Lisp code?"

This is a pattern of optimization in Lisp code that I want to achieve in Red:
(defmacro compute-at-compile (x)
`(+ ,(* pi 2) ,x))
(macroexpand '(compute-at-compile 1))
; => (+ 6.283185307179586 1)
How do I express this in Red? (I realize it may not be possible in today's implementation, I'm wondering about how one would express the code at the language level to get such an optimization. Would it require special markup in the source, or would it be automatic like Lisp?)
Trying to extend my answer to maybe cover another idea that may help you find what you are looking for.
Red/System
From my understanding, the Red/System #define directive can help with optimization (in reducing function calls). Here is a similar example in Red/System. Within Red, it would require using within #system or #system-globaldirective.
#define COMPUTE(x) (3.13159 * 2.0 + x)
b: COMPUTE(1.0)
print b
Processing the macro should result in:
b: (3.13159 * 2.0 + 1.0)
print b
and results
7.26318
Math between types isn't defined yet, so you'll run into issues multiplying/adding float! and integer! (hence the above use of float!)
Red/Rebol
You can also take a look at compose as a higher level way to optimize your code writing. I am unsure of the effect in terms of optimizing speed. What compose does is take a block and evaluate whatever is in parenthesis and not evaluate other elements in the block.
See the Rebol2 help definition for compose
>> help compose
USAGE:
COMPOSE value /deep /only
DESCRIPTION:
Evaluates a block of expressions, only evaluating parens, and returns a block.
COMPOSE is a native value.
ARGUMENTS:
value -- Block to compose (Type: any)
REFINEMENTS:
/deep -- Compose nested blocks
/only -- Inserts a block value as a block
This may be what you're looking for in terms of building expressions
red>> x: 1
== 1
red>> compose [3 + 2 + (x)]
== [3 + 2 + 1]
An example from the Rebol2 documentation:
>> probe compose [time: (now/time) date: (now/date)]
[time: 12:48:53 date: 5-Mar-2014]
== [time: 12:48:53 date: 5-Mar-2014]