IPA FE Subject B 2023 Question 5

A question IPA Japan published in 令和5年度 基本情報技術者試験 科目B 公開問題 問5, translated into English. ITPEC is based on the same exams.

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

Cosine similarity is a measure of the similarity of the directions of two vectors. The function calcCosineSimilarity receives the real-type arrays vector1 and vector2, each of which has n (n ≥ 1) elements, and returns the cosine similarity of the two arrays. When the array vector1 is {a1, a2, …, an} and the array vector2 is {b1, b2, …, bn}, the cosine similarity is calculated by the following formula. Here, it is assumed that neither the array vector1 nor the array vector2 has 0 stored in all of its elements.

a1b1 + a2b2 + … + anbn√a21 + a22 + … + a2n √b21 + b22 + … + b2n

[Program]
○ real: calcCosineSimilarity(real []: vector1,
real []: vector2)
real: similarity, numerator, denominator, temp ← 0
integer: i
numerator ← 0
for (increase i from 1 to the number of elements in vector1 by 1)
numerator ← numerator + A
endfor
for (increase i from 1 to the number of elements in vector1 by 1)
temp ← temp + vector1[i] squared
endfor
denominator ← the positive square root of temp
temp ← 0
for (increase i from 1 to the number of elements in vector2 by 1)
temp ← temp + vector2[i] squared
endfor
denominator ← B
similarity ← numerator ÷ denominator
return similarity

Answer group

OptionAB

Explanation

A: vector1[i] × vector2[i], B: denominator × (the positive square root of temp) — the first loop sums the products of matching elements, and the denominator is the product of the two vectors’ lengths.

The numerator, a1b1 + a2b2 + … + anbn, adds the products of matching elements, so each pass of the first loop adds vector1[i] × vector2[i]: A.

After the second loop denominator holds the first root of the formula. The third loop sums the squares of vector2 into temp, and the formula multiplies the two roots, so B is denominator × (the positive square root of temp).

Check with vector1 = {3, 4} and vector2 = {4, 3}: the numerator is 12 + 12 = 24, the denominator 5 × 5 = 25, the similarity 0.96.

Answer (d)

Why not others:

  • •(a), (b) and (c) — A takes the root of each product: for {3, 4} and {4, 3} the numerator becomes √12 + √12 ≈ 6.93, not 24, and a negative product has no real root
  • •(g), (h) and (i) — A adds the squares of vector1 only, so the numerator ignores vector2: {1, 0} and {0, 1}, at right angles, would not give 0
  • •(e) and (f) — B adds the two lengths (24 ÷ 10 = 2.4) or drops the first one (24 ÷ 5 = 4.8); a cosine can never exceed 1

Key rule: Cosine similarity is the dot product of the two vectors divided by the product of their lengths; it runs from −1 to 1, and 1 means the same direction.

Math & Data Science · AI-written, may contain errors

This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.