(wx)Maxima: return expressions using `sec` `csc` and `cot` instead of reciprocals? - output

Currently, the output for inputs involving sec or the other reciprocal trig functions seem to return expressions in terms of reciprocals, e.g.:
expand(integrate(6*sec(2*y)^7*tan(2*y)^3, y));
> 1/(3*cos(2*y)^9)\-3/(7*cos(2*y)^7)
Is there any way to have this returned in terms of sec(2*y) instead? I checked the documentation for display and the documentation for trigonometric functions but didn't see anything that seemed to address this.

Looks like trigreduce handles it (thanks to Stavros Macrakis for this info).
(%i2) 1/(3*cos(2*y)^9) - 3/(7*cos(2*y)^7);
1 3
(%o2) ----------- - -----------
9 7
3 cos (2 y) 7 cos (2 y)
(%i3) trigreduce(%);
9 7
sec (2 y) 3 sec (2 y)
(%o3) --------- - -----------
3 7

Related

How do I call a function passing in a matrix in APL?

How do i call a function that i just wrote in APL, passing in a matrix? I have already tried many things but I can't seem to find the solution. I would greatly appreciate any help.
I don't think this is a duplicate but please report if you find one :)
Thanks
Unless you declare otherwise, an APL function will accept any array as argument. For example, let's define a function which multiplies its argument by 2 and then adds 1:
∇ result←t2p1 argument
result←1+2×argument
∇
Now we define a 3-by-4 matrix of some numbers, and ask for its value:
matrix←3 4⍴3 1 4 1 5
matrix
3 1 4 1
5 3 1 4
1 5 3 1
Finally, we apply the function to the matrix:
t2p1 matrix
7 3 9 3
11 7 3 9
3 11 7 3

How to prevent Octave from summing row and column vectors?

In Octave, in general, '+' will only work when the two operands have the same dimension.
There seems to be an exception to this rule: if you '+' a row vector (1 x n) and a column vector (n x 1), Octave will produce a (reasonable) Matrix of dimensions (n x n):
>> a = [1, 2, 3, 4, 5]
a =
1 2 3 4 5
>> b = [1; 2; 3; 4; 5]
b =
1
2
3
4
5
>> a+b
ans =
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
Can this be prevented, or at least be made to generate a warning? It seems potentially unsafe; I was recently bitten by a bug that was being masked by this behaviour.
Thanks!
No, this cannot be prevented. You need to check the input of your functions. The exception you mention is not an exception, it's the language syntax.
Element-wise operations require that dimensions have dimensions of equal lengths or length of one. The feature you are trying to prevent is also the feature that makes this work:
octave:1> a = 1:4
a =
1 2 3 4
octave:2> a+1
ans =
2 3 4 5
octave:3> a == 2
ans =
0 1 0 0
In the above examples, the value in the dimension with length 1 (1x1) is broadcasted or expanded. This feature is named Broadcasting in Octave and Python, and Implicit Expansion in Matlab. There's a bunch of operators and functions, such as == and max which also broadcast.
For a while, in Octave 3.6 and 3.8, it was possible to disable this by turning the Octave:broadcast into an error. However, because the way errors are handled in the language, that effectively made all Octave functions that used broadcasting to error.

KDB: apply dyadic function across two lists

