Display symbolic expression in octave. Matrix multiplication as an expression and not as a result - octave

I have hard time finding out how to display matrix multiplication as an expression, not as a result of an expression. The expression must be displayed in command line, not as a plot.
Lets say I have
syms m00 m01 m10 m11;
M = [m00 m01; m10 m11];
syms x0 x1;
X = [x0; x1];
I want to see the expression M * X as a symbolic expression. Something that will be displayed like:
| m00 m01 | * | x0 |
| m10 m11 | | x1 |
And will not be displayed as a result of M * X evaluation:
| m00*x0 + m01*x1 |
| m10*x0 + m11*x1 |
I have read documentation on octave symbolic package. Can not seem to find the mechanics there. My thoughts are wrapped around converting expressions to latex,
m = latex(M)
x = latex(X)
Concatenating the result as a latex string, and somehow print the latex string in octave command line. No luck as of now.

Related

Storing coefficients from a Regression in Stata

I am trying to store the coefficients from a simulated regression in a variable b1 and b2 in the code below, but I'm not quite sure how to go about this. I've tried using return scalar b1 = _b[x1] and return scalar b2 = _b[x2], from the rclass() function, but that didn't work. Then I tried using scalar b1 = e(x1) and scalar b2 = e(x2), from the eclass() function and also wasn't successful.
The goal is to use these stored coefficients to estimate some value (say rhat) and test the standard error of rhat.
Here's my code below:
program montecarlo2, eclass
clear
version 11
drop _all
set obs 20
gen x1 = rchi2(4) - 4
gen x2 = (runiform(1,2) + 3.5)^2
gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)
gen y = 1.3*x1 + 0.7*x2 + 0.5*u
* OLS Model
regress y x1 x2
scalar b1 = e(x1)
scalar b2 = e(x2)
end
I want to do something like,
rhat = b1 + b2, and then test the standard error of rhat.
Let's hack a bit at your program:
Version 1
program montecarlo2
clear
version 11
set obs 20
gen x1 = rchi2(4) - 4
gen x2 = (runiform(1,2) + 3.5)^2
gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)
gen y = 1.3*x1 + 0.7*x2 + 0.5*u
* OLS Model
regress y x1 x2
end
I cut drop _all as unnecessary given the clear. I cut the eclass. One reason for doing that is the regress will leave e-class results in its wake any way. Also, you can if you wish add
scalar b1 = _b[x1]
scalar b2 = _b[x2]
scalar r = b1 + b2
either within the program after the regress or immediately after the program runs.
Version 2
program montecarlo2, eclass
clear
version 11
set obs 20
gen x1 = rchi2(4) - 4
gen x2 = (runiform(1,2) + 3.5)^2
gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)
gen y = 1.3*x1 + 0.7*x2 + 0.5*u
* OLS Model
regress y x1 x2
* stuff to add
end
Again, I cut drop _all as unnecessary given the clear. Now the declaration eclass is double-edged. It gives the programmer scope for their program to save e-class results, but you have to say what they will be. That's the stuff to add indicated by a comment above.
Warning: I've tested none of this. I am not addressing the wider context. #Dimitriy V. Masterov's suggestion of lincom is likely to be a really good idea for whatever your problem is.

Haskell Integer Odd Digits Checker

