Write a program in mathematica whith Givens rotation - math
How can I write a program which can solve a system of linear equations with Givens rotation in mathematica .
Code attempt:
ar = CoefficientArrays[
{x + y + z == 2, 3 x - 2 y + z == 4, x - y + 5 z == 6},
{x, y, z}];
an = Normal[ar];
b = an[[2]];
n = Length[b];
Do[Do[a[i, j] = b[[i]][[j]], {j, 1, n}], {i, 1, n}];
r1 = (a[1, 1]^2 + a[1, 2]^2)^(1/2);
c = a[1, 1]/r1;
s = a[1, 2]/r1;
Are you looking for something like this?
Following your code.
{r1, c, s}
{Sqrt[2], 1/Sqrt[2], 1/Sqrt[2]}
{a, b} = LinearSolve[{{c, -s}, {s, c}}, {r1, 0}]
{1, -1}
algorithm from wikipedia
GivensRotation[a_, b_] := Which[
b == 0, c = Sign[a]; s = 0; r = Abs[a],
a == 0, c = 0; s = -Sign[b]; r = Abs[b],
Abs[a] > Abs[b],
t = b/a; u = Sign[a]*Abs[Sqrt[1 + t*t]];
c = 1/u; s = -c*t; r = a*u,
True,
t = a/b;
u = Sign[b]*Abs[Sqrt[1 + t*t]];
s = -1/u; c = -s*t; r = b*u
]
GivensRotation[a, b];
{r, c, s}
{Sqrt[2], 1/Sqrt[2], 1/Sqrt[2]}
Edit
I'm not familiar with solving with Givens rotation. Here are other methods for solving the simultaneous equations, just for interest.
Solve[{
x + y + z == 2,
3 x - 2 y + z == 4,
x - y + 5 z == 6},
{x, y, z}]
{{x -> 1, y -> 0, z -> 1}}
also
LinearSolve[{{1, 1, 1}, {3, -2, 1}, {1, -1, 5}}, {2, 4, 6}]
{1, 0, 1}
or
Inverse[{{1, 1, 1}, {3, -2, 1}, {1, -1, 5}}].{2, 4, 6}
{1, 0, 1}
Related
Pooling Survreg Results Across Multiply Imputed Datasets - Error Message: log(1 - 2 * pnorm(width/2)) : NaNs produced
I am trying to run an interval regression using the survival r package (as described here https://stats.oarc.ucla.edu/r/dae/interval-regression/), but I am running into difficulties when trying to pool results across multiply imputed datasets. Specifically, although estimates are returned, I get the following error: log(1 - 2 * pnorm(width/2)) : NaNs produced. The estimates seem reasonable, at face value (no NaNs, very large or small SEs). I ran the same model on the stacked dataset (ignoring imputations) and on individual imputed datasets, but in either case, I do not get the error. Would someone be able to explain to me what is going on? Is this an ignorable error? If not, is there a workaround that avoids this error? Thanks so much! # A Reproducible Example require(survival) require(mice) require(car) # Create DF dat <- data.frame(dv = c(1, 1, 2, 1, 0, NA, 1, 4, NA, 0, 3, 1, 3, 0, 2, 1, 4, NA, 2, 4), catvar1 = factor(c(0, 0, 0, 0, 0, 1, 0, 0, 0, NA, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0)), catvar2 = factor(c(1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, NA, 0))) dat_imp <- mice(data = dat) # Transform Outcome Var for Interval Reg dat_imp_long <- complete(dat_imp, action = "long", include=TRUE) # 1-4 correspond to ranges (e.g., 1 = 1 to 2 times...4 = 10 or more) # create variables that reflect this range dat_imp_long$dv_low <- car::recode(dat_imp_long$dv, "0 = 0; 1 = 1; 2 = 3; 3 = 6; 4 = 10") dat_imp_long$dv_high <- car::recode(dat_imp_long$dv, "0 = 0; 1 = 2; 2 = 5; 3 = 9; 4 = 999") dat_imp_long$dv_high[dat_imp_long$dv_high > 40] <- Inf # Convert back to mids dat_mids <- as.mids(dat_imp_long) # Run Interval Reg model1 <- with(dat_mids, survreg(Surv(dv_low, dv_high, type = "interval2") ~ catvar1 + catvar2, dist = "gaussian")) # Warning message for both calls: In log(1 - 2 * pnorm(width/2)) : NaNs produced # Problem does not only occur with pool, but summary summary(model1) summary(pool(model1)) # Run Equivalent Model on Individual Datasets # No errors produced imp1 <- subset(dat_imp_long, .imp == 1) model2 <- survreg(Surv(dv_low, dv_high, type = "interval2") ~ catvar1 + catvar2, dist = "gaussian", data = imp1) summary(model2) imp2 <- subset(dat_imp_long, .imp == 2) model3 <- survreg(Surv(dv_low, dv_high, type = "interval2") ~ catvar1 + catvar2, dist = "gaussian", data = imp2) summary(model3) # Equivalent Analysis on Stacked Dataset # No error model <- with(dat_imp_long, survreg(Surv(dv_low, dv_high, type = "interval2") ~ catvar1 + catvar2, dist = "gaussian")) summary(model)
R optim() constraint optimization does not find the first best
my problem is summarized in finding a vector X with the best solution to the problem: L is the profits, R is the restrictions, P is a constraint parameters matrix, max SUM_i (x_i * l_i) or max(t(L)%*%X) restriction SUM_i(x_i*p_ij)<=r_j or P%*%X <= R. I find a solution for X, but not the best, which would be fb = c(.217,0,0,23,2865,0,13,427). How do I find the best solution? code: X<-matrix(rep(1,6),6,1) P<-matrix(c( 1, 1, 1, 2, 0, 0, 0, 1, 1, 2, 1, 1, 99.4, 37.75, 19.75, 54.40, 74.75, 53, 2.400, 1.540, 0, 0, 0, 0, 2.400, 1.960, 0, 0, 0, 0, 1.800, 3.300, 5.330, 0, 0, 0, 0, 0, 2.070, 0, 8.700, 0, 0, 0, .436, 0, 19.100, 12.363, 0, 3.000, .364, 0, 9.100, 26.737 ), 9,6,1) L <- matrix(c(83.4, 72.35, 27.3, 72.05, 217.25, 455), 6,1) R <- matrix(c(60,60,2000,351,448,479,338,424,359),9,1) farm<- function(par, P,R, L){ trues<- P%*%par<=R if (min(trues)==1 && min(par)>=0) { return(-t(L)%*%par) } else{ return(0) } } mtds = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN","Brent") out <- optim(par = X, # initial guess fn = farm, P = P, R = R , L = L, method = mtds[5]) # my result t(L)%*%out$par #A matrix: 1 × 1 of type dbl #7419.596 # the first best fb<- matrix(c(.217,0,0,23.2865,0,13.427),6,1) t(L)%*%fb #A matrix: 1 × 1 of type dbl #7805.175
I think you can try fmincon from package pracma library(pracma) objfun <- function(x) -t(L)%*%x res <- fmincon(x0 = X,fn = objfun,A = P,b = R,lb = rep(0,length(X))) and you will see that > res$par [1] 4.201711e-16 -1.239088e-15 1.863081e-17 2.310286e+01 [5] 5.566620e-01 1.323762e+01 > -res$value [,1] [1,] 7808.615
That looks very much like a model that could be solved by a linear programme. library("Rglpk") Rglpk_solve_LP(obj = L, mat = P, dir = rep("<=", 9), rhs = R, max = TRUE)
Calling ROI "LP "and "QP" functions
I am trying to reproduce some of the examples given by the ROI creators. For example in http://statmath.wu.ac.at/courses/optimization/Presentations/ROI-2011.pdf (slides 15-17) there is the example: library("ROI") #ROI: R Optimization Infrastructure #Installed solver plugins: cplex, lpsolve, glpk, quadprog, symphony, nlminb. #Default solver: glpk. (constr1 <- L_constraint(c(1, 2), "<", 4)) #An object containing 1 linear constraints. (constr2 <- L_constraint(matrix(c(1:4), ncol = 2), c("<", "<"), c(4, 5))) #An object containing 2 linear constraints. rbind(constr1, constr2) #An object containing 3 linear constraints. (constr3 <- Q_constraint(matrix(rep(2, 4), ncol = 2), c(1, 2), "<", 5)) #An object containing 1 constraints. #Some constraints are of type quadratic. foo <- function(x) {sum(x^3) - seq_along(x) %*% x} F_constraint(foo, "<", 5) lp <- LP(objective = c(2, 4, 3), L_constraint(L = matrix(c(3, 2, 1, 4, 1, 3, 2, 2, 2), nrow = 3), dir = c("<=", "<=", "<="), rhs = c(60, 40, 80)), maximum = TRUE) qp <- QP(Q_objective(Q = diag(1, 3), L = c(0, -5, 0)), L_constraint(L = matrix(c(-4, -3, 0, 2, 1, 0, 0, -2, 1), ncol = 3, byrow = TRUE), dir = rep(">=", 3), rhs = c(-8, 2, 0))) When I run it I get the errors Error in LP(objective = c(2, 4, 3), L_constraint(L = matrix(c(3, 2, 1, : could not find function "LP" and Error in QP(Q_objective(Q = diag(1, 3), L = c(0, -5, 0)), L_constraint(L = matrix(c(-4, : could not find function "QP" In fact the functions are not in ROI's namespace. e.g. ROI::LP Error: 'LP' is not an exported object from 'namespace:ROI' The same syntax appears in other examples I found on the web but the functions LP and QP are never defined. I am using ROI 0.3.0 Can someone tell me what is going wrong?
The commands LP and QP were both changed to OP. library("ROI") ## ROI: R Optimization Infrastructure ## Registered solver plugins: nlminb, alabama, cbc, cccp, clp, deoptim, ecos, glpk, ipop, lpsolve, msbinlp, neos, nloptr, ucminf, spg, cgm, vmm, bobyqa, newuoa, uobyqa, hjk, nmk, lbfgs, optimx, qpoases, quadprog, scs, symphony. ## Default solver: auto. (constr1 <- L_constraint(c(1, 2), "<", 4)) ## An object containing 1 linear constraint. (constr2 <- L_constraint(matrix(c(1:4), ncol = 2), c("<", "<"), c(4, 5))) ## An object containing 2 linear constraints. rbind(constr1, constr2) ## An object containing 3 linear constraints. (constr3 <- Q_constraint(matrix(rep(2, 4), ncol = 2), c(1, 2), "<", 5)) ## An object containing 0 linear constraints ## 1 quadratic constraint. foo <- function(x) {sum(x^3) - seq_along(x) %*% x} F_constraint(foo, "<", 5) ## An object containing 1 nonlinear constraint. lp <- OP(objective = c(2, 4, 3), L_constraint(L = matrix(c(3, 2, 1, 4, 1, 3, 2, 2, 2), nrow = 3), dir = c("<=", "<=", "<="), rhs = c(60, 40, 80)), maximum = TRUE) qp <- OP(Q_objective(Q = diag(1, 3), L = c(0, -5, 0)), L_constraint(L = matrix(c(-4, -3, 0, 2, 1, 0, 0, -2, 1), ncol = 3, byrow = TRUE), dir = rep(">=", 3), rhs = c(-8, 2, 0))) The slides you refer to are outdated. The new documentation is on http://roi.r-forge.r-project.org !
Prolog recursively replace elements of list with elements of another list
Apologies for lack of clarity in title. The following is a very specific predicate I'm building, which is only partially working as intended. % replace_elements(+SearchingElementsList,+ReplacementsList,+OriginalList,-ResultingList). % ResultingList consists of all elements of SearchingElementsList replaced by elements of ReplacementsList respectively. replace_elements([],[],_,_). replace_elements([H|T],[H2|T2],[H3|T3],List) :- H \= H3, % H is not H3, therefore replace_elements([H|T],[H2|T2],T3,List). % Skip this element and continue with T3. replace_elements([H|T],[H2|T2],[H|T3],[H2|List]) :- replace_elements(T,T2,T3,List). % H is found in OriginalList. Continue with tails. Currently: ?- replace_elements([1,2,3],[one,two,three],[1,2,3,4,5],Result). ?- Result = [one,two,three|_7636]. Expected: ?- Result = [one,two,three,4,5]. Any hint would be appreciated! Edit: Came up with a working answer for my specific problem. % Eventually, recursion starts from all empty lists. replace_elements([],[],[],[]). % Rules are empty, push remaining H to List. replace_elements([],[],[H|T],[H|List]) :- replace_elements([],[],T,List). % Empty list, just go through remaining rules. replace_elements([H|T],[H2|T2],[],List) :- replace_elements(T,T2,[],List). % H < H3, move to next element in rules. replace_elements([H|T],[H2|T2],[H3|T3],List) :- H < H3, replace_elements(T,T2,[H3|T3],List). % H > H3, move to next element in original list. replace_elements([H|T],[H2|T2],[H3|T3],[H3|List]) :- H > H3, replace_elements([H|T],[H2|T2],T3,List). % Element is the same, push replacement H2 to List. replace_elements([H|T],[H2|T2],[H|T3],[H2|List]) :- replace_elements(T,T2,T3,List).
Here is my implementation using if_/3 and extended version of memberd_t adding more list as parameters in order to achieve both checking the Searching Elements List and returning the result from ReplacementsList in one pass-traversing for efficiency: replace_elements( [], [], _, _). replace_elements([H|T], [H2|T1], Search_L, Replace_L):- if_( memberd_t(H, X, Search_L, Replace_L), ( H2 = X, replace_elements( T, T1,Search_L, Replace_L) ), ( H2 = H, replace_elements( T, T1,Search_L, Replace_L) ) ). memberd_t(H, X, Xs, Ys , T) :- i_memberd_t(Xs, Ys, H, X, T). i_memberd_t([], [], _, _, false). i_memberd_t([X|Xs], [Y|Ys], E, H, T) :- if_( X = E, (T = true, H = Y) , i_memberd_t(Xs, Ys, E, H, T) ). Some testcases: ?- replace_elements([1,2,3,4,5],Result,[1,2,3],[one,two,three]). Result = [one, two, three, 4, 5]. ?- replace_elements([1,2,3,4,5],Result,[1,2,3],Ts). Result = [_792, _894, _1032, 4, 5], Ts = [_792, _894, _1032]. ?- L = [1|L1], replace_elements(L ,[one,two,three,4,5],[1,2,3],[one,two,three]). L = [1, 2, 3, 4, 5], L1 = [2, 3, 4, 5] ; L = [1, 2, three, 4, 5], L1 = [2, three, 4, 5] ; L = [1, two, 3, 4, 5], L1 = [two, 3, 4, 5] ; L = [1, two, three, 4, 5], L1 = [two, three, 4, 5]. ?- replace_elements(L ,[one,two,three,4,5],[1,2,3],[one,two,three]). L = [1, 2, 3, 4, 5] ; L = [1, 2, three, 4, 5] ; L = [1, two, 3, 4, 5] ; L = [1, two, three, 4, 5] ; L = [one, 2, 3, 4, 5] ; L = [one, 2, three, 4, 5] ; L = [one, two, 3, 4, 5] ; L = [one, two, three, 4, 5]. In_L = Result, Result = [] ; In_L = [1], Result = [one] ; In_L = [1, 1], Result = [one, one] ; In_L = [1, 1, 1], Result = [one, one, one] ; In_L = [1, 1, 1, 1], Result = [one, one, one, one] ; In_L = [1, 1, 1, 1, 1], Result = [one, one, one, one, one] ; In_L = [1, 1, 1, 1, 1, 1], Result = [one, one, one, one, one, one] ; In_L = [1, 1, 1, 1, 1, 1, 1], Result = [one, one, one, one, one, one, one] ; In_L = [1, 1, 1, 1, 1, 1, 1, 1], Result = [one, one, one, one, one, one, one, one] ; In_L = [1, 1, 1, 1, 1, 1, 1, 1, 1], Result = [one, one, one, one, one, one, one, one, one]...and goes on.... ?- replace_elements([1,2,3,4,5],Result,[1,2,X],[one,two,three]). Result = [one, two, three, 4, 5], X = 3 ; Result = [one, two, 3, three, 5], X = 4 ; Result = [one, two, 3, 4, three], X = 5 ; Result = [one, two, 3, 4, 5], dif(X, 5), dif(X, 4), dif(X, 3). Result = [one, two, three, 4, 5], L = [1, 2, 3] ; Result = [one, two, 3, three, 5], L = [1, 2, 4] ; Result = [one, two, 3, 4, three], L = [1, 2, 5] ; Result = [one, two, 3, 4, 5], L = [1, 2, _22546], dif(_22546, 5), dif(_22546, 4), dif(_22546, 3) ; Result = [one, three, two, 4, 5], L = [1, 3, 2] ;...and goes on... until finally terminates (after a lot of answers) deterministicaly Result = [1, 2, 3, 4, 5], L = [_23992, _23998, _24004], dif(_23992, 5), dif(_23992, 4), dif(_23992, 3), dif(_23992, 2), dif(_23992, 1), dif(_23998, 5), dif(_23998, 4), dif(_23998, 3), dif(_23998, 2), dif(_23998, 1), dif(_24004, 5), dif(_24004, 4), dif(_24004, 3), dif(_24004, 2), dif(_24004, 1). ?- L=[_,_,_],replace_elements([1,2,3,4,5],[one,two,three,4,5],L,T). L = [1, 2, 3], T = [one, two, three] ; L = [1, 3, 2], T = [one, three, two] ; L = [2, 1, 3], T = [two, one, three] ; L = [3, 1, 2], T = [three, one, two] ; L = [2, 3, 1], T = [two, three, one] ; L = [3, 2, 1], T = [three, two, one] ; false. ?- replace_elements([1,2,3,4,5],[one,two,three,4,5],Fs,Ts). Fs = [1, 2, 3], Ts = [one, two, three] ; Fs = [1, 2, 3, 4], Ts = [one, two, three, 4] ; Fs = [1, 2, 3, 4, 5|_9700], Ts = [one, two, three, 4, 5|_9706] ; Fs = [1, 2, 3, 4, _10176], Ts = [one, two, three, 4, _10218], dif(_10176, 5) ; Fs = [1, 2, 3, 4, _10236, 5|_10244], Ts = [one, two, three, 4, _10284, 5|_10292], dif(_10236, 5) ; Fs = [1, 2, 3, 4, _10384, _10390], Ts = [one, two, three, 4, _10432, _10438], dif(_10384, 5), dif(_10390, 5) ; Fs = [1, 2, 3, 4, ...and goes on...
This relation can be expressed quite compactly with if_/3, =/3 and maplist/3: :- use_module(library(apply)). % for maplist/3 from_to_elem_repl([],[],E,E). from_to_elem_repl([X|Xs],[Y|Ys],E,R) :- if_(E=X,R=Y,from_to_elem_repl(Xs,Ys,E,R)). from_to_list_mapped(Fs,Ts,L,M) :- maplist(from_to_elem_repl(Fs,Ts),L,M). The predicate from_to_elem_repl/4 describes the relation between an element and its replacement. If the element E occurs in the from-list then it's replaced by the element at the corresponding position in the to-list: Y (recursive rule). If E does not occur in the from-list it is not replaced (base case). The predicate from_to_list_mapped/4 uses maplist/3 to map the predicate from_to_elem_repl/4 to the list L thus yielding the list with the replacements M. Your example query succeeds deterministically: ?- from_to_list_mapped([1,2,3],[one,two,three],[1,2,3,4,5],M). M = [one, two, three, 4, 5]. In the other direction all eight solutions are found: ?- from_to_list_mapped([1,2,3],[one,two,three],L [one,two,three,4,5]). L = [1, 2, 3, 4, 5] ; L = [1, 2, three, 4, 5] ; L = [1, two, 3, 4, 5] ; L = [1, two, three, 4, 5] ; L = [one, 2, 3, 4, 5] ; L = [one, 2, three, 4, 5] ; L = [one, two, 3, 4, 5] ; L = [one, two, three, 4, 5]. You could also ask for possible mapping pairs for a given list and its replacement: ?- from_to_list_mapped(Fs,Ts,[1,2,3,4,5],[one,two,three,4,5]). Fs = [1, 2, 3], Ts = [one, two, three] ; Fs = [1, 2, 3, 4], Ts = [one, two, three, 4] ; Fs = [1, 2, 3, 4, 5|_G5111], Ts = [one, two, three, 4, 5|_G5114] ; Fs = [1, 2, 3, 4, _G5258], Ts = [one, two, three, 4, _G5279], dif(_G5258, 5) ; Fs = [1, 2, 3, 4, _G5275, 5|_G5279], Ts = [one, two, three, 4, _G5299, 5|_G5303], dif(_G5275, 5) ; Fs = [1, 2, 3, 4, _G5316, _G5319], Ts = [one, two, three, 4, _G5340, _G5343], dif(_G5316, 5), dif(_G5319, 5) ; Fs = [1, 2, 3, 4, _G5333, _G5336, 5|_G5340], Ts = [one, two, three, 4, _G5360, _G5363, 5|_G5367], dif(_G5333, 5), dif(_G5336, 5) ; . . . Obviously there are infinitely many possibilities. But if you ask for a fixed length, the predicate terminates: ?- Fs=[_,_,_],from_to_list_mapped(Fs,Ts,[1,2,3,4,5],[one,two,three,4,5]). Fs = [1, 2, 3], Ts = [one, two, three] ; Fs = [1, 3, 2], Ts = [one, three, two] ; Fs = [2, 1, 3], Ts = [two, one, three] ; Fs = [3, 1, 2], Ts = [three, one, two] ; Fs = [2, 3, 1], Ts = [two, three, one] ; Fs = [3, 2, 1], Ts = [three, two, one] ; false. You can also ask for a mapping without specifying the replacement elements: ?- from_to_list_mapped([1,2,3],Ts,[1,2,3,4,5],R). Ts = [_G4920, _G4937, _G4965], R = [_G4920, _G4937, _G4965, 4, 5]. As you see the predicate is quite versatile. Play around with it to find other possible uses.
I'll provide a thought process for solving the problem. A couple of principles would be: Thinking relationally rather than imperatively Top down design You want: replace_elements(+SearchingElementsList, +ReplacementsList, +OriginalList, -ResultingList). I'll rename it a little to make it not be as imperative: translated_list(Trans1, Trans2, List1, List2). I removed the + and - because, ideally, we'd like this to be as general a relation as possible. This is a relation that the corresponding elements in List1 and List2 are related to each other via the "translation table" defined by corresponding elements in Trans1 and Trans2. In Prolog, when considering corresponding list elements, that makes me immediately think of using something like maplist. Also, it's awkward handling two independent lists to define a 1-1 relationship between terms. It is easier and cleaner to use a single translation table with elements that look like, say, t1-t2. translated_list(Trans1, Trans2, List1, List2) :- % TransTable is a translation table consisting of t1-t2 pairs from Trans1 and Trans2 maplist(trans_table_element, Trans1, Trans2, TransTable), % List1 and List2 are related using the translation table, TransTable maplist(corresponding_element(TransTable), List1, List2). Now we need a predicate trans_table that provides a relationship between translation table elements. This relation says that an element in the translation table is E1-E2 where the corresponding individual elements are E1 and E2: trans_table_element(E1, E2, E1-E2). And we need a predicate that describes two corresponding elements using a translation table. corresponding_element(TransTable, E1, E2) :- ( member(E1-E2, TransTable) -> true % Translation exists ; E1 = E2 % Translation doesn't exist ). From this, you get: | ?- translated_list([1,2,3], [one, two, three], [4,1,3,5,2,1,3], L). L = [4,one,three,5,two,one,three] yes | ?- As well as: | ?- translated_list([1,2,3], [one, two, three], L, [4,one,three,5,two,one,three]). L = [4,1,3,5,2,1,3] ? a no And | ?- translated_list(A, B, [4,1,3,5,2,1,3], [4,one,three,5,two,one,three]). A = [4,1,3,5,2] B = [4,one,three,5,two] ? ; A = [4,1,3,5,2,_] B = [4,one,three,5,two,_] ? ; ... If you want to enforce in your rules that the translation table only translates between different elements, then you could define: trans_table_element(E1, E2, E1-E2) :- dif(E1, E2). And then you'd get (using SWI Prolog this time): 2 ?- translated_list(A, B, [4,1,3,5,2,1,3], [4,one,three,5,two,one,three]). A = [1, 3, 2], B = [one, three, two] ; A = [1, 3, 2, _1240], B = [one, three, two, _1276], dif(_1240, _1276) ; A = [1, 3, 2, _1492, _1498], B = [one, three, two, _1534, _1540], dif(_1492, _1534), dif(_1498, _1540) ; ... UPDATE A possible improvement in the above implementation to make it more relational by avoiding the ->/; construct, we can define non-membership relationally using maplist. corresponding_element(TransTable, E1, E2) :- member(E1-E2, TransTable). corresponding_element(TransTable, E1, E2) :- maplist(dif(E1-E2), TransTable), E1 = E2. In addition to the above results, yields: 6 ?- translated_list([1,2,3],Ts,[1,2,3,4,5],R). Ts = [_9016, _9034, _9052], R = [_9016, _9034, _9052, 4, 5] ; Ts = [_9640, _9646, _9652], R = [_9640, _9646, 3, 4, 5], dif(3, _9652) ; Ts = [_9640, _9646, _9652], R = [_9640, 2, _9652, 4, 5], dif(2, _9646) ; Ts = [_9856, _9862, _9868], R = [_9856, 2, 3, 4, 5], dif(2, _9862), dif(3, _9868) ...
How to plot a 3d scatter plot in R?
I have MATLAB code that I am trying to convert into R. Here is the MATLAB code: x1=[2 3 1 2 2.5]; x2=[2 3 2.1 2 2.5]; yy=[2 3 6 6.5 9.5]; xx=[x1; x2]; AA=yy*xx'*inv(xx*xx'); xx1=[0 0 4 4]; xx2=[0 4 0 4]; xxx=[xx1; xx2]; yy1=AA*xxx; yy2=reshape(yy1,2,2); [mg1,mg2]=meshgrid(0:4:4); figure; plot3(x1,x2,yy,'r.'); grid on; axis([0 4 0 4 0 12]); hold on; surf(mg1,mg2,yy2); view([259 44]); xlabel('Feature 1');ylabel('Feature 2');zlabel("Responses"); This is the R code which I made from the above MATLAB code x1 = matrix(c(2, 3, 1, 2, 2.5), nrow=1, ncol = 5) x2 = matrix(c(2, 3, 2.1, 2, 2.5), nrow=1, ncol = 5) yy = matrix(c(2, 3, 6, 6.5, 9.5), nrow=1, ncol = 5) xx = rbind(x1,x2) AA = yy %*% t(xx) %*% solve(xx %*% t(xx)) xx1 = matrix(c(0, 0, 4, 4), nrow = 1, ncol = 4) xx2 = matrix(c(0, 4, 0, 4), nrow = 1, ncol = 4) xxx = rbind(xx1,xx2) yy1 = AA %*% xxx yy2 = matrix(yy1,nrow = 2, ncol = 2) s3d <-scatterplot3d(x1,x2,yy, pch=16, highlight.3d=TRUE, type="h", main="3D Scatterplot") fit <- lm(yy ~ x1+x2) s3d$plane3d(fit) grid() persp(mg1,mg2,yy2, theta=30, phi=30, expand=0.6, col='lightblue', shade=0.75, ltheta=120, ticktype='detailed') At this point, I get the following error: Error in s3d$plane3d(fit) : dims [product 55] do not match the length of object [0] Obtained Output: Required Output: