Vector of symbols?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Vector of symbols?

Judson


Is
there a way to multiply a matrix

times
a vector of symbols?  

For
instance, could I do this:

1      
0       0                          a

0        1      
0      times           b

-1       2      
1                          c                

 

which
should result in :  

                                                      a

                                                      b

                                                      c  �a
-2*b
where a, b, and c

are
as yet not assigned numeric values?  

........................... judson blake


     
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of symbols?

Jeff Newmiller
I believe it is possible, but R is not really a full-fledged symbolic algebra system so it wouldn't be an intuitive tool to use and what would you do with it once you had it? It is much more useful in R to do something like

f1 <- function( M, A, B, C ) {
    M %*% c( A, B, C )
}

m <- matrix( c( 1,0,-1,0,1,2,0,0,1 ), ncol=3 )
f1( m, 3, 4, 5 )
#      [,1]
# [1,]    3
# [2,]    4
# [3,]   10

which manipulates numeric values according to your definition.

For future reference... Please post on this list using plain text format... your email was a pain to decipher.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On October 31, 2015 8:52:51 AM PDT, Judson <[hidden email]> wrote:

>
>
>Is
>there a way to multiply a matrix
>
>times
>a vector of symbols?  
>
>For
>instance, could I do this:
>
>1      
>0       0                          a
>
>0        1      
>0      times           b
>
>-1       2      
>1                          c                
>
>
>
>which
>should result in :  
>
>                                                      a
>
>                                                      b
>
>                                                      c  �a
>-2*b
>where a, b, and c
>
>are
>as yet not assigned numeric values?  
>
>........................... judson blake
>
>
>    
> [[alternative HTML version deleted]]
>
>
>
>------------------------------------------------------------------------
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of symbols?

Boris Steipe
In reply to this post by Judson
You are describing an awkward way of doing this (and your example is unreadable because you are not following posting instructions for the list) ... but the following contains the essence of what I think is needed: parse() and eval().

v <- character()

v[1] <- sprintf("%d ^ %s", 2, "duck")
v[2] <- sprintf("%d * %s", 4, "crow")
# [1] "2 ^ duck" "4 * crow"

duck <- -2
crow <- pi

eval(parse(text=v[1]))
# [1] 0.25

eval(parse(text=v[2]))
# 12.56637


However, myself, I would  not use symbols, but a named vector of values - unless it's important to you that these are _actually_ symbols. You probably avoid some conversions from and to symbols.

v1 <- c(2, 4)
v2 <- c(duck = -2, crow = pi)

v1[1] * v2["duck"]
v1[2] * v2["crow"]

You can of course freely interconvert and cast your symbol names to character:
duck <- 100
v2[as.character(quote(duck))] <- duck
v1[1] * v2["duck"]
#[1] 200



B.




On Oct 31, 2015, at 11:52 AM, Judson <[hidden email]> wrote:

>
>
> Is
> there a way to multiply a matrix
>
> times
> a vector of symbols?  
>
> For
> instance, could I do this:
>
> 1      
> 0       0                          a
>
> 0        1      
> 0      times           b
>
> -1       2      
> 1                          c                
>
>
>
> which
> should result in :  
>
>                                                      a
>
>                                                      b
>
>                                                      c  �a
> -2*b
> where a, b, and c
>
> are
> as yet not assigned numeric values?  
>
> ........................... judson blake
>
>
>    
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.