Mupad cast boolean to integer - matlab

I give a simple example of what I want to do in Matlabs MuPad
S := matrix([[0,S_1,S_2]]);
sum(S[k]*(k < 2)* S[k] * (TRUE), k=1..3)
should be: "S_1^2 + S_2"
however I get: Error: The first argument must be of type 'Type::Arithmetical'. [sum]
I understand the error, I just don't know how to succeed.
Advice appreciated. I'm looking for some kind of indicator function.

The question:
Start with the inner term. To have a valid number 0 or 1, I used the following expression:
piecewise([A[k]>a*B[l],1],[Otherwise,0])
The rest is straight forward:
sum(sum(A[k]*B[l]*piecewise([A[k]>a*B[l],1],[Otherwise,0]), l=1..L), k=1..K)

S := matrix([[0,S_1,S_2]]);
sum(S[k]^(4-k), k=1..3)
I'm to really sure what you are trying to do.

Related

How to encode normalized(A,B) properly?

I am using clingo to solve a homework problem and stumbled upon something I can't explain:
normalized(0,0).
normalized(A,1) :-
A != 0.
normalized(10).
In my opinion, normalized should be 0 when the first parameter is 0 or 1 in every other case.
Running clingo on that, however, produces the following:
test.pl:2:1-3:12: error: unsafe variables in:
normalized(A,1):-[#inc_base];A!=0.
test.pl:2:12-13: note: 'A' is unsafe
Why is A unsafe here?
According to Programming with CLINGO
Some error messages say that the program
has “unsafe variables.” Such a message usually indicates that the head of one of
the rules includes a variable that does not occur in its body; stable models of such
programs may be infinite.
But in this example A is present in the body.
Will clingo produce an infinite set consisting of answers for all numbers here?
I tried adding number(_) around the first parameter and pattern matching on it to avoid this situation but with the same result:
normalized(number(0),0).
normalized(A,1) :-
A=number(B),
B != 0.
normalized(number(10)).
How would I write normalized properly?
With "variables occuring in the body" actually means in a positive literal in the body. I can recommend the official guide: https://github.com/potassco/guide/releases/
The second thing, ASP is not prolog. Your rules get grounded, i.e. each first order variable is replaced with its domain. In your case A has no domain.
What would be the expected outcome of your program ?
normalized(12351,1).
normalized(my_mom,1).
would all be valid replacements for A so you create an infinite program. This is why 'A' has to be bounded by a domain. For example:
dom(a). dom(b). dom(c). dom(100).
normalized(0,0).
normalized(A,1) :- dom(A).
would produce
normalize(0,0).
normalize(a,1).
normalize(b,1).
normalize(c,1).
normalize(100,1).
Also note that there is no such thing as number/1. ASP is a typefree language.
Also,
normalized(10).
is a different predicate with only one parameter, I do not know how this will fit in your program.
Maybe your are looking for something like this:
dom(1..100).
normalize(0,0).
normalize(X,1) :- dom(X).
foo(43).
bar(Y) :- normalize(X,Y), foo(X).

Tablix Expressions (Multiple Condition)

I'm currently experiencing
The Value expression for the textrun ‘Textbox137.Paragraphs[0].TextRuns[0]’ contains an error: [BC30198] ')' expected.
=(Variables!Seconds.Value <= 500,"PASS", "FAIL") OR (Variables!Seconds.Value < 0,"N/A","")
This is a results column. In event that seconds is negative number it will be N/A. which means anything less then 0.
any thoughts on my syntax.
Not really sure where to begin with this as the syntax is almost entirely wrong, unfortunately. From my best inference, I am guessing you want N/A if less than 0, PASS if less than or equal to 500, and FAIL for anything above that. I'm also not sure why you're using variables rather than populating your data with a query and using the Fields!... syntax, but that's another issue entirely. To fix your current issue, you've neglected to include the IIF function that you seem to be trying to use. I think the expression you'll want is the following.
=IIF(Variables!Seconds.Value < 0, "N/A", IIF(Variables!Seconds.Value <= 500, "PASS", "FAIL"))
This will first check the variable to see if it is less than 0, printing N/A if so. If false, it will evaluate the second IIF that will print PASS for less than 500 and FAIL for anything above 500.

converge / over monadic function leads to scope error

I cant see to put converge (/) inside the function:
i:0
arg:0
{x+:1;i+:1}/[{i~0};0]
i ' Leads to answer 1
Works where i comes out to 1. The following segment will return an error:
depp:{[arg] i:0; {x+:1;i+:1}/[{i~0};0]; :i}
depp[0] ' Cant recognize i
Why?
you will either have to pass i to the lambdainside the function depp or use global assignment for i i.e
depp:{[arg] i::0; {x+:1;i+:1}/[{i~0};0]; :i}

Where is my mistake and how can i correct it?

I'm using this code for parameter estimation. it gives me an error in line given below. how can i correct this. thanks in advance.
while dcnorm>1E-6 & iter<10
f=a*(b.^(c.^t))-y;
Ji1=b.^(c.^t);
Ji2=a*(b^(c.^(t-1)))*(c.^t); %ERROR LINE
Ji3=a*(b^(c.^t))*ln(b)*t*(c.^(t-1));
J=[Ji1 Ji2 Ji3];
dc=-J\f; tahmin=tahmin+dc;
dcnorm=norm(dc); iter=iter+1;
a=tahmin(1); b=tahmin(2); c=tahmin(3);
D=[iter a b c norm(f) norm(dc)]
end
try (explicitly use element-wise operations throughout the expression):
Ji2=a.*(b.^(c.^(t-1))).*(c.^t);
My guess you'll need to modify the next line as well.

Handle divisor 0 error when some divisors are NULL

I am getting dividing by 0 errors. I noticed that sometimes the field I'm dividing by is NULL.
(SUM(RentMonths * SQFT) / SUM(SQFT))
What's the proper way to handle this situation when SQFT can be null at times? I know it's probably bad data but that's besides the point; I can't fix that right now.
Dividing by NULL is perfectly valid - the result is simply NULL. The problem is (as the error message states) when you try to divide by zero.
You can use NULLIF to solve this problem:
(SUM(RentMonths * SQFT) / NULLIF(SUM(SQFT), 0))
The result will be NULL if the divisor is 0 or if either operand is NULL.
As pointed out in the comments, a CASE statement could also be used:
CASE WHEN SUM(SQFT) <> 0 THEN (SUM(RentMonths * SQFT) / SUM(SQFT)) END
The advantage is that this will work in almost any database, but a disadvantage is that it repeats the expression to calculate the divisor.
An IF statement seems pretty appropriate here, no?
Don't know if this is correct TSQL syntax but:
IF #SQFT IS NOT NULL (SUM(RentMonths * SQFT) / SUM(SQFT))