G'MIC math evaluator now deals with vectors (and matrices, and custom functions!)

Do you mean from within the math evaluator? If so:

gmic eval "M=[1,2,1,2,4,2,1,2,1];ext('(',vtos(M),')')"

Thanks. It is partially OK, but I need a square image 3x3, not an one row of 9 pixels.

A few ways to do that using resize with memory content mode (the -1):

# if size is known in advance
gmic eval "M=[1,2,1,2,4,2,1,2,1];ext('(',vtos(M),') r. 3,3,1,1,-1');"

# or outside the evaluator
gmic eval "M=[1,2,1,2,4,2,1,2,1];ext('(',vtos(M),')')"  r. 3,3,1,1,-1

# if size must be calculated
gmic eval "M=[1,2,1,2,4,2,1,2,1];W=sqrt(size(M));ext('(',vtos(M),') r. ',vtos([W,W]),',1,1,-1');"

And so on. Some other commands if you need to re-order afterwards: transpose unroll permute

Edit: and I forgot to say hello - welcome to the forum (and to G’MIC) @helour !

1 Like

Thank you for welcoming :slight_smile:

I’ve created this:

  _M=[1,2,1,2,4,2,1,2,1]
  ...
  size={sqrt(size($_M))}
  {ext('(',vtos($_M),')')} #  <- How to properly insert image from expression "(1,2,1,2,4,2,1,2,1)"?
  resize[-1] $size,$size,1,1,-1

but the error has appeared:

[gmic]-1.// Set global variable '_M=[1,2,1,2,4,2,1,2,1]'.
[gmic]-1.// Set local variable 'size=3'.
[gmic]-1.//*ext/ Input image at position 1, with values (1,2,1,2,4,2,1,2,1) (1 image 9x1x1x1).
[gmic]-2.// *** Error *** Command 'input': Invalid argument 'nan'.

In the G’MIC reference manual, under “Input Data Items”, https://gmic.eu/reference.shtml#section5, there appears this item:

(v1,v2,…): Insert a new image from specified prescribed values. Value separator inside parentheses can be , (column separator), ; (row separator), / (slice separator) or ^ (channel separator). For instance, expression (1,2,3;4,5,6;7,8,9) creates a 3x3 matrix (scalar image), with values running from 1 to 9.

1 Like

I’m wondering if a broader view would help, please forgive statements about which you already know.

There are primarily two ways to use matrix algebra in g’mic. First, you can treat an image as a matrix and use the various “native” commands with that (here using run to avoid command line escaping issues):

gmic run "(1,2,0;3,1,0;4,0,1) invert transpose (1;2;3) mmul"

That way you immediately get to visualise the output as an image. The second way is within the “math evaluator”, which is in a way a language within the g’mic language:

gmic eval "M=[1,2,0,3,1,0,4,0,1];M=inv(M);M=transp(M,3);R=M*[1,2,3];echo(vtos(R))"

Mixing the two can get a little tricky if you aren’t very familiar with g’mic string/macro handling. Anyway, if you really do want to mix both methods this is how you would:

test_matrix :
  M=[1,2,1,2,4,2,1,2,1] # an unquoted string, not a vector
  size={sqrt(size($M))} # the string $M is now evaluated as a vector
  ({$M}) # string -> vector -> comma sep. values -> input an image
  resize[-1] $size,$size,1,1,-1 # resize it
2 Likes
({$M})

Big Thanks. That’s exactly what I need ( string → vector → comma sep. values ). I want/need to separate the mathematical calculation(s) from “image processing”.

1 Like

Hi! I’m so sorry, but I just made this account to ask for help (and you seemed to know the most), so I can’t DM yet.
I’m working on something that requires a “spiral matrix transform inverse,” but I have NO IDEA how to do it. I’ve been trying to at least get myself acquainted with G’MIC-Qt, but I really can’t. I was wondering if you could tell me what to do, or something, please? I know this thread is seven years old, but I really need help. Can you?
So sorry again! Thanks!

There’s already a GUI effect that points to how to do spiral matrix transform. This code was modified from original code for readability. So, what is it you want to do with this?

#@gui Spiral Matrix Transformation : fx_jr_spiral_transform, fx_jr_spiral_transform_preview(1)
#@gui : note = note("<small>Transforms images into rectangular spirals. The main use is to run effects on the transformed images and then use a corresponding reverse transformation for unusual effects. Preview may be highly-inaccurate. Based off an unfinished script from the <a href="https://forums.getpaint.net/topic/30276-glitch-effect-plugin-polyglitch-v14b/">PolyGlitch Paint.NET plugin</a>.</small>\n\n Details of the Spiral Matrix Transform's mathematics can be found in this thread - <i><a href="https://discuss.pixls.us/t/challenge-spiralbw-as-a-parametric-function-x-t-y-t/12779">Challenge: Spiralbw as a parametric function &#40;x&#40;t&#41;,y&#40;t&#41;&#41;</a></i>.")
#@gui : sep  = separator()
#@gui : Mode = choice("Spiral","Inverse Spiral")
#@gui : Spiral Rotation = choice("Clockwise","Anticlockwise")
#@gui : Direction = choice("Down","Up")
#@gui : Starting Axis = choice("X","Y")
#@gui : Spiral Start = choice("Outside","Inside")
#@gui : sep  = separator(), note = note("<small>Author: Reptorian, <i><a href="http://bit.ly/2CmhX65">David Tschumperl&#233;</a></i>.,Joan Rake and Garagecoder. Latest Update: <i>2019/5/28</i>.</small>")
fx_jr_spiral_transform:
foreach {
	if $4 permute yxzc fi
	
	spiralbw {[w,h]},0
	
	if $2 mirror[1] x fi
	if $3 mirror[1] y fi
	
	if $5 f[1] "iM-i" fi
	
	[0]
	if $1
		f[2] "I(#0,i0#1%w,floor(i0#1/w),0)=I"
	else
		f[0] "I(i#1%w,floor(i#1/w))"
	fi
	
	k[0]
	
	if $4 permute yxzc fi
}

Wait, sorry! The person I was working with let me know they did it! My apologies for bothering you, thank you!

1 Like