# Fuction for sequence polarity

## Recommended Posts

OMers,

I wrote this function and use it in my composition work. Please feel free to steal use it. It returns the "polarity" of a sequence, defined as the ratio of consecutive uneven and even interval classes (IC) in a sequence. I use this as a way of testing if a generated sequence contains a balanced mix of even or uneven ICs.

```(defun polarity (in-seq)
(when (equal (length in-seq) 0)
return 0
)
(let((accum 0) (int-pairs (interval-class in-seq)))
(mapcar (lambda (x) (when (evenp x) (incf accum) )) int-pairs)
(/ (* accum 1.0) (- (length in-seq) 1))
)
)```

Some examples:

;; The polarity of a chromatic scale is 0.0, as there are no even interval classes.

```OM 9 > (polarity '(0 1 2 3 4 5 6 7 8 9 10))
0.0```

;; The polarity of a whole-tone scale is 1.0, as there are no uneven interval classes.

```OM 10 > (polarity '(0 4 8 2 6 10))
1.0```

;; The polarity of a chromatic scale is 0.0, as there are no even interval classes.

```OM 9 > (polarity '(0 1 2 3 4 5 6 7 8 9 10))
0.0```

;; The polarity of a sequence of alternating even and uneven ICs is 0.5.

```OM 14 > (polarity `(0 6 11 5 10 4 9 3 8 2 7 1 6))
interval-class```

Right now it only works on sequences of numbers and there's no error handling. I would appreciate very much any advice on a concise and OM-ian way to extend it to accepts either integers or pitch symbols like '(c4 fs4 b4 f5 bb5).

Thanks!
Paul Marquardt

##### Share on other sites

Is this what you are looking for:

```(defun polarity (seq &key sum)
(do-verbose ("polarity")
(flet ((polarity-i (in-seq)
(let ((integers (if (omn-pitchp (car in-seq)) (pitch-to-midi in-seq) in-seq)))
(when (equal (length integers) 0)
return 0
)
(let ((accum 0) (int-pairs (interval-class integers)))
(mapcar (lambda (x) (when (evenp x) (incf accum) )) int-pairs)
(/ (* accum 1.0) (- (length integers) 1))
))))
(let ((result (loop for i in (lists! seq)
collect (polarity-i i))))
(if sum (/ (find-sum (flatten result)) (length result)) result)))))

(polarity '(1 3 6 4 2))
=> (0.75)
(polarity '((c4 e4 c3 ds4 b5) (c4 cs4 d4 ds4 f4)))
=> (0.75 0.25)
(polarity '((c4 e4 c3 ds4 b5) (c4 cs4 d4 ds4 f4)) :sum t)
=> 0.5```

##### Share on other sites

This is excellent -- and the feature to accept lists of lists is very helpful -- thank you

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
• #### Browser

• Video Gallery

• Lessons