I seem to be stuck on a question and have no idea how to approach it or what Im doing wrong with my current code.
I have to write a function called oddDigits which takes a single integer argument and returns a boolean result. It should return True if and only if the argument is a positive integer with an odd number of digits. If the argument is zero or negative, the function should stop with an error message.
Also, cant convert the argument into a string. Have to use recursion.
I have a feeling each digit could be stored in a list recursively and then the length of the list could determine the answer.
So far, I have this:
oddDigits :: Integer -> Bool
lst = []
oddDigits x
| (x < 0) || (x == 0) = error
| x `mod` 10 ++ lst ++ oddDigits(x `div` 10)
| length(lst) `mod` 2 /= 0 = True
| otherwise = False
Sorry if the code looks horrible. I am new to Haskell and still learning. What exactly am I doing wrong and how could I correct it?
First off, this seems a pretty weird thing to check. Perhaps what you're doing wrong is to ever consider this problem...
But if you persist you want to know the property of an integer having an odd number of digits... oh well. There's a lot that could be improved. For starters, (x < 0) || (x == 0) doesn't need the parentheses – < and == (infix 4) bind more tightly than ||. If you're not sure about this, you can always ask GHCi:
Prelude> :i ==
class Eq a where
(==) :: a -> a -> Bool
...
-- Defined in ‘GHC.Classes’
infix 4 ==
Prelude> :i ||
(||) :: Bool -> Bool -> Bool -- Defined in ‘GHC.Classes’
infixr 2 ||
But here you don't need || anyway because there's a dedicated operator for less-than-or-equal. Hence you can just write
oddDigits x
| x <= 0 = error "bla bla"
| ...
Then, you can “convert” the number to a string. Converting to string is generally a really frowned-upon thing to do because it throws all structure, typechecking etc. out of the window; however “number of digits” basically is a property of a string (the decimal expansion), rather than a number itself, so this is not entirely unsensible for this specific task. This would work:
oddDigits x
| x <= 0 = error "blearg"
| length (show x)`mod`2 /= 0 = True
| otherwise = False
however it's a bit redundancy department redundant. You're checking if something is True, then give True as the result... why not just put it in one clause:
oddDigits x
| x <= 0 = error "blearg"
| otherwise = length (show x)`mod`2 /= 0
That's perhaps in fact the best implementation.
For any proper, sensible task, I would not recommend going the string route. Recursion is better. Here's what it could look like:
oddDigits 1 = True
oddDigits x
| x <= 0 = error "blearg"
| otherwise = not . oddDigits $ x`div`10
There's nothing wrong with your general approach of converting to a list of digits, then finding the length of the list. Really where you went wrong is trying to cram everything into one function. As you found out first hand, it makes it very difficult to debug. Functional programming works best with very small functions.
If you separate out the responsibility of converting an integer to a list of digits, using a digs function like the one from this answer, the rest of your algorithm simplifies to:
oddDigits x | x <= 0 = error
oddDigits x = odd . length $ digs x
leftaroundabout's eventual answer is very nice, however it fails for numbers like 2,3 and 23. Here's a fix.
oddDigits x
| x <= 0 = error "blearg"
| x < 10 = True
| otherwise = not . oddDigits $ x`div`10
Its much more elegant than my initial answer, below. I'm including it to introduce a common functional paradigm, a worker/wrapper transformation of the problem. Here the wrapper gives the interface and passes off the work to another function. Notice that the negativity check only needs to be done once now.
oddDigits :: Integer -> Bool
oddDigits x
| x <= 0 = False
| otherwise = oddDigits' True x
oddDigits' :: Bool -> Integer -> Bool
oddDigits' t x
| x < 10 = t
| otherwise = oddDigits' (not t) $ x `div` 10
oddDigits' carries a piece of internal data with it, the initial Bool. My first first thought was to have that Bool be a digit accumulator, counting the number of digits. In that case, an "unwrapper" needs to be supplied, in this case the standard "odd" function:
oddDigits x
| x <= 0 = False
| otherwise = odd . oddDigits'' 1 $ x
where oddDigits'' :: Integer -> Integer -> Integer.

Backwards stepwise regression approach in Stata 13

. stepwise, pr(.05) : logit y1 (x1-x7)
begin with full model
p < 0.0500 for all terms in model
Logistic regression Number of obs = 28900
LR chi2(66) = 1182.91
Prob > chi2 = 0.0000
Log likelihood = -28120.170 Pseudo R2 = 0.0213
------------------------------------------------------------------------------
churn | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 | .0019635 .0007981 2.46 0.014 .0003992 .0035278
x2 | -.0002809 .0000496 -5.66 0.000 -.0003782 -.0001836
x3 | -.0031225 .0008888 -3.51 0.000 -.0048645 -.0013806
x4 | -.0011958 .0059387 -0.20 0.840 -.0128354 .0104439
x5 | .0007603 .0002804 2.71 0.007 .0002106 .0013099
x6 | .0070912 .0020636 3.44 0.001 .0030467 .0111357
x7 | -.0004919 .0000535 -9.19 0.660 -.0005968 -.0003871
_cons | .1497005 .0952738 1.57 0.116 -.0370327 .3364336
------------------------------------------------------------------------------
Note: 0 failures and 1 success completely determined.
As you can see, in the above logistic regression output, x4 and x7 both have p-values that are >0.05... however, Stata is telling me that p < 0.0500 for all terms in model, thereby rendering my stepwise approach useless.
Can anyone please advise what I may be doing wrong?
You insisted with your syntax that all the variables be kept together, so Stata has nowhere to go from where it started in this case. Hence there can be nothing stepwise with your syntax: it's either all in or all out.
See the help: a varlist in parentheses indicates that this group of variables is to be included or excluded together. All the predictors are so bound by what you typed.
After reading the help, all you may need to do is to omit the parentheses.
(Lack of a Stata tag for a month cut down mightily on the Stata users reading this.)

