Related
I have this piece of code:
for i=1:10
v=[2i,i]
#show v
end
and I get this result:
v = [2, 1]
v = [4, 2]
v = [6, 3]
v = [8, 4]
v = [10, 5]
v = [12, 6]
v = [14, 7]
v = [16, 8]
v = [18, 9]
v = [20, 10]
Now what I want to do is to collect all these outputs of into one array of arrays, something like:
[[2,1],[4,2],[6,3]]
and I don't really know how to do it, I've tried several solutions that didn't work.
You can use array comprehensions for this:
julia> x = [[2i,i] for i in 1:10]
10-element Array{Array{Int64,1},1}:
[2, 1]
[4, 2]
[6, 3]
[8, 4]
[10, 5]
[12, 6]
[14, 7]
[16, 8]
[18, 9]
[20, 10]
or go with the manual route of constructing an empty initial array, and pushing the inner arrays into it one-by-one:
julia> y = []
0-element Array{Any,1}
julia> for i in 1:10
push!(y,[2i,i])
end
julia> y
10-element Array{Any,1}:
[2, 1]
[4, 2]
[6, 3]
[8, 4]
[10, 5]
[12, 6]
[14, 7]
[16, 8]
[18, 9]
[20, 10]
It works without initial value:
reduce(+, [2 3 4])
Tried multiple ways to provide initial value - nothing works
reduce(+, [2 3 4], 1)
reduce(+, 1, [2 3 4])
Also seems like reduce could be used only with 2 argument operator. What function should be used to reduce collection with custom functions that accept current value and accumulator? Something like code below?
reduce((accumulator, value) -> push!(accumulator, value^2), [1, 2, 3], [])
# => [1, 4, 9]
This example could be implemented as map(x -> x^2, [1, 2, 3]) but I would like to know how to implement it as reduce with accumulator.
julia version 1.1.1
The init argument to reduce is a keyword argument:
julia> reduce(+, [2 3 4], init = 1)
10
julia> reduce((accumulator, value) -> push!(accumulator, value^2), [1, 2, 3], init = [])
3-element Array{Any,1}:
1
4
9
The matrix A is 7 1
4 5
0-25 represent a-z in alphabetical order.
Find A inverse and hence decrypt the ciphertext fact .working modulo 26
Got this question in a test answer was supposed to be a word I got veho can anyone help?
The inverse of
A = [7 1]
[4 5]
computed in Z_26 (the integers modulo 26)
is
A^-1 = [1 5]
[20 17]
This is just the formula that, if
A = [a b]
[c d]
Then
A^-1 = 1/ * [d -b]
(ad - bc) [-c a]
To use this mod 26, you need to find the reciprocal of ad-bc = 31 = 5. This is just its multiplicative inverse, which is 21 (since 5*21 = 105 = 1 in Z_26).
Thus A^-1 = 21 * [5 -1] = 21 * [5 25] = [105 525] = [1 5]
[-4 7] [22 7] [462 147] [20 17]
To test,
[1 5] [7 1] = [27 26] = [1 0] (mod 26)
[20 17] [4 5] [208 105] [0 1]
It should be easy enough to use this inverse to decrypt -- just make sure to do all the arithmetic mod 26.
I inverted
[ 7 1 ]
[ 4 5 ]
and came up with
[ 5/31 -1/31 ]
[ -4/31 7/31 ]
I had a little help from http://matrix.reshish.com/inverCalculation.php.
Sorry, but I don't see how these mixed fractions could correspond to letters.
Is there any easy way to generate an array of permutations of length N, drawn from k values? Example:
N = 2
k = [[0 0], [0 1], [1 0], [1 1]]
Permutations = [
[[0 0], [0 0]],
[[0 0], [0 1]],
[[0 0], [1 0]],
[[0 0], [1 1]],
[[0 1], [0 0]],
[[0 1], [0 1]],
[[0 1], [1 0]],
[[0 1], [1 1]],
...
]
An important note here: If possible, I'd like the result to be arrays all the way down (the product function in the Iterators package generates tuples)
If it helps, the Haskell equivalent would be: replicateM 2 [[0, 0], [0, 1], [1, 0], [1, 1]]
Just in case there's a more idiomatic way to achieve what I'm trying to do, here's the function I'm writing:
function generate_states(length)
# "tuples" contains what I want, but it needs a lot of transformation to
# be usable later
tuples = [collect(t) for t in
product([product(0:1, 0:1) for _ in 1:length]...)]
states = collect(distinct(imap(x -> kron([[i...] for i in x]...), tuples)))
return states
end
Which works, and does what I want, but ideally I'd like it to look something like this:
function generate_states(length)
arrays = replicateM(3, Array[[0 0], [0 1], [1 0], [1 1]])
states = collect(distinct(imap(x -> kron(x...), arrays)))
return states
end
UPDATE / FIX
The question actually wants to generate all the sequences of length N of elements from k.
This could be achieved using:
using Iterators # install package with `Pkg.add("Iterators")`
N = 2
k = Array[[0 0], [0 1], [1 0], [1 1]]
res = [Array[e...] for e in product(fill(k,N)...)]
OLDER INTERPRETATION - permutations of objects
collect(combinations(['a','b','c','d'],2)) generates the right collection disregarding the elements being permuted.
The specific elements in your code [0 0] are row vectors (i.e. 1x2 matrices). This is more awkward than column vectors in Julia. The example with column vectors is:
julia> combinations(Array[[0,0],[0,1],[1,0],[1,1]],2) |> collect
6-element Array{Array{Array{Int64,1},1},1}:
[[0,0],[0,1]]
[[0,0],[1,0]]
[[0,0],[1,1]]
[[0,1],[1,0]]
[[0,1],[1,1]]
[[1,0],[1,1]]
Note the explicit typing of [] to prevent flattening of internal elements by vcat. With row vectors, as in the OP:
combinations(Array[[0 0],[0 1],[1 0],[1 1]],2) |> collect
(standard output is messy)
There's a simple solution with tuples as follows:
K = [(0,0), (0,1), (1,0), (1,1)]
vec( [(i,j) for i in K, j in K] )
If you really need arrays (why do you need this?) you can do
K2 = [ [a...] for a in K ]
[ a for a in vec( [(i,j) for i in K2, j in K2] ) ]
If you need arrays of arrays then that is also possible, but even more messy. So the question is, can you really not get use tuples of tuples?
I am very new to Clojure and just started learning it last week. I am trying to evaluate a nested vector of vectors in this form:
[[1 2] [3 1] [-1 0]] which represents polynomials so the vector represents:
x^2 + 3*x - 1 (with the first number of each set being the coefficient and the second being the exponent)
I have a function named eval-term() that takes in a number input and does the calculation of the ((number^exponent)*coefficient) for each term, so I want it to iterate through the nested vector [[1 2] [3 1] [-1 0]] and essentially evaluate [1 2] and then [3 1] and then [-1 0]
I am getting an error:
clojure.lang.Compiler$CompilerException: java.lang.IllegalArgumentException: Don't know how to create ISeq from: user$make_poly$fn__11617, compiling:(null:53:1)
Compiler.java:3463 clojure.lang.Compiler$InvokeExpr.eval
Compiler.java:408 clojure.lang.Compiler$DefExpr.eval
Compiler.java:6624 clojure.lang.Compiler.eval
Compiler.java:6608 clojure.lang.Compiler.eval
Compiler.java:6582 clojure.lang.Compiler.eval
core.clj:2852 clojure.core/eval
eval.clj:77 lighttable.nrepl.eval/->result
AFn.java:163 clojure.lang.AFn.applyToHelper
AFn.java:151 clojure.lang.AFn.applyTo
core.clj:619 clojure.core/apply
core.clj:2396 clojure.core/partial[fn]
RestFn.java:408 clojure.lang.RestFn.invoke
core.clj:2487 clojure.core/map[fn]
LazySeq.java:42 clojure.lang.LazySeq.sval
LazySeq.java:60 clojure.lang.LazySeq.seq
RT.java:484 clojure.lang.RT.seq
core.clj:133 clojure.core/seq
core.clj:2523 clojure.core/filter[fn]
LazySeq.java:42 clojure.lang.LazySeq.sval
LazySeq.java:67 clojure.lang.LazySeq.seq
Cons.java:39 clojure.lang.Cons.next
RT.java:598 clojure.lang.RT.next
core.clj:64 clojure.core/next
core.clj:2781 clojure.core/dorun
core.clj:2796 clojure.core/doall
eval.clj:126 lighttable.nrepl.eval/eval-clj
RestFn.java:442 clojure.lang.RestFn.invoke
sonar.clj:215 lighttable.nrepl.sonar/eval5570[fn]
AFn.java:159 clojure.lang.AFn.applyToHelper
AFn.java:151 clojure.lang.AFn.applyTo
core.clj:617 clojure.core/apply
core.clj:1788 clojure.core/with-bindings*
RestFn.java:425 clojure.lang.RestFn.invoke
sonar.clj:203 lighttable.nrepl.sonar/eval5570[fn]
MultiFn.java:227 clojure.lang.MultiFn.invoke
core.clj:98 lighttable.nrepl.core/queued[fn]
core.clj:2330 clojure.core/comp[fn]
interruptible_eval.clj:138 clojure.tools.nrepl.middleware.interruptible-eval/run-next[fn]
AFn.java:24 clojure.lang.AFn.run
ThreadPoolExecutor.java:1142 java.util.concurrent.ThreadPoolExecutor.runWorker
ThreadPoolExecutor.java:617 java.util.concurrent.ThreadPoolExecutor$Worker.run
Thread.java:745 java.lang.Thread.run
Caused by: java.lang.IllegalArgumentException: Don't know how to create ISeq from: user$make_poly$fn__11617
RT.java:505 clojure.lang.RT.seqFrom
RT.java:486 clojure.lang.RT.seq
core.clj:133 clojure.core/seq
protocols.clj:26 clojure.core.protocols/seq-reduce
protocols.clj:41 clojure.core.protocols/fn
protocols.clj:13 clojure.core.protocols/fn[fn]
core.clj:6175 clojure.core/reduce
(Unknown Source) user/make-poly
AFn.java:161 clojure.lang.AFn.applyToHelper
AFn.java:151 clojure.lang.AFn.applyTo
Compiler.java:3458 clojure.lang.Compiler$InvokeExpr.eval
I think my
(defn poly-maker [polyvector]
(reduce + #(map eval-term polyvector %))
)
is wrong because the "map" is not going through the sets in [[1 2] [3 1] [-1 0]] and evaluating them one at a time. I don't know how to resolve this, please help!
You want something like this:
(defn poly-maker [polyvector]
(fn [x] (reduce + (map #(eval-term % x) polyvector))))
(def example (poly-maker [[1 2] [3 1] [-1 0]]))
;TEST
(example 3)
Note that the result is 17, not 9.
Your representation is fallible: you could have two or more values for the same coefficient, for example [[1 2] [3 2]].
So use a map: power -> coefficient. For example, {2 1, 1 3, 0 -1} instead of [[1 2] [3 1] [-1 0]].