I've got a situation where I'm building a struct from NTuples, and it would be convenient if N could equal 1. My experiments in the Julia REPL have not been encouraging. Is there a way of doing this?
I am not sure what problem you have exactly, but here is a way to create 1-element tuple and check if this is a NTuple{1}:
julia> (1,) isa NTuple{1}
true
Related
I'm new to Crystal, and am working through the documentation as I build out a test project to help me reinforce some of the principles inside Crystal.
I need to be able to iterate x number of times in a loop. The x is arbitrary, not tied to an arrays length, but is a set value at the time of the programs execution.
What's the Crystal way to do this?
I know that I can setup a while and get the behavior I'm looking for, but is there a specific way this is meant to be implemented in Crystal?
For example, in Ruby, you could say x.times { }or in Python you could say for num in range(x).
There is an x.times {} in Crystal too.
I realized that there is an easy way to init an integer array by using range(), or even just by saying e.g. [1..10].
Is there an easy way to init a [Bool] as well? (given that I know the size of the array beforehand). -- By easy I mean without defining a function just to do the init...
The answer depends on what you want to initialise it with, if it is just a list of all of the same value then you could just use replicate
replicate 5 True -- [True,True,True,True,True]
As well as this solution proposed by Simon Gibbons, for a fixed size array where all elements are identical
replicate 5 True -- [True,True,True,True,True]
you can also make use of list comprehensions to define more complex lists, for example to get an alternating False/True list you could do
[even x | x <- [1..6]] -- [False,True,False,True,False,True]
I want to find all the real numbers that satisfy a particular equation. I have no trouble finding these values in Mathematica with
Solve[n*9^5 == 10^n-1, n]
which gives both 0 and 5.51257; but when I use SymPy's (0.7.3; Python 2.7.5) solve
n = sympy.symbols('n')
sympy.solve(n*9**5 - 10**n-1, n)
I seem to only get something that looks like 0, and not the second value, which is what I'm really seeking.
How can I get SymPy to produce the non-trivial solution I'm looking for? Is there a different function or package I should be using instead?
solve only gives symbolic solutions, so if it cannot find a closed form for a solution, it will not return it. If you only care about numeric solutions, you want in SymPy is nsolve, or you can use a more numerical oriented Python library. For example
sympy.nsolve(n*9**5 - 10**n-1, n, 5)
will give you the solution you are looking for.
The problem with using solve is that there are infinitely many solutions, each corresponding to a branch of the LambertW function. See WolframAlpha for the full solution set. Unfortunately, only the principal branch of LambertW is implemented in SymPy.
Until this is fixed, another way to fix the issue would be to manually evaluate the LambertW returned by solve on another branch, using mpmath.lambertw. The easiest way to do this is with lambdify:
s = sympy.solve(n*9**5 - 10**n-1, n)
import sympy.mpmath
# Replace -1 with any integer. -1 gives the other real solution, the one you want
lambdify([], s, [{'LambertW': lambda x: sympy.mpmath.lambertw(x, -1)}, "mpmath"])()
This gives [mpf('5.5125649309411875')].
The dictionary tells lambdify to evaluate the LambertW function using the -1 branch, using mpmath. The "mpmath" tells it to use mpmath for any other functions that may be in the solution.
I have a several questions connected with fold_left/right.
How to accumulate two or more values? If using a tuple is good solution ?
How to abort work of fold ? For example we must find firstly occurrence of any number, and return position. I mean a break (C++).
zurgl's comment is a great answer (maybe move it down to the answer region).
You can use an exception to terminate a fold early. It's good to try to structure your code so you don't have to do this (in my opinion). Code without exceptions is easier to understand, more composable, parallelizable, etc.
we must find firstly occurrence of any number then you must traverse all your list [1;1;1;1;5], no need to abort here. To deal with partial recursion there are other function like dropwhile, takewhile ... Or you can define the one you need. See folding as projection between type, you have a source type and a target type with a seed value (the accumulator), then folding is a procedure which realize this transformation. Yes using tuple is good solution, IMO.
I have two lists, let's say for example :
let x = [1;2];;
let y = [true;false];;
I want to essentially have a (int*boolean) list list with
[[(1,true);(2;false)];[(1,true);(2,true);];[(1,false);(2,true);];[(1,false);(2,false);]]
Anyone have any idea how to do this?
The question is not specified very well as to what exactly is the operation. What about [(1,true);(1,false)]? It seems that the operation is not symmetric -- the things in x always appear in that order, while the things in y can repeat and appear in any order. Maybe it would help if you break it down into two tasks:
Every permutation with repetition of the second list
Zip every result with the first list
I think nobody is answering because this kinda looks like a homework problem. I would start by considering just the second elements of all the pairs in your desired result. I see a pretty regular structure: TF, TT, FT, FF. It would be a little more regular in the order (say) FF, FT, TF, TT. Anyway once you can make a list of lists that looks like this, it's pretty easy to pair up lists with other lists using functions from the List module.
(You might get more answers if you showed some things you tried and explained why they didn't work.)
Regards,
You can consult List module's document
Otherwise, just type
$ cd `ocamlc -where`
$ less list.mli
You may find functions for what you want to do.