create ++ operator in VHDL

I would like to have a new C++ style operator for the STD_LOGIC_VECTOR type. So far I managed to create and use the following function:
FUNCTION PLUS_ONE ( a : STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR is
BEGIN
RETURN std_logic_vector( unsigned( a ) + 1);
END FUNCTION;
now if i create this:
FUNCTION "++" ( a : STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR is
BEGIN
RETURN std_logic_vector( unsigned( a ) + 1);
END FUNCTION;
ISE throws the following error:
"++" is not a predefined operator.
Now the question is, is it possible to create new operators in VHDL an I am missing something
You can only overload operators in VHDL, you cannot create new operator symbols. Quoting the LRM (section 4.5.2):
The declaration of a function whose designator is an operator symbol
is used to overload an operator. The sequence of characters of the
operator symbol shall be one of the operators in the operator classes
defined in 9.2.
And the corresponding section of the manual says:
condition_operator ::= ??
logical_operator ::= and | or | nand | nor | xor | xnor
relational_operator ::= = | /= | < | <= | > | >= | ?= | ?/= | ?< | ?<= | ?> | ?>=
shift_operator ::= sll | srl | sla | sra | rol | ror
adding_operator ::= + | – | &
sign ::= + | –
multiplying_operator ::= * | / | mod | rem
miscellaneous_operator ::= ** | abs | not
As much as I like brevity, I must admit that choosing shorthand operators over standard ways of writing expressions is "syntactic sugar", and has a potential to obfuscate the code. It is interesting to note that "trendier" languages like Python and Ruby don't have a ++ operator as well.
Could VHDL support the ++ operator? I'm currently working on a VHDL parser, and I risk saying that adding a postfix ++ operator would break quite a few rules of the language grammar, especially because unary operators expect to take an operand to the right of the symbol. Owing to this and to the fact that aren't many strong arguments in favor of such a change, I don't expect to see it anytime soon. All thigs considered, my personal choice has been to stick with value := value + 1 for standard data types.

Order-dependant Bit Fields

How would one go about storing positional information in bit fields (the order in which the fields are OR'd or otherwise)?
Background: It popped into my head last night while writing a part of my game engine. Let's say that we are trying to describe a colour, and as part of that we have the colours that are present in the descriptor (and their order). For example we have the following colour orders on most graphics cards today:
RGBA
BGRA
The following flags can be used to describe colours that are supported:
None = 0x0
A = 0x1
R = 0x2
G = 0x4
B = 0x8
However, by using those fields A | R | G | B is the same thing as B | G | R | A. How would you design the flags and/or operations that can be used to add positional dependence? Bonus marks for adding exclusivity (you can't have R and G in position 1, for example) and for utility (some clever way that it could be used, possibly in this case scenario).
You can shift the bit field before adding each flag, by the number of bits required for each unique flag. The following flags would be used:
None = 0x0
A = 0x1
R = 0x2
G = 0x4
B = 0x8
Shift = 0x4
Mask = 0xF (A | R | G | B)
On a little-endian system you would shift it left by Shift (<<) before each OR. The shift left on None can be eliminated because 0 << x = 0. Given the original example:
A1 = A
A1R2 = (A1 << Shift) | R
A1R2G3 = (A1R1 << Shift) | G
A1R2G3B4 = (A1R1G3 << Shift) | B
B1 = B
B1G2 = (B1 << Shift) | G
B1G2R3 = (B1G2 << Shift) | R
B1G2R3A4 = (B1G2R3 << Shift) | A
To extract the position of each you would repeatedly shift it right (little-endian) and AND it with Mask. Repeating this until the current value reaches None would give you the reverse order.
let cur = the bit field we want to check
loop until cur = None:
let val = cur AND Mask
emit the name of val
let cur = cur >> Shift
This does not offer exclusivity (you can easily do a AAGB) and it doesn't look like it has any utility.