NUL-byte between every other character in output - powershell

I'm using Ruby to read and then print a file to stdout, redirecting the output to a file in Windows PowerShell.
However, when I inspect the files, I get this for the input:
PS D:> head -n 1 .\inputfile
<text id="http://observer.guardian.co.uk/osm/story/0,,1009777,00.html"> <s> Hooligans NNS hooligan
, , , unbridled JJ unbridled passion NN passion
- : - and CC and no DT no executive JJ executiv
e boxes NNS box . SENT . </s>
... yet this for the output:
PS D:> head -n 1 .\outputfile
ÿ_< t e x t i d = " h t t p : / / o b s e r v e r . g u a r d i a n . c o . u k / o s m / s t o r y / 0 , , 1 0 0 9 7 7 7 , 0
0 . h t m l " > < s > H o o l i g a n s N N S h o o l i g a n , ,
, u n b r i d l e d J J u n b r i d l e d p a s s i o n N N p a s s i o n
- : - a n d C C a n d n o D T n o e x e c u t i v e J J
e x e c u t i v e b o x e s N N S b o x . S E N T . < / s >
How can this happen?
Edit: since my problem didn't have anything to do with Ruby, I've removed the Ruby-code, and included my usage of the Windows shell.

In PowerShell > is effectively the same as | Out-File and Out-File defaults to Unicode encoding. Try this instead of using >:
... | Out-File outputfile -encoding ASCII

Related

Read a csv file with comments and empty lines in q/kdb

