Solve under given group - wolfram-mathematica
I've created the group (its GF4 algebra) that has 4 elements:
OrderMat = {0, 1, lambda, lambda + 1}
And definition of operation (.) with this Matrix realized by function:
| 0 | 1 | lambda | lambda+1 |
______________________________________________
0|| 0 | 0 | 0 | 0 |
1|| 0 | 1 | lambda | lambda+1 |
lambda|| 0 | lambda | lambda+1 | 1 |
lambda+1|| 0 |lambda+1| 1 | lambda |
OPMatrix = {{0, 0, 0, 0},
{0, 1, lambda, lambda + 1},
{0, lambda, lambda + 1, 1},
{0, lambda + 1, 1, lambda}}
GF4Mult[x_, y_] := OPMatrix[[Position[OrderMat, x][[1]][[1]]]][[Position[OrderMat, y][[1]][[1]]]]
Now I would like to Solve equations in this group.
for example:
Solve[x.lambda == 1,x] ... x=>lambda+1
or like this:
Solve[GF4Mult[x,lambda]== 1,x] ... x=>lambda+1
Is this possible ? do i have to use some other structure to define group ?
I don't know if this is a good way of doing this, but it seems closer to what you are asking for than the last couple of things I tried. And it uses the notation that you chose, except for Mathematica reordering lambda+1 to be 1+lambda on output.
First let's define your multiplication operator
times={{0,0,0},{0,1,0},{0,lambda,0},{0,lambda+1,0},
{1,0,0},{1,1,1},{1,lambda,lambda},{1,lambda+1,lambda+1},
{lambda,0,0},{lambda,1,lambda},{lambda,lambda,lambda+1},{lambda,lambda+1,1},
{lambda+1,0,0},{lambda+1,1,lambda+1},{lambda+1,lambda,1},{lambda+1,lambda+1,lambda}};
That is exactly what you had except for my flattening that into a vector.
Now lets show a method somewhat similar to Solve that might work for you.
Suppose as a first example you wonder is there a lambda+1*something=lambda+1
Cases[times,{lambda+1,x_,lambda+1}]
and that shows you there is only one value which satisfies that, the identity.
{{1+lambda,1,1+lambda}}
Another example
Cases[times,{lambda+1,x_,lambda}]
gives you
{{1+lambda,1+lambda,lambda}}
Another example, is there lambda+1*anythingBUTlambda+1=lambda
Cases[times,{lambda+1,Except[lambda+1],lambda}]
gives you
{}
which shows there is no such value.
Another example
Cases[times,{lambda+1,x_,Except[x_]}]
gives you
{{1+lambda,1,1+lambda},{1+lambda,lambda,1},{1+lambda,1+lambda,lambda}}
That has a lot of flexibility because you can have unknowns in any position. But because of that flexibility it doesn't just return a single value to you. Perhaps you can use this for what you are thinking of or perhaps you can think of ways to adapt this to what you are trying to do.
If you want to extract one value of a result then you can do things like this:
Cases[times,{lambda+1,x_,lambda}:>x]
which will return
{1+lambda}
which is the value, or values, of x which satisfied that.
Check this carefully to see if you can find any mistakes before you depend on it.
Related
Applying a particular rule to a held (or unevaluated) function in Mathematica
I am a very novice Mathematica user and still can't get my head around its evaluation control, all possible constructs related to it (e.g. Hold, Unevaluated, etc.) and how they work, despite the thorough documentation and the numerous StackExchange and StackOverflow questions discussing this topic. So, apologies for any possible duplicates. My use case is the following: I have a function (say f) defined by thousands of rules and patterns (DownValues). I want to start from an unrolled representation of f[expr] (for some expr) and get the result of applying a single, particular rule to f[expr]. I want the result to stay unrolled as well. As a particular example, suppose we have the following: In[1]: nat[0] := 0 In[2]: nat[n_] := 1 + nat[n - 1] In[3]: DownValues[nat] Out[3]: {HoldPattern[nat[0]] :> 0, HoldPattern[nat[n_]] :> 1 + nat[n - 1]} In[4]: nat[10] Out[4]: 10 Now, I want to start from an expression represented as nat[10] (unevaluated!) and want to specifically apply the second rule (HoldPattern[nat[n_]] :> 1 + nat[n - 1]) to obtain the expression in the form of 1 + nat[9]. Analogously, shall I wish to apply the first rule (HoldPattern[nat[0]] :> 0), I would expect the result to stay unchanged in its original form, i.e. nat[10]. Thank you for your help!
This should help with your understanding of Mathematica's method of operation. Wolfram reference : The Ordering of Definitions I.e. Mathematica "looks for the value of an expression of the form f[n], it tries the special case f[1] first, and only if this does not apply, it tries the general case f[n_]." So with the functions below nat[0] is always tried first, but of course it only evaluates if the argument is 0. Then nat[n_] is tried. nat[0] := 0 nat[n_] := 1 + nat[n - 1] For your question as to obtaining 1 + nat[9] here is one way Clear[nat] nat[0] := 0 nat[n_] := HoldForm[1 + nat[o]] /. o -> n - 1 ans = nat[10] 1 + nat[9] Do[ans = ReleaseHold[ans], 10] ans 10 Alternatively (and better) Clear[nat] nat[0] := 0 nat[n_] := With[{m = n - 1}, HoldForm[1 + nat[m]]] ans = nat[10] 1 + nat[9] Do[ans = ReleaseHold[ans], 9] ans 9 + (1 + nat[0]) Note this is the result after 10 iterations. The final ReleaseHold results in nat[0] evaluating to 0. ReleaseHold[ans] 10 You might find it easier to see what is happening if you use Hold instead of HoldForm in the above demonstration.
As posted as a reply in a parallel discussion in Mathematica's StackExchange, I found a relatively more direct and straightforward way of dealing with the problem: In[6] rules = DownValues[nat] Out[6] {HoldPattern[nat[0]] :> 0, HoldPattern[nat[n_]] :> 1 + nat[n - 1]} In[7] DownValues[nat] = {} Out[7] {} In[8] nat[10] Out[8] nat[10] In[9] nat[10] /. rules[[1]] Out[9] nat[10] In[10] nat[10] /. rules[[2]] Out[10] 1 + nat[9]
Problems with triangulation in PolyK
I did some triangulation tasks with the library PolyK. Just for comparison purposes for my simple polygons. I have a Polygon with 7 vertices. But I get with this library only 9 indexes == 3 triangles that is to less I guess. In the following code you see my implemention of PolyK.js in THREE.js var pts3 = []; var ids2 = PolyK.Triangulate(pts3); for (var k = 0; k < ids2.length; k+=3) { geometry.faces.push(new THREE.Face3(ids2[k], ids2[k + 1], ids2[k + 2])); } My points array: (7 points with x and y value) var points = [ 158.56000000005588, 336.73000000044703, 158.60000000009313, 335.21999999973923, 161.589999999851, 335.3099999995902, 161.7799999997951, 329.820000000298, 155.52000000001863, 329.62000000011176, 155.29999999981374, 336.62999999988824, 158.56000000005588, 336.73000000044703 ]; This is the current result: This is the expected result: Is there something wrong regarding the usage of PolyK in the source code?
These are the points: 4 _______________ 3 | | | | | | | | | | | _______| | |1 2 |_______| 5 0/6 I tested in a fiddle here and it seems to work like you said. I get result: [5, 6, 0, 0, 1, 2, 0, 2, 3] It is the same incorrect result as you showed in your drawing. At first I thought removing the duplicated point ( 0 is same as 6 ) would solve the issue, but that also gives a incorrect result. I would suggest using a different library like earcut or poly2tri. Check also this triangulation adapter/library on GitHub that I made for three.js. It might be useful for you.
d3 scales: wrapping when x domain threshold reached
I am fairly new to the framework and I simply cannot find a simple example of a matrix that uses scales to compute the x and y of a rect. Here's an example of something I am trying to achieve: w = 400 h = 600 | column1 | column2 | -------------------------- row1 | value1 | value2 | row2 | value3 | value4 | row3 | value5 | value6 | My dataset looks something like this: dataset = [ [row1-col1, row1,col2], [row2-col1, row2,col2], [row3-col1, row3,col2], ] I merge it, using d3.merge, so that I append rects. NB: The resulting array as 6 elements and not groups of 2. .selectAll('rect') .data(d3.merge(dataset)) .enter() .append('rect') Question: What scale do I use so that it wraps on two columns based on the width? That is, I would like the following behaviour when i is passed to the xscale when the width == 2.: xscale(1) == 0, xscale(2) == 1 xscale(3) == 0, xscale(4) == 1 xscale(5) == 0, xscale(6) == 1 So, [1,3,5] -> 0, [2,4,5] -> 1. I also presume this is possible for the y scale as well? I would be very surprised if this were not possible, however I have been hunting around and I cannot find a simple example demonstrating such functionality. I do not want to have to manually wrap (using %) when the row limit is reached. If this isn't part of the standard library it should be included. I wonder how many people are manually implementing this. Thanks for the help
Inscrutable Ruby: Vector Assignment Example
Consider the following: a=[0,1] #our starting value a=[a,1] #=> [[0,1],1] as expected I would anticipate the following to have the same result: a=[0,1] #same starting place a[0]=a #should make a the same thing as it was above, right? a #=> [[...],1] !!! In the first example, the second assignment refers to the value of a before the assignment was made. In the second example, the second assignment performs a recursive assignment. This feels like different behavior to me. Is this behavior in fact consistent? If so can someone please explain why?
In the first example you are creating a new array with the value [[0,1], 1]. Then you are reassigning a to refer to this array. In the second example you are not creating a new array, nor are you changing what a refers to. You are changing the existing array to contain a reference to itself. That's very different. More details The first example is roughly equivalent to this code: a = [0, 1] # Step 1 b = [a, 1] # Step 2 a = b # Step 3 In pictures it looks like this: Step 1 - create an array: --- |a| --- | v [0, 1] Step 2 - create another array which includes a reference to the first: --- --- |a| |b| --- --- | | | v | [ref, 1] | | +------------+ v [0, 1] Step 3 - change a to point to the array created in step 2: --- --- |a| |b| --- --- | | +----------+ v [ref, 1] | +-------------+ v [0, 1] On the other hand, the code in the second example gives you this: --- |a| --- | +---+ | v | [ref, 1] | | +-----+ Here there is still only one array, and a still points to it. But now the first element in the array refers to the array itself.
In ruby all variables (without exceptions) are references. In this case a is a reference to Array. Kinda like a in int *a in C. By doing a[0] = a you make a an array where the first element is reference to a.
Code Golf: Countdown Number Game
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Challenge Here is the task, inspired by the well-known British TV game show Countdown. The challenge should be pretty clear even without any knowledge of the game, but feel free to ask for clarifications. And if you fancy seeing a clip of this game in action, check out this YouTube clip. It features the wonderful late Richard Whitely in 1997. You are given 6 numbers, chosen at random from the set {1, 2, 3, 4, 5, 6, 8, 9, 10, 25, 50, 75, 100}, and a random target number between 100 and 999. The aim is to use the six given numbers and the four common arithmetic operations (addition, subtraction, multiplication, division; all over the rational numbers) to generate the target - or as close as possible either side. Each number may only be used once at most, while each arithmetic operator may be used any number of times (including zero.) Note that it does not matter how many numbers are used. Write a function that takes the target number and set of 6 numbers (can be represented as list/collection/array/sequence) and returns the solution in any standard numerical notation (e.g. infix, prefix, postfix). The function must always return the closest-possible result to the target, and must run in at most 1 minute on a standard PC. Note that in the case where more than one solution exists, any single solution is sufficient. Examples: {50, 100, 4, 2, 2, 4}, target 203 e.g. 100 * 2 + 2 + (4 / 4) (exact) e.g. (100 + 50) * 4 * 2 / (4 + 2) (exact) {25, 4, 9, 2, 3, 10}, target 465 e.g. (25 + 10 - 4) * (9 * 2 - 3) (exact) {9, 8, 10, 5, 9, 7}, target 241 e.g. ((10 + 9) * 9 * 7) + 8) / 5 (exact) {3, 7, 6, 2, 1, 7}, target 824 e.g. ((7 * 3) - 1) * 6 - 2) * 7 (= 826; off by 2) Rules Other than mentioned in the problem statement, there are no further restrictions. You may write the function in any standard language (standard I/O is not necessary). The aim as always is to solve the task with the smallest number of characters of code. Saying that, I may not simply accept the answer with the shortest code. I'll also be looking at elegance of the code and time complexity of the algorithm! My Solution I'm attempting an F# solution when I find the free time - will post it here when I have something! Format Please post all answers in the following format for the purpose of easy comparison: Language Number of characters: ??? Fully obfuscated function: (code here) Clear (ideally commented) function: (code here) Any notes on the algorithm/clever shortcuts it takes.
Python Number of characters: 548 482 425 421 416 413 408 from operator import * n=len def C(N,T): R=range(1<<n(N));M=[{}for i in R];p=1 for i in range(n(N)):M[1<<i][1.*N[i]]="%d"%N[i] while p: p=0 for i in R: for j in R: m=M[i|j];l=n(m) if not i&j:m.update((f(x,y),"("+s+o+t+")")for(y,t)in M[j].items()if y for(x,s)in M[i].items() for(o,f)in zip('+-*/',(add,sub,mul,div))) p|=l<n(m) return min((abs(x-T),e)for t in M for(x,e)in t.items())[1] you can call it like this: >>> print C([50, 100, 4, 2, 2, 4], 203) ((((4+2)*(2+100))/4)+50) Takes about half a minute on the given examples on an oldish PC. Here's the commented version: def countdown(N,T): # M is a map: (bitmask of used input numbers -> (expression value -> expression text)) M=[{} for i in range(1<<len(N))] # initialize M with single-number expressions for i in range(len(N)): M[1<<i][1.0*N[i]] = "%d" % N[i] # allowed operators ops = (("+",lambda x,y:x+y),("-",lambda x,y:x-y),("*",lambda x,y:x*y),("/",lambda x,y:x/y)) # enumerate all expressions n=0 while 1: # test to see if we're done (last iteration didn't change anything) c=0 for x in M: c +=len(x) if c==n: break n=c # loop over all values we have so far, indexed by bitmask of used input numbers for i in range(len(M)): for j in range(len(M)): if i & j: continue # skip if both expressions used the same input number for (x,s) in M[i].items(): for (y,t) in M[j].items(): if y: # avoid /0 (and +0,-0,*0 while we're at it) for (o,f) in ops: M[i|j][f(x,y)]="(%s%s%s)"%(s,o,t) # pick best expression L=[] for t in M: for(x,e) in t.items(): L+=[(abs(x-T),e)] L.sort();return L[0][1] It works through exhaustive enumeration of all possibilities. It is a bit smart in that if there are two expressions with the same value that use the same input numbers, it discards one of them. It is also smart in how it considers new combinations, using the index into M to prune quickly all the potential combinations that share input numbers.
Haskell Number of characters: 361 350 338 322 Fully obfuscated function: m=map f=toRational a%w=m(\(b,v)->(b,a:v))w p[]=[];p(a:w)=(a,w):a%p w q[]=[];q(a:w)=[((a,b),v)|(b,v)<-p w]++a%q w z(o,p)(a,w)(b,v)=[(a`o`b,'(':w++p:v++")")|b/=0] y=m z(zip[(-),(/),(+),(*)]"-/+*")++m flip(take 2 y) r w=do{((a,b),v)<-q w;o<-y;c<-o a b;c:r(c:v)} c t=snd.minimum.m(\a->(abs(fst a-f t),a)).r.m(\a->(f a,show a)) Clear function: -- | add an element on to the front of the remainder list onRemainder :: a -> [(b,[a])] -> [(b,[a])] a`onRemainder`w = map (\(b,as)->(b,a:as)) w -- | all ways to pick one item from a list, returns item and remainder of list pick :: [a] -> [(a,[a])] pick [] = [] pick (a:as) = (a,as) : a `onRemainder` (pick as) -- | all ways to pick two items from a list, returns items and remainder of list pick2 :: [a] -> [((a,a),[a])] pick2 [] = [] pick2 (a:as) = [((a,b),cs) | (b,cs) <- pick as] ++ a `onRemainder` (pick2 as) -- | a value, and how it was computed type Item = (Rational, String) -- | a specification of a binary operation type OpSpec = (Rational -> Rational -> Rational, String) -- | a binary operation on Items type Op = Item -> Item -> Maybe Item -- | turn an OpSpec into a operation -- applies the operator to the values, and builds up an expression string -- in this context there is no point to doing +0, -0, *0, or /0 combine :: OpSpec -> Op combine (op,os) (ar,as) (br,bs) | br == 0 = Nothing | otherwise = Just (ar`op`br,"("++as++os++bs++")") -- | the operators we can use ops :: [Op] ops = map combine [ ((+),"+"), ((-), "-"), ((*), "*"), ((/), "/") ] ++ map (flip . combine) [((-), "-"), ((/), "/")] -- | recursive reduction of a list of items to a list of all possible values -- includes values that don't use all the items, includes multiple copies of -- some results reduce :: [Item] -> [Item] reduce is = do ((a,b),js) <- pick2 is op <- ops c <- maybe [] (:[]) $ op a b c : reduce (c : js) -- | convert a list of real numbers to a list of items items :: (Real a, Show a) => [a] -> [Item] items = map (\a -> (toRational a, show a)) -- | return the first reduction of a list of real numbers closest to some target countDown:: (Real a, Show a) => a -> [a] -> Item countDown t is = snd $ minimum $ map dist $ reduce $ items is where dist is = (abs . subtract t' . fst $ is, is) t' = toRational t Any notes on the algorithm/clever shortcuts it takes: In the golf'd version, z returns in the list monad, rather than Maybe as ops does. While the algorithm here is brute force, it operates in small, fixed, linear space due to Haskell's laziness. I coded the wonderful #keith-randall algorithm, but it ran in about the same time and took over 1.5G of memory in Haskell. reduce generates some answers multiple times, in order to easily include solutions with fewer terms. In the golf'd version, y is defined partially in terms of itself. Results are computed with Rational values. Golf'd code would be 17 characters shorter, and faster if computed with Double. Notice how the function onRemainder factors out the structural similarity between pick and pick2. Driver for golf'd version: main = do print $ c 203 [50, 100, 4, 2, 2, 4] print $ c 465 [25, 4, 9, 2, 3, 10] print $ c 241 [9, 8, 10, 5, 9, 7] print $ c 824 [3, 7, 6, 2, 1, 7] Run, with timing (still under one minute per result): [1076] : time ./Countdown (203 % 1,"(((((2*4)-2)/100)+4)*50)") (465 % 1,"(((((10-4)*25)+2)*3)+9)") (241 % 1,"(((((10*9)/5)+8)*9)+7)") (826 % 1,"(((((3*7)-1)*6)-2)*7)") real 2m24.213s user 2m22.063s sys 0m 0.913s
Ruby 1.9.2 Number of characters: 404 I give up for now, it works as long as there is an exact answer. If there isn't it takes way too long to enumerate all possibilities. Fully Obfuscated def b a,o,c,p,r o+c==2*p ?r<<a :o<p ?b(a+['('],o+1,c,p,r):0;c<o ?b(a+[')'],o,c+1,p,r):0 end w=a=%w{+ - * /} 4.times{w=w.product a} b [],0,0,3,g=[] *n,l=$<.read.split.map(&:to_f) h={} catch(0){w.product(g).each{|c,f|k=f.zip(c.flatten).each{|o|o.reverse! if o[0]=='('};n.permutation{|m|h[x=eval(d=m.zip(k)*'')]=d;throw 0 if x==l}}} c=h[k=h.keys.min_by{|i|(i-l).abs}] puts c.gsub(/(\d*)\.\d*/,'\1')+"=#{k}" Decoded Coming soon Test script #!/usr/bin/env ruby [ [[50,100,4,2,2,4],203], [[25,4,9,2,3,10],465], [[9,8,10,5,9,7],241], [[3,7,6,2,1,7],824] ].each do |b| start = Time.now puts "{[#{b[0]*', '}] #{b[1]}} gives #{`echo "#{b[0]*' '} #{b[1]}" | ruby count-golf.rb`.strip} in #{Time.now-start}" end Output → ./test.rb {[50, 100, 4, 2, 2, 4] 203} gives 100+(4+(50-(2)/4)*2)=203.0 in 3.968534736 {[25, 4, 9, 2, 3, 10] 465} gives 2+(3+(25+(9)*10)*4)=465.0 in 1.430715549 {[9, 8, 10, 5, 9, 7] 241} gives 5+(9+(8)+10)*9-(7)=241.0 in 1.20045702 {[3, 7, 6, 2, 1, 7] 824} gives 7*(6*(7*(3)-1)-2)=826.0 in 193.040054095 Details The function used for generating the bracket pairs (b) is based off this one: Finding all combinations of well-formed brackets
Ruby 1.9.2 second attempt Number of characters: 492 440(426) Again there is a problem with the non-exact answer. This time this is easily fast enough but for some reason the closest it gets to 824 is 819 instead of 826. I decided to put this in a new answer since it is using a very different method to my last attempt. Removing the total of the output (as its not required by spec) is -14 characters. Fully Obfuscated def r d,c;d>4?[0]:(k=c.pop;a=[];r(d+1,c).each{|b|a<<[b,k,nil];a<<[nil,k,b]};a)end def f t,n;[0,2].each{|a|Array===t[a] ?f(t[a],n): t[a]=n.pop}end def d t;Float===t ?t:d(t[0]).send(t[1],d(t[2]))end def o c;Float===c ?c.round: "(#{o c[0]}#{c[1]}#{o c[2]})"end w=a=%w{+ - * /} 4.times{w=w.product a} *n,l=$<.each(' ').map(&:to_f) h={} w.each{|y|r(0,y.flatten).each{|t|f t,n.dup;h[d t]=o t}} puts h[k=h.keys.min_by{|i|(l-i).abs}]+"=#{k.round}" Decoded Coming soon Test script #!/usr/bin/env ruby [ [[50,100,4,2,2,4],203], [[25,4,9,2,3,10],465], [[9,8,10,5,9,7],241], [[3,7,6,2,1,7],824] ].each do |b| start = Time.now puts "{[#{b[0]*', '}] #{b[1]}} gives #{`echo "#{b[0]*' '} #{b[1]}" | ruby count-golf.rb`.strip} in #{Time.now-start}" end Output → ./test.rb {[50, 100, 4, 2, 2, 4] 203} gives ((4-((2-(2*4))/100))*50)=203 in 1.089726252 {[25, 4, 9, 2, 3, 10] 465} gives ((10*(((3+2)*9)+4))-25)=465 in 1.039455671 {[9, 8, 10, 5, 9, 7] 241} gives (7+(((9/(5/10))+8)*9))=241 in 1.045774539 {[3, 7, 6, 2, 1, 7] 824} gives ((((7-(1/2))*6)*7)*3)=819 in 1.012330419 Details This constructs the set of ternary trees representing all possible combinations of 5 operators. It then goes through and inserts all permutations of the input numbers into the leaves of these trees. Finally it simply iterates through these possible equations storing them into a hash with the result as index. Then it's easy enough to pick the closest value to the required answer from the hash and display it.