Consider a function F[x;y] that generates a table. I also have two lists; xList:[x1;x2;x3] and yList:[y1;y2;y3]. What is the best way to do a simple comma join of F[x1;y1],F[x1;y2],F[x1;y3],F[x2;y1],..., thereby producing one large table?
You have asked for the cross product of your argument lists, so the correct answer is
raze F ./: xList cross yList
Depending on what you are doing, you might want to look into having your function operate on the entire list of x and the entire list of y and return a table, rather than on each pair and then return a list of tables which has to get razed. The performance impact can be considerable, for example see below
q)g:{x?y} //your core operation
q)//this takes each pair of x,y, performs an operation and returns a table for each
q)//which must then be flattened with raze
q)fm:{flip `x`y`res!(x;y; enlist g[x;y])}
q)//this takes all x, y at once and returns one table
q)f:{flip `x`y`res!(x;y;g'[x;y])}
q)//let's set a seed to compare answers
q)\S 1
q)\ts do[10000;rm:raze fm'[x;y]]
76 2400j
q)\S 1
q)\ts do[10000;r:f[x;y]]
22 2176j
q)rm~r
1b
Setup our example
q)f:{([] total:enlist x+y; x:enlist x; y:enlist y)}
q)x:1 2 3
q)y:4 5 6
Demonstrate F[x1;y1]
q)f[1;4]
total x y
---------
5 1 4
q)f[2;5]
total x y
---------
7 2 5
Use the multi-valent apply operator together with each' to apply to each pair of arguments.
q)raze .'[f;flip (x;y)]
total x y
---------
5 1 4
7 2 5
9 3 6
Another way to achieve it using each-both :
x: 1 2 3
y: 4 5 6
f:{x+y}
f2:{ a:flip x cross y ; f'[a 0;a 1] }
f2[x;y]
5j, 6j, 7j, 6j, 7j, 8j, 7j, 8j, 9j

GNU Octave: How to find n maximum elements of a vector

I have vector in Octave like this:
[ 4 5 1 2 3 6 ]
Is there any function that returns n maximum elements of that vector, in this case, the three biggest are 6, 5, and 4?
[6 5 4]
The Octave max function only returns one maximum element. I want n maximum elements.
In GNU Octave, get the biggest n elements of a vector:
octave:2> X = [3 8 2 9 4]
octave:2> sort(X)
ans =
2 3 4 8 9
octave:8> sort(X)(end-2:end)
ans =
4 8 9
Description
What sort(X)(end-2:end) means is "sort the vector X, and give me the elements from 2 minus the end to the end, also known as the last 3 elements".
You can use the sort function for this.

base 13 - The Hitchhiker's Guide to the Galaxy [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
I know base 13 is not practical. However, i was checking out The Hitchhiker's Guide to the Galaxy again because todays date is 10/10/10 = 42 in binary. (you know, the answer to the ultimate question of life, the universe, and everything) It takes Deep Thought 7½ million years to compute and check the answer, which turns out to be 42. Unfortunately, The Ultimate Question itself is unknown. Anyways, he said ""Six by nine. Forty two." "That's it. That's all there is.""I always thought something was fundamentally wrong with the universe"
My question is how is 6 x 9 in base 13 = 42?
I know how to convert base 10, 2,16,8 but going from base 10 to base 13 is a mystery to me.
I understand that in base 13 that 6 probably =6 and 9 probably =9 S0, it is just a matter of multiplying in base 13?
Can someone work it out?
I have found this but doesnt help much
in base 13, 6 13 × 9 13 is actually 4213
(as 4 × 13 + 2 = 54, i.e. 54 in decimal is
equal to 42 expressed in base 13).
This is a method that can convert a base-10 number to base-13:
Start with a number 9x6=54, we want to find the equivalent of 54 in base 13.
54 / 13 = 4 remainder 2
4 / 13 = 0 remainder 4
and we concatenate the remainders, from bottom-up, 42.
A more general algorithm, start with a decimal number N, we want to find the equivalent of N in base B.
N / B = a1 remainder r1
a1 / B = a2 remainder r2
....
an / B = 0 remainder rn
and concatenate the digits, bottom-up: rn . ... . r2 . r1
an iterative implementation in Python:
digits = '0123456789abcdefghijklmnopqrstuvwxyz'
def rebase(n, base=2):
''' Convert a positive integer to number string with base `base` '''
s = []
while n != 0:
n, rem = divmod(n, base)
s.append(digits[rem])
return ''.join(reversed(s))
a recursive implementation in Python:
digits = '0123456789abcdefghijklmnopqrstuvwxyz'
def rebase(n, base=2):
''' Convert a positive integer to number string with base `base` '''
return rebase(n // base, base) + digits[n % base] if n != 0 else ''
even more generally, if you have a string representing a number in base N and you want to convert it to a string representing the number in base M:
digits = '0123456789abcdefghijklmnopqrstuvwxyz'
def rebase(orig, orig_base=10, target_base=2):
''' Convert a positive integer to number string with base `base` '''
num = 0
for i, n in enumerate(reversed(orig)):
num += digits.find(n) * (orig_base**i)
target = []
while num != 0:
num, rem = divmod(num, target_base)
target.append(digits[rem])
return ''.join(reversed(target))
It's not a matter of multplying a number in a different base, but about expressing the product in that base
Lets start with a really easy base, unary, which is expressed in only ones (not even zeroes)
6x9 in unary is 111111 x 111111111. we can perform that calculation by replacing all of the ones in one term with the ones in the other term. copy and paste the nine ones six times
111111111111111111111111111111111111111111111111111111
When we want to express this number in more convenient bases, we group the ones by the radix. If there are enough groups to group the groups, we group them. we then replace the counts of groups with digits. We'll do that in decimal
111111111111111111111111111111111111111111111111111111
^ ^ ^ ^ ^
Each arrow is a group of 10, and there are 4 ones left over, so in the tens place, we put a 5 and in the ones place a 4, 54.
lets do the same for a smaller base so we can get a good idea how to generalize the groups
of groups:
1 111111111111111111111111111111111111111111111111111111
2 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
4 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8 ^ ^ ^ ^ ^ ^
16 ^ ^ ^
32 ^
We could make groups five times. starting in the ones place, there we no left over ones after we grouped by two, so the first digit is 0. when we grouped by 4, there was still a group of 2 left over, so the next digit is a 1. when we grouped by 8, there was still a group of 4 left over, another 1 is the next digit. when we grouped by 16, there was one remaining group of 8. when grouping by 32, there's a group of 16 left. we can't make a group of anything as large as 64, so all of the digits for places above 32 are 0. so the binary representation would be
110110
finally, base 13. this is just as easy as the base 10
111111111111111111111111111111111111111111111111111111
^ ^ ^ ^
there are 4 groups of 13. there are two digits left over after we make those 4 groups. thus the product of 6 x 9, when represented in base 13 is '42'
The Answer 42 has nothing to do with base 13, it was just a computational error.
The answer to this is very simple. It
was a joke. It had to be a number, an
ordinary, smallish number, and I chose
that one. Binary representations, base
thirteen, Tibetan monks are all
complete nonsense. I sat at my desk,
stared into the garden and thought '42
will do'. I typed it out. End of
story.
http://en.wikipedia.org/wiki/Answer_to_the_Ultimate_Question_of_Life,_the_Universe,_and_Everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe_and_Everything_.2842.29