I'm trying to read a csv file with comments and empty lines, from which I need to fetch lines which are not empty or commented.
File looks like this:
Test File for dry run:
#This is a comment
# This is a comment with, comma
# This,is,a,comment with exact number of commas as valid lines
h1,h2,h3,h4
a,b,c,d
e,f,g,h
i,j,k,l
m,n,o,p
Expected Output:
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p
Unsuccessful attempt:
q)("SSSS";enlist ",")0: ssr[;;]each read0 `:test.csv // tried various options with ssr but since '*' wildcard gives error with ssr so not sure of how to use regex here
This provides the required result:
q)("SSSS";enlist",")0:t where not""~/:t:5_read0`:test.csv
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p
To ignore any number of comments you could use:
q)("SSSS";enlist",")0:t where not any each(" ";"#")~\:/:first each t:read0`:test.csv
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p

MATLAB eigenvalue from scratch

I'm not so familiar with MATLAB, i've just recently started using it, and a need help fixing some problems i've encountred.
a = input('Skriv inn en verdi for a: ');
b = input('Skriv inn en verdi for b: ');
c = input('skriv inn en verdi for c: ');
d = input('Skriv inn en verdi for d: ');
A = [a b; c d]
%Tester om matrisen er rektangulær
[na, ma] = size(A);
if na ~= ma
disp('ERROR');
return
end
syms lambda
det =((A(1,1)-lambda)*(A(2,2)-lambda))-(A(1,2)*A(2,1))
%Currently under development
e1 = (t + sqrt(t^2 - 4*n*d))/2;
e2 = (t - sqrt(t^2 - 4*n*d))/2;
if A ~= 0
x1 = [a-e1; b];
end
when I enter 1 as the value for a b c and d i get this
Skriv inn en verdi for a: 1
Skriv inn en verdi for b: 1
skriv inn en verdi for c: 1
Skriv inn en verdi for d: 1
A =
1 1
1 1
det =
(lambda - 1)^2 - 1
I want it to be written out as lambda^2 + 2lambda +- (c) = 0 (i don't know how to set the whole equation as = 0
To rewrite (lambda - 1)^2 - 1 to lambda^2 + 2lambda +- (c) you can use expand. If you want to be able to work further with det in the form of the equation lambda^2 + 2lambda +- (c) = 0 then you need a double =:
det = expand(((A(1,1)-lambda)*(A(2,2)-lambda))-(A(1,2)*A(2,1))) == 0
When printed there will also be a double =. If you need det just for printing purpose then you can use:
det = expand(((A(1,1)-lambda)*(A(2,2)-lambda))-(A(1,2)*A(2,1)));
disp([char(det), ' = 0'])

How can I calculate all of the possible combinations of two 1x6 vectors in MATLAB?

I have two 1x6 vectors that I am eventually trying to just sum up, but I need to get all of the possible combinations of these vectors before doing so. The vectors will look like so:
V1=[a b c d e f];
V2=[A B C D E F];
What I need is to find all possible combinations of variables that will remain a 1x6 vector. I have been messing around for a while now and I think I have found a way by using various matrices but it seems terribly inefficient. An example of what I am looking for is as follows.
M=[a b c d e f;
A b c d e f;
A B c d e f;
A B C d e f;
A B C D e f;
A B C D E f;
A B C D E F;
. . .]
And so on and so forth until all combinations are found. Unfortunately I am not a MATLAB whiz hence the reason I'm reaching out. I'm sure there has to be a much simpler way than what I have been trying. I hope that my question was relatively clear. Any help is much appreciated! Thanks!
I used cellfun to create the indexes:
V1=['abcdef'];
V2=['ABCDEF'];
VV = [V1;V2];
l = length(V1);
pows = 0:l-1;
x = num2cell(2.^pows);
L = x{end};
rows = cellfun(#(x) reshape([ones(x,L/x);2*ones(x,L/x)],[2*L 1]),x,'Uniformoutput',0);
rows = cell2mat(rows);
cols = repmat(1:l,[2*L 1]);
idxs = sub2ind(size(VV),rows,cols);
M = VV(idxs);
and you get:
M =
abcdef
Abcdef
aBcdef
ABcdef
abCdef
AbCdef
aBCdef
ABCdef
abcDef
AbcDef
...

Maple: specify variable over which to maximize

This is a very simple question, but found surprisingly very little about it online...
I want to find the minimizer of a function in maple, I am not sure how to indicate which is the variable of interest? Let us take a very simple case, I want the symbolic minimizer of a quadratic expression in x, with parameters a, b and c.
Without specifying something, it does minimize over all variables, a, b, c and x.
f4 := a+b*x+c*x^2
minimize(f4, location)
I tried to specify the variable in the function, did not work either:
f5 :=(x) ->a+b*x+c*x^2
minimize(f5, location)
How should I do this? And, how would I do if I wanted over two variables, x and y?
fxy := a+b*x+c*x^2 + d*y^2 +e*y
f4 := a+b*x+c*x^2:
extrema(f4, {}, x);
/ 2\
|4 a c - b |
< ---------- >
| 4 c |
\ /
fxy := a+b*x+c*x^2 + d*y^2 +e*y:
extrema(fxy, {}, {x,y});
/ 2 2\
|4 a c d - b d - c e |
< --------------------- >
| 4 c d |
\ /
The nature of the extrema will depend upon the values of the parameters. For your first example above (quadratic in x) it will depend on the signum of c.
The command extrema accepts an optional fourth argument, such as an unassigned name (or an uneval-quoted name) to which is assigns the candidate solution points (as a side-effect of its calculation). Eg,
restart;
f4 := a+b*x+c*x^2:
extrema(f4, {}, x, 'cand');
2
4 a c - b
{----------}
4 c
cand;
b
{{x = - ---}}
2 c
fxy := a+b*x+c*x^2 + d*y^2 +e*y:
extrema(fxy, {}, {x,y}, 'cand');
2 2
4 a c d - b d - c e
{---------------------}
4 c d
cand;
b e
{{x = - ---, y = - ---}}
2 c 2 d
Alternatively, you may set up the partial derivatives and solve them manually. Note that for these two examples there is just a one result (for each) returned by solve.
restart:
f4 := a+b*x+c*x^2:
solve({diff(f4,x)},{x});
b
{x = - ---}
2 c
normal(eval(f4,%));
2
4 a c - b
----------
4 c
fxy := a+b*x+c*x^2 + d*y^2 +e*y:
solve({diff(fxy,x),diff(fxy,y)},{x,y});
b e
{x = - ---, y = - ---}
2 c 2 d
normal(eval(fxy,%));
2 2
4 a c d - b d - c e
---------------------
4 c d
The code for the extrema command can be viewed, by issuing the command showstat(extrema). You can see how it accounts for the case of solve returning multiple results.

Digit sum in Prolog

How can I define in Prolog the rule to calculate the digit sum of an integer?
Example:
digitsum[Integer, Result] :- ...
so that the query digitsum(123, X). results in X = 6 (=1+2+3). A solution running under SWI-Prolog is appreciated.
This problem naturally leads to an implementation using library(clpfd):
:- use_module(library(clpfd)).
digitsum(Z,S) :-
Z #= 0,
S #= 0.
digitsum(Z0, S0) :-
Z0 #> 0, S0 #> 0,
S0 #= Z0 mod 10 + S1,
Z1 #= Z0 // 10, % should be rather div
digitsum(Z1, S1).
?- digitsum(D,S).
D = S, S = 0
; D = S, S in 1..9
; D in 10..99, D/10#=_A, D mod 10#=_B,
_A in 1..9, _B+_A#=S, _B in 0..9, S in 1..18
; ... .
The first answer is about zero. The second says that a number 1..9 is the same as its sum.
Then it talks about the numbers 10..99 etc.