Managing signed numerical macro arguments in Latex - macros

I have signed numbers with the unwanted effects :
x+-5
x--5
(I don't know in advance whether the constant \const is positive or negative).
I created these macros to cope with the problem :
\def\plus#1{\ifthenelse{#1<0}{#1}{+#1}}
\def\cutMinus-#1{#1} % to remove the minus in front of a negative number
\def\minus#1{\ifthenelse{#1<0}{+\cutMinus#1}{-#1}}
Then with
x\plus{\const}
I get the expression correctly :
x-5
Goal : I wish I could use \plus and \minus as a replacement of the standard + ans - as follows (if possible without the curly braces which make things longer to enter and less legible) :
x\plus\const
x\minus\const
Here are my tests : \plus, \cutMinus and \minus have different behaviors depending of the way the argument is given.
\def\const{-123456}\\
%
*1**x\plus{-12345} ****\\
%*2**x\plus -12345 ****\\ % compilation error : ! Missing number, treated as zero.
%*3**x\plus-12345 ****\\ % compilation error : ! Missing number, treated as zero.
*4**x\plus{\const} ****\\
*5**x\plus\const ****\\
%*6**\plus-\const ***\\ % compilation error : ! Missing number, treated as zero.
%
%*11**\cutMinus{-12345} ***\\ % compilation error : ! Use of \cutMinus doesn't match its definition. ! Too many }'s.
*12**\cutMinus -12345 ***\\
*13**\cutMinus-12345 ***\\
%*14**\cutMinus{\const} ***\\ % compilation error : ! Use of \cutMinus doesn't match its definition. ! Too many }'s.
%*15**\cutMinus\const ***\\ % compilation error : ! Use of \cutMinus doesn't match its definition.
*16**\cutMinus-\const ***\\
%
*21**x\minus{-12345} ****\\
%*22**x\minus -12345 ****\\ % compilation error : ! Missing number, treated as zero.
%*23**x\minus-12345 ****\\ % compilation error : ! Missing number, treated as zero.
%*24**x\minus{\const} ****\\ % compilation error : ! Use of \cutMinus doesn't match its definition.
%*25**x\minus\const ****\\ % compilation error : ! Use of \cutMinus doesn't match its definition.
%*26**\minus-\const ***\\ % compilation error : ! Missing number, treated as zero.
%
I don't understand all these errors. It seems that \const and -12345 are not equivalent. A question of internal coding of numbers ?
How shall I amend my macros to have them functional ?
More precisely :
* \minus don't work, ever, with a macro \const
* \cutMinus, the way I use to get rid of the minus sign in front of a negative number, doesn't work either with a macros \const. May be there is another way to get rid of the "-" sign ?
* I wish I can use \minus like this : x\minus\const gives : x+12345 (with \const equals -13245)

Related

sympref --- 'MatrixWithSquareBrackets' not work

I used sympref('MatrixWithSquareBrackets',true).But raise an error:
Expected input to match one of these values:
'FourierParameters', 'HeavisideAtOrigin', 'AbbreviateOutput', 'TypesetOutput'
The input, 'MatrixWithSquareBrackets', did not match any of the valid values.
Error in Untitled3 (line 7)
sympref('MatrixWithSquareBrackets',true);
I want to use symbolic matrix square brackets in my answer.How can I do this?

Loop through modelica array fails

I am using openmodelica and I am trying to loop through an array in order to find the maximum value. I was able to reduce my code to a very simple test case that still gives the error. Is this something that I am doing wrong, or is this a bug in openmodelica? Here is a very simple case that does give the error:
package TestLoop
model ItemA
Real p;
end ItemA;
model ItemB
ItemA a[n];
parameter Integer n = 5;
Real p;
equation
for i in 1:n loop
a[i].p = time;
end for;
algorithm
for i in 1:n loop
p := a[i].p;
end for;
end ItemB;
end TestLoop;
The problem is in my algorithm section. Here is the error that I am getting:
TestLoop.ItemB.c:155:13: warning: implicit declaration of function '$Pa$lB' is invalid in C99 [-Wimplicit-function-declaration]
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
TestLoop.ItemB.c:155:20: error: unexpected type name 'modelica_integer': expected expression
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
1 warning and 1 error generated.
Any suggestions for why this might be, or how I can work around it? If I replace the assignment with a fixed value, p:=a[1].p;, the code does run (although that is not useful to me). What I ultimately want to do in the algorithm section is find the largest value of a[n].p, where I do have an equation section that does useful calculations into the array of items.
Yes, the code generation is an error of OpenModelica (it does not like unknown array indexes). Your problem is very easy to solve in a single line though (one of the following):
p = max(r for r in a.p);
p = max(a.p);

Invalid Input: Second argument is missing

I'm trying to use Newton's method to approximate where the gradient of a function = 0 But I'm guessing error messages when I try to put in guesses for an iteration of Newton's, and I don't know what the problem is.
(For reference, Profit = 144TVa - 0.07TVb TVb - 0.01 TVa^2 + 174TVb -0.01TVb^2 - 4E5)
with(LinearAlgebra):
with(VectorCalculus):
DProfit := Gradient(Profit, [TVa, TVb])
F := unapply(DProfit, TVa, TVb)
J := Jacobian(DProfit)
Guess := V->evalf(V-Multiply(Jinv,F(V))):
But when I try to evaluate Guess at any points, it gives me an error:
Guess(3000,3000)
Error, (in Guess) invalid input: F uses a 2nd argument, TVb, which is missing
Guess(<3000,3000>
Error, (in Guess) invalid input: F uses a 2nd argument, TVb, which is missing
even though F(3000,3000) returns [-66,-36]
Thanks for the help.
Figured out how to fix it!
Instead of:
Guess := V->evalf(V-Multiply(Jinv,F(V))):
I did:
Guess := V->evalf(V-Multiply(Jinv,F(V[1],V[2]))):

error in using M block (in Xilinx system generator)?

I wrote this code inside the M Blcok (one of Xilinx blockstes in Simulink):
function z= discorr(x,y)
t=zeros(12288,1);
i=zeros(12288,1);
k=zeros(12288,1);
i(4096:8191,1)=x(1:4096,2); %output of the image filter
t(4096:8191,1)=y(1:4096,2); %output of the tamplate filter
i=i';
z=A(1:4096,1);
for n=1:8191
k=zeros(12288,1);
k(n:n+4095,1)=t(4096:8191,1);
z(n,2)=i*k;
end
end
Its telling me:
Error("discreatcorr.m"): Syntax error: Lexical error at line 15, column 0. Encountered: after : "\';\r\nz=A(1:4096,1);\r\nfor n=1:8191\r\n k=zeros(12288,1);\r\n k(n:n+4095,1)=t(4096:8191,1);\r\n z(n,2)=i*k;\r\n end\r\nend\r\n"
Error("discreatcorr.m"): Syntax error: Lexical error at line 15, column 0. Encountered: after : "\';\r\nz=A(1:4096,1);\r\nfor n=1:8191\r\n k=zeros(12288,1);\r\n k(n:n+4095,1)=t(4096:8191,1);\r\n z(n,2)=i*k;\r\n end\r\nend\r\n"
Error occurred during "Block Configuration".
althogh there is nothing in line 15 in the code
it is giving an error at the end of the code
Any Ideas??
The problem is that your system misinterprets the ' symbol as string symbol. Replacing the line i=i'; by i=transp(i); should solve the problem.

Using elementwise operation and element indexing together in Matlab

I want to write an anonymous function taking a vector theta as its input and compute the sum of the fourth-squared of the first half elements of theta:
L=#(theta) sum(theta.^4(1:length(theta)/2))
But Matlab reports an error
??? Error: File: c2n9.m Line: 3 Column: 27
Unbalanced or unexpected parenthesis or bracket.
I identified the error same as the following simpler example
>> a=ones(1,4)
a =
1 1 1 1
>> a.^4(1:2)
??? a.^4(1:2)
|
Error: Unbalanced or unexpected parenthesis or bracket.
>> (a.^4)(1:2)
??? (a.^4)(1:2)
|
Error: Unbalanced or unexpected parenthesis or bracket.
I wonder how I can make the simple example as well as the anonymous function work?
Thanks!
You could instead do
a(1:2).^4
you should do the indexing before the per-element raising to the power
instead of:
L=#(theta) sum(theta.^4(1:length(theta)/2))
try
L=#(theta) sum(theta(1:round(length(theta)/2)).^4)
note that I also added a round to take care of the case where the length of theta is odd
Err, aren't you missing a multiplication sign at the place that the first error message points to ? Or something else ?